【发布时间】:2017-11-17 04:02:37
【问题描述】:
我正在尝试在照片中的脚周围画一个框。我可以用其他对象来做这件事,包括那些不完全在图像中的对象(我在下面粘贴了我的代码)。但是,当我尝试将它与脚一起使用时,它会完美地绘制轮廓,但矩形始终是整个图像的周围。我在下面附上了结果的图片。有没有办法改变我的代码来解决这个问题,或者有没有其他方法可以在脚周围画一个框?
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);
【问题讨论】:
-
你好@jacob Perks 你需要在这里检查一下,他们在汽车周围制作了矩形。可能是你从这里得到的参考。 stackoverflow.com/questions/46044853/… 或 docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/…