使用opencv检测棕色对象 [英] Detect Brown colour object using opencv

查看:209
本文介绍了使用opencv检测棕色对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Image中找到Brown color Object。我已经完成了以下过程:

I want find Brown colour Object in Image.I have done following process:


  1. 将图像BGR转换为HSV

  2. 我使用opencv lib的inRange函数来查找棕色。




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

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




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

输入图片

问题

我想检测上面图像中的棕色眼睛。当我使用上面的棕色系列时我得到了零轮廓。

I want detect brown colour of eye in above image .When I use above range for brown colour i got zero contour.

是否在棕色以上的范围是正确的?应该是什么?

is above range for brown colour is correct? what should be it?

推荐答案

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

You can segment a brown object in an image playing around with HSV ranges. Since brown is somehow a darker red, you need to tweak the parameters a little. If you post a reference image we could find a more accurate range.

一旦有了对象蒙版(通常将一些形态应用于 clean 掩码),您可以使用 findContours 轻松获得轮廓。

Once you have the object mask (you usually apply some morphology to clean the mask), you can easily get the contours with findContours.

以下示例说明了这一点:

The example below explains this:

#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;
}

初始图片

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

使用实际图片进行更新

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

Since the image you posted is somehow more difficult then the one in my former example (because you have a lot of almost brown color outside the pupil), you need also to:


  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;
}

结果:

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

Note: if you need something more accurate, you should post a new question asking specifically for pupil detection. I'll drop a few useful links, just in case:

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/201 2/11/04 / simple-accurate-eye-center-tracking-in-opencv /

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

这篇关于使用opencv检测棕色对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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