Currently, C ++ (for example, Visual C ++) is used for digital image processing programming, because it is flexible and fast and efficient. However, it also makes development more difficult and more efficient. C # Is a new object-oriented programming language developed by Microsoft. It is derived from C/C ++ and retains the powerful functions and flexibility of C/C ++, at the same time, it has the simple syntax structure and efficient development capability of Visual Basic. For the Research on Digital Image Processing that focuses on algorithms, C # is suitable for implementation, verification, and development of some algorithms. This article introduces the basic method of Image Processing Based on C #, and introduces the commonly used color space in color image processing and the C # Implementation of conversion.
1. Color Image Processing Color Space
Color Space is the basis for color image processing. The selection of Color Space directly affects the processing methods and effects. The most common color spaces in color image processing include RGB color spaces and HSI (HSV) color spaces. The former is often used in computer input and output devices (including scanners and digital cameras), and the latter is more in line with Human descriptions and explanations of colors.
In the RGB color space, each color appears in the primary color spectral components of Red, Green, and Blue. The model is based on the Cartesian coordinate system. In this color space, the displayed image consists of three image components (RGB.
The HSI color space represents the color of an object through the color (Hue), Saturation (Saturation), and brightness (Intensity. The color H defines the wavelength of the color, reflecting the spectral wavelength closest to the color, which is different from the color of light. Saturation S indicates the depth of the color. The higher the value of S, the deeper the color. I indicates the intensity or brightness. I is irrelevant to the color information, while H and S contain the color information.
The I component in the HSI color space has nothing to do with the color information, and the H and S components are closely related to the way the human eyes obtain the color. Therefore, it is suitable for color image processing. The storage and display of color digital images are generally in the RGB format. Therefore, the conversion between the RGB color space and the HSI color space is often required. The mutual conversion formulas of RGB and HSI have different forms, but the basic idea is the same. From the perspective of color image processing, as long as the color is an angle and the saturation and brightness are independent, the conversion is reversible and will not affect the processing result. The descriptions of these formulas are omitted here.
2 C # basic image processing methods
C # There are three ways to process images: pixel, memory, and pointer. The pixel method applies the methods in GDI +, which is easy to understand and simple, but slow to run. The memory method copies images to the memory and processes the data in the memory directly, the operation speed is much faster than the pixel method, and the program is not difficult. The pointer method uses the pointer method to process the image, which is the fastest. However, the pointer is not recommended for C #. Because the pointer is used, the Code is not only difficult to write and debug, but also cannot use the CRL memory type security check. It cannot take full advantage of the C # expertise. The following describes the basic methods for image processing using the memory method.
First, define a string (image file name) and a Bitmap type data member (image object) in the Form class for image processing. Then, you can use OpenFileDialog to select an image file and read the file name, use Image again. fromFile: Creates a graphic object. For example:
Define the string (image file name) and Bitmap data member (image object) in the Form class ):
Private string curFileName;
Private System. Drawing. Bitmap curBitmap;
Use Image. FromFile to create a graphic object:
CurBitmap = (Bitmap) Image. FromFile (curFileName );
Copy the image to the memory:
Rectangle rect = new Rectangle (0, 0, curBitmap. Width, curBitmap. Height); // bitmap Rectangle
// Lock all bitmap pixels in read/write mode
System. Drawing. Imaging. Bitmap DatabmpData =
CurBitmap. LockBits (rect, System. Drawing. Imaging. ImageLockMode. ReadWrite, curBitmap. PixelFormat );
IntPtr = BMP data. Scan0; // obtain the first address.
Int bytes = curBitmap. Width * curBitmap. Height; // The number of 24-bit BMP bitmap bytes.
Byte [] rgbValues = new byte [bytes * 3]; // defines a bitmap Array
System. Runtime. InteropServices. Marshal. Copy (ptr, rgbValues, 0, bytes * 3); // Copy the locked bitmap pixel in place graph Array
3 C #-based color space conversion
The mutual conversion formulas of RGB and HSI have different forms, but the basic idea is the same. The following is a form of conversion from RGB to HSI. Given an RGB color image, the color component H depends on the relationship between B and G. When B ≤ G, H = arccos {0.5 × [(R-G) + (R-B)] cosine [(R-G) 2 + (R-B) 2 + (G-B) 2] 0.5}, when B> G, H = 2 π-arccos {0.5 × [(R-G) + (R-B)] California [(R-G) 2 + (R-B) 2 + (G-B) 2] 0.5 }; saturation component S = 1-[3 Gb/s (R + G + B)] × [min (R, G, B)]; brightness component I = (R + G + B) lifecycle 3.
In this conversion process, H is obtained by nonlinear transformation of R, G, and B. If the RGB value is normalized to the range of [0, 1], the values of the S and I components of the HSI are also in the range of [0, 1, H can be divided by the value obtained from the above formula and normalized to the range of [0, 1] By 2π. The following is the C # implementation.
Double [] hue = new double [bytes]; // color component
Double [] sat = new double [bytes]; // saturation component
Byte [] inten = new byte [bytes]; // brightness component
Double r, g, B;
For (int I = 0; I <bytes; I ++)
{R = rgbValues [I * 3 + 2];
G = rgbValues [I * 3 + 1];
B = rgbValues [I * 3];
Double theta = Math. acos (0.5 * (r-g) + (r-B)/Math. sqrt (r-g) * (r-g) + (r-B) * (g-B) + 1)/(2 * Math. PI );
Hue [I] = (B <= g )? Theta: (1-theta ));
Sat [I] = 1.0-3.0 * Math. Min (Math. Min (r, g), B)/(r + g + B + 1 );
Inten [I] = (byte) (r + g + B)/3 );}
The conversion from HIS to RGB is similar. These formulas and C # implementation descriptions are omitted here.
4. Summary
The focus of Digital Image Processing Research and software development is on algorithms. To reduce the difficulty and improve efficiency, C # is suitable for implementing, verifying and developing these algorithms. As described in the memory method above, copying images to the memory directly processes the data in the memory, which is fast and highly efficient in development. Through verification, the algorithm is correct and effective, it can also be converted into a pointer method to meet higher speed requirements (such as real-time processing requirements ).
References:
[1] Gong shengrong. digital image processing and analysis [M]. Beijing: Tsinghua University Press, 2006.
[2] Gonzalez. Digital Image Processing [M]. Beijing: Electronics Industry Press, 2009.