首页 > 代码库 > OpenCV编程->RGB直方图统计
OpenCV编程->RGB直方图统计
我们在处理彩色图像时,特别是在做局部图像的阈值分割时,需要一个直观的RGB统计图。
接下来开始实现。
代码:
void CalcHistRGB() { IplImage* img_source; if (img_source = cvLoadImage("101.jpg",1)) { IplImage* RedChannel = cvCreateImage( cvGetSize(img_source), 8, 1); IplImage* GreenChannel = cvCreateImage( cvGetSize(img_source), 8, 1); IplImage* BlueChannel = cvCreateImage( cvGetSize(img_source), 8, 1); IplImage* alphaChannel = cvCreateImage( cvGetSize(img_source), 8, 1); IplImage* gray_plane = cvCreateImage(cvGetSize(img_source),8,1); //分割为单通道图像 cvSplit(img_source,BlueChannel,GreenChannel,RedChannel,0); // 显示图像 cvNamedWindow( "RedChannel", 1 ); cvNamedWindow( "GreenChannel", 1 ); cvNamedWindow( "BlueChannel", 1 ); cvNamedWindow( "lphaChannel", 1 ); cvShowImage( "RedChannel", RedChannel ); cvShowImage( "GreenChannel", GreenChannel ); cvShowImage( "BlueChannel", BlueChannel ); cvShowImage( "lphaChannel", alphaChannel ); cvCvtColor(img_source,gray_plane,CV_BGR2GRAY); cvNamedWindow("GrayPlane",1); cvShowImage("GrayPlane",gray_plane); //OpenCV中不管是Windows中Load的还是摄像头取得的都是BGR顺序排列的 //然后为这四幅图创建对应的直方图结构。 int hist_size = 100; int hist_height = 100; float range[] = {0,255}; float* ranges[]={range}; CvHistogram* r_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1); CvHistogram* g_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1); CvHistogram* b_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1); CvHistogram* gray_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1); //接下来计算直方图,创建用于显示直方图的图像,略去了一部分重复代码,以下也是 cvCalcHist(&RedChannel,r_hist,0,0); cvCalcHist(&GreenChannel,g_hist,0,0); cvCalcHist(&BlueChannel,b_hist,0,0); cvCalcHist(&gray_plane,gray_hist,0,0); cvNormalizeHist(gray_hist,1.0); cvNormalizeHist(r_hist,1.0); cvNormalizeHist(g_hist,1.0); cvNormalizeHist(b_hist,1.0); int scale = 2; IplImage* hist_image = cvCreateImage(cvSize(hist_size*scale,hist_height*4),8,3); cvZero(hist_image); //然后开始显示,这里对直方图进行了标准化处理,不然的话无法观察到明显的变化。 float r_max_value = http://www.mamicode.com/0;>计算结果如下:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。