I'm using the below code in order to find mat contours of an Image. I found the contours correct. But when I try to crop the image at contours the app crashes. Where am I wrong?
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
for(int i=0; i< contours.size();i++){
if (Imgproc.contourArea(contours.get(i)) > 50 ){
Rect rect = Imgproc.boundingRect(contours.get(i));
if (rect.height > 28){
Core.rectangle(image, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255));
Mat ROI = image.submat(rect.x, rect.x + rect.heigth, rect.x, rect.x + rect.width);
Highgui.imwrite("/mnt/sdcard/images/test4.png",ROI);
}
}
}
your submat looks broken:
Mat ROI = image.submat(rect.x, rect.x + rect.heigth, rect.x, rect.x + rect.width);
it should like this (we're in row/col world here):
Mat ROI = image.submat(rect.y, rect.y + rect.heigth, rect.x, rect.x + rect.width);
http://docs.opencv.org/java/org/opencv/core/Mat.html#submat(int,%20int,%20int,%20int)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With