如何将Hough变换应用于我的图像,最终目标是检测矩形?

时间:2017-09-26 06:01:46

标签: java image opencv hough-transform

我一直试图让我的Hough变换来修改我的代码下面的图像。到目前为止,我已经在图像上运行了RGB2GRAY变换和Canny Edge检测功能。我的最终目标是在类似下图的图像上检测VIN贴纸的位置。

我尝试使用Hough变换,但我认为它没有正确实现,因为它对输出图像没有影响。

我目前最困惑的部分是HoughLinesP函数中的“lines”变量实际上是什么。

我最重要的问题:如何将Hough变换的结果应用于输出图像?

我的第二个问题:在应用Hough变换后,我的下一步是识别VIN贴纸的位置?

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;

import javax.imageio.ImageIO;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

public class VinDetector {

public static void main(String args[]) throws Exception {
    try {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        File input = new File("image.jpg");
        BufferedImage image = ImageIO.read(input);  

        byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        Mat sourceImg = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
        sourceImg.put(0, 0, data);

        Mat destImage = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC1);
        Imgproc.cvtColor(sourceImg, destImage, Imgproc.COLOR_RGB2GRAY);

        Imgproc.Canny(destImage, destImage, 50, 100);
        Imgproc.threshold(destImage, destImage, 0, 255, Imgproc.THRESH_BINARY);

        Mat lines = new Mat();
        int threshold = 50; // minimum number of intersections to detect a line 
        int minLineSize = 25; // minimum number of points that can be considered a line
        int lineGap = 10; // maxmimum number of points between two points to be still considered the same line
        Imgproc.HoughLinesP(destImage, lines, 1, Math.PI/180, threshold, minLineSize, lineGap);

        byte[] data1 = new byte[destImage.rows() * destImage.cols() * (int)(destImage.elemSize())];
        destImage.get(0, 0, data1);
        BufferedImage image1 = new BufferedImage(destImage.cols(),destImage.rows(), BufferedImage.TYPE_BYTE_GRAY);
        image1.getRaster().setDataElements(0, 0, destImage.cols(), destImage.rows(), data1);

        File ouptut = new File("image TEST.jpg");
        ImageIO.write(image1, "jpg", ouptut);

    } catch (Exception e) {
        System.out.println("error: " + e.getMessage());
    }
}

}

This image of a VIN on a carthis

0 个答案:

没有答案