OpenCv 020---图像直方图反向投影
Posted vince-wu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCv 020---图像直方图反向投影相关的知识,希望对你有一定的参考价值。
1 前备知识
2 所用到的主要OpenCv API
/** @brief 计算(一个或多个)数组的直方图
*/
void calcHist( const Mat* images, int nimages,
const int* channels, InputArray mask,
OutputArray hist, int dims, const int* histSize,
const float** ranges, bool uniform = true, bool accumulate = false );
/** @brief 计算一个直方图的反向投影
*/
CV_EXPORTS void calcBackProject( const Mat* images, int nimages, const int* channels, InputArray hist, OutputArray backProject, const float** ranges, double scale = 1, bool uniform = true );
3 程序代码
#include <opencv2/opencv.hpp> #include <iostream> #include<stdio.h> using namespace cv; using namespace std; const int bins = 256; Mat src; const char* winTitle = "input image"; void backProjection_demo(Mat &image, Mat &model); int main(int argc, char** argv) { Mat src = imread("images/target.png"); Mat model = imread("images/sample.png"); if (src.empty() || model.empty()) { printf("could not load image... "); return 0; } namedWindow(winTitle, WINDOW_AUTOSIZE); imshow(winTitle, src); imshow("model", model); backProjection_demo(src, model); waitKey(0); return 0; } void backProjection_demo(Mat &image, Mat &model) { Mat model_hsv, image_hsv; cvtColor(model, model_hsv, COLOR_BGR2HSV); cvtColor(image, image_hsv, COLOR_BGR2HSV); // 定义直方图参数与属性 int h_bins = 32; int s_bins = 32; int histSize[] = { h_bins, s_bins }; // hue varies from 0 to 179, saturation from 0 to 255 float h_ranges[] = { 0, 180 }; float s_ranges[] = { 0, 256 }; const float* ranges[] = { h_ranges, s_ranges };//相当于2*2 int channels[] = { 0, 1 }; Mat roiHist; calcHist(&model_hsv, 1, channels, Mat(), roiHist, 2, histSize, ranges, true, false);//计算模板图像hsv直方图 normalize(roiHist, roiHist, 0, 255, NORM_MINMAX, -1, Mat());//归一化 MatND backproj; calcBackProject(&image_hsv, 1, channels, roiHist, backproj, ranges, 1.0);//opencv反向投影计算 imshow("BackProj", backproj); }
4 运行结果
5 扩展及注意事项
6*目前只做大概了解,知道有这一算法,后续具体使用再做具体分析
以上是关于OpenCv 020---图像直方图反向投影的主要内容,如果未能解决你的问题,请参考以下文章