2016-08-03 469 views
0

我正在開發一個程序來檢測矩形形狀並繪製邊界框到檢測區域。Java OpenCV - 使用霍夫變換的矩形檢測

對於邊緣檢測,我使用了Canny邊緣檢測。然後,我使用霍夫變換來提取線條。

這是原始圖像 enter image description here

這是結果圖像 enter image description here

我的問題是我不能畫一個邊界框所檢測到的區域。 看來我的程序只能檢測到一條水平線。 如何檢測矩形形狀並將矩形線繪製爲檢測到的形狀?

我讀過類似的問題,它需要找到矩形的4個角點,檢查點是否是90度,並找到交點。我真的很困惑如何在Java opencv中編寫它。其他方法來檢測形狀和繪製邊界框到檢測到也可以。

下面的代碼

import org.opencv.core.Core; 
import org.opencv.core.CvType; 
import org.opencv.core.Mat; 
import org.opencv.core.Point; 
import org.opencv.core.Scalar; 
import org.opencv.core.Size; 
import org.opencv.imgcodecs.*; 
import org.opencv.imgproc.Imgproc; 

public class HoughTransformCV2 { 

    public static void main(String[] args) { 
     try { 
      System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 
      Mat source = Imgcodecs.imread("rectangle.jpg", Imgcodecs.CV_LOAD_IMAGE_ANYCOLOR); 
      Mat destination = new Mat(source.rows(), source.cols(), source.type()); 

      Imgproc.cvtColor(source, destination, Imgproc.COLOR_RGB2GRAY); 
      Imgproc.equalizeHist(destination, destination); 
      Imgproc.GaussianBlur(destination, destination, new Size(5, 5), 0, 0, Core.BORDER_DEFAULT); 

      Imgproc.Canny(destination, destination, 50, 100); 
      //Imgproc.adaptiveThreshold(destination, destination, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 40); 
      Imgproc.threshold(destination, destination, 0, 255, Imgproc.THRESH_BINARY); 

      if (destination != null) { 
       Mat lines = new Mat(); 
       Imgproc.HoughLinesP(destination, lines, 1, Math.PI/180, 50, 30, 10); 
       Mat houghLines = new Mat(); 
       houghLines.create(destination.rows(), destination.cols(), CvType.CV_8UC1); 

       //Drawing lines on the image 
       for (int i = 0; i < lines.cols(); i++) { 
        double[] points = lines.get(0, i); 
        double x1, y1, x2, y2; 
        x1 = points[0]; 
        y1 = points[1]; 
        x2 = points[2]; 
        y2 = points[3]; 

        Point pt1 = new Point(x1, y1); 
        Point pt2 = new Point(x2, y2); 

        //Drawing lines on an image 
        Imgproc.line(source, pt1, pt2, new Scalar(0, 0, 255), 4); 
       } 

      } 

      Imgcodecs.imwrite("rectangle_houghtransform.jpg", source); 

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

在Java的任何幫助,將不勝感激:) 非常感謝你!

回答

3

您可以在這些步驟做:

  1. 你做了轉換的顏色以灰色後,執行Canny邊緣。

    int threshold = 100;

    Imgproc.Canny(grayImage,edges,threshold,threshold * 3);

  2. 現在找到邊緣圖像中的輪廓。

Imgproc.findContours(edges, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

  • 然後環上遍及輪廓
  • .....

    MatOfPoint2f matOfPoint2f = new MatOfPoint2f(); 
    MatOfPoint2f approxCurve = new MatOfPoint2f(); 
    
    for (int idx = 0; idx >= 0; idx = (int) hierarchy.get(0, idx)[0]) { 
        MatOfPoint contour = contours.get(idx); 
        Rect rect = Imgproc.boundingRect(contour); 
        double contourArea = Imgproc.contourArea(contour); 
        matOfPoint2f.fromList(contour.toList()); 
        Imgproc.approxPolyDP(matOfPoint2f, approxCurve, Imgproc.arcLength(matOfPoint2f, true) * 0.02, true); 
        long total = approxCurve.total(); 
        if (total == 3) { // is triangle 
         // do things for triangle 
        } 
        if (total >= 4 && total <= 6) { 
         List<Double> cos = new ArrayList<>(); 
         Point[] points = approxCurve.toArray(); 
         for (int j = 2; j < total + 1; j++) { 
          cos.add(angle(points[(int) (j % total)], points[j - 2], points[j - 1])); 
         } 
         Collections.sort(cos); 
         Double minCos = cos.get(0); 
         Double maxCos = cos.get(cos.size() - 1); 
         boolean isRect = total == 4 && minCos >= -0.1 && maxCos <= 0.3; 
         boolean isPolygon = (total == 5 && minCos >= -0.34 && maxCos <= -0.27) || (total == 6 && minCos >= -0.55 && maxCos <= -0.45); 
         if (isRect) { 
          double ratio = Math.abs(1 - (double) rect.width/rect.height); 
          drawText(rect.tl(), ratio <= 0.02 ? "SQU" : "RECT"); 
         } 
         if (isPolygon) { 
          drawText(rect.tl(), "Polygon"); 
         } 
        } 
    } 
    

    輔助方法:

    private double angle(Point pt1, Point pt2, Point pt0) { 
        double dx1 = pt1.x - pt0.x; 
        double dy1 = pt1.y - pt0.y; 
        double dx2 = pt2.x - pt0.x; 
        double dy2 = pt2.y - pt0.y; 
        return (dx1*dx2 + dy1*dy2)/Math.sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10); 
    } 
    
    private void drawText(Point ofs, String text) { 
        Imgproc.putText(colorImage, text, ofs, Core.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar(255,255,25)); 
    } 
    

    希望這可以幫助你!

    +0

    @Bahrandum Adil你會澄清if方法中的「角度」,「drawText」和「三角形」方法嗎?我是新的opencv,我不知道這些是庫中的靜態方法還是你自己實現的函數。你對Imgproc.Canny使用什麼「門檻」? –

    +0

    @JonathanBarbero嗨!大部分OpenCV函數都是靜態的。像Imgproc或Core的函數一樣,您可以直接使用這些函數,只需使用Class name函數名即可。答案也已更新,您可以再次查看答案。祝你好運! –