查找和消除轮廓 opencv

Find and eliminate contours opencv

我正在开发一个程序,提取拼图上的贴纸,然后找到它们的 RGB。目前,我正处于想要删除任何不像 "square" 的轮廓的地步。我想知道我怎么能做到这一点。

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

有什么方法可以画出轮廓而不是填充它们?并删除周围尺寸不大致相同或角度几乎为 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);
}

这是初始图像:

这是绘制了轮廓的图像:

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

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