2015-02-20 2 views
0

Я работаю над приложением для Android. Я могу обнаружить линии с использованием преобразования Canny и Hough, но я не знаю, как получить углы и длины обнаруженных линий, можете ли вы помочь? Вот код:получить длины и углы обнаруженных прямых линий

public void countStraightLines(View view) { 
    try{ 
     int iCannyLowerThreshold = 45; 
     int iCannyUpperThreshold = 75; 

     Mat rgba = Utils.loadResource(getApplicationContext(), res); 
     Bitmap bmp = Bitmap.createBitmap(rgba.width(), rgba.height(), Bitmap.Config.ARGB_8888); 
     Mat gray = new Mat(); 
     Imgproc.cvtColor(rgba, gray, Imgproc.COLOR_BGRA2GRAY, 4); 
     Imgproc.Canny(gray, gray, iCannyLowerThreshold, iCannyUpperThreshold); 
     Utils.matToBitmap(gray, bmp); 
     imgSource.setImageBitmap(bmp); 

    } catch (IOException e) { 
     Log.e(TAG, "ERROR Loading Mat"); 
     e.printStackTrace(); 
    } 


} 

@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, 
     long id) { 

    Globals.pictSelected=parent.getItemAtPosition(position).toString(); 
    res = getResources().getIdentifier(parent.getItemAtPosition(position).toString(), "drawable", this.getPackageName()); 

    try { 

     Mat rgba = Utils.loadResource(getApplicationContext(), res); 
     Bitmap bmp = Bitmap.createBitmap(rgba.width(), rgba.height(), Bitmap.Config.ARGB_8888); 
     Mat gray = new Mat(); 
     Imgproc.cvtColor(rgba, gray, Imgproc.COLOR_BGRA2GRAY, 4); 

     int iCannyLowerThreshold = 45; 
     int iCannyUpperThreshold = 75; 
     int iHoughLinesThreshold = 50; 
     int iHoughLinesMinLineSize = 40; 
     int iHoughLinesGap = 20; 

     Imgproc.Canny(gray, gray, iCannyLowerThreshold, iCannyUpperThreshold); 
     Mat lines = new Mat(); 
     Imgproc.HoughLinesP(gray, lines, 1, Math.PI/180, iHoughLinesThreshold, iHoughLinesMinLineSize, iHoughLinesGap); 

     int x = 0; 
     char s = 'N'; 
     for (; x < Math.min(lines.cols(), 100); x++) 
     { 
      double[] vec = lines.get(0, x); 

      if (vec == null) 
       break; 

      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(rgba, start, end, new Scalar(255, 0, 0, 255), 1); 

      if (x >= 40){s = 'C';} 
      else {s = 'S';} 
     } 
     text.setText("Line Count: " + x + " and The Picture is " + s); 
     Utils.matToBitmap(rgba, bmp); 
     imgSource.setImageBitmap(bmp); 

    } catch (IOException e) { 
     Log.e(TAG, "ERROR Loading Mat"); 
     e.printStackTrace(); 
    } 

} 

Спасибо!

+0

краевые детекторы, такие как Canny, просто манипулируют изображением, они не дают вам линий или позиций. вам понадобятся, например, houghlines или findContours, чтобы получить координаты. – berak

ответ

0

Ну, похоже, у вас есть начальная точка и конечная точка - длина просто ((starty-endy)^2 + (startx-endx)^2)^(1/2). Вы можете получить угол между ними, используя точечный продукт двух векторов A и B A.B=|A|*|B|*cos(alpha), где альфа - это угол между ними. Итак, alpha= arccos((A.B)/(|A|*|B|)).

Смежные вопросы