opencv学习之路(20)、直方图应用

一、直方图均衡化--equalizeHist()

 #include "opencv2/opencv.hpp"
using namespace cv; void main()
{
6 Mat srcImg = imread("E://02.jpg", 0); //以灰度方式打开,需要输入单通道图像
7 imshow("src", srcImg);
8 Mat dstImg; //均衡化后的图像
9 equalizeHist(srcImg, dstImg);
10 imshow("dst", dstImg);

//绘制src直方图
MatND dstHist; //定义存储直方图变量
int dims = ; //需要统计的特征数目(只统计灰度值)
float hranges[] = {, }; //范围[0,256)注意是最大值加1
const float* ranges[] = {hranges};
int bins = ;
int channels = ;
calcHist(&srcImg, , &channels, Mat(), dstHist, dims, &bins, ranges);
int scale = ;
Mat HistImg(bins * scale, bins*, CV_8UC3, Scalar()); //定义直方图输出图像
double minValue = ;
double maxValue = ;
minMaxLoc(dstHist, &minValue, &maxValue, , );
int hpt = saturate_cast<int>(0.9*bins); //设置最大值并防止溢出
int j=;
for(int i=; i<; i++)
{
float binValue = dstHist.at<float>(i);
int realValue = saturate_cast<int>(binValue*hpt/maxValue); //归一化数据
line(HistImg, Point(i*scale, bins-), Point(i*scale, bins-realValue), Scalar(, , ), , );
}
imshow("src_hist", HistImg); //绘制dst直方图
calcHist(&dstImg, , &channels, Mat(), dstHist, dims, &bins, ranges);
Mat HistImg2(bins * scale, bins*, CV_8UC3, Scalar()); //定义直方图输出图像
for(int i=; i<; i++)
{
float binValue = dstHist.at<float>(i);
int realValue = saturate_cast<int>(binValue*hpt/maxValue); //归一化数据
line(HistImg2, Point(i*scale, bins-), Point(i*scale, bins-realValue), Scalar(, , ), , );
}
imshow("dst_hist", HistImg2); waitKey();
}

opencv学习之路(20)、直方图应用

注意:红色部分为均衡化的主要代码

彩色图像直方图均衡化

 #include "opencv2/opencv.hpp"
using namespace cv; void main()
{
Mat src = imread("E://05.jpg");
imshow("src", src); //分割通道
vector<Mat>channels;
split(src,channels);
Mat blue,green,red,dst;
blue=channels.at();
green=channels.at();
red=channels.at();
//分别对BGR通道做直方图均衡化
equalizeHist(blue,blue);
equalizeHist(green,green);
equalizeHist(red,red);
//合并通道
merge(channels,dst);
imshow("dst", dst); waitKey();
}

opencv学习之路(20)、直方图应用

二、直方图对比

opencv学习之路(20)、直方图应用

opencv学习之路(20)、直方图应用

 #include "opencv2/opencv.hpp"
