3 Ways of C # Digital Image processing

Source: Internet
Author: User

In this paper, we introduce the 3 methods of C # processing digital Image by color image grayscale, the bitmap class, BitmapData class and graphics class are 3 important classes of C # processing images.

Bitmap as long as the objects that are used to process images defined by pixel data, the main methods and properties are as follows:

GetPixel method and SetPixel method, gets and sets the color of the specified pixel of an image.

PixelFormat property that returns the pixel format of the image.

Palette property, Gets or uses the color palette of the origami image.

The Height property and width property, which returns the heights and widths of the image.

The LockBits method and the Unlockbits method are used to lock and unlock the bit image element in the memory of the system respectively.

The BitmapData object specifies the properties of the bitmap:

Height property, which is locked to the heights of the bitmap.

Width property, which is locked to a bitmap.

PixelFormat property, the actual pixel format of the data.

The Scan0 property, the first byte address of the locked array.

Stride Properties, stride, also called scanning width.

Grayscale of color image

The 24-bit color image is represented by 3 bytes per pixel, and each byte corresponds to the brightness of the R, G, and B components (red, green, and blue). When 3 components do not want to be simultaneously displayed as grayscale images. Here are three conversion formulas:

Gray (I,j) is the grayscale value of the converted grayscale image at (i,j) point. Since the human eye is different in color, the following conversion formula is available:

observed that the largest proportion of green, so the conversion of the direct use of G value as a result of conversion:

The 3 methods of image processing are: Extracting pixel method, memory method and pointer method respectively, they have their own characteristics.

Extract Pixel method

The Bitmap.getpixel and Bitmap.setpixel methods in GDI + are used.















{ &nb Sp;   newbitmap = Bitmap. Clone () as bitmap;     color Pixel;     int ret;     for  (int x = 0; x < Newbitmap. Width; x + +)     {         for  (int y = 0; y < Newbitmap. Height; y++)         {              pixel = Newbitmap. GetPixel (x, y);             ret = (int) (pixel. R * 0.299 + pixel. G * 0.587 + pixel. B * 0.114);             newbitMap. SetPixel (x, Y, Color.FromArgb (ret, ret, ret));         }     }     picturebox1.image = Newbitmap. Clone () as image; }
1
2
3
4
5
6
7
8
9
10




if  bitmap (!= null)

Memory method

Memory method is to copy the image data directly into memory, so that the speed of the program can be greatly improved.

1
2
3
4
5
6
7
8
9
10





[
]

[
] style= "Borde" r:0;padding:0; " >if  (bitmap!= null)
{
    newbitmap = bitmap. Clone () as bitmap;
    rectangle rect = new rectangle (0, 0, Newbitmap. Width, Newbitmap. Height);
    system.drawing.imaging.bitmapdata bmpdata = Newbitmap. LockBits (Rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, Newbitmap. PixelFormat);
    intptr ptr = bmpdata. Scan0;
    int bytes = Newbitmap. Width * Newbitmap. Height * 3;
    byte[] Rgbvalues = new byte[bytes];
    system.runtime.interopservices.marshal.copy (PTR, rgbvalues, 0, bytes);
    double colortemp =0;
    for  (int i = 0; i < rgbvalues. Length; i + 3)
    {
        colortemp = rgbvalues[i + 2] * 0.299 + rgbvalues[i + 1] * 0.587 + rgbvalues[i] * 0.114;
        rgbvalues[i] = rgbvalues[i + 1] = rgbvalues[i + 2] = (byte) colortemp ;
    }
    system.runtime.interopservices.marshal.copy (rgbvalues, 0, ptr, bytes);
    newbitmap. Unlockbits (Bmpdata);
    picturebox1.image = Newbitmap. Clone () as image;
}

Pointer method

This method is similar to the memory method, beginning with the LockBits method to get the first address of the bitmap, this method is more concise, direct bitmap operation with the pointer. Therefore, the operation of the memory needs to be operated under unsafe.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
if  (bitmap!= null)
{
    newbitmap = bitmap. Clone () as bitmap;
    rectangle rect = new rectangle (0, 0, Newbitmap. Width, Newbitmap. Height);
    system.drawing.imaging.bitmapdata bmpdata = Newbitmap. LockBits (Rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, Newbitmap. PixelFormat);  
    byte temp;
    unsafe
     {
        byte* ptr = (byte*) (Bmpdata. SCAN0);
        for  (int x = 0; x < Bmpdata. Width; x + +)
        {
             for  (int y = 0; y < Bmpdata. Height; y++)
            {
  &nbSp;             temp = (byte) (0.299 * Ptr[2] + 0.587 * Ptr[1] + 0.114 * ptr[0]);
                ptr[0] = ptr [1] = ptr[2] = temp;
                ptr + 3;
            }
            ptr + = Bmpdata. Stride-bmpdata. Width * 3;
        }
    }
    newbitmap. Unlockbits (Bmpdata);
    picturebox1.image = Newbitmap. Clone () as image;
}

Comparison of 3 methods

It can be concluded that the extraction of pixel method is simpler, but the efficiency is low; the efficiency of the memory method has been greatly improved, but the code is more complex, the pointer method is more efficient than the memory method, but unsafe. On the other, the memory method is better, the efficiency is high and the advantage of C # security can be played.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.