2015-12-03 60 views
3

我的mRgba對象的尺寸爲0X0,因此它根本不會在圖片上返回任何線條 。我想它是空的。代碼中有什麼問題?有沒有辦法在黑色背景上顯示 ?使用openCV在android中檢測Hough線

下面是代碼

mat = new Mat(); 
    edges = new Mat(); 
    Size kernel = new Size(5, 5); 
    Mat gauss = new Mat(); 
    Mat mRgba = new Mat(612,816, CvType.CV_8UC1); 
    Mat lines = new Mat(612,816, CvType.CV_8UC1); 
    binary_image = new Mat(); 
    Utils.bitmapToMat(bitmap, mat); 
    Imgproc.GaussianBlur(mat, gauss, kernel, 10000, 10000); 
    Imgproc.Canny(gauss, edges, 50, 90); 
    Imgproc.threshold(edges, binary_image, 0, 255, Imgproc.THRESH_BINARY_INV); 

    int threshold = 50; 
    int minLineSize = 20; 
    int lineGap = 20; 

    Imgproc.HoughLinesP(binary_image, lines, 1, Math.PI/180,threshold,minLineSize,lineGap); 



    for (int x = 0; x < lines.cols(); x++) { 
     double[] vec = lines.get(0, x); 
     double x1 = vec[0], 
       y1 = vec[1], 
       x2 = vec[2], 
       y2 = vec[3]; 
     Point start = new Point(x1, y1); 
     Point end = new Point(x2, y2); 

     Core.line(mRgba, start, end, new Scalar(255, 0, 0), 3); 

    } 

    Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888); 

    Utils.matToBitmap(mRgba, bmp); 
     bitmap=bmp; 

編輯:這個問題已經被去除高斯模糊和閾值法

mat = new Mat(); 
    edges = new Mat();  
    Mat mRgba = new Mat(612,816, CvType.CV_8UC1); 
    Mat lines = new Mat(); 

    Utils.bitmapToMat(bitmap, mat); 

    Imgproc.Canny(mat, edges, 50, 90); 


    int threshold = 50; 
    int minLineSize = 20; 
    int lineGap = 20; 

    Imgproc.HoughLinesP(edges, lines, 1, Math.PI/180,threshold,minLineSize,lineGap); 



    for (int x = 0; x < lines.cols(); x++) { 
     double[] vec = lines.get(0, x); 
     double x1 = vec[0], 
       y1 = vec[1], 
       x2 = vec[2], 
       y2 = vec[3]; 
     Point start = new Point(x1, y1); 
     Point end = new Point(x2, y2); 

     Core.line(mRgba, start, end, new Scalar(255, 0, 0), 3); 

    } 

    Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888); 

    Utils.matToBitmap(mRgba, bmp); 
     bitmap=bmp; 

回答

2
  1. 您沒有指定大小mRgba當你創建它解決,它應該與您嘗試查找線條的圖像具有相同的尺寸。
  2. 您應該檢查lines對象,看看您是否能夠找到線或 而不是。您可以通過檢查mRgba來了解它。
  3. 這似乎並未是整個代碼,它沒有圖像載入中。分享整個代碼和示例圖像可能會更好。
+0

here Utils.bitmapToMat(bitmap,mat);我正在從相機加載圖像,並且在調試期間它絕對精細並且可見。我如何設置Mat對象的大小? – marti6addams

+0

@ marti6addams我沒有說有關於加載圖像的問題,我說它在這裏丟失。對於你的問題,請閱讀一些文件。在這裏:http://docs.opencv.org/2.4.11/modules/core/doc/basic_structures.html#mat-mat – guneykayim

+0

@ marti6addams,你可以理解你是否找到線條或不是因爲我在第2點傷心,沒有可視化,但仍然可視化總是很好。 – guneykayim