opencv 裁剪 java_OpenCV绘制轮廓和裁剪
我是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 > 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;
}
這將為照片中的所有對象繪制輪廓 . 但是如何才能在白紙上提取物體?例如在這張圖片中:
我只想從圖像裁剪卡,但我不知道如何繼續 . 誰能幫我嗎?
總結
以上是生活随笔為你收集整理的opencv 裁剪 java_OpenCV绘制轮廓和裁剪的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 建信融通是干嘛的
- 下一篇: 银含量925‰是什么意思