1

I am trying to use contours without using the standard openCV contour functions.

At the moment I am trying to take out the first "line" and the last "line" in each contour and I have got a bit stuck on how to read the numpy array correctly. After a lot of messing about this is the current state of the code which doesn't work. Can anyone provide an example of how I should be doing this?

contours,hierarchy = cv2.findContours(mask, 1, 2)

for cnt in contours:
    #draw first line
    img = cv2.line(img,(cnt[0][0],cnt[0][1]),(cnt[1][0], cnt[1][1]),(255,0,0),2)
    #draw last line
    img = cv2.line(img,(cnt[cnt.size-1][0],cnt[cnt.size-1][1]),(cnt[cnt.size-1][0], cnt[cnt.size-1][1]),(255,0,0),2)
chrispepper1989
  • 2,100
  • 2
  • 23
  • 48
  • In `#draw last line`, your endpoints are the same. Was that intentional? How does the resulting image look different from what you expect? – beaker Jan 13 '15 at 20:23
  • One of the difficulty you face may come from the fact that there is no "first" and "last" point in a contour: `cv2.findContours` **always** returns "closed" contours, even if they seem open when being drawn on an image. Sorry to advertise for my own answer, but maybe [this one](http://stackoverflow.com/a/27469439/777285) will be of interest. – Arnaud P Jan 14 '15 at 09:33
  • @beaker the code I have there does not compile/run. Complains of accessing outside of an array – chrispepper1989 Jan 14 '15 at 13:05
  • @ArnaudP Thank you, looking at your answer shows that I have a lot to learn about Numpy arrays, perhaps I should start there – chrispepper1989 Jan 14 '15 at 13:06
  • Indeed there are a couple of useful methods in numpy to make things faster. To expand on my previous comment: the points visually appearing as "end points" in a contour are not special for `opencv`, in that they can be at any location in the contour array. Hence the additional computations that have to be performed to find "u-turns" in each array. – Arnaud P Jan 14 '15 at 14:09
  • my goal is to extract any 2 lines, get the perpendicular bisect, and highlight where they intersect. As I am trying to find the center of a circle. – chrispepper1989 Jan 14 '15 at 14:11

0 Answers0