gpt4 book ai didi

java - 查找并消除轮廓opencv

转载 作者:行者123 更新时间:2023-12-02 12:23:31 26 4
gpt4 key购买 nike

我正在开发一个程序,可以提取拼图上的贴纸,然后找到它们的 RGB。目前,我想要删除任何不是“方形”的轮廓。我想知道我怎样才能做到这一点。

我所做的就是加载图像,对其进行灰度化、模糊化、精明的边缘检测、扩大图像、找到轮廓并绘制它们。

有没有办法可以围绕轮廓绘制而不是填充它们?并删除周围大小不大致相同或几乎呈 90 度角的轮廓?

public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

Mat capturedFrame = Imgcodecs.imread("first.png");

//Gray
Mat gray = new Mat();
Imgproc.cvtColor(capturedFrame, gray, Imgproc.COLOR_BGR2GRAY);

//Blur
Mat blur = new Mat();
Imgproc.blur(gray, blur, new Size(3,3));
//Canny image
Mat canny = new Mat();
Imgproc.Canny(blur, canny, 20, 40, 3, true);

Imgcodecs.imwrite("test.png", canny);

//System.exit(0);
Mat kernel = Imgproc.getStructuringElement(1, new Size(3,3));
Mat dilated = new Mat();
Imgproc.dilate(canny,dilated, kernel);

List<MatOfPoint> contours = new ArrayList<>();
//find contours
Imgproc.findContours(dilated, contours, new Mat(), Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_NONE);
//draw contours

Imgproc.cvtColor(capturedFrame, capturedFrame, Imgproc.COLOR_BGR2RGB);
for(int i = 0; i < contours.size(); i++){
Imgproc.drawContours(capturedFrame, contours, i, new Scalar(0, 0, 255), -1);
}

Imgcodecs.imwrite("after.png", capturedFrame);

Imshow img = new Imshow("firstImg");
img.show(capturedFrame);
}

这是初始图像:

enter image description here

这是绘制轮廓的图像:

enter image description here

最佳答案

要绘制非填充轮廓,请使用非负厚度: Imgproc.drawContours(capturedFrame,contours, i, new Scalar(0, 0, 255), 1);例如。

删除不必要的查找轮廓区域,并在绘制时跳过太大或太小的轮廓区域。

关于java - 查找并消除轮廓opencv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45616499/

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