Robert
Robert

Reputation: 4406

How to convert an int array to a bitmap object?

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?

Upvotes: 1

Views: 2029

Answers (1)

Brandon
Brandon

Reputation: 23525

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

Upvotes: 2

Related Questions