1 | initial version |
Create a mask image for the area to be cropped
2.Find the bounding box for mask, you should do the following
3.Crop the source using bound rect.
Code:
Mat src=imread("barcode.png",1);
Mat thr,gray;
cvtColor(src,gray,CV_BGR2GRAY);
threshold( gray, thr, 50, 255, CV_THRESH_BINARY_INV );
Size kernalSize (21,21);
Mat element = getStructuringElement (MORPH_RECT, kernalSize, Point(1,1) );
morphologyEx( thr, thr, MORPH_CLOSE, element );
vector< vector <Point> > contours;
vector< Vec4i > hierarchy;
findContours( thr, contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );
Rect ROI=boundingRect(contours[0]); //Contour size should bealways 1
Mat crop=src(ROI);
imwrite("crop.jpg",crop);
2 | No.2 Revision |
Create a mask image for the area to be cropped
2.Find the bounding box for mask, you should do the following
3.Crop the source using bound rect.
Code:
Mat src=imread("barcode.png",1);
Mat thr,gray;
cvtColor(src,gray,CV_BGR2GRAY);
threshold( gray, thr, 50, 255, CV_THRESH_BINARY_INV );
Size kernalSize (21,21);
Mat element = getStructuringElement (MORPH_RECT, kernalSize, Point(1,1) );
morphologyEx( thr, thr, MORPH_CLOSE, element );
vector< vector <Point> > contours;
vector< Vec4i > hierarchy;
findContours( thr, contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );
Rect ROI=boundingRect(contours[0]); //Contour size should bealways 1
Mat crop=src(ROI);
imwrite("crop.jpg",crop);
3 | No.3 Revision |
Create a mask image for the area to be cropped
2.Find the bounding box for mask, you should do the following
3.Crop the source using bound rect.
Code:
Mat src=imread("barcode.png",1);
Mat thr,gray;
cvtColor(src,gray,CV_BGR2GRAY);
threshold( gray, thr, 50, 255, CV_THRESH_BINARY_INV );
Size kernalSize (21,21);
Mat element = getStructuringElement (MORPH_RECT, kernalSize, Point(1,1) );
morphologyEx( thr, thr, MORPH_CLOSE, element );
vector< vector <Point> > contours;
vector< Vec4i > hierarchy;
findContours( thr, contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );
Rect ROI=boundingRect(contours[0]); //Contour size should bealways 1
Mat crop=src(ROI);
imwrite("crop.jpg",crop);