Basic Digital Image Processing Algorithm Implementation (2)-section3.3 histogram Processing

Source: Internet
Author: User

From the last section3.2 to the present, after a while, I finally made out the things of the histogram of section3.3 intermittently. I still do not want speed and robustness, but I want to implement it myself, try to write code by yourself without using ready-made functions.

The section3.3 of Digital Image Processing Processing-3E gangsps (DIP) is histogram processing with histogram balancing and histogram normalization, the local histogram processing and image enhancement using the local direct histogram are mainly these, so this time we have implemented these algorithms, with a simple function for displaying the histogram, A Histogram can be displayed in a curve or bar, with one or three channels. Of course, some OpenCV functions are used.

The algorithm content is written in accordance with the book. If you don't talk nonsense, first declare:

enum HistType{Line_hist, Bin_hist};class section_3_3{public://calculate image histogram;bool getImgHist(Mat &src, vector< vector<int> > &hist, int chanels_use);//equalize given imageMat & equalizeImgHist(Mat &src);//show given histogram--note! this change the histogram! bool showHist(string wndname, vector< vector<int> > &hist, HistType histType, int thickness=1,bool bSave=false, string savename=string());//normalize histogram to given range; void normalizeHist(vector< vector<int> > &hist, int maxLeval);//hist matching or hist normalization;//use the point given histogram curve;Mat& histMatching(Mat &src, vector<cv::Point> &hist_graph);//use the given histogram;Mat& histMatching(Mat &src, vector<int> &dest_hist);//equalize use local hist;Mat& localEqualize(Mat &src, int rgSize=3);//image enhancement use local hist;Mat& enhanceImg(Mat &src, int rgSize=3, float E=4.0f, float k0=0.4f, float k1=0.02f, float k2=0.4f);private:Mat m_destMat;Mat m_showMat;};

 

The main function is the previously mentioned functions, which use STL vector to store the histogram. Note that the normalizeHist function is to limit the bin value of the histogram to maxLeval, to facilitate histogram display, histMatching is a histogram normalization or Histogram Matching. Two implementations are provided. One is to use a vector point set to specify the inflection points of the histogram curve, and the other is to directly input a target histogram, convert the histogram of the source image to the same as the target histogram (roughly the same ). Below is the function implementation -- a little long:

bool section_3_3::getImgHist(Mat &src, vector< vector<int> > &hist, int chanels_use){if(src.channels() < chanels_use || !(chanels_use==1||chanels_use==3))return false;Mat temp_gray;if(chanels_use ==1 && src.channels()==3)cvtColor(src, temp_gray, COLOR_BGR2GRAY);//create hist vectors;vector<int> hh(256, 0);for(int i=0; i<chanels_use; ++i){hist.push_back(hh);}//calculate pixelsfor (int i=0; i<src.rows; ++i){uchar *src_ptr = src.ptr<uchar>(i);for (int j=0; j<src.cols; ++j){if(chanels_use==1){++hist[0][src_ptr[j]];}else{int j3 = j*3;++hist[0][src_ptr[j3]];++hist[1][src_ptr[j3+1]];++hist[2][src_ptr[j3+2]];}}}return true;}//normalize hist to the given range, maxLeval gives the upper bound, lower bound is 0;void section_3_3::normalizeHist(vector< vector<int> > &hist, int maxLeval){for (size_t i=0; i

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.