Home > other >  Based on OpenCV image processing programming interface - about QT, MFC, and the introduction of GOCW
Based on OpenCV image processing programming interface - about QT, MFC, and the introduction of GOCW

Time:11-21

Based on OpenCV image processing projects, in addition to the algorithm, a problem is more important is the interface design problem, for programmers in c + + language, generally a QT/MFC two considerations, QT is powerful, especially the QML write android interface is a set of (https://www.cnblogs.com/jsxyhelu/p/8286476.html), design is also very convenient on the raspberry pie (https://www.cnblogs.com/jsxyhelu/p/7839062.html); But using QT is a real problem and the combination of the existing platform, such as customer need to export to excel, using QT is awkward (of course not to say that can not), so now I generally to do so: for Android and PI, or you need to run on Linux projects, using QT interface to write, call Opencv function; For the project needs to run on Windows, using MFC interface, can be directly quoted OpenCV,
Someone teases MFC is very complicated to use, that I couldn't agree more, but the MFC after so many years of development, today still has the vigor, and a short period of time will not go away, because compared to some other wysiwyg (QT/Csharp language and environment, MFC message mapping mechanism and coordinate system, etc., does have its advantages, especially for image processing program; Be accumulated, can quickly make a lot of professional; Recent emergence of ribbon interface also many plus for MFC (https://www.cnblogs.com/jsxyhelu/p/9209052.html)
Chose the MFC in this direction, think about image processing procedures, generally divided into "image" and "process video two categories:" for the image processing, I provide GOPaint framework (https://www.cnblogs.com/jsxyhelu/p/6440910.html) can provide a basic framework of static image processing; And GOMFCTemplate2 video (https://www.cnblogs.com/jsxyhelu/p/GOMFCTemplate2.html) is suitable for processing, the two were successfully applied to a variety of video processing projects,
But here I want to go a step further: hope to be able to use Csharp write interface, because it is better to use; But don't want to introduce EmguCV similar libraries, because a lot of things is not what I need, so the most direct way is to use Csharp call based on Opencv writing class library (Dll) file, I named GreenOpenCsharpWarper (GOCW)
After long time exploration research, the current GOCW can directly in the form of function in memory transfer bitmap and Mat object, has reached the level of function of application, because it involves the managed code, namely the CLR programming, so there is a complicated place; In order to show the excellent characteristics of GOCW, I write the implementation GOGPY projects, as well as a "Csharp write interface, OpenCV algorithm of real-time video processing procedures", the details contain among them, the name "GPY", is the collection hardware, I adopted the high image quality good instrument equipment (GaoPaiYi),
Here is simple to explain the core content, the core issue of GOCW, is based on the two directions of CLR data flow transformation, core functions for the
Bitmap ^ GOClrClass: : testMethod (cli: : array ^ pCBuf1)
{
Pin_ptr & lt; System: : Byte> P1=& amp; PCBuf1 [0].
Unsigned char * pby1=p1;
CV: : Mat img_data1 (pCBuf1 - & gt; Length, 1, CV_8U pby1);
CV: : Mat img_object=CV: : imdecode (img_data1 IMREAD_UNCHANGED);//get data into img_object
//////////////////////////////////processing///////////////////////////////////////
CvtColor (img_object, img_object, 40);

/////////////////////////////////////////////////////////////////////////////////
Bitmap ^ bb=MatToBitmap (img_object);
if (! Img_object. Data)
return nullptr;
STD: : vector Buf.
CV: : imencode (" JPG ", img_object, buf);
Return bb;
}
And
System: : Drawing: : Bitmap ^ MatToBitmap (const CV: : Mat& Img)
{
If (img. Type ()!=CV_8UC3)
{
Throw gcnew a NotSupportedException (" Only images of the type CV_8UC3 are supported for conversion to Bitmap ");
}
//create the bitmap and the get the pointer to the data
PixelFormat FMT (PixelFormat: : Format24bppRgb);
Bitmap ^ bmpimg=gcnew Bitmap (img. Cols, img. Rows, FMT);
BitmapData ^ data=https://bbs.csdn.net/topics/bmpimg-> LockBits (System: : Drawing: : a Rectangle (0, 0, img. Cols, img. Rows), ImageLockMode: : WriteOnly, FMT);
//byte * dstData= byte * (data -> Scan0. ToPointer ());
Byte * dstData= Byte * (data -> Scan0. ToPointer ());
Unsigned char * srcData=https://bbs.csdn.net/topics/img.data;
For (int row=0; The row & lt; Data - & gt; Height; + + row)
{
Memcpy (reinterpret_cast & lt; Void * & gt; (& amp; DstData [row * data - & gt; Stride]), reinterpret_cast & lt; Void * & gt; (& amp; SrcData [img row * step]), img. Cols * img. Channels ());
}
Bmpimg - & gt; UnlockBits (data);
Return bmpimg;
}

In chsarp, direct
Bitmap b=new Bitmap (CAM) Width, CAM. Height, CAM, Stride, PixelFormat. Format24bppRgb, m_ip);
//If the image is upsidedown
B.R otateFlip (RotateFlipType RotateNoneFlipY);
SrcImage=b;
If (picPreview Image!=null)
PicPreview. Image. The Dispose ();
//call the CLR + opencv image processing module
MemoryStream ms=new MemoryStream();
B.S ave (ms, System. Drawing. Imaging. ImageFormat. Jpeg);
Byte [] bytes=Ms. GetBuffer ();
Bitmap Bitmap=client. TestMethod (bytes);
Can call, and get the result,

The following content for the 2017 updated content, appropriate reference:
A, the CLR DLL written in part
1, according to the normal method is introduced into Opencv.
2, provides the interface function, image processing (here only realized cvtColor, can use to write your own in the process of actual complex function)
String ^ Class1: : Method (cli: : array ^ pCBuf1)
{
Pin_ptr & lt; System: : Byte> P1=& amp; PCBuf1 [0].
Unsigned char * pby1=p1;
CV: : Mat img_data1 (pCBuf1 - & gt; Length, 1, CV_8U pby1);
CV: : Mat img_object=CV: : imdecode (img_data1 IMREAD_UNCHANGED);
//////////////////////////////////processing/////////
CvtColor (img_object, img_object, 40);
/////////////////////////////////////////////////////////////////////////////////
if (! Img_object. Data)
return nullptr;
//get the directory, save the file
CV: : imwrite (" c:/Method. JPG ", img_object);
Return "c:/Method. JPG";
}

String ^ Class1: : an (cli: : array ^ pCBuf1)
{
Pin_ptr & lt; System: : Byte> P1=& amp; PCBuf1 [0].
Unsigned char * pby1=p1;
CV: : Mat img_data1 (pCBuf1 - & gt; Length, 1, CV_8U pby1);
CV: : Mat img_object=CV: : imdecode (img_data1 IMREAD_UNCHANGED);
//////////////////////////////////processing///////////////////////
CvtColor (img_object img_object, 6);
/////////////////////////////////////////////////////////////////////////////////
if (! Img_object. Data)
return nullptr;
//get the directory, save the file
CV: : imwrite (" c:/an JPG ", img_object);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull