[Blog Project actual combat faction]csharp through the DLL call OpenCV function, the picture as a parameter

Source: Internet
Author: User

[Blog Project actual combat faction]csharp through the DLL call OpenCV function, the picture as a parameter always want to do the research, but because this aspect of knowledge is too small, but also because of their ability to find information is weaker, know today only found a better information. One is Thinimage, and the other is Basework, which implements the "CSharp calls the OpenCV function through the DLL, and takes the picture as the parameter". Here is a summary below. about how to "CSharp Calling the OpenCV function through a DLL, please refer to the previous blog post, which is mainly about how to "take pictures as parameters". in the writing of image recognition/enhancement/Machine vision projects, the algorithm will generally be packaged into DLL files to the customer, if the interface is written in MFC, it is very good to achieve this function. But there are also times when you choose CSharp as the interface because of special needs, or to achieve richer interface effects faster. At this time, "picture as parameter" is required. This project is an experimental project, the function is CSharp read into a 24-bit color picture, call OpenCV for YCBCR transformation, and then csharp shown above. Because in the process of the invocation, only pointers are passed,soThis conversion needs to be done in two ways. One aspect is CSharp side, in particular, the need to create a bitmap structure, and in the process of the call lock memory, one aspect of the OPENCV and DLL writing this side, to be able to read into the memory area, and translated into the mat structure. Then write the related handlers on this basis. continue the Open source project Thinyimage, the process to achieve. Thinyimage itself is CSharp call C + + written DLL implementation of the image processing software, but also to achieve a number of functions:then add the Go algorithm interface above the existing programCSharp this way, for memory locking and function call operations Private voidToolstripmenuitem_click (object sender, EventArgs e)
{
Startimageprocess ();
Try
{
BitmapData BitmapData=M_bitmap. LockBits ( NewRectangle (0,0, M_bitmap. Width, M_bitmap. Height),
Imagelockmode.readwrite, Pixelformat.format32bppargb);
Nativemethods.directcopy (Bitmapdata.scan0, Bitmapdata.width, Bitmapdata.height, Bitmapdata.stride, M_BPP);
M_bitmap. Unlockbits (BitmapData);
}
Catch(System.Exception)
{
}
Endimageprocess ();
}Add the program interface accordingly,//Union OPENCV template function
voidDirectcopy (Tibitmapdata&Bitmap) {
//Get basic parameters
intWidth=Bitmap. GetWidth ();
intHeight=Bitmap. GetHeight ();
intStride=Bitmap. Getstride ();
intBpp=Bitmap. GETBPP ();
U8*Bmpdata=Bitmap. Getbmpdata ();
intOffset=Stride-Width*bpp
Mat SRC=Mat::Zeros (WIDTH,HEIGHT,CV_8UC3);
//Copy the contents of bitmap to src
for( intI=0; I<Src.rows;i++){
for( intJ=0; j<Src.cols;j++){
src.at<Vec3b>(I,J) [0]=( int) Bmpdata[tiblue];
src.at<Vec3b>(I,J) [1]=( int) Bmpdata[tigreen];
src.at<Vec3b>(I,J) [2]=( int) bmpdata[tired];
Bmpdata+=bpp
}
Bmpdata+=Offset
}
/////OPENCV specific operation process////////////////////////
Mat DST;
Cvtcolor (SRC,DST,COLOR_BGR2YCRCB);
/////OPENCV process End////////////////////////
Bmpdata=Bitmap. Getbmpdata ();
for( intI=0; I<Src.rows;i++){
for( intJ=0; j<Src.cols;j++){
Bmpdata[tiblue]=dst.at<Vec3b>(I,J) [0];
Bmpdata[tigreen]=dst.at<Vec3b>(I,J) [1];
Bmpdata[tired]=dst.at<Vec3b>(I,J) [2];
Bmpdata+=bpp
}
Bmpdata+=Offset
}
}
}complete the corresponding function 



From for notes (Wiz)



[Blog Project actual combat faction]csharp through the DLL call OpenCV function, the picture as a parameter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.