请你扮演一个精通opencv的高手,使用opencv写一个c++代码,输入两个彩色图片名称分别为“原始图片.jpg”“待匹配图片.jpg”,完成彩色直方图匹配
时间: 2024-05-24 14:10:32 浏览: 64
基于C++与OPENCV的图片处理软件及源代码.zip
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
//读取原始图片和待匹配图片
Mat src = imread("原始图片.jpg", IMREAD_COLOR);
Mat target = imread("待匹配图片.jpg", IMREAD_COLOR);
//将原始图片和待匹配图片转换为HSV格式
Mat src_hsv, target_hsv;
cvtColor(src, src_hsv, COLOR_BGR2HSV);
cvtColor(target, target_hsv, COLOR_BGR2HSV);
//计算原始图片和待匹配图片各自的直方图
Mat src_hist, target_hist;
int hbins = 256, sbins = 256;
int histSize[] = { hbins, sbins };
float hranges[] = { 0, 180 };
float sranges[] = { 0, 256 };
const float* ranges[] = { hranges, sranges };
int channels[] = { 0, 1 };
calcHist(&src_hsv, 1, channels, Mat(), src_hist, 2, histSize, ranges, true, false);
calcHist(&target_hsv, 1, channels, Mat(), target_hist, 2, histSize, ranges, true, false);
//归一化直方图
normalize(src_hist, src_hist, 0, 1, NORM_MINMAX, -1, Mat());
normalize(target_hist, target_hist, 0, 1, NORM_MINMAX, -1, Mat());
//进行彩色直方图匹配
Mat matched;
calcBackProject(&target_hsv, 1, channels, src_hist, matched, ranges, 1, true);
//显示原始图片、待匹配图片和匹配结果
imshow("Original", src);
imshow("Target", target);
imshow("Matched", matched);
//等待按键事件
waitKey(0);
return 0;
}
阅读全文