The image is displayed on the loadPictureBox control.
Method 1
// Read image 001.jpg
IntPtr img = CvInvoke. cvLoadImage ("001.jpg", Emgu. CV. CvEnum. LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_COLOR );
// Convert IntPtr to Image. For details, see the IntPtr2Image method.
LoadPictureBox. Image = IntPtr2Image (img );
// Display the image window
CvInvoke. cvShowImage ("view", img );
// The window is retained for 2000 milliseconds, that is, 2 seconds
CvInvoke. cvWaitKey (2000 );
// Close the window
CvInvoke. cvDestroyWindow ("view ");
// Save the image
CvInvoke. cvSaveImage ("002.jpg", img );
// Release
CvInvoke. cvReleaseImage (ref img );
Private Image IntPtr2Image (IntPtr src)
{
MIplImage img = (MIplImage) Marshal. PtrToStructure (src, typeof (MIplImage ));
Bitmap disp = new Bitmap (img. width, img. height, PixelFormat. Format24bppRgb );
BitmapData bmp = disp. LockBits (new Rectangle (0, 0, img. width, img. height), ImageLockMode. WriteOnly, PixelFormat. Format24bppRgb );
Long linebytes = (img. width * 24 + 31)/32*4;
Unsafe
{
Byte * pixel = (byte *) bmp. Scan0.ToPointer ();
If (img. nChannels = 3)
{
For (int I = 0; I {
For (int j = 0, n = 0; j {
Byte B = (byte *) img. imageData + img. widthStep * I) [3 * j];
Byte g = (byte *) img. imageData + img. widthStep * I) [3 * j + 1];
Byte r = (byte *) img. imageData + img. widthStep * I) [3 * j + 2];
* (Pixel + linebytes * (I) + n) = B;
N ++;
* (Pixel + linebytes * (I) + n) = g;
N ++;
* (Pixel + linebytes * (I) + n) = r;
}
}
}
Else if (img. nChannels = 1)
{
For (int I = 0; I {
For (int j = 0, n = 0; j {
Byte g = (byte *) img. imageData + img. widthStep * I) [j];
* (Pixel + linebytes * (I) + n) = g;
N ++;
* (Pixel + linebytes * (I) + n) = g;
N ++;
* (Pixel + linebytes * (I) + n) = g;
}
}
}
Else
{
Return null;
}
}
Disp. UnlockBits (bmp );
Return (Image) disp;
}
Method 2
Image <Bgr, Byte> img = new Image <Bgr, byte> ("001.jpg ");
LoadPictureBox. Image = img. ToBitmap ();
Author: "Edge City camels-improving by 1% every day"