title: Any tips or tricks to recognize lines better using HAAR cascades?
body: <p>I'm currently developing an Android app for optical music recognition, using OpenCV. It must run in real time, not using photos. I created a HAAR cascade for the staff (five main lines), but it's incredibly slow (surely under 5 fps) and not so accurate. Isn't there a better way to do this? The problem is that I need it to recognize full width of those lines, but it instead recognizes many smaller lines inside.</p>
  
  <p><a href="https://i.stack.imgur.com/fu11s.png" rel="nofollow noreferrer">The real recognition is green, what I want is blue</a></p>
  
  <p>C++ code:</p>
  
  <pre><code>String lines_cascade_name = "storage/emulated/0/data/haar_lines.xml";
  CascadeClassifier lines_cascade;
  
  std::vector&lt;Rect&gt; lines;
  Mat frame_gray;
  
  cvtColor( frame, frame_gray, CV_BGR2GRAY );
  equalizeHist( frame_gray, frame_gray );
  
  lines_cascade.detectMultiScale( frame_gray, lines, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );
  
  for( size_t i = 0; i &lt; lines.size(); i++ ) {
      rectangle(frame, lines[i].tl(), lines[i].br(), Scalar(0, 255, 0), 2, 8, 0);
  
      Mat faceROI = frame_gray( lines[i] ); Scalar( 255, 0, 0 ), 4, 8, 0 );
  }
  </code></pre>
  
  <p>I need this first step settled, and then recognize notes and other symbols, and calculate where on the clef they are positioned, and play them when they intersect the middle line.</p>
  
  <p>Wouldn't it be better to convert the preview to Canny, since it draws only the lines and edges?</p>
  
  <p>If you had any ideas, I'd be very grateful.</p>
  
  <p>EDIT:
  So I'm trying to implement a Hough Lines Transform in C++, but I keep getting the "OpenCV Error: Unspecified error". The code is following:</p>
  
  <pre><code>JNIEXPORT void JNICALL Java_com_ryu_musicreader_OpencvClass_musicDetection
  (JNIEnv *, jclass, jlong addrRgba){
  Mat&amp; frame = *(Mat*)addrRgba;
  find_lines(frame);
  }
  
  void find_lines(Mat&amp; src){
  
  Mat dst, cdst;
  Canny(src, dst, 50, 200, 3);
  
  cvtColor(dst, cdst, COLOR_GRAY2BGR);
  
  vector&lt;Vec4i&gt; lines;
  HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );
  
  for( size_t i = 0; i &lt; lines.size(); i++ )
  {
      Vec4i l = lines[i];
      line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, 0);
  }
  
  imshow("detected lines", cdst);
  }
  </code></pre>
  
  <p>Included libraries are in my the header file:</p>
  
  <pre><code>#include &lt;jni.h&gt;
  #include "opencv2/opencv.hpp"
  #include "opencv2/highgui/highgui.hpp"
  #include "opencv2/imgproc/imgproc.hpp"
  #include &lt;iostream&gt;
  
  using namespace cv;
  </code></pre>