I have recently touched on image analysis and recorded some experiences. If there are any mistakes, please criticize and correct them!
I used the opencv API to read and write images,
This well-known library can be obtained at http://sourceforge.net/projects/opencvlibrary.
Image analysis and related operations are implemented using C.
First, let's take a look at how the histogram of the grayscale graph is calculated. It counts the number of occurrences of a gray scale in each pixel.
(Gray level indicates the brightness level range of the grayscale image, and gray level indicates the brightness value of a single pixel of the grayscale image)
The horizontal axis of the histogram is gray, and the vertical axis is the number of occurrences.
// Obtain the original image histogram/* parameter: iplimage * BMP input grayscale image int * hist output array form 256 gray level histogram */void drawimagehist (iplimage * BMP, int * hist) {int I = 0; int COUNT = BMP-> height * BMP-> width; memset (Hist, 0, sizeof (INT) * 256); for (I; I <count; I ++) {(* (hist + (uchar *) (BMP-> imagedata) [I]) ++; // The number of pixels in the same gray scale is auto-incrementing }}
Image equalization is a ing and conversion process between the original gray level and the new gray level. The new gray level is generated based on the Accumulation density of the original gray level.
After equalization, the histogram tends to be flat, the gray level is reduced, and the gray level is merged. For example, after the entire picture is balanced, it can become black and white.
// Balance the original image to obtain the original histogram, cumulative histogram density, balanced histogram and gray-level ing/* parameter: iplimage * srcfile input grayscale image int * hist output array form 256 gray level histogram float * histcumudens output array form 256 gray level histogram cumulative density int * equalhist output array form 256 gray level returned value of the balanced histogram: result image */iplimage * drawequalimage (iplimage * srcfile, int * Hist, float * histcumudens, int * Temporary hist) {int temp [256]; // 256 gray-grade, contains the cumulative int I = 0 corresponding to the gray level of the original histogram; int pointcount = srcfile-> width * srcfile-> height; int mapping [256]; float COUNT = (float) pointcount; iplimage * resfile; resfile = cvcloneimage (srcfile); drawimagehist (srcfile, hist); memset (temp, 0, sizeof (INT) * 256); temp [0] = * hist; // accumulate for (I = 1; I <256; I ++) {temp [I] = temp [I-1] + * (hist + I );} for (I = 0; I <pointcount; I ++) {int glevel = (uchar *) (srcfile-> imagedata) [I]; // retrieve the gray int accumulate = temp [glevel] of the vertex; // obtain the accumulation corresponding to the gray level (uchar *) (resfile-> imagedata )) [I] = (INT) (255 * accumulate/pointcount);} for (I = 0; I <256; I ++) {int accumulate = temp [I]; * (mapping + I) = (INT) (255 * accumulate/pointcount); // ing includes the balanced gray level corresponding to the original gray level} memset (equalhist, 0, sizeof (INT) * 256); for (I = 0; I <pointcount; I ++) {(* (equalhist + (uchar *) (resfile-> imagedata )) [I]) ++;} // obtain the balanced histogram for (I = 0; I <256; I ++) {* (histcumudens + I) = (float) temp [I]/pointcount;} return resfile ;}
Normalization is also a process of gray-level ing conversion. It converts the gray-level distribution of input images into a histogram that defines an expected gray-level distribution.
First, we can obtain the cumulative density of the original image and the expected image (which can be achieved through equalization)
Then, based on the cumulative density of the original gray level, find the gray level equal (not the most similar) in the expected cumulative density to establish a ing relationship.
Finally, the image is converted based on the gray-level ing relationship.
// Define the original image/* parameter: iplimage * srcfile input grayscale image float * srchistcumudens input array form of original histogram cumulative density float * tarhistcumudens input array form of target histogram cumulative density return value: result image */iplimage * drawspecimage (iplimage * srcfile, float * srchistcumudens, float * tarhistcumudens) {int I = 0; int tar_count = 0; Int J = 0; float max = 1; float last_scale =-1; iplimage * resfile; resfile = cvcloneimage (srcfile); int mapping [256]; for (I = 0; I <256; I ++) {int cur_scale = 0; If (* (srchistcumudens + I) = last_scale) continue; else {for (j = tar_count; j <256; j ++) {float diff = * (srchistcumudens + I)-* (tarhistcumudens + J); If (diff = 0) {cur_scale = J; break ;} else {If (max> diff) & (-max <diff) {cur_scale = J; If (diff> = 0) {max = diff ;} else {max =-diff ;}}} last_scale = * (srchistcumudens + I); tar_count = cur_scale; max = 1; * (mapping + I) = cur_scale ;} for (INT I = 0; I <srcfile-> width * srcfile-> height; I ++) {int gvalue = (uchar *) (srcfile-> imagedata )) [I]; (uchar *) (resfile-> imagedata) [I] = mapping [gvalue];} return resfile ;}
----------------------------------
Author: Chen Jin)
This article is an original article. If you need to repost and quote it, please specify the original author and link. Thank you.