OpenCV--c++ 学习笔记

直方图方向投影

作用:在输入图像中寻找与模板图像最匹配的区域,即定位模板图像出现在输入图像的位置。

步骤:

  1. 加载模板图像和待反向投影图像;
  2. 加载图像颜色空间,gray/HSV;
  3. 计算模板图像的直方图;
  4. 将待方向投影和模板图像的直方图赋值给反向投影函数,得到投影结果。
    #include <opencv2/opencv.hpp>
    #include <iostream>
    #include <math.h>
    
    using namespace cv;
    using namespace std;
    
    void drawHist(Mat& hist, int type, string name) {
        int hist_w = 512;
        int hist_h = 400;
        int width = 2;
        Mat histImage = Mat::zeros(hist_h, hist_w, CV_8UC3);
        normalize(hist, hist, 255, 0, type, -1, Mat());  // normalize hist values between 0 and 1
        namedWindow(name, WINDOW_NORMAL);
        imshow(name, hist);
    }
    
    int main() {
        //加载图像
        Mat img = imread("D:/images/hamd3.jpg");
        Mat sub_img = imread("D:/images/hand1.jpg");
        resize(img, img, Size(img.cols / 3, img.rows / 3), 0, 0, INTER_LINEAR);
        resize(sub_img, sub_img, Size(sub_img.cols / 3, sub_img.rows / 3), 0, 0, INTER_LINEAR);
        //cvtColor(img, img, COLOR_BGR2GRAY);
        //cvtColor(sub_img, sub_img, COLOR_BGR2GRAY);
        Mat img_HSV, sub_HSV, hist;
        if (img.empty() || sub_img.empty()) {
            printf("could not load image...\n");
            return -1;
        }
    
        imshow("img", img);
        imshow("sub_img", sub_img);
        //转换色彩空间
        cvtColor(img, img_HSV, COLOR_BGR2HSV);
        cvtColor(sub_img, sub_HSV, COLOR_BGR2HSV);
        //计算模板图像直方图
        int h_bins = 32;
        int s_bins = 32;
        int histSize[] = { h_bins, s_bins };
        float h_ranges[] = { 0, 180 };
        float s_ranges[] = { 0, 256 };
        const float* ranges[] = { h_ranges, s_ranges };  // ranges should be an array of pointers
        int channels[] = { 0, 1 };  // the channels to be used for hist computation
    
        // Compute the histogram
        calcHist(&sub_HSV, 1, channels, Mat(), hist, 2, histSize, ranges, true, false);
        drawHist(hist, NORM_INF, "hist");
        //反向投影
        Mat backproj;
        // Perform backprojection
        calcBackProject(&img_HSV, 1, channels, hist, backproj, ranges, 1.0);
        imshow("反向投影后结果", backproj);  // Avoid using Chinese characters in window names
    
        waitKey(0);
        return 0;
    }
    

代码运行结果 

相关推荐

  1. OpenCV C++ 学习笔记(一)

    2024-07-15 14:58:01       36 阅读
  2. opencv c++ 学习1

    2024-07-15 14:58:01       24 阅读
  3. OpenMV学习笔记

    2024-07-15 14:58:01       48 阅读
  4. OpenCV学习笔记】- 学习笔记目录

    2024-07-15 14:58:01       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 14:58:01       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 14:58:01       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 14:58:01       62 阅读
  4. Python语言-面向对象

    2024-07-15 14:58:01       72 阅读

热门阅读

  1. HTML5 Input 验证身份证

    2024-07-15 14:58:01       25 阅读
  2. 使用jsencrypt在web前端对字符串进行Ras加密

    2024-07-15 14:58:01       23 阅读
  3. c# Bitmap

    2024-07-15 14:58:01       21 阅读
  4. C++ 分析一个链表是不是回文,有什么思路么

    2024-07-15 14:58:01       27 阅读
  5. druid 1.2.23版本配置监控页面

    2024-07-15 14:58:01       20 阅读
  6. Python学习1---深浅拷贝

    2024-07-15 14:58:01       21 阅读
  7. 多语言环境大师:在PyCharm中管理多个Python解释器

    2024-07-15 14:58:01       23 阅读
  8. SSLRec代码分析

    2024-07-15 14:58:01       21 阅读