OPENCV Learning Note 9 histogram equalization and plotting histograms

Source: Internet
Author: User
Tags ranges

To do histogram equalization and draw the histogram, the following functions are mainly required:

1, Cvapi (void)  cvequalizehist (const cvarr* SRC, cvarr* DST);
This function is very simple to use, only need to pass in the source image and the initialized target image.

First parameter: const cvarr* SRC: Source image to be processed;

Second parameter: cvarr* DST: Target image;

In Cvequalizehist (), the original image and the target image must be a single-channel, 8-bit image of the same size. For color images, you must first use Cvsplite () to separate each channel, and then process it separately.

2,cvapi (void) cvcvtcolor (const cvarr* SRC, cvarr* dst, int code);
Known by the previous function, because the source image we pass in must be a single-channel image, we convert the image to a single-channel image, using Cvcvtcolor (), before we do anything else.

First parameter: const cvarr* SRC: Source image to be processed;

Second parameter: cvarr* DST: Target image;

The third parameter: int code: The mode of color space conversion, which implements different types of color space conversions. For example, Cv_bgr2gray is converted to grayscale, CV_BGR2HSV converts the picture from RGB space to HSV space. When code chooses Cv_bgr2gray, DST needs to be a single-channel picture. When code selects CV_BGR2HSV, the RGB values need to be normalized to 0-1 for 8-bit graphs. So the range of H in the HSV graph is 0-360,s and V is 0-1.

3,cv_inline void Cvcalchist (iplimage** image, cvhistogram* hist,

                             int accumulate Cv_default (0),                             const cvarr* Mask cv_default (NULL)) <span style= "Font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > </span>
The first parameter: iplimage** Image: Enter images (you can also use cvmat**).
Second parameter: cvhistogram* hist:: Histogram pointer
Third parameter: int accumulate: Cumulative identity. If set, the histogram is not zeroed at the beginning. This feature guarantees that a single histogram can be computed for multiple images, or that the histogram is updated online.
The fourth parameter: Const cvarr* Mask: Operation Mask, determines which pixel of the input image is counted function Cvcalchist calculates the histogram of single-channel or multi-channel image. The array elements used to add straight squares can be extracted from the same location as the corresponding input image.

4,Cvapi (cvhistogram*) cvcreatehist (int dims, int* sizes, int type,

                                   float** Ranges Cv_default (NULL),                                   int uniform cv_default (1));

The 3rd function shows that we want to calculate the histogram, we must have a histogram pointer, then we have to first use Cvcreathist () to create a histogram pointer;

The first parameter: Dim is a few dimensional space, that is, the general color image is 3-channel, dim=3, and the grayscale figure is 1-channel, Dim=1
Second parameter: sizes if you want to create 255 intervals in the image, set the sizes=10
The third parameter: The representation format of the histogram: Cv_hist_array means that the histogram data is represented as a multidimensional dense array cvmatnd; Cv_hist_tree means that the histogram data is represented as a multidimensional sparse array cvsparsemat.
Fourth parameter: An array of square extents in the diagram. Its content depends on the value of the parameter uniform. The purpose of this range is to determine when to calculate histograms or to determine the inverse mapping (backprojected), which corresponds to which/which set of values for the input image. If the image is ipl_depth_8u, that is, the depth is 8, then set to 255 is more appropriate, if the depth of the 8-bit image ranges set to 0-255, then the pixels on the image will be counted in the 0~255 (that is, the first histogram box).
Fifth parameter: Normalized identity. If not 0, then Ranges[i] (0<=i<cdims, translator Note: Cdims is the dimension of the histogram, for a grayscale figure of 1, a color figure of 3) is a range array containing two elements, including the upper and lower bounds of the histogram I-dimension. The entire area on the first dimension [Lower,upper] is divided into dims[i] equal blocks (translator note: Dims[i] represents the number of blocks of the histogram I-dimension), these blocks are used to determine the first value of the input pixel (translator Note: For color images, I determine r, G, or b) corresponding blocks If 0, then Ranges[i] is an array of ranges containing dims[i]+1 elements, including Lower0, Upper0, lower1, Upper1 = = Lower2, ..., upperdims[i]-1, where Lowerj And UPPERJ are the upper and lower bounds of the first J block of the Histogram I-dimension (for the first value of the input pixels). In any case, if the input value is outside the range specified by a straight square, it will not be counted by cvcalchist and will be zeroed by the function Cvcalcbackproject. The function cvcreatehist creates a histogram of the specified size and returns a pointer to the created histogram. If the ranges of an array is 0, the range of the straight squares must be specified later by the function cvsethistbinranges. Although Cvcalchist and Cvcalcbackproject can handle 8-bit images without having to set the range of any straight squares, they are assumed to divide the space between the 0..255.


5,cvapi (void) cvrectangle (cvarr* img, cvpoint pt1, Cvpoint pt2,

                          Cvscalar color, int thickness cv_default (1),                          int line_type cv_default (8),                          int shift Cv_default (0));
function function: Draws a simple, specified thickness, or filled rectangle with two vertices on the diagonal. Because we want to draw the histogram, we choose the rectangle drawing function.

Parameter description:
The first parameter: IMG--an image.
Second parameter: pt1--a vertex of the rectangle.
Third parameter: pt2--another vertex on the diagonal of a rectangle
Fourth parameter: color-line Color (RGB) or luminance (grayscale image) (grayscale images).
Fifth parameter: thickness-the thickness of the line that makes up the rectangle. A negative value (such as the cv_filled) function draws a color-filled rectangle.
Sixth parameter: Line_type-the type of line. See Cvline's description
The seventh parameter: Shift-the number of decimal places of the coordinate point.
Eighth parameter: cvsize cvsize (int height,int width)

6,cvapi (void) cvgetminmaxhistvalue (const cvhistogram* hist,

                                   float* Min_value, float* max_value,                                   int* min_idx cv_default (null),                                   int* max_idx cv_default (null));
function function: Find maximum and minimum straight squares

Parameter description:

First parameter: hist histogram
Second parameter: A pointer to the minimum value of the Min_value histogram
Third parameter: A pointer to the maximum value of the Max_value histogram
Fourth parameter: A pointer to the smallest coordinate in the MIN_IDX array
Fifth parameter: A pointer to the largest coordinate in the MAX_IDX array
The function Cvgetminmaxhistvalue finds the largest and smallest straight squares and their positions. Any output variable is optional. In a few extrema with the same value, returns the one with the smallest subscript index (in alphabetical order).

7,cvqueryhistvalue_1d (hist, idx0)
function function: Access the histogram element, the same as the cvgetreal2d function.

OK, now that you've covered all the required functions, here's a sample code that's verified:

Note: This routine has not been optimized, and the code that draws the histogram section is somewhat repetitive and should be simplified to a function call. But for beginners, it may be easier to understand this approach.

#include <cv.h> #include The results are shown below:

Original image and Histogram:


The result image and histogram after the histogram equalization:




OPENCV Learning Note 9 histogram equalization and plotting histograms

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.