2014-12-04 71 views
1

我有一個int數組,是在路的盡頭的位圖。我想暫時將數組轉換爲位圖,以便在最終形式呈現給屏幕之前,我可以在邊緣上進行平滑處理。我的代碼以平滑這裏的位圖:如何將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是我想要轉換,以便我可以平滑它。

可以做到這一點嗎?

+0

你也可以從int數組創建一個位圖,並且可以在不轉換位圖的情況下平滑一個int數組*。這裏有一個類似的問題:http://stackoverflow.com/q/13745093/3651800。是否有創建位圖的特定部分導致您的困難? – 2014-12-04 03:23:28

+0

@MattCoubrough我沒有看到他在那裏談論一個int數組? – Robert 2014-12-05 00:06:30

+0

您必須通過對數組中的每個項目進行簡單操作將「ints」的數組映射到字節,然後從字節數組構建一個位圖。我看到你已經接受了一個也需要這個答案。但是,從你的問題來看,我不清楚你想如何將整數映射到位圖像素。您只能說明整數的數組是「在路的盡頭的位圖」。每個int代表什麼?每個「int」是來自位圖的4字節RGBA值還是每個灰度像素亮度的數字,需要按比例縮放到一個字節或其他東西? – 2014-12-05 00:24:03

回答

1

取決於您如何定義「位圖」。位圖是GDI +位圖嗎?這是你創建的課程嗎?它是一個帶有字節映射的文件嗎?

如果GDI +位圖,則:http://msdn.microsoft.com/en-us/library/windows/desktop/ms536312(v=vs.85).aspx構造函數會工作得很好,如下圖所示(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); 
} 

否則看到我的答案在這裏爲原料手工位圖製作:Incorrect Bitmap Copy/Output