Ask Your Question
0

finding ONLY border points of a contour

asked 2018-04-19 04:55:28 -0600

Tahir47 gravatar image

updated 2018-04-19 05:05:28 -0600

berak gravatar image

complete noob at open cv and numpy here. here is the image: here is my code:

import numpy as np
import cv2

im = cv2.imread('test.jpg')
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
imgray = cv2.medianBlur(imgray, ksize=7)

ret, thresh = cv2.threshold(imgray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_, contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

print ("number of countours detected before filtering %d -> "%len(contours))
new = np.zeros(imgray.shape)

new = cv2.drawContours(im,contours,len(contours)-1,(0,0,255),18)

cv2.namedWindow('Display',cv2.WINDOW_NORMAL)
cv2.imshow('Display',new)
cv2.waitKey()

mask = np.zeros(imgray.shape,np.uint8)
cv2.drawContours(mask,[contours[len(contours)-1]],0,255,-1)
pixelpoints = cv2.findNonZero(mask)

cv2.imwrite("masked_image.jpg",mask)

print(len(pixelpoints))
print("type of pixelpoints is %s" %type(pixelpoints))

the length of pixelpoints is nearly 2 million since it contains all the point covered by the contours. But i only require the bordering point of that contour. How do I do it? I have tried several methods from opencv documentation but always errors with tuples and sorting operations. please...help?

I only require the border points of the contour :(

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-04-19 06:56:00 -0600

berak gravatar image

you should try to find the largest contour:

(simply going for len() here, but cv2.contourArea() would also be a good option)

largest=-1
nc = 0
for i, c in enumerate(contours):
   print i, len(c)
   if len(c)>nc:
      nc = len(c)
      largest = i

print ("number of countours detected before filtering %d -> "%len(contours))
new = np.zeros(imgray.shape)
new = cv2.drawContours(img,contours,largest,(0,0,255),3)

edit flag offensive delete link more

Comments

@moHe, ofc. you're right. above only shows, howto filter for some contour's property at all.

berak gravatar imageberak ( 2018-04-19 07:29:25 -0600 )edit

the funniest thing i found out that they always put the largest contour as the last element in contours list, so i used len(contours)-1 . thanks for your suggestion. still need a way to find only contour border points, any suggestions?

Tahir47 gravatar imageTahir47 ( 2018-04-19 12:24:53 -0600 )edit

@Tahir47 - you can't rely on the order (it's arbitrary)

"still need a way to find only contour border points" -- unclear, what you mean

berak gravatar imageberak ( 2018-04-19 12:26:09 -0600 )edit

@berak, see the red outline on the pic? i only need the pixel coordinates of that red outline border

Tahir47 gravatar imageTahir47 ( 2018-04-20 03:38:16 -0600 )edit

contours are coordinates, where is the problem ?

berak gravatar imageberak ( 2018-04-20 03:39:01 -0600 )edit

@berak, would you mind to write a complete program? I cannot place your code into Tahir's code. thanks.

sliawatimena gravatar imagesliawatimena ( 2018-05-12 10:47:00 -0600 )edit

sorry, but not so. we can give you hints, but we won't write YOUR program.

@sliawatimena , it's not your question, either, you're only trying to get something for nothing (aka - no effort at all). just learn to use your own head, please.

berak gravatar imageberak ( 2018-05-12 10:48:11 -0600 )edit

I can combine your code into Tahir. how to fix fingers at his right hand? When I use a fish picture with white background, the code cannot find its contour. How to fix it? thanks.

sliawatimena gravatar imagesliawatimena ( 2018-05-12 11:00:17 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-19 04:55:28 -0600

Seen: 4,048 times

Last updated: Apr 19 '18