码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCv 020---图像直方图反向投影

时间:2019-11-05 21:42:14      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:算法   sample   strong   roi   get   iostream   window   turn   printf   

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...\n");
        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---图像直方图反向投影

标签:算法   sample   strong   roi   get   iostream   window   turn   printf   

原文地址:https://www.cnblogs.com/Vince-Wu/p/11766359.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!