Project notes --- CSharp Image Processing
Recently, due to the project's need for binarization processing of images, I learned about image processing, from the significance of binarization at the beginning to the binarization of Dynamic Threshold Value detection, and so on, and using C # For application, I learned a lot and shared my personal experience with you, I hope to help you. To simplify binarization, A 0/1 operation is performed on a colored image to display a black and white image, most of its significance lies in the segmentation and identification of the image after binarization. Most of the Verification Code tools for automatic identification are binarization first, and then the verification code is finally deduced in pattern recognition; in my project, because the hardware only supports black and white, we need to process the user's image and display it on the hardware. In the process of in-depth understanding of binarization, I found a lot of interesting things that interest me very much, that is, various image processing algorithms. Because it is a common image with few colors, it is barely acceptable for a relatively simple image after binarization. However, after binarization processing, a beautiful image looks very ugly, or it doesn't make any sense at all. In this case, everyone began to study and think about it. I would like to add some knowledge first, because I don't think that if I don't clarify the basic principles, we may not find out how interesting these algorithms are (of course, or I like these things too much ). The process is like this. A colored image needs to be phased-out first (some add R + G + B by 3 and take the average value and then pay R = G = B, you can also perform gray-scale division based on the weight value, for example (0.299 * r + 0.587 * g + 0.114 * B). This is a gray-scale algorithm that optimizes the discrimination of different RGB colors based on human eyes, it's interesting, but we can't think of humans recognizing different colors ). After grayscale, the actual color value of each color is R = G = B = (between 0 and 127), so that we can certainly divide it, if it is less than 127, it is considered to be close to black 0, otherwise it is considered to be close to white 255. After dividing all the color values based on 127, the image will become a black-and-white binarization image. Looking back, let's take a look at it. The "distortion" of the image after binarization processing is still very serious. Is there any way to optimize it? Of course, it is difficult for experts who study these algorithms. Ordered dithering Ordered jitter is a magic Algorithm for decomposing data. The details of the algorithm are not further explored. It is probably based on an algorithm matrix, and then the image points are processed. The following is an image comparison. This image is the original image. This image is a 128 binarization image with a global threshold. This image is a binarization image after sequential jitter processing, black and white binarization images (not grayscale). All vertices are non-black and white, and visual errors of grayscale images are generated. This is amazing. (Note: If the source image is a big image, the effect is more obvious .) In addition, there are many excellent algorithms to process images, most of which are produced by how to process and determine the "threshold value. AForge. Net. Image found a powerful AForge. Net when searching for C # Open-source class libraries. You can refer to its official website for more details. This open-source class library is so powerful that it not only contains various image processing algorithm methods, video processing methods, but also various practices of artificial intelligence, all written based on C, code cleanliness is also worth learning, so if you have time to study it carefully in the future. In addition, the official documentation and Sample are all perfect and powerful. N multiple image processing methods. Refer to the Demo and you will find it extremely simple to use ~~ The code sample is too long. The following shows how the code is implemented: the Code may not be complete here. Please refer to the official AForge.. NET Framework-2.2.5 \ Samples \ Imaging \ FiltersDemo this Demo copies the code Bitmap temp = AForge. imaging. image. clone (new Bitmap (SrcPic), PixelFormat. format24bppRgb); // load the image and forcibly convert it to the Format24bppRgb format temp = Grayscale. commonAlgorithms. RMY. apply (temp); // grayscale the image according to the RY algorithm. Many algorithms are grayscale before processing. PictureBox. image = (new OrderedDithering ()). apply (sourceImage); // Apply Filter. Here, select the Filter copy code of the OrderedDithering type. This is the application AForge. net implements a variety of image processing code, which is very simple and highly scalable and worth learning. For more code, refer to the official Sample. If you have any questions, please reply to me. Note that although there is a very powerful AForge. net, but you still need to write code for some specific image processing requirements. Of course, you can also use AForge to implement it, here I just want to emphasize how to handle it if I manually write code and what should I pay attention. First of all, from the grayscale of color images: the so-called grayscale is to convert the R, G, and B values into the same value according to a certain algorithm, among them, the common practice is to (R + G + B)/3 to take the average value, the other is the weighted algorithm (0.299 * r + 0.587 * g + 0.114 * B) = R = G = B Based on human eyes recognition of different colors. Copy the code /// <summary> /// grayscale implementation /// </summary> /// <param name = "bmp"> </param> // <param name = "foo"> </param> // <returns> </returns> private static Bitmap WeightGrayScaleImple (Bitmap bmp, func <double, byte> foo) {Bitmap thisMap = bmp; Rectangle rect = new Rectangle (0, 0, thisMap. width, thisMap. height); BitmapData bmp data = thisMap. lockBits (rect, ImageLockMode. readWrite, thisMap. pixel Format); unsafe {byte * ptr = (byte *) (BMP data. scan0); for (int I = 0; I <BMP data. height; I ++) {for (int j = 0; j <BMP data. width; j ++) {ptr [0] = ptr [1] = ptr [2] = foo (ptr [2], ptr [1], ptr [0]); ptr + = 4;} ptr + = BMP data. stride-BMP data. width * 4 ;}} thisMap. unlockBits (BMP data); return thisMap;} copy the code and copy the code. // Foo implements private static byte WeightGrayBinaraztion (double r, double g, double B) {return (Byte) (0.299 * r + 0.587 * g + 0.114 * B); // Feature Weight} copy the code. Note: when processing pointer types such as image values in C, unsafe must be enabled; otherwise, the efficiency is extremely low. (Enable the unsafe switch in the project: Project properties ---> Generate ---> allow Insecure code.) Basically, the above content is all the content used to process images in the project, we hope that the above content will help you. If you have any questions, please reply. Thank you.