#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <cv.h>
#include<time.h>
#include<cmath>
using namespace std;
//using namespace cv;
/*
void salt(cv::Mat &image,int n)//产生椒盐噪声
{
for(int k = 0;k < n;k++)
{
int i = rand()%image.cols;
int j = rand()&image.rows;
if (image.channels() == 1)
{
image.at<uchar>(j,i) = 255;
}
else if (image.channels() == 3)
{
image.at<cv::Vec3b>(j,i)[0] = 255;
image.at<cv::Vec3b>(j,i)[1] = 255;
image.at<cv::Vec3b>(j,i)[2] = 255;
}
}
}*/
class ColorHistogram
{
private:
int histSize[3];
float hranges[2];
const float* ranges[3];
int channels[3];
public:
ColorHistogram(){
histSize[0] = histSize[1] = histSize[2] = 256;
hranges[0] = 0.0;
hranges[1] = 255.0;
ranges[0] = hranges;
ranges[0] = hranges;
ranges[0] = hranges;
channels[0] = 0;
channels[1] = 1;
channels[2] = 2;
}
cv::Mat colorReduce(cv::Mat &image,int div)//遍历图像并且减少颜色数目
{
cv::Mat_<cv::Vec3b>::iterator it
= image.begin<cv::Vec3b>();
cv::Mat_<cv::Vec3b>::iterator end
= image.end<cv::Vec3b>() - image.rows;
for (;it != end;it++)
{
(*it)[0] = (*it)[0] / div * div + div / 2;
(*it)[1] = (*it)[1] / div * div + div / 2;
(*it)[2] = (*it)[2] / div * div + div / 2;
}
return image;
}
cv::MatND getHistogram(const cv::Mat &image)
{
cv::MatND hist;
cv::calcHist(&image,1,channels,cv::Mat(),hist,1,histSize,ranges);
return hist;
}
};
class ImageComparator
{
private:
cv::Mat reference;
cv::Mat input;
cv::MatND refH;
cv::MatND inputH;
ColorHistogram hist;
int div;
public:
ImageComparator():div(32){}
void setColorReduction(int factor)
{
div = factor;
}
int getColorReduction()
{
return div;
}
void setReferenceImage(cv::Mat &image)
{
reference = hist.colorReduce(image,div);
refH = hist.getHistogram(reference);
}
double compare(cv::Mat &image)
{
input = hist.colorReduce(image,div);
inputH = hist.getHistogram(input);
return cv::compareHist(refH,inputH,CV_COMP_CORREL);
}
};
/*对图片锐化
void sharpen(const cv::Mat &image,cv::Mat &result)
{
result.create(image.size(),image.type());
for (int j = 1;j < image.rows - 1;j++)
{
const uchar* previous =
image.ptr<const uchar>(j - 1);
const uchar* current =
image.ptr<const uchar>(j);
const uchar* next =
image.ptr<const uchar>(j+1);
uchar* output = result.ptr<uchar>(j);
for (int i = 1;i < image.cols - 1;i++)
{
*output++ = cv::saturate_cast<uchar>(
5 * current[i] - current[i-1]-current[i+1]
-previous[i]-next[i]);
}
}
result.row(0).setTo(cv::Scalar(0));
result.row(result.rows - 1).setTo(cv::Scalar(0));
result.col(0).setTo(cv::Scalar(0));
result.col(result.cols - 1).setTo(cv::Scalar(0));
}*/
int main(int argc,char **argv)
{
// imageROI = image(cv::Rect(20,20,200,200));
// Rect ROI(68,55,444,418);
// cv::Mat imageROI = image(ROI);
// cvSetImageROI(imageROI,(cv::Rect(20,20,200,200)));
// cv::Mat imageClone = image.clone();
// sharpen(image,res);
cv::Mat image = cv::imread("7_.jpg");
cv::Mat c = cv::imread("1_.jpg");
// cv::Mat c = cv::imread("5.jpg");
ImageComparator com;
// com.input = cv::imread("5.jpg");
// com.reference = cv::imread("5.jpg");
com.setReferenceImage(c);
// com.setReferenceImage(image);
double res = com.compare(image);
printf("根据检测得出两图的相似性为:%lf\n",res);
cv::namedWindow("Reference",1);
cv::imshow("Reference",c);
cv::namedWindow("input",1);
cv::imshow("input",image);
cvWaitKey(0);
system("pause");
return 0;
}
基于直方图比较的图片检测
最新推荐文章于 2023-08-09 14:56:32 发布