Histogram matching based on OPENCV and C + + underlying implementations

Source: Internet
Author: User

Histogram matching algorithm, also known as the histogram of the regulation. Simply put, it is based on a function, or another picture of the guide, so that the original image changes.

The best feeling to explain is: http://www.360doc.com/content/13/1106/16/10724725_327179043.shtml

Full code: GitHub (It also includes Ostu/Otsu algorithm, histogram equalization algorithm, including two kinds of test pictures).

Because my personal interests and hobbies (P is the teacher forced ...) ), it is not allowed to use a histogram function that is OpenCV encapsulated. According to the example, we understand the bottom operation of the histogram matching algorithm (say more, this example can be the best histogram matching algorithm I have seen to explain, it is difficult to see the example of this algorithm, must read).

Note: the meaning of 0->3 in the example is that the pixels of gray level 0 in the original image are all converted to 3 shades in the original image.


On the code (where Srcimg is the original, dstimg is the graph that needs to be matched, the flag flag is either RGB or grayscale):

Cv::mat ycmatchhist (Cv::mat srcimg, Cv::mat dstimg, int flag) {//****** if an RGB picture is converted to grayscale picture operation ******mat Out (SRCIMG); if (flag = = Yc_rgb) {Cvtcolor (srcimg, out, Cv_bgr2gray);} else if (flag = = Yc_gray) {}int graylevel[colvl];for (int i=0; i<colvl; i++) graylevel[i] = I;int grayarr[colvl];int srcRo w = srcimg.rows;int Srccol = srcimg.cols;int Dstrow = dstimg.rows;int Dstcol = dstimg.cols;float SrcCdfArr[colvl] = {0.F} Float DSTCDFARR[COLVL] = {0.f};float tmp;//* * * the cumulative histogram (probability) distribution of the source picture * * * memset (grayarr, 0, sizeof (Grayarr)); for (size_t nro w = 0; Nrow < Srcrow;   nrow++) for (size_t ncol = 0; Ncol < Srccol; ncol++) {int tag = srcimg.at<uchar> (Nrow, Ncol);   grayarr[tag]++; }tmp = 0;for (int i=0; i<colvl; i++) {tmp + = grayarr[i];srccdfarr[i] = tmp/(Srcrow * srccol);//STD::COUT&LT;&LT;SRCCDF Arr[i]<<std::endl;} To solve the cumulative histogram (probability) distribution of the target picture * * * * memset (grayarr, 0, sizeof (Grayarr)); for (size_t nrow = 0; nrow < dstrow; nrow++) F or (size_t ncol = 0; Ncol < DstcoL   ncol++) {int tag = dstimg.at<uchar> (Nrow, Ncol);   grayarr[tag]++; }tmp = 0;for (int i=0; i<colvl; i++) {tmp + grayarr[i];d stcdfarr[i] = tmp/(Dstrow * dstcol);}  Histogram matching algorithm ***int histmap[colvl];int mintag;for (int i=0; i<colvl; i++) {Float minmap = 10.f;for (int j=0; j<colvl; J + +) {if (Minmap > abs (Srccdfarr[i]-dstcdfarr[j])) {Minmap = ABS (Srccdfarr[i]-dstcdfarr[j]); mintag = j;}} Histmap[i] = Mintag;} for (size_t nrow = 0, nrow < out.rows; nrow++) for (size_t ncol = 0; Ncol < out.cols; ncol++) {int tag =   Out.at<uchar> (Nrow, Ncol);   Out.at<uchar> (Nrow, ncol) = Histmap[tag]; }return out;}

The experimental results are as follows:

The original is;

The graph that needs to be matched is;

The final output is a graph.

The gray cumulative histogram of matching images is:;

The final output of the grayscale cumulative histogram is:

The two are very close, proving that the matching algorithm is feasible (of course, if I say wrong, welcome to face, common progress haha ~)

Histogram matching based on OPENCV and C + + underlying implementations

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.