【问题标题】:How to convert an int array to a bitmap object?如何将 int 数组转换为位图对象?
【发布时间】:2014-12-04 03:00:10
【问题描述】:

我有一个 int 数组,它是道路尽头的位图。我想将数组临时转换为位图,以便在以最终形式呈现到屏幕之前对边缘进行一些平滑处理。我在这里有平滑位图的代码:

//.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();

                }
            }
        }
    }
}

我需要某种方法将 int 数组转换为位图,以便在渲染之前对其进行平滑处理。

//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);

int* pOutputData 是我想要转换的,以便我可以平滑它。

可以这样做吗?

【问题讨论】:

  • 您还可以从int 数组创建位图,并且可以平滑int 数组而不转换为位图。这是一个类似的问题:stackoverflow.com/q/13745093/3651800。创建位图的特定部分是否给您带来了困难?
  • @MattCoubrough 我没有看到他在那里谈论 int 数组?
  • 您必须通过对数组中每个项目的简单操作将ints 数组映射到字节,然后从字节数组构建位图。我看到你已经接受了一个同样需要的答案。但是从你的问题中我并不清楚你想如何将你的整数映射到位图像素。您只声明整数数组是“路尽头的位图”。每个 int 代表什么?每个int 是位图中的 4 字节 RGBA 值,还是需要缩放到一个字节或其他内容的每个灰度像素的亮度数字?

标签: c++ arrays bitmap


【解决方案1】:

取决于您如何定义“位图”。位图是 GDI+ 位图吗?这是您创建的课程吗?它是具有字节映射的文件吗?

如果是 GDI+ 位图,则:http://msdn.microsoft.com/en-us/library/windows/desktop/ms536312(v=vs.85).aspx 构造函数将正常工作,如下所示(或 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);
}

否则,请在此处查看我对原始手动位图创建的回答:Incorrect Bitmap Copy/Output

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多