// HIstogram.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "opencv2/opencv.hpp" #define cvQueryHistValue_1D( hist, idx0 ) \ ((float)cvGetReal1D( (hist)->bins, (idx0))) int main() { IplImage* src= cvLoadImage("F:\\Opencv_picture\\05.jpg"); IplImage* gray_dst= cvCreateImage(cvGetSize(src), 8, 1); cvCvtColor(src, gray_dst, CV_BGR2GRAY); //一维维数 int dims= 1; //直方图的尺寸 int size= 256; //直方图的高度 int height = 256; //灰度图的范围0到255 float range[]= {0, 256}; float *ranges[]= {range}; //创建一维直方图 CvHistogram* hist; hist= cvCreateHist(dims, &size, CV_HIST_ARRAY, ranges, 1); //计算灰度图的一维直方图 cvCalcHist(&gray_dst, hist, 0, 0); //归一化直方图 cvNormalizeHist(hist, 1.0); int scale= 2; //创建图像,用于显示直方图 IplImage* hist_img= cvCreateImage(cvSize(size* scale, height), 8, 3); //图像置零 cvZero(hist_img); //计算直方图的最大方块值,初始化为0 float max_value= 0; cvGetMinMaxHistValue(hist, 0, &max_value, 0, 0); //绘制直方图 for(int i=0; i<size; i++) { float bin_val= cvQueryHistValue_1D(hist, i); //像素i的概率 int intensity = cvRound(bin_val* height/ max_value); // 绘制的高度 cvRectangle(hist_img, cvPoint(i* scale, height- 1), cvPoint((i+1)* scale- 1, height- intensity), CV_RGB(255, 255, 255)); } cvNamedWindow("gray"); cvShowImage("gray", gray_dst); cvNamedWindow("Histogram"); cvShowImage("Histogram", hist_img); cvWaitKey(0); cvReleaseImage(&src); cvReleaseImage(&gray_dst); cvReleaseImage(&hist_img); cvDestroyWindow("gray"); cvDestroyWindow("Histogram"); }
看了小魏的修行路写的灰度直方图后写的,但我写的程序中出现f:\编程杂物区\histogram\histogram\histogram.cpp(45) : error C3861: “cvQueryHistValue_1D”: 找不到标识符的错误。经过搜索在程序中添加了
#define cvQueryHistValue_1D( hist, idx0 ) \ ((float)cvGetReal1D( (hist)->bins, (idx0)))
程序就不报错了。
但是一样的程序(没有添加程序)在VC++6.0+opencv1.0上就不会出现报错的问题
// HIstogram.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "cv.h" #include "highgui.h" int main() { IplImage* src= cvLoadImage("F:\\Opencv_picture\\05.jpg"); IplImage* gray_dst= cvCreateImage(cvGetSize(src), 8, 1); cvCvtColor(src, gray_dst, CV_BGR2GRAY); //一维维数 int dims= 1; //直方图的尺寸 int size= 256; //直方图的高度 int height = 256; //灰度图的范围0到255 float range[]= {0, 256}; float *ranges[]= {range}; //创建一维直方图 CvHistogram* hist; hist= cvCreateHist(dims, &size, CV_HIST_ARRAY, ranges, 1); //计算灰度图的一维直方图 cvCalcHist(&gray_dst, hist, 0, 0); //归一化直方图 cvNormalizeHist(hist, 1.0); int scale= 2; //创建图像,用于显示直方图 IplImage* hist_img= cvCreateImage(cvSize(size* scale, height), 8, 3); //图像置零 cvZero(hist_img); //计算直方图的最大方块值,初始化为0 float max_value= 0; cvGetMinMaxHistValue(hist, 0, &max_value, 0, 0); //绘制直方图 for(int i=0; i<size; i++) { float bin_val= cvQueryHistValue_1D(hist,i); //像素i的概率 int intensity = cvRound(bin_val* height/ max_value); // 绘制的高度 cvRectangle(hist_img, cvPoint(i* scale, height- 1), cvPoint((i+1)* scale- 1, height- intensity), CV_RGB(255, 255, 255)); } cvNamedWindow("gray"); cvShowImage("gray", gray_dst); cvNamedWindow("Histogram"); cvShowImage("Histogram", hist_img); cvWaitKey(0); cvReleaseImage(&src); cvReleaseImage(&gray_dst); cvReleaseImage(&hist_img); cvDestroyWindow("gray"); cvDestroyWindow("Histogram"); return 0; }