1 | initial version |
Hi If you are having multiple objects or otherwise multiple contours in your image & you simply want the Bounding box of the overall mask you can easily find the Bounding box as follows!
Mat mSource_Bgr,mSource_Gray,mThreshold;
mSource_Bgr= imread(List_Files[i],1);
cvtColor(mSource_Bgr,mSource_Gray,COLOR_BGR2GRAY);
threshold(mSource_Gray,mThreshold,254,255,THRESH_BINARY_INV);
Mat Points;
findNonZero(mThreshold,Points);
Rect Min_Rect=boundingRect(Points);
rectangle(mSource_Bgr,Min_Rect.tl(),Min_Rect.br(),Scalar(0,255,0),2);
imshow("Result",mSource_Bgr);
Source Image
Threshold Mask
Cropped Bounding Rect
2 | No.2 Revision |
Hi If you are having multiple objects or otherwise multiple contours in your image & you simply want the Bounding box of the overall mask you can easily find the Bounding box as follows!
Mat mSource_Bgr,mSource_Gray,mThreshold;
mSource_Bgr= imread(List_Files[i],1);
cvtColor(mSource_Bgr,mSource_Gray,COLOR_BGR2GRAY);
threshold(mSource_Gray,mThreshold,254,255,THRESH_BINARY_INV);
Mat Points;
findNonZero(mThreshold,Points);
Rect Min_Rect=boundingRect(Points);
//Here you can also use minAreaRect() also to find Rotated Rect.
rectangle(mSource_Bgr,Min_Rect.tl(),Min_Rect.br(),Scalar(0,255,0),2);
imshow("Result",mSource_Bgr);
Source Image
Threshold Mask
Cropped Bounding Rect