如何在Java中使用OpenCV通过轮廓裁剪Mat?

4

我正在使用以下代码查找图像的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);

                }
            }
        }
1个回答

9

3
另一个可能的解决方案是直接传递您的矩形参数:Mat roi = image.submat(rect); - panc_fab

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接