2017-02-24 4 views
0

C++에서 opencv로 만든 dll에 비트 맵을 전달해야합니다. dll에서는 이미지 처리를 위해 Mat 객체를 사용합니다. Bitmap 객체를 어떻게 Mat 객체로 변경할 수 있는지 알고 싶습니다. IntPtr을 사용하여 시도했지만 Mat 생성자가 IntPtr을 지원하지 않기 때문에 Mat 객체를 빌드하는 방법을 모르겠습니다. 아무도 내가 이것을 어떻게 할 수 있는지 안다? 코드 조각을 가지고 나를 도울 수 있다면 가장 좋을 것입니다. 감사.Bitmap 객체를 Mat 객체 (opencv)로 변환하는 방법은 무엇입니까?

+0

당신이 본 적이
[DllImport("addborders.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int main(IntPtr pointer, uint height,uint width); unsafe { fixed (byte* p = ImageToByte(img)) { var pct = (IntPtr) p; x = main(pct, (uint)img.Height, (uint)img.Width); } textBox1.Text = x.ToString(); public static byte[] ImageToByte(Image img) { ImageConverter converter = new ImageConverter(); return (byte[])converter.ConvertTo(img, typeof(byte[])); } 

[이] (http://docs.opencv.org/java/2.4.9/org/opencv/android/를 Utils.html) –

답변

0

그것을 할 수있는 간단한 방법은 다음과 같습니다

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 

using namespace System; 
using namespace System::Drawing; 

int main(array<System::String ^> ^args) { 
    Bitmap^ img = gcnew Bitmap(10, 10,  System::Drawing::Imaging::PixelFormat::Format24bppRgb); 
    // or: Bitmap^ img = gcnew Bitmap("input_image_file_name"); 

    System::Drawing::Rectangle blank = System::Drawing::Rectangle(0, 0, img->Width, img->Height); 
    System::Drawing::Imaging::BitmapData^ bmpdata = img->LockBits(blank, System::Drawing::Imaging::ImageLockMode::ReadWrite,  System::Drawing::Imaging::PixelFormat::Format24bppRgb); 
    cv::Mat cv_img(cv::Size(img->Width, img->Height), CV_8UC3, bmpdata->Scan0.ToPointer(), cv::Mat::AUTO_STEP); 
    img->UnlockBits(bmpdata); 

    cv::imwrite("image.png", cv_img); 
    return 0; 
} 

BTW, 당신이 C++/CLI로 작업하는 질문에 언급 할 가치가있다.

1

도움 주셔서 감사합니다. 다른 방법으로 찾았습니다. 내 코드를 확인 : C 번호 :

C++
extern "C" 
{ 
__declspec(dllexport) 
int main(unsigned char* image,unsigned int height,unsigned int width) 
{ 

    cv::Mat img = cv::Mat(height, width, CV_8UC1, image); 
} 
}