gpt4 book ai didi

Java - OpenCV 忽略额外的轮廓

转载 作者:太空宇宙 更新时间:2023-11-03 23:17:38 26 4
gpt4 key购买 nike

我有一张图片:

enter image description here

我想裁剪它,所以这本书本身就是。

我正在使用 OpenCV 尝试获取图像的轮廓。一旦我画了它们,它看起来像这样。如何忽略图像右侧的额外轮廓?我已经尝试使用具有标准差的离群值。现在它获取矩形内的每个点,并将其添加到数组列表中以供以后处理。我有一个完整的点数组列表,还有 2 个,因此在计算统计分析时可以从最小到最大对点进行排序。

这是现在的样子:

enter image description here

import java.awt.Point;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Rect;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class imtest {



public static void main(String args[]) throws IOException{
String filename="C:/image.png";
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat torect=new Mat();
Mat torect1=Imgcodecs.imread(filename,0);
Imgproc.Canny(torect1, torect, 10, 100);


List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(torect.clone(), contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
ArrayList<Point> outlie=new ArrayList<Point>();
ArrayList<Integer> ylist=new ArrayList<Integer>();
ArrayList<Integer> xlist=new ArrayList<Integer>();

MatOfPoint2f approxCurve = new MatOfPoint2f();

//For each contour found
for (int i=0; i<contours.size(); i++)
{
//Convert contours(i) from MatOfPoint to MatOfPoint2f
MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(i).toArray() );
//Processing on mMOP2f1 which is in type MatOfPoint2f
double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

//Convert back to MatOfPoint
MatOfPoint points = new MatOfPoint( approxCurve.toArray() );

// Get bounding rect of contour
Rect rect = Imgproc.boundingRect(points);
int xoffset=rect.x;
int yoffset=rect.y;
for (int y = 0; y < rect.height; y++) {
for (int x = 0; x < rect.width; x++) {
if (yoffset>1 & xoffset>1)
{
outlie.add(new Point(xoffset+x,yoffset+y));
ylist.add(yoffset+y);
xlist.add(xoffset+x);
}
}
}


}
}
}

最佳答案

调整 canny 的阈值可控制生成图像中的轮廓量。

关于Java - OpenCV 忽略额外的轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36681424/

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