【OpenCV】OpenCV1.0图像直方图
2016-04-19 00:42
232 查看
#include "cv.h" #include "highgui.h" #include "cxcore.h" IplImage* histdraw(CvHistogram* hist,float scalex=1,float scaley=1) { float histmax=0; cvGetMinMaxHistValue(hist,0,&histmax,0); IplImage* result=cvCreateImage(cvSize(256*scalex,64*scaley),8,1); cvZero(result); for(int i=0;i<255;i++) { float cvnowvalue=cvQueryHistValue_1D(hist,i); float cvnextvalue=cvQueryHistValue_1D(hist,i+1); CvPoint pt0=cvPoint(i*scalex,64*scaley); CvPoint pt1=cvPoint((i+1)*scalex,64*scaley); CvPoint pt2=cvPoint((i+1)*scalex, (1-(cvnextvalue/histmax))*64*scaley); CvPoint pt3=cvPoint(i*scalex,(1-(cvnowvalue/histmax))*64*scaley); int numpts=5; CvPoint pts[5]; pts[0]=pt0;pts[1]=pt1;pts[2]=pt2;pts[3]=pt3;pts[4]=pt0; cvFillConvexPoly(result,pts,numpts,cvScalar(255)); return (result); } } int main(int argc, char* argv[]) { IplImage* scr=cvLoadImage("lena.jpg",0); cvNamedWindow("win"); cvShowImage("win",scr); cvWaitKey(0); //直方图创建 int dims=1; int size=256; float rang[]={0,255}; float* rangs[]={rang}; CvHistogram* hist; hist=cvCreateHist(dims,&size,CV_HIST_ARRAY,rangs,1); cvClearHist(hist); //彩色图分解为三通道 IplImage* imgblue=cvCreateImage(cvGetSize(scr),8,1); IplImage* imggreen=cvCreateImage(cvGetSize(scr),8,1); IplImage* imgred=cvCreateImage(cvGetSize(scr),8,1); cvSplit(scr,imgblue,imggreen,imgred,NULL); //计算直方图 cvCalcArrHist(&imgblue,hist,0,0); IplImage* blue=histdraw(hist);//调用显示函数 cvClearHist(hist); cvCalcArrHist(&imggreen,hist,0,0); IplImage* green=histdraw(hist); cvClearHist(hist); cvCalcArrHist(&imgred,hist,0,0); IplImage* red=histdraw(hist); cvClearHist(hist); cvNamedWindow("b"); cvNamedWindow("g"); cvNamedWindow("r"); cvShowImage("b",blue); cvShowImage("g",green); cvShowImage("r",red); cvWaitKey(0); cvReleaseImage(&scr); cvReleaseImage(&blue); cvReleaseImage(&green); cvReleaseImage(&red); cvDestroyWindow("b"); cvDestroyWindow("g"); cvDestroyWindow("r"); return 0; }
#include "cv.h" #include "highgui.h" #include "cxcore.h" void FillWhite(IplImage *pImage) { cvRectangle(pImage, cvPoint(0, 0), cvPoint(pImage->width, pImage->height), CV_RGB(255, 255, 255), CV_FILLED); } // 创建灰度图像的直方图 CvHistogram* CreateGrayImageHist(IplImage **ppImage) { int nHistSize = 256; float fRange[] = {0, 255}; //灰度级的范围 float *pfRanges[] = {fRange}; CvHistogram *pcvHistogram = cvCreateHist(1, &nHistSize, CV_HIST_ARRAY, pfRanges); cvCalcHist(ppImage, pcvHistogram); return pcvHistogram; } // 根据直方图创建直方图图像 IplImage* CreateHisogramImage(int nImageWidth, int nScale, int nImageHeight, CvHistogram *pcvHistogram) { IplImage *pHistImage = cvCreateImage(cvSize(nImageWidth * nScale, nImageHeight), IPL_DEPTH_8U, 1); FillWhite(pHistImage); //统计直方图中的最大直方块 float fMaxHistValue = 0; cvGetMinMaxHistValue(pcvHistogram, NULL, &fMaxHistValue, NULL, NULL); //分别将每个直方块的值绘制到图中 int i; for(i = 0; i < nImageWidth; i++) { float fHistValue = cvQueryHistValue_1D(pcvHistogram, i); //像素为i的直方块大小 int nRealHeight = cvRound((fHistValue / fMaxHistValue) * nImageHeight); //要绘制的高度 cvRectangle(pHistImage, cvPoint(i * nScale, nImageHeight - 1), cvPoint((i + 1) * nScale - 1, nImageHeight - nRealHeight), cvScalar(i, 0, 0, 0), CV_FILLED ); } return pHistImage; } int main( int argc, char** argv ) { const char *pstrWindowsSrcTitle = "原图"; const char *pstrWindowsGrayTitle = "灰度图"; const char *pstrWindowsHistTitle = "直方图"; // 从文件中加载原图 IplImage *pSrcImage = cvLoadImage("lena.jpg", CV_LOAD_IMAGE_UNCHANGED); IplImage *pGrayImage = cvCreateImage(cvGetSize(pSrcImage), IPL_DEPTH_8U, 1); // 灰度图 cvCvtColor(pSrcImage, pGrayImage, CV_BGR2GRAY); // 灰度直方图 CvHistogram *pcvHistogram = CreateGrayImageHist(&pGrayImage); // 创建直方图图像 int nHistImageWidth = 255; int nHistImageHeight = 150; //直方图图像高度 int nScale = 2; IplImage *pHistImage = CreateHisogramImage(nHistImageWidth, nScale, nHistImageHeight, pcvHistogram); // 显示 cvNamedWindow(pstrWindowsSrcTitle, CV_WINDOW_AUTOSIZE); cvNamedWindow(pstrWindowsGrayTitle, CV_WINDOW_AUTOSIZE); cvNamedWindow(pstrWindowsHistTitle, CV_WINDOW_AUTOSIZE); cvShowImage(pstrWindowsSrcTitle, pSrcImage); cvShowImage(pstrWindowsGrayTitle, pGrayImage); cvShowImage(pstrWindowsHistTitle, pHistImage); cvWaitKey(0); cvReleaseHist(&pcvHistogram); cvDestroyWindow(pstrWindowsSrcTitle); cvDestroyWindow(pstrWindowsGrayTitle); cvDestroyWindow(pstrWindowsHistTitle); cvReleaseImage(&pSrcImage); cvReleaseImage(&pGrayImage); cvReleaseImage(&pHistImage); return 0; }
相关文章推荐
- fastDFS 一二事 - 简易服务器搭建(单linux)
- Shell编程详解
- 关于Windows和Linux的那些事儿
- (原创)关于PopupWindow的showAsDropDown()和showAtLocation()使用方式及其区别
- 【整理】Linux下中文检索引擎coreseek4安装,以及PHP使用sphinx的三种方式(sphinxapi,sphinx的php扩展,SphinxSe作为mysql存储引擎)
- with check option使用
- Android架构知识
- Zookeeper+HDFS+YARN监控
- studyphp环境软件Nginx隐藏index.php和Pathinfo模式配置例子
- 电脑手机微网站三站合一对企业有哪些优势?
- 最强大经典的php企业网站源码模板,一键安装就可以使用
- CenOS 7.1安装Docker、Docker-compose
- docker registrator配合consul使用的问题
- Centos 6.4打包tengine的rpm安装包教程
- linux云服务器硬盘分区及挂载
- nginx reload 命令报:nginx: [error] invalid PID number
- shell脚本基础知识
- System.getProperty
- 2.1Linux系统基础入门
- 2.2Linux命令行接口及基础命令