我正在使用以下代码来查找图像的垫子轮廓。我发现轮廓正确。但是当我尝试在轮廓处裁剪图像时,应用程序崩溃了。我错在哪里?
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);
}
}
}
你的子垫看起来坏了:
Mat ROI = image.submat(rect.x, rect.x + rect.heigth, rect.x, rect.x + rect.width);
它应该是这样的(我们在这里的行/列世界中):
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)