【问题标题】:Detect Brown colour object using opencv使用opencv检测棕色对象
【发布时间】:2015-10-23 23:12:28
【问题描述】:

我想在图像中找到棕色对象。我已经完成了以下过程:

  1. 将图像 BGR 转换为 HSV
  2. 我使用 opencv lib 的 inRange 函数来查找棕色。

cv::inRange(src, Scalar(9, 95, 95),Scalar(17, 255, 255), dest);

  1. 并找到轮廓,但我没有得到轮廓。

输入图像

问题

我想在上图中检测眼睛的棕色。当我将以上范围用于棕色时,我得到零轮廓。

高于棕色范围是否正确?应该是什么?

【问题讨论】:

  • 请附上您正在使用的代码...

标签: opencv image-processing


【解决方案1】:

您可以在使用 HSV 范围的图像中分割棕色对象。由于棕色在某种程度上是一种较深的红色,因此您需要稍微调整一下参数。如果您发布参考图片,我们可以找到更准确的范围。

一旦你有了对象蒙版(你通常应用一些形态来清理蒙版),你可以很容易地用findContours得到轮廓。

下面的例子解释了这一点:

#include <iostream>
#include <vector>
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

int main()
{
    Mat3b img = imread("path_to_image");

    Mat3b hsv;
    cvtColor(img, hsv, COLOR_BGR2HSV);

    Mat1b mask1, mask2;
    inRange(hsv, Scalar(0, 100, 20), Scalar(10, 255, 255), mask1);
    inRange(hsv, Scalar(170, 100, 20), Scalar(180, 255, 255), mask2);

    Mat1b mask = mask1 | mask2;

    Mat1b kernel = getStructuringElement(MORPH_ELLIPSE, Size(7,7));
    morphologyEx(mask, mask, MORPH_OPEN, kernel);

    vector<vector<Point>> contours;
    findContours(mask.clone(), contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

    Mat3b res = img.clone();
    for(int i=0; i<contours.size(); ++i)
    {
        drawContours(res, contours, i, Scalar(0,255,0));

        RotatedRect r = minAreaRect(contours[i]);
        Point2f pts[4];
        r.points(pts);

        for (int j = 0; j < 4; ++j)
        {
            line(res, pts[j], pts[(j + 1) % 4], Scalar(0,0,255));
        }

        Rect box = boundingRect(contours[i]);
        rectangle(res, box, Scalar(255,0,0));
    }

    imshow("Original", img);
    imshow("Segmented", res);
    waitKey();

    return 0;
}

初始图片

分段的棕色物体(美式足球)


更新为实际图像

由于您发布的图片比我之前示例中的图片更难(因为您的瞳孔外有很多几乎是棕色的颜色),您还需要:

  1. 正确的范围值
  2. 找到最大的 blob

这段代码显示了这个:

#include <iostream>
#include <vector>
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

int main()
{
    Mat3b img = imread("D:\\SO\\img\\eye.jpg");

    Mat3b hsv;
    cvtColor(img, hsv, COLOR_BGR2HSV);

    Mat1b mask;
    inRange(hsv, Scalar(2, 100, 65), Scalar(12, 170, 100), mask);

    Mat1b kernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
    morphologyEx(mask, mask, MORPH_OPEN, kernel);

    vector<vector<Point>> contours;
    findContours(mask.clone(), contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);

    if (contours.empty()) {return -1;}

    int idx_largest_blob = 0;
    int size_largest_blob = contours[0].size();
    if (contours.size() > 1)
    {
        for (int i = 0; i < contours.size(); ++i)
        {
            if (size_largest_blob < contours[i].size())
            {
                size_largest_blob = contours[i].size();
                idx_largest_blob = i;
            }
        }
    }

    Mat3b res = img.clone();

    drawContours(res, contours, idx_largest_blob, Scalar(0, 255, 0));

    RotatedRect r = minAreaRect(contours[idx_largest_blob]);
    Point2f pts[4];
    r.points(pts);

    for (int j = 0; j < 4; ++j)
    {
        line(res, pts[j], pts[(j + 1) % 4], Scalar(0, 0, 255));
    }

    Rect box = boundingRect(contours[idx_largest_blob]);
    rectangle(res, box, Scalar(255, 0, 0));

    imshow("Original", img);
    imshow("Segmented", res);
    waitKey();

    return 0;
}

结果:

注意:如果您需要更准确的信息,您应该发布一个专门询问瞳孔检测的新问题。 我会删除一些有用的链接,以防万一:

http://answers.opencv.org/question/12034/face-eyes-and-iris-detection/

https://github.com/trishume/eyeLike

https://github.com/laoyang

http://cmp.felk.cvut.cz/~uricamic/flandmark/

http://opencv-code.com/tutorials/pupil-detection-from-an-eye-image/

http://thume.ca/projects/2012/11/04/simple-accurate-eye-center-tracking-in-opencv/

http://opencv-code.com/tutorials/eye-detection-and-tracking/

【讨论】:

    猜你喜欢
    • 2012-07-12
    • 2021-08-29
    • 2013-05-05
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    相关资源
    最近更新 更多