Project notes --- CSHARP Image Processing

Source: Internet
Author: User

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.

 

Binarization

In terms of binary simplification, A 0/1 operation is performed on a color image, and a black and white image is displayed. 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, the hardware only supports black and white, therefore, you must process your images and display them 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 figure shows a binarization image with 128 global thresholds.

 

 

This figure is a binarization image after the orderly jitter processing. The black-and-white binarization image (not grayscale), all vertices are non-black and white, visually, the visual error of the grayscale image is generated, which is magical. (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

When searching for C # Open-source class libraries, you find the powerful aforge. net. 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 ~~

Sample Code

After so many arguments, the following shows how the code is implemented:

The code here may not be complete. Please refer to the demo of the official aforge. NET Framework-2.2.5 \ samples \ imaging \ filtersdemo

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 of ordereddithering type.

 

This is a variety of image processing Code implemented by the application aforge. net, which is simple and highly scalable and worth learning.

For more code, refer to the official sample. If you have any questions, please reply to me.

Postscript

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.

/// <Summary> /// grayscale implementation /// </Summary> /// <Param name = "BMP"> </param> /// <Param name = "foo"> </param> // <returns> </returns> Private Static bitmap weightgrayscaleimple (bitmap BMP, func <Double, double, 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. pixelformat); 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 ;}
        
// Foo implementation
Private Static byte weightgraybinaraztion (Double R, double G, double B) {return (byte) (0.299 * r + 0.587 * g + 0.114 * B); // feature weight}

Note: When processing pointer types such as image values in C #, you must enable unsafe; 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. I hope the above content will be helpful to you. If you have any questions, please reply. Thank you.

 


Reference

Http://en.wikipedia.org/wiki/Ordered_dithering

Http://www.aforgenet.com/aforge/framework/

 

Project notes --- CSHARP Image Processing

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.