You are on page 1of 5

Main.

cpp
#include <iostream> using namespace std; #include "histogram.h" void main(){ // READ IMAGE 1 cv::Mat img1 = cv::imread("pic1.jpg"); cvtColor(img1,img1,CV_RGB2GRAY); // easy way to have grayscale :) // READ IMAGE 2 cv::Mat img2 = cv::imread("pic2.jpg"); cvtColor(img2,img2,CV_RGB2GRAY); // easy way to have grayscale :) cv::Mat img_new = img1.clone(); int grayLevel = 256; //HISTOGRAM Histogram1D h; cv::MatND histogram1 = h.getHistogram(img1); //GET HISTOGRAM IMAGE 1 cv::MatND histogram2 = h.getHistogram(img2); //GET HISTOGRAM IMAGE 2 //HISTOGRAM BEFORE SPECIFICATION IMAGE 1 cv::namedWindow("Histogram Image 1"); cv::imshow("Histogram Image 1",h.getHistogramImage(img1)); // IMAGE BEFORE cv::namedWindow("My Image Image 1"); cv::imshow("My Image Image 1",img1); //HISTOGRAM IMAGE 2 cv::namedWindow("Histogram Image 2"); cv::imshow("Histogram Image 2",h.getHistogramImage(img2)); // IMAGE 2 cv::namedWindow("My Image 2"); cv::imshow("My Image 2",img2); //-----------------------------------------------// SPECIFICATION float * prImg1 = new float[grayLevel]; float * mapImg1 = new float[grayLevel];

float * prImg2 = new float[grayLevel]; float n1 = img1.rows * img1.cols; float n2 = img2.rows * img2.cols; cout << "Xac Suat" << endl; // TINH XAC SUAT for(int i = 0 ; i < grayLevel ; i++) { float nk = histogram1.at<float>(i); prImg1[i] = nk / n1; nk = histogram2.at<float>(i); prImg2[i] = nk / n2; mapImg1[i] = 0; } cout << "Cong don" << endl; // CONG DON for(int i = 1 ; i < grayLevel ; i++) { prImg1[i] = prImg1[i - 1] + prImg1[i]; prImg2[i] = prImg2[i - 1] + prImg2[i]; } prImg1[grayLevel - 1 ] = prImg2[grayLevel - 1 ] = 1; cout << "Mapping" << endl; // MAPPING int currentID = 0; for(int i = 1 ; i < grayLevel ; i++) { bool mapped = false; while(mapped == false) { if( prImg1[i] <= prImg2[ currentID ] ) { mapImg1[i] = currentID; mapped = true; } else { currentID++; } }

} cout << "New image" << endl; // New Image for(int i = 0 ; i < img_new.rows ; i++) { uchar* data = img_new.ptr<uchar>(i); for(int j = 0 ; j < img_new.cols ; j++) { int grayValue = (int)data[j * img_new.channels() + 0 ]; float val = mapImg1[ grayValue ]; data[j * img_new.channels() + 0 ] = val ; } } //-----------------------------------------------// HISTOGRAM AFTER cv::namedWindow("Histogram IMAGE NEW"); cv::imshow("Histogram IMAGE NEW",h.getHistogramImage(img_new)); //---Image After cv::namedWindow("IMAGE NEW"); cv::imshow("IMAGE NEW",img_new); cv::waitKey(0); }

Histogram.h
#ifndef _HISTOGRAM #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.hpp> // -> cvtColor class Histogram1D{ private: int histSize[1]; //number of bins float hranges[2]; //min & max pixel value

const float* ranges[1]; int channels[1]; // only 1 channels public: Histogram1D(){ // Prepare arguments for 1D histogram histSize[0] = 256; hranges[0] = 0.0; hranges[1] = 255.0; ranges[0] = hranges; channels[0] = 0; } // get Histogram cv::MatND getHistogram(const cv::Mat &image){ cv::MatND hist; //compute histogram cv::calcHist(&image, 1, // 1 image only channels, //channels cv::Mat(), // no mask hist, // the result 1, // 1D histogram histSize, // number of bins ranges // pixel value range ); return hist; } cv::Mat getHistogramImage(const cv::Mat &image) { //Compute Hist cv::MatND hist = getHistogram(image); //Get Min and Max Bin Values double max = 0; double min = 0; cv::minMaxLoc(hist,&min,&max,0, 0); //Histogram Image cv::Mat histImg (histSize[0], histSize[0],

CV_8U,cv::Scalar(255)); //set highest point int hpt = static_cast<int>(0.9* histSize[0]); // Draw bins for(int h = 0; h < histSize[0]; h++){ float value = hist.at<float>(h); int intensity = static_cast<int>(value * hpt / max); // Draw lines cv::line(histImg, cv::Point(h,histSize[0]), cv::Point(h,histSize[0] - intensity), cv::Scalar::all(0) ); } return histImg; } }; #endif

You might also like