在Canny边缘的结果周围绘制Rect

时间:2016-01-06 10:59:11

标签: c++ opencv edge-detection rect

我想在检测到的canny边缘周围绘制Rect。我有这个图像,它是眼睛检测,形态学操作和精细边缘的结果。

http://www.otherwebsite.com

我尝试使用等高线将其绑定到rect但结果不准确。

我怎样才能得到像这张图片的东西?

enter image description here

我正在使用此功能绘制轮廓:

void find_contour(Mat image)
{
    Mat src_mat, gray_mat, canny_mat;
    Mat contour_mat;
    Mat bounding_mat;

    contour_mat = image.clone();
    bounding_mat = image.clone();

    cvtColor(image, gray_mat, CV_GRAY2BGR);

    // apply canny edge detection

    Canny(gray_mat, canny_mat, 30, 128, 3, false);

    //3. Find & process the contours
    //3.1 find contours on the edge image.

    vector< vector< cv::Point> > contours;
    findContours(canny_mat, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

    //3.2 draw contours & property value on the source image.

    int largest_area = 0;
    int largest_contour_index = 0;
    Rect bounding_rect;

    for (size_t i = 0; i< contours.size(); i++) // iterate through each contour.
    {
        double area = contourArea(contours[i]);  //  Find the area of contour

        if (area > largest_area)
        {
            largest_area = area;
            largest_contour_index = i;               //Store the index of largest contour
            bounding_rect = boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
        }
    }

    drawContours(image, contours, largest_contour_index, Scalar(0, 255, 0), 2);

    imshow("Bounding ", image);
}

2 个答案:

答案 0 :(得分:1)

在代码中

根本没有绘制边界矩形。试试这个:

void find_contour(Mat image)
{
Mat src_mat, gray_mat, canny_mat;
Mat contour_mat;
Mat bounding_mat;

contour_mat = image.clone();
bounding_mat = image.clone();

cvtColor(image, gray_mat, CV_GRAY2BGR);

// apply canny edge detection

Canny(gray_mat, canny_mat, 30, 128, 3, false);

//3. Find & process the contours
//3.1 find contours on the edge image.

vector< vector< cv::Point> > contours;
findContours(canny_mat, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

//3.2 draw contours & property value on the source image.

int largest_area = 0;
int largest_contour_index = 0;
Rect bounding_rect;

for (size_t i = 0; i< contours.size(); i++) // iterate through each contour.
{
    // draw rectangle around the contour:
    cv::Rect boundingBox = boundingRect(contours[i]);
    cv::rectangle(image, boundingBox, cv::Scalar(255,0,255)); // if you want read and "image" is color image, use cv::Scalar(0,0,255) instead

    // you aren't using the largest contour at all? no need to compute it...
    /*
    double area = contourArea(contours[i]);  //  Find the area of contour

    if (area > largest_area)
    {
        largest_area = area;
        largest_contour_index = i;               //Store the index of largest contour
        bounding_rect = boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
    }
    */
}

//drawContours(image, contours, largest_contour_index, Scalar(0, 255, 0), 2);

imshow("Bounding ", image);
}

答案 1 :(得分:0)

你也可以这样做,

findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
vector<RotatedRect> minRect( contours.size() );

/// Draw contours
Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
     Scalar color = Scalar(255, 255, 255);
     cv::Rect boundingBox = cv::boundingRect(cv::Mat(contours[i]));
     minRect[i] = minAreaRect(Mat(contours[i]));
     drawContours( drawing, contours, i, color, 1, 8, hierarchy, 0, Point() );
}
for( int i = 0; i< contours.size(); i++ )
{
     // rotated rectangle
     Point2f rect_points[4]; minRect[i].points( rect_points );
     for( int j = 0; j < 4; j++ )
     line( drawing, rect_points[j], rect_points[(j+1)%4], Scalar(0,0,255), 1, 8 );
}