C # OpenCV learning notes 2: Two Methods for reading and writing Images

Source: Internet
Author: User

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"
 

Related Article

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.