我是OpenCV的新手 . 首先,将物体放在白纸上,然后使用机器人相机拍摄照片 . 在下一步,我试图使用OpenCV(找到轮廓和绘制轮廓)提取放在白纸上的对象 . 我想将这个对象用于我的机器人项目 .
示例图片:
这是我试过的代码:
int main(int argc, char* argv[]){
int largest_area=0;
int largest_contour_index=0;
Rect bounding_rect;
// read the file from console
Mat img0 = imread(argv[1], 1);
Mat img1;
cvtColor(img0, img1, CV_RGB2GRAY);
// Canny filter
Canny(img1, img1, 100, 200);
// find the contours
vector< vector<Point> > contours;
findContours(img1, contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
printf("%ld\n", contours.size());
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
}
}
cout << "contour " << contours.size() << endl;
cout << "largest contour " << largest_contour_index << endl;
Scalar color = Scalar(0,0,255);
drawContours(img0, contours, -1, color);
Mat roi = Mat(img0, bounding_rect);
// show the images
imshow("result", img0);
imshow("roi",roi);
imwrite("result.png",roi);
waitKey();
return 0;
}
这将为照片中的所有对象绘制轮廓 . 但是如何才能在白纸上提取物体?例如在这张图片中:
我只想从图像裁剪卡,但我不知道如何继续 . 谁能帮我吗?
1 回答
在源图像上应用ROI,如下所示:
选择矩形的适当尺寸应从图像中删除白板外的区域