OpenCV-删除错误的轮廓

时间:2018-10-15 15:28:42

标签: java opencv qr-code opencv-contour opencv-drawcontour

我对OpenCV以及寻找特定形状有一个简短的问题。在我的PC上,我得到了一些形状的图片,但是我只想要矩形的轮廓:

输入文件: Input File:

我的输出应该是: What my output should be

我的输出实际上是: What my output actually is


我做了什么:

  1. 打开我的图像并将其转换为OpenCV Mat。
  2. 进行一些图像处理[灰度,模糊]
  3. 发现了Canny的优势
  4. 使用“ findContours”找到轮廓
  5. 使用“ boundingRect”在我的轮廓周围绘制矩形

这就是我坚持的地方。我不知道如何消除错误的轮廓。我尝试了遍历我的轮廓,并删除了不正确的轮廓。但是我不知道如何找到错误的轮廓。有没有我需要使用的配方师?像这样?我找到了带有“ arcLength”的东西,但我不明白这一点。


这是我的代码:

package main;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;


import helper.ImageProcHelper;


public class Main {


    public static void main(String[] args) throws Exception {

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        File file = new File("C:\\Users\\Enrico Gründig\\Desktop\\Samples\\pic4.png");

        Mat mat = new Mat(CvType.CV_8UC4);
        Mat procMat = new Mat();
        Mat hierarchy = new Mat();
        Scalar color = new Scalar(0,0,255);
        List<MatOfPoint> contours = new ArrayList<>();

        try {
            BufferedImage picture = ImageIO.read(file);
            BufferedImage image = new BufferedImage(picture.getWidth(), picture.getHeight(), 5);
            image.getGraphics().drawImage(picture, 0, 0, null);

            System.out.println(image.getType());
            mat = ImageProcHelper.ImageToMat(image);

            Imgproc.cvtColor(mat, procMat, Imgproc.COLOR_RGBA2GRAY);
            Imgproc.blur(procMat, procMat, new Size(3,3));
            Imgproc.Canny(procMat, procMat, 127, 255);

            //Konturen finden           
            Imgproc.findContours(procMat, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);

            MatOfPoint2f[] contoursPoly = new MatOfPoint2f[contours.size()];
            Rect[] boundRect = new Rect[contours.size()];           


            for(int i = 0; i < contours.size(); i++) {
                contoursPoly[i] = new MatOfPoint2f();
                Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 0.1,  true);
                boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contours.get(i).toArray()));     
            }


            for (int i = 0; i < contours.size(); i++) {
                Imgproc.rectangle(mat, boundRect[i].tl(), boundRect[i].br(), color, 1);
            }

            image = ImageProcHelper.MatToImage(mat);
            ImageIO.write((RenderedImage)image, "png", new File ("C:\\Users\\Enrico Gründig\\Desktop\\Samples\\output.png"));


        } catch (IOException e) {
            System.out.println("Error");
        }   
    }
}

这个项目有什么意义:

我有一个IP摄像机正在播放视频。在这个项目中,我想找到流中的所有QR码,将它们裁剪并传递给解码器(例如ZXing)。我只用ZXing尝试过,但是角度,大小等方面都有问题。这就是为什么我要使用OpenCV查找代码并操纵它们以减少从IP摄像机到解码器的流量,并(也许)增加点击率的原因。

QR代码示例: QR Code Sample

这应该是我的输出: This should be my output

这是我的输出: This is my output


非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我没有足够的声誉来发表评论,但是您似乎缺少的是检查每个轮廓的边。在您的代码中,您需要使用ContoursPoly [i] .size()来区分不同的形状。您的代码最终需要看起来像这样:

package main;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;


import helper.ImageProcHelper;


public class Main {


    public static void main(String[] args) throws Exception {

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        File file = new File("C:\\Users\\Enrico Gründig\\Desktop\\Samples\\pic4.png");

        Mat mat = new Mat(CvType.CV_8UC4);
        Mat procMat = new Mat();
        Mat hierarchy = new Mat();
        Scalar color = new Scalar(0,0,255);
        List<MatOfPoint> contours = new ArrayList<>();

        try {
            BufferedImage picture = ImageIO.read(file);
            BufferedImage image = new BufferedImage(picture.getWidth(), picture.getHeight(), 5);
            image.getGraphics().drawImage(picture, 0, 0, null);

            System.out.println(image.getType());
            mat = ImageProcHelper.ImageToMat(image);

            Imgproc.cvtColor(mat, procMat, Imgproc.COLOR_RGBA2GRAY);
            Imgproc.blur(procMat, procMat, new Size(3,3));
            Imgproc.Canny(procMat, procMat, 127, 255);

            //Konturen finden           
            Imgproc.findContours(procMat, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);

            MatOfPoint2f[] contoursPoly = new MatOfPoint2f[contours.size()];
            Rect[] boundRect = new Rect[contours.size()];           


            for(int i = 0; i < contours.size(); i++) {
                contoursPoly[i] = new MatOfPoint2f();
                Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 0.1,  true);
                boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contours.get(i).toArray()));  
            }


            for (int i = 0; i < contours.size(); i++) {
                if (contoursPoly[i].size()>15){
                    Imgproc.rectangle(mat, boundRect[i].tl(), boundRect[i].br(), color, 1);
                }
            }

            image = ImageProcHelper.MatToImage(mat);
            ImageIO.write((RenderedImage)image, "png", new File ("C:\\Users\\Enrico Gründig\\Desktop\\Samples\\output.png"));


        } catch (IOException e) {
            System.out.println("Error");
        }   
    }
}

我没有使用OpenCV进行Java设置,因此我无法测试此代码,但是这个想法来自这个link。您可能需要弄乱“ 15”才能区分矩形和圆形。