有点晚了先开个头,明天翻译具体内容
第四章 用直方图统计像素
这一章包括:
计算图像的直方图
应用查表以修改图像外观
补偿图像直方图
幕后使用直方图以检测特定的图像内容
使用平均移动算法以找到物体
使用直方图比较以恢复相似图像
计算灰度图像的直方图,并用图显示出来:
1 #include <cv.h> 2 #include <highgui.h> 3 4 using namespace std; 5 using namespace cv; 6 7 class Histogram1D{ 8 private: 9 int histSize[1]; 10 float hranges[2]; 11 const float* ranges[1]; 12 int channels[1]; 13 public: 14 Histogram1D(){ 15 histSize[0]=256; 16 hranges[0]=0.0; 17 hranges[1]=255.0; 18 ranges[0]=hranges; 19 channels[0]=0; 20 } 21 22 MatND getHistogram(const Mat& image){ 23 MatND hist; 24 calcHist(&image, 1, channels, Mat(), hist, 1, 25 histSize, ranges); 26 return hist; 27 } 28 Mat getHistogramImage(const Mat& image){ 29 MatND hist=getHistogram(image); 30 double maxVal=0; 31 double minVal=0; 32 minMaxLoc(hist, &minVal, &maxVal, 0, 0); 33 Mat histImg(histSize[0], histSize[0], CV_8U, Scalar(255)); 34 int hpt=static_cast<int>(0.9*histSize[0]); 35 for(int h=0; h<histSize[0]; h++){ 36 float binVal=hist.at<float>(h); 37 int intensity=static_cast<int>(binVal*hpt/maxVal); 38 line(histImg, Point(h, histSize[0]), Point(h, histSize[0]-intensity),Scalar::all(0)); 39 } 40 return histImg; 41 } 42 }; 43 44 45 46 int main(){ 47 Mat image=imread("C:/testdir/barcode.bmp"); 48 Histogram1D h; 49 MatND histo=h.getHistogram(image); 50 /* for(int i=0; i<256; i++){ 51 cout << "Value " << i << "=" << 52 histo.at<float>(i) << endl; 53 } 54 */ 55 namedWindow("Histogram"); 56 imshow("Histogram", h.getHistogramImage(image)); 57 waitKey(0); 58 }
threshold是边缘检测函数,例如可以这样应用
1 #include <cv.h> 2 #include <highgui.h> 3 4 using namespace std; 5 using namespace cv; 6 7 class Histogram1D{ 8 private: 9 int histSize[1]; 10 float hranges[2]; 11 const float* ranges[1]; 12 int channels[1]; 13 public: 14 Histogram1D(){ 15 histSize[0]=256; 16 hranges[0]=0.0; 17 hranges[1]=255.0; 18 ranges[0]=hranges; 19 channels[0]=0; 20 } 21 22 MatND getHistogram(const Mat& image){ 23 MatND hist; 24 calcHist(&image, 1, channels, Mat(), hist, 1, 25 histSize, ranges); 26 return hist; 27 } 28 Mat getHistogramImage(const Mat& image){ 29 MatND hist=getHistogram(image); 30 double maxVal=0; 31 double minVal=0; 32 minMaxLoc(hist, &minVal, &maxVal, 0, 0); 33 Mat histImg(histSize[0], histSize[0], CV_8U, Scalar(255)); 34 int hpt=static_cast<int>(0.9*histSize[0]); 35 for(int h=0; h<histSize[0]; h++){ 36 float binVal=hist.at<float>(h); 37 int intensity=static_cast<int>(binVal*hpt/maxVal); 38 line(histImg, Point(h, histSize[0]), Point(h, histSize[0]-intensity),Scalar::all(0)); 39 } 40 return histImg; 41 } 42 }; 43 44 45 46 int main(){ 47 Mat image=imread("C:/testdir/Koala.jpg"); 48 // Mat image=imread("C:/testdir/barcode.bmp"); 49 Histogram1D h; 50 Mat thresholded; 51 threshold(image, thresholded, 60, 255, THRESH_BINARY); 52 MatND histo=h.getHistogram(image); 53 /* for(int i=0; i<256; i++){ 54 cout << "Value " << i << "=" << 55 histo.at<float>(i) << endl; 56 } 57 */ 58 namedWindow("Histogram"); 59 imshow("Image", thresholded); 60 imshow("Histogram", h.getHistogramImage(image)); 61 waitKey(0); 62 }
使用equalizeHist函数的时候报错,google之后发现要求图片格式应该为CV_8UC1才可以用的,所以平凡图像需要先转化咯:
1 #include <cv.h> 2 #include <highgui.h> 3 4 using namespace std; 5 using namespace cv; 6 7 Mat equalize(const Mat& image){ 8 Mat result(image); 9 equalizeHist(image, result); 10 return result; 11 } 12 13 int main(){ 14 char* name="C:/testdir/Koala.jpg"; 15 IplImage *img = cvLoadImage( name, CV_LOAD_IMAGE_GRAYSCALE ); 16 Mat src( img, 0 ); 17 src = Mat_<float>( src ); 18 Mat dst = Mat::zeros( src.rows, src.cols, src.type() ); 19 src = Mat_<uchar>(src); 20 equalizeHist( src, dst ); 21 imshow("dst", src); 22 waitKey(0); 23 return 0; 24 }
时间: 2024-09-28 18:33:29