在 OpenCV 中使用 inRange() 检测范围内的颜色 [英] Using inRange() in OpenCV to detect colors in a range

查看:60
本文介绍了在 OpenCV 中使用 inRange() 检测范围内的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 OpenCV 编写一个用于月球陨石坑检测的 C++ 程序,它似乎只能准确地检测到一小部分陨石坑.我对这种方法的策略是首先将图像转换为 HSV,然后使用 inRange() 捕捉一系列值中的颜色以产生阈值,然后高斯模糊它并使用 HoughCircles() 检测圆圈.

I'm writing a C++ program with OpenCV for lunar crater detection which seems to detect only a small fraction of craters accurately. My strategy for this approach was to first convert the image to HSV, then use inRange() to catch the colors in a range of values to produce a threshold, then Gaussian blur it and use HoughCircles() to detect the circles.

我不完全理解的一件事是,当我给 inRange() 一个颜色的低和高阈值时,它根本不返回任何内容.只是一个黑色的图像.它仅在我将低阈值设置为 Scalar(0,0,0) 时才有效,但我相信这会使其有些不准确.有什么我不明白的吗?我的测试图片如下.

One thing that I am not fully understanding is that when I give inRange() a low and high threshold around a color, it simply does not return anything. Just a black image. It only works when I set the low threshold to Scalar(0,0,0) however I believe this makes it somewhat inaccurate. Is there something I am not understanding about this? My test image is below.

月球表面

这是我用来测试这张图片的代码:

This is the code that I used to test this image:

#include <cstdio>
#include <iostream>

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/features2d/features2d.hpp"

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
    // using namespace cv;

    printf("%s\n", argv[1]);
    Mat src=imread(argv[1]);

    if (!src.data) {
        std::cout << "ERROR:\topening image" <<std::endl;
        return -1;
    }

    // converts the image to hsv so that circle detection is more accurate
    Mat hsv_image;
    cvtColor(src, hsv_image, COLOR_BGR2HSV);
    // high contrast black and white
    Mat imgThreshold;
    inRange(hsv_image,
        Scalar(0, 0, 0),
        Scalar(48, 207, 74),
        imgThreshold);

    // Applies a gaussian blur to the image
    GaussianBlur( imgThreshold, imgThreshold, Size(9, 9), 2, 2 );
    // fastNlMeansDenoisingColored(imgThreshold, imgThreshold, 10, 10, 7, 21);

    vector<Vec3f> circles;
    // applies a hough transform to the image
    HoughCircles(imgThreshold, circles, CV_HOUGH_GRADIENT,
        2, // accumulator resolution (size of image / 2)
        100, //minimum dist between two circles
        400, // Canny high threshold
        10, // minimum number of votes
        10, 65); // min and max radius

    cout << circles.size() << endl;
    cout << "end of test" << endl;

    vector<Vec3f>::
          const_iterator itc = circles.begin();
    // Draws the circles on the source image
    while (itc!=circles.end()) {

        circle(src, // src_gray2
            Point((*itc)[0], (*itc)[1]), // circle center
            (*itc)[2],       // circle radius
            Scalar(0,0,255), // color
            5);              // thickness

        ++itc;
    }
    namedWindow("Threshold",CV_WINDOW_AUTOSIZE);
    resize(imgThreshold, imgThreshold, Size(src.cols/2,src.rows/2) ); // resizes it so it fits on our screen
    imshow("Threshold",imgThreshold); // displays the source iamge

    namedWindow("HSV Color Space",CV_WINDOW_AUTOSIZE);
    resize(hsv_image, hsv_image, Size(src.cols/2,src.rows/2) ); // resizes it so it fits on our screen
    imshow("HSV Color Space",hsv_image); // displays the source iamge

    namedWindow("Source Image",CV_WINDOW_AUTOSIZE);
    resize(src, src, Size(src.cols/2,src.rows/2) ); // resizes it so it fits on our screen
    imshow("Source Image",src); // displays the source iamge

    waitKey(0);
    return 0;
}

推荐答案

这是我的尝试:

int main(int argc, char** argv)
{
    Mat src;
    src = imread("craters1.jpg", 1);
    cvtColor(src, hsv_image, COLOR_BGR2HSV);

    Mat imgThreshold1, imgThreshold2, imgThreshold;
    inRange(hsv_image,
        Scalar(0, 0, 0),
        Scalar(48, 207, 74),
        imgThreshold1);

    inRange(hsv_image,
        Scalar(140, 0, 0),
        Scalar(180, 207, 114),
        imgThreshold2);

    imgThreshold = max(imgThreshold1, imgThreshold2); // combining the two thresholds

    Mat element_erode = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));
    Mat element_dilate = getStructuringElement(MORPH_ELLIPSE, Size(10, 10));
    /// Apply the erosion and dilation operations
    erode(imgThreshold, imgThreshold, element_erode);
    dilate(imgThreshold, imgThreshold, element_dilate);

    GaussianBlur(imgThreshold, imgThreshold, Size(9, 9), 2, 2);

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    /// Find contours
    findContours(imgThreshold, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    for (int i = 0; i < contours.size(); i++)
    {
        drawContours(src, contours, i, Scalar(0,0,255), 2, 8, hierarchy, 0, Point());
    }

    namedWindow("Display Image", WINDOW_AUTOSIZE);
    imshow("Display Image", imgThreshold);
    imshow("Final result", src);

    waitKey(0);

    return 0;
}

与您的代码的主要区别在于我不使用 HoughCircles.我不确定它会产生好的结果,因为陨石坑没有完美的圆形.相反,我使用 findContours 来圈出陨石坑.这是我的结果:希望能帮到你!

The main difference with your code is that I don't use HoughCircles. I'm not sure it will give good results as craters don't have a perfect circular shape. Instead, I used findContours to circle the craters. Here is the result I have: Hope it helps!

这篇关于在 OpenCV 中使用 inRange() 检测范围内的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