// 直方图的RGB.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "opencv2/opencv.hpp" #define cvQueryHistValue_1D( hist, idx0 ) \ ((float)cvGetReal1D( (hist)->bins, (idx0))) IplImage* DrawHistogram(CvHistogram* hist, int scaleX= 1, int scaleY= 1) { float histMax= 0; cvGetMinMaxHistValue(hist, 0, &histMax, 0, 0); //获取直方图的最大值 if(!histMax) { histMax= 1; } IplImage* imgHist= cvCreateImage(cvSize(256* scaleX, 64* scaleY), 8, 1); cvZero(imgHist); //置零 for(int i=0; i< 255; i++) { float histValue= cvQueryHistValue_1D(hist, i); //获取第i个bin 的值 float nextValue= cvQueryHistValue_1D(hist, i+1); CvPoint pt1= cvPoint((i+ 100)* scaleX, 64* scaleY); //构建四个点 CvPoint pt2= cvPoint((i+ 101)* scaleX, 64* scaleY); CvPoint pt3= cvPoint((i+101)* scaleX, (int )((1- nextValue/ histMax)* 64* scaleY)); CvPoint pt4= cvPoint((i+ 100)* scaleX, (int)((1-histValue/ histMax)* 64* scaleY)); int numPts= 5; CvPoint pts[5]; pts[0]= pt1; pts[1]= pt2; pts[2]= pt3; pts[3]= pt4; pts[4]= pt1; cvFillConvexPoly(imgHist, pts, numPts, cvScalar(255)); } return imgHist; } int _tmain(int argc, _TCHAR* argv[]) { IplImage* src0= cvLoadImage("F://Opencv_picture\\05.jpg"); IplImage* src= cvCreateImage(cvGetSize(src0), IPL_DEPTH_8U, 3); cvConvertScale(src0, src, 1.0, 0); cvNamedWindow("Histogram"); cvShowImage("Histogram", src); int dims= 1; //直方图的维度、 int sizes = 256; //直方图数据个数 float range[]= {0, 255}; //直方图每个维度取值范围 float * ranges[]= {range}; CvHistogram* hist; hist= cvCreateHist(dims, &sizes, CV_HIST_ARRAY, ranges, 1); cvClearHist(hist); IplImage* b_img= cvCreateImage(cvGetSize(src), 8, 1); IplImage* g_img= cvCreateImage(cvGetSize(src), 8, 1); IplImage* r_img= cvCreateImage(cvGetSize(src), 8, 1); cvSplit(src, b_img, g_img, r_img, NULL); //分解图像到三个图像的内存空间 cvCalcHist(&b_img, hist, 0, 0); // 统计一个通道的数据额到hist b_img= DrawHistogram(hist); cvClearHist(hist); cvCalcHist(&b_img, hist, 0, 0); g_img= DrawHistogram(hist); cvClearHist(hist); cvCalcHist(&r_img, hist, 0, 0); r_img= DrawHistogram(hist); cvClearHist(hist); cvNamedWindow("B"); cvNamedWindow("G"); cvNamedWindow("R"); cvShowImage("B", b_img); cvShowImage("G", g_img); cvShowImage("R", r_img); cvWaitKey(0); cvReleaseImage(&src); cvReleaseImage(&r_img); cvReleaseImage(&b_img); cvReleaseImage(&g_img); cvDestroyWindow("Histogram"); cvDestroyWindow("B"); cvDestroyWindow("G"); cvDestroyWindow("R"); return 0; }
FillConvexPoly
填充凸多边形
void cvFillConvexPoly( CvArr* img, CvPoint* pts, int npts,
CvScalar color, int line_type=8, int shift=0 );
img
图像。
pts
指向单个多边形的指针数组。
npts
多边形的顶点个数。
color
多边形的颜色。
line_type
组成多边形的线条的类型。参见cvLine
shift
顶点坐标的小数点位数。
函数cvFillConvexPoly填充凸多边形内部。这个函数比函数cvFillPoly 更快。它除了可以填充凸多边形区域还可以填充任何的单调多边形。例如:一个被水平线(扫描线)至多两次截断的多边形。