OpenCV image recognition from zero to proficient (-----histogram equalization and histogram stretching

Source: Internet
Author: User
Tags ranges scalar

First, histogram equalization

Histogram equalization is an important application of gray-scale transformation, it is widely used in image enhancement processing, it is based on the cumulative distribution function transformation of the histogram correction method, can produce a gray scale distribution with uniform probability density of the image, expand the value dynamic range of the pixel. The gray value of many images is non-uniform distribution, in which the gray value concentration in a small interval of the image is very common, histogram equalization is a way to enhance the image contrast by re-evenly distributing the gray values, after the histogram equalization of the image to two value threshold selection is very advantageous. In general, histogram corrections can improve the subjective quality of an image, so it is useful when working with art images. The central idea of the histogram equalization process is to change the gray histogram of the original image from a certain gray area in the comparative concentration to the uniform distribution in the whole gray range.


The call in OpenCV is the following function, which is convenient

<span style= "FONT-SIZE:18PX;" >void equalizehist (Inputarray src,outputarraydst);</span>

(1) It is usually used to increase the global contrast of many images, especially when the contrast of useful data of an image is quite close.

(2) Brightness can be better distributed on the histogram. This can be used to enhance the local contrast without affecting the overall contrast, histogram equalization by effectively extending the common brightness to achieve this function.

(3) It is useful for images that are too bright or too dark for backgrounds and foreground, especially for better bone structure display in X-ray images and for better detail in overexposed or less exposed photos.

(4) One of the main advantages is that it is a fairly intuitive technique and is reversible, and if the equalization function is known, the original histogram can be restored and the computational amount will be small.

(5) One drawback is that it does not choose to process the data, it may increase the contrast of background noise and reduce the contrast of useful signals.

Use histogram equalization to get images separately

<span style= "FONT-SIZE:18PX;" > #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream>using Namespace Cv;using namespace Std;int main (int argc, const char** argv) {Mat img = imread ("Lena.jpg", Cv_load_image_c Olor);          Open and read the image if (Img.empty ()) {cout << "image cannot be loaded..!!" << Endl;     return-1; } cvtcolor (IMG, IMG, cv_bgr2gray);     Change the color image to grayscale image Mat img_hist_equalized; Equalizehist (IMG, img_hist_equalized);     Equalize the Histogram//create Windows Namedwindow ("Original Image", cv_window_autosize);     Namedwindow ("Histogram equalized", cv_window_autosize);     Show the image imshow ("Original image", IMG);     Imshow ("Histogram equalized", img_hist_equalized); Waitkey (0); Wait for Key press Destroyallwindows (); Destroy all open windows return 0;} </span>


Because the histogram is still balanced, so the histogram will be used, if you use the histogram equalization alone, there is only one function, so this can be processed after the image histogram and equalization of images

<span style= "FONT-SIZE:18PX;" > #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <stdio.h> #include <opencv2\core\core.hpp>using namespace cv;using namespace Std;int img_hist (mat&        Image) {if (!image.data) {cout << "fail to load image" << Endl;    return 0;    } Mat Img_gray;    GRAY if (image.channels () ==3) {Cvtcolor (image, Img_gray, Cv_bgr2gray);    } else {Image.copyto (img_gray);    } cv::imwrite ("Img_gray.jpg", Img_gray);           Matnd hist;    int dims = 1;    Float hranges[] = {0, 255};   const FLOAT *ranges[] = {Hranges};    Here is required for the const type int size = 256;    int channels = 0;    Calculate the histogram of the image calchist (&img_gray, 1, &channels, Mat (), Hist, dims, &size, ranges);    CV is cvcalchist int scale = 1;    Mat imageshow (Size * scale, size, cv_8u, Scalar (0));    Gets the maximum and minimum values of double minval = 0;    Double maxval = 0; MinmaXloc (Hist,&minval, &maxval, 0, 0);    CV is the Cvgetminmaxhistvalue//display histogram of the image int HPT = saturate_cast<int> (0.9 * size);           for (int i = 0; i < i++) {Float value = hist.at<float> (i);        Note that hist is in float type CV with cvqueryhistvalue_1d int realvalue = saturate_cast<int> (value * hpt/maxval);    Rectangle (Imageshow,point (I*scale, size-1), point ((i+1) *scale-1, Size-realvalue), Scalar (255));    } namedwindow ("Hist");    Imshow ("Hist", imageshow);    Cv::imwrite ("Hist.jpg", imageshow);    Mat equalize_hist;    Cv::equalizehist (img_gray,equalize_hist);    Namedwindow ("Equalize_hist");    Imshow ("Equalize_hist", equalize_hist);    Cv::imwrite ("Equalize_hist.jpg", equalize_hist);    Calculate the histogram of the image calchist (&equalize_hist, 1, &channels, Mat (), Hist, dims, &size, ranges);       CV is cvcalchist Mat imageshow_equal (Size * scale, size, cv_8u, Scalar (0)); Gets the maximum and minimum values of Minmaxloc (Hist,&minval,&maxval, 0, 0);       CV is the Cvgetminmaxhistvalue//display histogram of the image HPT = saturate_cast<int> (0.9 * size);           for (int i = 0; i < i++) {Float value = hist.at<float> (i);           Note that hist is in float type CV with cvqueryhistvalue_1d int realvalue = saturate_cast<int> (value * hpt/maxval);       Rectangle (Imageshow_equal,point (I*scale, size-1), point ((i+1) *scale-1, Size-realvalue), Scalar (255));       } namedwindow ("Hist_equalize");       Imshow ("Hist_equalize", imageshow_equal);    Cv::imwrite ("Hist_equalize.jpg", imageshow_equal);    Waitkey (0); return 0;}    int main (int args, char** argv) {Mat image = Imread ("lena.jpg", 1); This can also be BGR but think of the extract contour effect is the same as the Imshow ("original", image);    Img_hist (image); Waitkey (); return 0;} </span>

Second, straight histogram extension

Transform function: Transforms a grayscale value of an image into another grayscale.
The core of the histogram transformation is the transformation function, S=t (R), R is the gray value before the transformation, S is the transformed gray value, if we want to transform [a, b] interval of gray to the [0,255] range, then the transformation function is:T (R) =255* (r-a)/(B-A).

First we need to find Imin IMAX.

<span style= "FONT-SIZE:18PX;" >int imax,imin;  for (imin=0;imin<256;imin++)  {      if (hist.at<uchar> (imin) >minvalue) break          ;  }  for (imax=255;imax>-1;imax--)  {      if (hist.at<uchar> (IMAX) >minvalue) break          ;  }  </span>

Then there is a mapping function

<span style= "FONT-SIZE:18PX;" >mat lookup (1,256,cv_8u);  for (int i=0;i<256;i++)  {      if (lut.at<uchar> (i) <imin)          lut.at<uchar> (i) =0;      else if (lut.at<uchar> (i) >imax)          lut.at<uchar> (i) =255;      else          lut.at<uchar> (i) =static_cast<uchar> (          255.0* (i-imin)/(imax-imin) +0.5);  }  </span>


Finally, using a Lut

<span style= "FONT-SIZE:18PX;" >lut (Image,lut,result);</span>


Good Article sharing http://blog.csdn.net/cv_ronny/article/details/17507671



OpenCV image recognition from zero to proficient (-----histogram equalization and histogram stretching

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.