Xcode histogram图像显示测试
2015-07-12 22:59
429 查看
#include <iostream>
#include <cv.h>
#include <highgui.h>
#include <string.h>
int main()
{
int hist_size=255;
float range_0[]={0,256};
float *ranges[]={range_0};
int i,bin_w;
float max_value=0,min_value=0;
int min_idx=0,max_idx=0;
double mean=0,variance=0;
IplImage *pImage=NULL;
IplImage *pImgGray=NULL;
IplImage *img=cvLoadImage("/Users/guofeng/Documents/histogram/test.bmp",1);
pImage=cvCloneImage(img);
cvNamedWindow("original");
cvShowImage("original", pImage);
pImgGray=cvCreateImage(cvGetSize(pImage),
IPL_DEPTH_8U, 1);
cvCvtColor(pImage, pImgGray,
CV_BGR2GRAY);
CvRect rect=cvRect(0,0,512,512);
cvSetImageROI(pImgGray, rect);
IplImage *histImage=cvCreateImage(cvSize(320,320),
8, 1);
CvHistogram *hist=cvCreateHist(1, &hist_size,
CV_HIST_ARRAY,ranges,1);
cvCalcHist(&pImgGray, hist);
cvGetMinMaxHistValue(hist, &min_value, &max_value,&min_idx,&max_idx);
cvScale(hist->bins,hist->bins,((double)histImage->height/max_value),0);
cvSet(histImage,cvScalarAll(255),0);
bin_w=cvRound((double)histImage->width/hist_size);
for(i=0;i<hist_size;i++)
{
cvRectangle(histImage,
cvPoint(i*bin_w,histImage->height),
cvPoint((i+1)*bin_w, histImage->height-cvRound(cvGetReal1D(hist->bins, i))),
cvScalarAll(0),-1,8,0);
float *bins=cvGetHistValue_1D(hist, i);
mean+=bins[0];
}
mean/=hist_size;
for(i=0;i<hist_size;i++)
{
float *bins=cvGetHistValue_1D(hist, i);
variance+=pow((bins[0]-mean),2);
}
variance/=hist_size;
cvNamedWindow("gray");
cvShowImage("gray", pImgGray);
cvNamedWindow("histogram");
cvShowImage("histogram", histImage);
CvFont *pfont=new
CvFont;
cvInitFont(pfont,
CV_FONT_HERSHEY_SIMPLEX, 1,
1,0,0);
char Result[]="histogram mean:";
//Result+=str+"\n";
cvPutText(histImage, Result,
cvPoint(0,80), pfont,
CV_RGB(255,255,0));
cvShowImage("histogram", histImage);
cvSaveImage("/Users/guofeng/Documents/histogram/histogram.jpg", histImage);
cvWaitKey(0);
cvReleaseImage(&pImage);
cvReleaseImage(&pImgGray);
cvReleaseImage(&img);
cvDestroyAllWindows();
return 0;
}
显示直方图:
#include <cv.h>
#include <highgui.h>
#include <string.h>
int main()
{
int hist_size=255;
float range_0[]={0,256};
float *ranges[]={range_0};
int i,bin_w;
float max_value=0,min_value=0;
int min_idx=0,max_idx=0;
double mean=0,variance=0;
IplImage *pImage=NULL;
IplImage *pImgGray=NULL;
IplImage *img=cvLoadImage("/Users/guofeng/Documents/histogram/test.bmp",1);
pImage=cvCloneImage(img);
cvNamedWindow("original");
cvShowImage("original", pImage);
pImgGray=cvCreateImage(cvGetSize(pImage),
IPL_DEPTH_8U, 1);
cvCvtColor(pImage, pImgGray,
CV_BGR2GRAY);
CvRect rect=cvRect(0,0,512,512);
cvSetImageROI(pImgGray, rect);
IplImage *histImage=cvCreateImage(cvSize(320,320),
8, 1);
CvHistogram *hist=cvCreateHist(1, &hist_size,
CV_HIST_ARRAY,ranges,1);
cvCalcHist(&pImgGray, hist);
cvGetMinMaxHistValue(hist, &min_value, &max_value,&min_idx,&max_idx);
cvScale(hist->bins,hist->bins,((double)histImage->height/max_value),0);
cvSet(histImage,cvScalarAll(255),0);
bin_w=cvRound((double)histImage->width/hist_size);
for(i=0;i<hist_size;i++)
{
cvRectangle(histImage,
cvPoint(i*bin_w,histImage->height),
cvPoint((i+1)*bin_w, histImage->height-cvRound(cvGetReal1D(hist->bins, i))),
cvScalarAll(0),-1,8,0);
float *bins=cvGetHistValue_1D(hist, i);
mean+=bins[0];
}
mean/=hist_size;
for(i=0;i<hist_size;i++)
{
float *bins=cvGetHistValue_1D(hist, i);
variance+=pow((bins[0]-mean),2);
}
variance/=hist_size;
cvNamedWindow("gray");
cvShowImage("gray", pImgGray);
cvNamedWindow("histogram");
cvShowImage("histogram", histImage);
CvFont *pfont=new
CvFont;
cvInitFont(pfont,
CV_FONT_HERSHEY_SIMPLEX, 1,
1,0,0);
char Result[]="histogram mean:";
//Result+=str+"\n";
cvPutText(histImage, Result,
cvPoint(0,80), pfont,
CV_RGB(255,255,0));
cvShowImage("histogram", histImage);
cvSaveImage("/Users/guofeng/Documents/histogram/histogram.jpg", histImage);
cvWaitKey(0);
cvReleaseImage(&pImage);
cvReleaseImage(&pImgGray);
cvReleaseImage(&img);
cvDestroyAllWindows();
return 0;
}
显示直方图:
相关文章推荐
- OLAP 支持---ROLLUP和CUBE语句 group by 的增强版本语句!可进行分层分组模式外 (小计与总计)
- android入门学习-UML(统一建模语言)介绍
- SQL Server ->> 分区表上创建唯一分区索引
- Section 1.3
- 适配器模式Adapter、外观模式Facade-- 学习HeadFirst设计模式记录
- Lowest Common Ancestor of a Binary Search Tree
- 对比iOS中的四种数据存储
- 【PHP教程二】PHP基本知识,运行自己写的.php文件
- mysql必知必会(六)
- 优化Mysql数据库的8个方法
- Thread
- 实时获取UITextField内容
- gpio
- linux下mysql安装和安装遇到的问题
- python用for循环实现冒泡排序
- lightoj 1021 Painful Bases (状压dp)
- 转:CocoaPods pod install/pod update更新慢的问题
- Spring boot 学习笔记 ---分分钟构建一个web程序(一)
- Linux内核RCU(Read Copy Update)锁简析-前传
- Leetcode# 38 Count and Say