如何在opencv for java中使用轮廓裁剪垫子

如何在opencv for java中使用轮廓裁剪垫子,java,android,opencv,Java,Android,Opencv,我使用下面的代码来查找图像的mat轮廓。我发现轮廓是正确的。但当我试图在轮廓处裁剪图像时,应用程序崩溃了。我错在哪里 List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

我使用下面的代码来查找图像的mat轮廓。我发现轮廓是正确的。但当我试图在轮廓处裁剪图像时,应用程序崩溃了。我错在哪里

        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);

                }
            }
        }
List contours=new ArrayList();
Imgproc.findContours(imageA、等高线、new Mat()、Imgproc.RETR\u列表、Imgproc.CHAIN\u近似值\u简单值);
对于(int i=0;i50){
Rect Rect=Imgproc.boundingRect(contours.get(i));
如果(垂直高度>28){
矩形(图像,新点(矩形x,矩形y),新点(矩形x+矩形宽度,矩形y+矩形高度),新标量(0,0255));
Mat ROI=image.submat(rect.x,rect.x+rect.heigth,rect.x,rect.x+rect.width);
imwrite(“/mnt/sdcard/images/test4.png”,ROI);
}
}
}

您的submat看起来已损坏:

Mat ROI = image.submat(rect.x, rect.x + rect.heigth, rect.x, rect.x + rect.width);
它应该是这样的(我们在这里的row/col世界):


另一种可能的解决方案是直接传递您的Rect:
Mat roi=image.submat(Rect)
Mat ROI = image.submat(rect.y, rect.y + rect.heigth, rect.x, rect.x + rect.width);