【问题标题】:OpenCV, equalize histogram of image contourOpenCV,均衡图像轮廓的直方图
【发布时间】:2015-11-13 20:16:43
【问题描述】:

我知道可以均衡图像的直方图,例如:

equalizeHist(image, image);

如果我愿意,我可以定义一个 roi 并均衡图像中的这个 roi:

Mat aux3 = image.clone();
equalizeHist(image(Rect(0,100, 200,200)), aux3(Rect(0,100, 200,200)));

我现在想做的(我不知道是否可能)是使用点向量(cv::vector contour)定义一个 Roi(con​​tour)并均衡这个 roi(这个 roi并不总是矩形)

所以,这里的问题是:

是否可以使用 openCV 函数均衡图像中不是矩形的部分?

【问题讨论】:

    标签: c++ opencv image-processing


    【解决方案1】:

    OpenCV 中没有使用掩码执行直方图均衡的内置函数。您仍然可以编写自定义的。


    获取灰度图像:

    // Load image
    Mat3b img = imread("path_to_image");
    
    // Convert to grayscale
    Mat1b gray;
    cvtColor(img, gray, COLOR_BGR2GRAY);
    

    从你的观点中形成一个面具:

    // Your vector of points
    vector<Point> pts = { Point(300, 180), Point(450, 150), Point(600, 200), Point(650, 350), Point(300,300) };
    
    // Create the mask
    Mat1b mask(img.rows, img.cols, uchar(0));
    vector<vector<Point>> ptsarray{pts};
    fillPoly(mask, ptsarray, Scalar(255));
    

    调用您的自定义函数equalizeHistWithMask,使用蒙版均衡图像:

    // Equalize with mask
    Mat1b equalized;
    equalizeHistWithMask(gray, equalized, mask);
    

    这里有完整的代码供参考,带有equalizeHistWithMask函数:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <opencv2/opencv.hpp>
    using namespace std;
    using namespace cv;
    
    void equalizeHistWithMask(const Mat1b& src, Mat1b& dst, Mat1b mask = Mat1b())
    {
        int cnz = countNonZero(mask);
        if (mask.empty() || ( cnz == src.rows*src.cols))
        {
            equalizeHist(src, dst);
            return;
        }
    
        dst = src.clone();
    
        // Histogram
        vector<int> hist(256,0);
        for (int r = 0; r < src.rows; ++r) {
            for (int c = 0; c < src.cols; ++c) {
                if (mask(r, c)) {
                    hist[src(r, c)]++;
                }
            }
        }
    
        // Cumulative histogram
        float scale = 255.f / float(cnz);
        vector<uchar> lut(256);
        int sum = 0;
        for (int i = 0; i < hist.size(); ++i) {
            sum += hist[i];
            lut[i] = saturate_cast<uchar>(sum * scale);
        }
    
        // Apply equalization
        for (int r = 0; r < src.rows; ++r) {
            for (int c = 0; c < src.cols; ++c) {
                if (mask(r, c)) {
                    dst(r, c) = lut[src(r,c)];
                }
            }
        }
    }
    
    int main()
    {
        // Load image
        Mat3b img = imread("path_to_image");
    
        // Convert to grayscale
        Mat1b gray;
        cvtColor(img, gray, COLOR_BGR2GRAY);
    
        // Your vector of points
        vector<Point> pts = { Point(300, 180), Point(450, 150), Point(600, 200), Point(650, 350), Point(300,300) };
    
        // Create the mask
        Mat1b mask(img.rows, img.cols, uchar(0));
        vector<vector<Point>> ptsarray{pts};
        fillPoly(mask, ptsarray, Scalar(255));
    
        // Equalize with mask
        Mat1b equalized;
        equalizeHistWithMask(gray, equalized, mask);
    
        imshow("Gray", gray);
        imshow("Mask", mask);
        imshow("Equalized", equalized);
        waitKey();
    
        return 0;
    }
    

    学分

    代码基于 answers.opencv.org

    上的this 问题

    【讨论】:

      猜你喜欢
      • 2010-12-23
      • 1970-01-01
      • 1970-01-01
      • 2013-02-07
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      相关资源
      最近更新 更多