[英]how to crop mat using contours in opencv for java
我正在使用以下代碼來查找圖像的墊輪廓。 我發現輪廓正確。 但是當我嘗試在輪廓處裁剪圖像時,應用程序崩潰了。 我哪里錯了?
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);
}
}
}
你的submat看起來很破碎:
Mat ROI = image.submat(rect.x, rect.x + rect.heigth, rect.x, rect.x + rect.width);
它應該是這樣的(我們在這里的行/ col世界):
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.