Three ways to handle images in C # comparison

Source: Internet
Author: User

C # itself has a certain image processing capabilities, even without relying on the EMGU CV, there is a great potential.

However, the recent processing of a large number of pictures, found that the number of images is small, processing itself brought about by the delay will not be sensitive, but the number of large, the program spends a lot of time in preprocessing the picture, resulting in the program is easily false alarm line Cheng too large, leading to false error program. For this problem for a long time, after all, not CS professional background, for image processing and some algorithms and the use of the library after all labored.

Today, after reading some digital image processing books and feeling a lot of benefit, now do a little experiment to compare your mistakes before.

Before looking at Ms C#api time found that there is a graphics very suitable for me to use now, so then apart open dry, today only to find that the package is too good, but will bring a lot of drag, as the hands of the carefree operation.

Not much, the test is compared to the graphics, operating memory, operating pointer three performance gaps.

Test content: Grayscale processing of the same picture, directly see how much delay between the three

1. Test the basic scene, more ugly, just look at ...

2. Extract pixel button, call graphics grayscale processing

private void Button2_Click (object sender, EventArgs e) {if (curBitMap1! = null) {                Color Curcolor;                int ret;                Personal run time judgment class GetRuntime getruntime = new GetRuntime ();                Stopwatch start Getruntime.start ();                    graphics//grayscale for (int i = (int) (0); i < (int) (curbitmap1.width); i++) {                        for (int j = 0; J < Curbitmap1.height; J + +) {//Get pixels                        Curcolor = Curbitmap1.getpixel (i, j);                        Get RGB ret = (int) (CURCOLOR.R * 0.299 + curcolor.g * 0.587 + curcolor.b * 0.114);                    Set pixel Curbitmap1.setpixel (i, J, Color.FromArgb (ret, ret, ret));                }} Invalidate (); Labelgetpixel.text = "Extracting pixels takes time:" + getruntime.getruntime () +"MS"; }        }

  

3. In-memory button, direct copy data to memory, direct operation

private void Button5_click (object sender, EventArgs e) {if (CURBITMAP2! = null) {                GetRuntime getruntime = new GetRuntime ();                Getruntime.start ();                Rectangle rect = new Rectangle (0, 0, curbitmap2.width, curbitmap2.height); Lock bitmap in read-write mode System.Drawing.Imaging.BitmapData bmpdata = curbitmap2.lockbits (rect, System .                Drawing.Imaging.ImageLockMode.ReadOnly, Curbitmap2.pixelformat);                First address IntPtr ptr = bmpdata.scan0;                Total number of bytes in 24-bit bmp int bytes = Curbitmap2.width * Curbitmap2.height * 3;                byte[] rgbvalues = new Byte[bytes];                Copy the locked image to Rgbvalues System.Runtime.InteropServices.Marshal.Copy (PTR, rgbvalues, 0, bytes);                Double colortemp = 0; Grayscale for (int i = 0; i < bmpdata.height; i++) {for (int j = 0; J < Bmpdata.width * 3; j+=3) {//skip blank block colortemp = rgbvalues[i * bmpdata.stride  + j + 2] * 0.299 + rgbvalues[i * bmpdata.stride + j + 1] * 0.587 +                        Rgbvalues[i * bmpdata.stride + j] * 0.114;                             Rgbvalues[i * bmpdata.stride + j + 2] = Rgbvalues[i * Bmpdata.stride + j + 1]                    = Rgbvalues[i * Bmpdata.stride + j] = (byte) colortemp; }}//Copy back bitmap System.Runtime.InteropServices.Marshal.Copy (rgbvalues, 0, PTR, by                TES);                Curbitmap2.unlockbits (Bmpdata);                Invalidate ();            Labelcache.text = "Extracting pixel Memory method takes time:" + getruntime.getruntime () + "MS"; }        }

  

4. Pointer method button, directly copy data to memory, pointer operation 

private void Button6_click (object sender, EventArgs e) {getruntime getruntime = new GetRuntime ();            Getruntime.start ();            Rectangle rect = new Rectangle (0, 0, curbitmap3.width, curbitmap3.height); Lock bitmap in read-write mode System.Drawing.Imaging.BitmapData bmpdata = curbitmap3.lockbits (rect, System.dra Wing.            Imaging.ImageLockMode.ReadOnly, Curbitmap3.pixelformat);            byte temp = 0;                unsafe {//first address byte* ptr = (byte*) (bmpdata.scan0); Grayscale for (int i = 0, i < bmpdata.height; i++) {for (int j = 0; J &lt ; bmpdata.width;j++) {temp = (byte) (0.299 * ptr[2] + 0.587 * ptr[1] + 0.114 * PT                        R[0]);                        Ptr[0] = ptr[1] = ptr[2] = temp;                    PTR + = 3; }//Skip blank block ptr + = Bmpdata.strIde-bmpdata.width * 3;            }}//Unlock bitmap curbitmap3.unlockbits (bmpdata);            Invalidate ();        Labelpoint.text = "Extracting pixel Memory method takes time:" + getruntime.getruntime () + "MS"; }

5, simple and perfect start to open the picture to see the comparison between the running time

Summary: Although the computer is rotten, but still can see the gap between the three. Copying into the memory direct operation and using the pointer operation are greatly improved efficiency, if the processing of a picture to save 60ms, that processing a large number of pictures will save how much time? So in the future when the image processing, if not emgu, try not to use graphics, or to do their own hands-on memory. (It will be said that you do not use C + + for hair, OK, I was in the black graphics, you bite me ~)

Three ways to handle images in C # comparison

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.