1

I have an int array that is a bitmap at the end of the road. I want to convert the array temporarily to a bitmap so that I can do some smoothing on the edges before it is rendered to the screen in its final form. I have the code to smooth the bitmap here:

//.h file code:

using namespace Emgu::CV;
using namespace Emgu::CV::Structure;

namespace Microsoft
{
    namespace Samples
    {
        namespace Kinect
        {
            namespace BodyIndexBasics
            {
                /// <summary>
                /// Class responsible for extracting out the contours of an image.
                /// </summary>
                class FindContours
                {
                    /// <summary>
                    /// Method used to process the image and set the output result images.
                    /// </summary>
                    /// <param name="colorImage">Source color image.</param>
                    /// <param name="thresholdValue">Value used for thresholding.</param>
                    /// <param name="processedGray">Resulting gray image.</param>
                    /// <param name="processedColor">Resulting color image.</param>
                public:
                    void IdentifyContours(Bitmap *colorImage, int thresholdValue, bool invert, Bitmap *&processedGray, Bitmap *&processedColor);
                };
            }
        }
    }
}

//.cpp file code:

using namespace Emgu::CV;
using namespace Emgu::CV::Structure;
namespace Microsoft
{
    namespace Samples
    {
        namespace Kinect
        {
            namespace BodyIndexBasics
            {

                void FindContours::IdentifyContours(Bitmap *colorImage, int thresholdValue, bool invert, Bitmap *&processedGray, Bitmap *&processedColor)
                {

                    BlurBitmapEffect *myBlurEffect = new BlurBitmapEffect();

                    Image<Gray*, unsigned char> *grayImage = new Image<Gray*, unsigned char>(colorImage);
                    Image<Bgr*, unsigned char> *color = new Image<Bgr*, unsigned char>(new Bitmap(colorImage->Width, colorImage->Height));



                    grayImage = grayImage->ThresholdBinary(new Gray(thresholdValue), new Gray(255));
                    if (invert)
                    {
                        grayImage->_Not();
                    }



                    MemStorage *storage = new MemStorage();
                    try
                    {
                        for (Contour<Point> contours = grayImage->FindContours(Emgu::CV::CvEnum::CHAIN_APPROX_METHOD::CV_CHAIN_APPROX_SIMPLE, Emgu::CV::CvEnum::RETR_TYPE::CV_RETR_TREE, storage); contours != nullptr; contours = contours->HNext)
                        {
                            Contour<Point> *currentContour = contours->ApproxPoly(contours->Perimeter * 0.015, storage);
                            if (currentContour->BoundingRectangle->Width > 20)
                            {
                                CvInvoke::cvDrawContours(color, contours, new MCvScalar(255), new MCvScalar(255), -1, 5, Emgu::CV::CvEnum::LINE_TYPE::EIGHT_CONNECTED, Point(0, 0));
                            }
                        }
                    }

                    finally
                    {
                        if (storage != nullptr)
                        {
                            storage.Dispose();
                        }
                    }



                    processedColor = color->ToBitmap();
                    processedGray = grayImage->ToBitmap();

                }
            }
        }
    }
}

What I need is some way to convert an int array to the bitmap so that I can smooth it before rendering.

//This is the int array that I want to convert temp
int* pOutputData = nullptr;
byte* pOutputDataByte = nullptr;
hr = spOutputBufferByteAccess->Buffer(&pOutputDataByte);
if (FAILED(hr))
{
    return false;
}

pOutputData = (int*)pOutputDataByte;

DepthSpacePoint* pDepthPoints = m_depthPoints->Data;
byte* pBodyIndexFrameArray = bodyIndexframeArray->Data;

ZeroMemory(pOutputData, outputDataBuffer->Capacity);

the int* pOutputData is what I want to convert so that I can smooth it.

Is it possible to do this?

Robert
  • 4,306
  • 11
  • 45
  • 95
  • You can also create a bitmap from an `int` array and you can smooth an `int` array *without* converting to bitmap. Here is a similar question: http://stackoverflow.com/q/13745093/3651800. Is there a specific part of creating the bitmap that is causing you difficulty? – Matt Coubrough Dec 04 '14 at 03:23
  • @MattCoubrough I don't see him talking about an int array there? – Robert Dec 05 '14 at 00:06
  • You have to map your array of `ints` to bytes with a simple operation on each item in the array, and then build a bitmap from the byte array. I see you've accepted an answer that also requires that. But it isn't immediately clear to me from your question how you want to map your integers to bitmap pixels. You only state that the array of ints is "a bitmap at the end of the road". What does each int represent? Is each `int` the 4 byte RGBA values from a bitmap or is it a number for the brightness of each grayscale pixel that needs to be scaled to a byte or something else? – Matt Coubrough Dec 05 '14 at 00:24

1 Answers1

2

Depends on how you define "Bitmap". Is the Bitmap a GDI+ Bitmap? Is it a class you created? Is it a file with a byte mapping?

If it is GDI+ Bitmap then: http://msdn.microsoft.com/en-us/library/windows/desktop/ms536312(v=vs.85).aspx constructor will work just fine as shown below (OR http://msdn.microsoft.com/en-us/library/windows/desktop/ms536288(v=vs.85).aspx):

#include <gdiplus.h>

Gdiplus::Bitmap* CreateBitmap(void* data, unsigned int width, unsigned int height)
{
    BITMAPINFO Info;
    memset(&Info, 0, sizeof(Info));

    Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    Info.bmiHeader.biWidth = width;
    Info.bmiHeader.biHeight = height;
    Info.bmiHeader.biPlanes = 1;
    Info.bmiHeader.biBitCount = 32;
    Info.bmiHeader.biCompression = BI_RGB;
    Info.bmiHeader.biSizeImage = 0;  //(((32 * width + 31) & ~31) / 8) * height;

    return new Gdiplus::Bitmap(&Info, data);
}

Otherwise see my answer here for raw manual Bitmap creation: Incorrect Bitmap Copy/Output

Community
  • 1
  • 1
Brandon
  • 22,723
  • 11
  • 93
  • 186