【OpenCV】绘制图像的直方图
2015-12-22 22:08
405 查看
#include "stdafx.h" #include <cv.h> #include <highgui.h> #include <cxcore.h> #include <vector> #include <cmath> using namespace std; using namespace cv; 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("5.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); cvReleaseImage(&pSrcImage); cvReleaseImage(&pGrayImage); cvReleaseImage(&pHistImage); cvDestroyAllWindows(); return 0; }
相关文章推荐
- openGL 在光源处画一个球,移动光源
- Linux -- chown . Ctrl 命令
- Hadoop Ls命令增加显示条数限制参数
- 在Linux下伪装MAC地址
- web在线打印,打印阅览,打印维护,打印设计
- linux常用命令
- Linux NFS服务器的安装与配置
- Linux笔记(基于CentOS)
- Linux修改 DNS
- 2015 DevOps状态调查报告
- Centos开发环境搭建
- Windows和 Centos7 双系统的搭建
- Linux文件系统(分区)操作管理指令总结
- centos 6.5 阿里云 源码安装php swoole详解
- opus交叉编译
- centos6下通用二进制安装mysql5.5.33
- centos6下通用二进制安装mysql5.5.33
- Linux 安装maven
- chapter7:Linux文件与目录管理之(1)执行变量PATH
- Linux下ls怎么屏蔽波浪线~作为文件名结尾的文件