1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <iostream> #include <opencv2/opencv.hpp>
using namespace std; using namespace cv;
int p_cnt[256]; float p_sum[256]; int s[256]; int rows,cols; void DrawHistogram(string name,Mat src){ for(int i=0;i<256;i++)p_cnt[i]=0; int maxvalue = 0; for(int i=0;i<rows;i++) { for(int j=0;j<cols;j++) { int p = src.at<uchar>(i,j); p_cnt[p]+=1; if(p_cnt[p]>maxvalue){ maxvalue=p_cnt[p]; } } } cv::Mat histogram(Size(256,256),CV_8U,Scalar(0)); for(int i=0;i<256;i++){ int h=p_cnt[i]*255/maxvalue; line(histogram,Point(i,255),Point(i,255-h),Scalar(255)); } imshow(name,histogram); } int main(int argc,char **argv){ Mat img_in,img_out; img_in=imread(argv[1],IMREAD_UNCHANGED); if(img_in.empty()){ cout<<"Error! Input image cannot be read...\n"; return -1; } imshow("src",img_in); cv::Mat grayimg; cv::cvtColor(img_in,grayimg,CV_BGR2GRAY); imshow("均衡化前的图像",grayimg); rows = grayimg.rows; cols = grayimg.cols; int N = cols*rows; DrawHistogram("均衡化前的直方图",grayimg); for(int i=0;i<256;i++){ if(i==0){ p_sum[i]=1.0*p_cnt[i]/N; } else p_sum[i]=p_sum[i-1]+1.0*p_cnt[i]/N; s[i] = p_sum[i]*255+0.5; } cv::Mat finaimg(grayimg); for(int i=0;i<rows;i++) { for(int j=0;j<cols;j++) { int p = grayimg.at<uchar>(i,j); finaimg.at<uchar>(i,j) = s[p]; } } imshow("均衡化后的图像",finaimg); DrawHistogram("均衡化后的直方图",finaimg); cout << "Press any key to exit...\n"; waitKey(); return 0; }
|