F
F
Fedor2016-12-02 18:49:16
C++ / C#
Fedor, 2016-12-02 18:49:16

How to crop extra path fragments in OpenCV?

Hello. I am engaged in the task of finding the central vein on the leaf of a plant.
For example, there are the following photographs, where I circled in red what I need to highlight:
1686e1387cb0493aa4baf527529b2e35.pngcbc8c4f6bbe248e1babb3c2f5b8416b2.pngce81c4a1e14e414c9264a57966d7fa70.png
and using the search for contours and games with color and channels, side veins are also highlighted: I
563081f70da54e1a952c967f2c36153b.pngf55998ae6d8f449f8f9e1a17d0e8e40d.png
used the search for lines, but it turns out to be porridge
28c0bded7d7b4704984c8da568fe07c3.jpg
from which I mostly could clear the side contours by discarding them according to a certain condition,
but there are still garbage lines highlighted with red squares ....
32a5ba2ce0be46538b9a3d4b6ad8f3bf.jpg
and an attempt to convert the lines back to a contour leads to the following result:
52271865d3ba447e9f68c8de6c512cce.jpg
Who has any ideas how to solve this problem?
PS> I used tips like the binarization threshold at the very first stage, and to highlight the contour of the veins I use the following code:

src = qimage2mat(img);
    cvtColor(src,src, CV_RGB2HSV_FULL);
    // Create a structuring element (SE)
    int morph_size = 2;
    Mat element = getStructuringElement( MORPH_RECT, Size(morph_size+1, morph_size+1), Point( morph_size, morph_size) );
    morphologyEx( src, dst, MORPH_TOPHAT, element, Point(-1,-1),15 );
    // Apply the specified morphology operation
    vector<Mat> rgbChannels(3);
    split(dst, rgbChannels);
    bitwise_not(rgbChannels[1],tst);
    short int thVmin = 190;
    threshold(tst,dst, thVmin, 255, THRESH_BINARY);

    vector<vector<Point> > contours;
    vector<Point> leafContour;
    vector<Vec4i> hierarchy;
    findContours(dst, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

PSS> Maybe someone knows a more advanced line search algorithm?
PSSS> code that I used to find lines and clean up unnecessary garbage, but never cleaned it up ...
//ищем линии
          Mat cdst;
          Canny(drawing, dst, 50, 200,5);
          cvtColor(dst, cdst, COLOR_GRAY2BGR);
          vector<Vec4i> lines, lines1;
          //параметры 20,5,30 определены эксперементально
          HoughLinesP(dst, lines, 1, CV_PI/360, 20, 5, 30 );
          int mmY1 = mmY-80;
          int mmY2 = mmY+80;
          //делаем первую операцию по очистке
          for (unsigned int i = 0; i < lines.size(); i++ ) {
              Vec4i l = lines[i];
              if( (l[1] > mmY1 &&  l[1]<mmY2) &&
                   (l[3] > mmY1 &&  l[3]<mmY2)) {
                  lines1.push_back(l);
              }
          }
          lines.clear();
          int dmX = round(mmX/7);
          for(size_t i=0; i < lines1.size(); i++) {
              Vec4i l = lines1[i];
              if(l[2] < dmX) { //хвостик листа
                  if (abs(l[0]-l[2])>abs(l[1]-l[3])/2) {
                      lines.push_back(l);
                  }
              } else { //остальная часть листа
                  if (abs(l[1]-l[3]) < 20 && abs(l[0]-l[2]) > 20) { //откидываем острый угол
                      if (abs(l[0]-l[2])/2>abs(l[1]-l[3])) {
                          lines.push_back(l);
                      }
                  }
              }
          }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question