Ask Your Question

Why does BinaryDescriptor::compute give segfault when modifying the input KeyLine vector

asked 2015-06-02 04:41:41 -0500

takahashi gravatar image

I am using OpenCV 3.0.0-rc1. I want to detect lines, filter according to line length and compute the descriptors. However, it always gives a segfault and I don't get why it does so. When I don't use the filtered keylines vector (created by the function keylineLengthFilter), but the original keylines vector, it works perfectly. Or when I set min_length = 0.0, it also works. When I set it to, e.g., min_length = 10.0, it still detects a non-zero number of keylines, but segfault occurs. The documentation where I got example code from is here. Any help is greatly appreciated!

My code is:

The length filter:

vector<KeyLine> keylineLengthFilter(vector<KeyLine>& keylines, const float &length)
      /* keyline filter that returns a binary vector of keyline entries with length larger than input value */
      vector<KeyLine> keylines_filtered;
      for (int i = 0; i < (int)keylines.size(); ++i)
        if (keylines[i].lineLength >= length)
      return keylines_filtered;

The Code: (image, min_length and mask are not included here, but that part works fine on my original code)

vector<KeyLine> keylines_filtered;
      /* create a pointer to an LSDDetector object */
      Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
      /* create a pointer to a BinaryDescriptor object with default parameters */
      Ptr<LSDDetector> lsd = LSDDetector::createLSDDetector();
      /* compute lines */
      lsd->detect(image, keylines, 2, 1, mask);
      /* filter keylines */
      keylines_filtered = keylineLengthFilter(keylines, min_length);

      // THE SEGFAULT HAPPENS HERE, but only when I use keylines_filtered as argument:
      /* compute descriptors */
      bd->compute(image, keylines_filtered, descriptors); // this gives segfault 
      bd->compute(image, keylines, descriptors); // this works
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-02-22 09:43:30 -0500

I think the problem is the following: You are reorganising the KeyLines, but you are only reorganising the vector. The Keyline itself is not reorganized if you filter. For example the classid member are still the same. For example if you have 2 keylines one keyline with classid 1 and another keyline with classid 2 and you filter out the first one, after reorganising the vector your vector contains a Keyline with classid 2. Now if you call compute from the binary descriptor it relies on that classids. For example in line 570 - 580 of binary_descriptor.cpp it calculates numLines depending the max classid, which would be now 2, but your vector has only size 1 after filtering. That causes the segfault. Dont know if its just enough to reorder the classid. Maybe you cann give it a try.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-06-02 04:41:41 -0500

Seen: 138 times

Last updated: Jun 02 '15