gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:53:23 24 4
gpt4 key购买 nike

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

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

它应该像这样(我们在这里处于行/列世界):

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)

关于java - 如何在 opencv for java 中使用轮廓裁剪垫子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22769747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com