#include<iostream>
using namespace cv;
using namespace std; void main()
{
Mat src1 = imread("E://a.jpg");
Mat src2 = imread("E://b.jpg");
imshow("src1", src1);
imshow("src2", src2); MatND dstHist; //定义存储直方图变量
int dims = ; //需要统计的特征数目(只统计灰度值)
float hranges[] = {, }; //范围[0,256)注意是最大值加1
const float* ranges[] = {hranges};
int bins = ;
int channels = ;
calcHist(&src1, , &channels, Mat(), dstHist, dims, &bins, ranges);
int scale = ;
Mat HistImg(bins * scale, bins*, CV_8UC3, Scalar()); //定义直方图输出图像
double minValue = ;
double maxValue = ;
minMaxLoc(dstHist, &minValue, &maxValue, , );
int hpt = saturate_cast<int>(0.9*bins); //设置最大值并防止溢出
int j=;
for(int i=; i<; i++)
{
float binValue = dstHist.at<float>(i);
int realValue = saturate_cast<int>(binValue*hpt/maxValue); //归一化数据
line(HistImg, Point(i*scale, bins-), Point(i*scale, bins-realValue), Scalar(, , ), , );
}
imshow("src1_hist", HistImg); MatND dstHist2; //定义存储直方图变量
calcHist(&src2, , &channels, Mat(), dstHist2, dims, &bins, ranges);
Mat HistImg2(bins * scale, bins*, CV_8UC3, Scalar()); //定义直方图输出图像
minMaxLoc(dstHist2, &minValue, &maxValue, , );
for(int i=; i<; i++)
{
float binValue = dstHist2.at<float>(i);
int realValue = saturate_cast<int>(binValue*hpt/maxValue); //归一化数据
line(HistImg2, Point(i*scale, bins-), Point(i*scale, bins-realValue), Scalar(, , ), , );
}
imshow("src2_hist", HistImg2); double matchValue0 = compareHist(dstHist, dstHist2, CV_COMP_CORREL); //值越大相似度越高
double matchValue1 = compareHist(dstHist, dstHist2, CV_COMP_CHISQR); //值越小相似度越高
double matchValue2 = compareHist(dstHist, dstHist2, CV_COMP_INTERSECT); //值越大相似度越高
double matchValue3 = compareHist(dstHist, dstHist2, CV_COMP_BHATTACHARYYA); //值越小相似度越高 cout<<"matchValue0(max_best)="<<matchValue0<<endl;
cout<<"matchValue1(min_best)="<<matchValue1<<endl;
cout<<"matchValue2(max_best)="<<matchValue2<<endl;
cout<<"matchValue3(min_best)="<<matchValue3<<endl; waitKey();
}

opencv学习之路(20)、直方图应用

三、反向投影

opencv学习之路(20)、直方图应用

opencv学习之路(20)、直方图应用

opencv学习之路(20)、直方图应用

 #include "opencv2/opencv.hpp"
using namespace cv; #define WINDOW_NAME "【原始图】"
Mat g_hueImage;
int g_bins = ;//直方图组距 void on_BinChange(int, void* )
{
MatND hist;
int histSize = MAX( g_bins, );
float hue_range[] = { , };
const float* ranges = { hue_range };
calcHist( &g_hueImage, , , Mat(), hist, , &histSize, &ranges, true, false );
normalize( hist, hist, , , NORM_MINMAX, -, Mat() ); MatND backproj;
calcBackProject( &g_hueImage, , , hist, backproj, &ranges, , true );
imshow( "反向投影图", backproj ); int w = ; int h = ;
int bin_w = cvRound( (double) w / histSize );
Mat histImg = Mat::zeros( w, h, CV_8UC3 );
for( int i = ; i < g_bins; i ++ )
{
rectangle( histImg, Point( i*bin_w, h ), Point( (i+)*bin_w, h - cvRound( hist.at<float>(i)*h/255.0 ) ), Scalar( , , ), - );
}
imshow( "直方图", histImg );
} void main()
{
Mat g_srcImage = imread( "E://1.jpg" );
Mat g_hsvImage;
resize(g_srcImage, g_srcImage, Size(g_srcImage.cols/, g_srcImage.rows/));//原图太大,进行缩放
cvtColor( g_srcImage, g_hsvImage, CV_BGR2HSV ); g_hueImage.create( g_hsvImage.size(), g_hsvImage.depth() );
int ch[ ] = { , };
mixChannels( &g_hsvImage, , &g_hueImage, , ch, );//从输入中拷贝某通道到输出中特定的通道 namedWindow( WINDOW_NAME , CV_WINDOW_AUTOSIZE );
createTrackbar("色调组距 ", WINDOW_NAME , &g_bins, , on_BinChange );
on_BinChange(, ); imshow( WINDOW_NAME , g_srcImage );
waitKey();
}

opencv学习之路(20)、直方图应用

上一篇:织梦dedeCMS数据库结构字段说明-简略说明


下一篇:Pyquery API中文版