OpenCV:检索轮廓中心的颜色

时间:2013-11-30 17:45:01

标签: opencv

我试图使用OpenCV检测黑色图像中一组形状的颜色,我使用Canny检测。然而,颜色输出总是以黑色返回。

std::vector<std::pair<cv::Point, cv::Vec3b> > Asteroids::DetectPoints(const cv::Mat &image)
{
    cv::Mat imageGray;
    cv::cvtColor( image, imageGray, CV_BGR2GRAY );
    cv::threshold(imageGray, imageGray, 1, 255, cv::THRESH_BINARY);
    cv::Mat canny_output;

    std::vector<std::vector<cv::Point> > contours;
    std::vector<cv::Vec4i> hierarchy;

    int thresh = 10;
    // Detect edges using canny
    cv::Canny( imageGray, canny_output, thresh, thresh*2, 3 );
    // Find contours
    cv::findContours( canny_output, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cv::Point(0, 0) );

    std::vector<std::pair<cv::Point, cv::Vec3b> > points;

    for(unsigned int i = 0; i < contours.size(); i++ )
    {
            cv::Rect rect = cv::boundingRect(contours[i]);
            std::pair<cv::Point, cv::Vec3b> posColor;
            posColor.first = cv::Point( rect.tl().x + (rect.size().width / 2), rect.tl().y + (rect.size().height / 2));

            posColor.second = image.at<cv::Vec3b>( posColor.first.x, posColor.first.y );

            //Dont add teh entry to the list if one with the same color and position is already pressent,
            //The contour detection sometimes returns duplicates
            bool isInList = false;
            for(unsigned int j = 0; j < points.size(); j++)
                    if(points[j].first == posColor.first && points[j].second == posColor.second)
                            isInList = true;

            if(!isInList)
                    points.push_back( posColor );
    }
    return points;
}

我知道它必须是这些方面的职位或问题,但我无法弄清楚是什么

1 个答案:

答案 0 :(得分:1)

我可能错了,但不在我的头顶:

不应该阅读

posColor.second = image.at<cv::Vec3b>(posColor.first.y, posColor.first.x);

而不是像你做的那样反过来?

矩阵表示法,而不是笛卡尔符号?