【问题标题】:OpenCV iOS Drawing Bounding BoxOpenCV iOS 绘图边界框
【发布时间】:2017-11-17 04:02:37
【问题描述】:

我正在尝试在照片中的脚周围画一个框。我可以用其他对象来做这件事,包括那些不完全在图像中的对象(我在下面粘贴了我的代码)。但是,当我尝试将它与脚一起使用时,它会完美地绘制轮廓,但矩形始终是整个图像的周围。我在下面附上了结果的图片。有没有办法改变我的代码来解决这个问题,或者有没有其他方法可以在脚周围画一个框?

Original Image

Contour and Bounding Box

RNG rng(12345);
Mat threshold_output;
vector<vector<cv::Point> > contours;
vector<Vec4i> hierarchy;

Mat srcImage;
UIImageToMat(image, srcImage);
cvtColor(srcImage, srcImage, CV_BGR2GRAY);
blur(srcImage, srcImage, cv::Size(3,3));

threshold(srcImage, threshold_output, 100, 255, THRESH_BINARY );
findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );


vector<vector<cv::Point> > contours_poly( contours.size() );
vector<cv::Rect> boundRect( contours.size() );

for( int i = 0; i < contours.size(); i++ ) {
    approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
    boundRect[i] = boundingRect( Mat(contours_poly[i]) );
}

Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
    if (contourArea(contours[i]) < 1000) {
        continue;
    }
    Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
    drawContours(drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, cv::Point());
    rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
    cout << "Height:" << boundRect[i].height << " Width: " << boundRect[i].width << endl;
}

return MatToUIImage(drawing);

【问题讨论】:

标签: ios opencv


【解决方案1】:

由于脚的底部到达图像边界,轮廓似乎环绕图像的周边,从而创建了一个包含整个图像的大边界框。

一个快速而肮脏的解决方案是在应用findContours() 之前将图像的边框像素设置为零。为了获得更好的解决方案,您必须考虑您要解决的实际问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-03
    • 2013-01-21
    • 2021-10-20
    • 1970-01-01
    • 2021-11-26
    • 2013-01-08
    • 1970-01-01
    • 2021-12-04
    相关资源
    最近更新 更多