0

I have an image, were few crossing lines produce four polygons. I want to measure area of each polygon. In my script I am trying to create contours and, probably, uses the wrong methods. I appreciate any advice ... source image

import cv2
import numpy as np
import time


# define range of  color in HSV
lower_red = np.array([150,135,160])
upper_red = np.array([180,250,200])
white_line  = np.array([255,255,255])
red_line   = np.array([0,0,255])



im = cv2.imread('laser.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
cv2.imshow('imgray', im)
ret,thresh = cv2.threshold(imgray,127,255,0)
cv2.imshow('thresh', thresh)

# Remove some small noise if any.
dilate = cv2.dilate(thresh,None)
erode = cv2.erode(dilate,None)
cv2.imshow('erode', erode)


_, contours, hierarchy = cv2.findContours(erode,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
##for contour in contours:
##    if (cv2.contourArea(contour) > 50):  
##        print contours
im2 =  np.zeros((500,500,3), np.uint8)
cv2.drawContours(im2,contours,0,(125,125,0),1)
cv2.imshow('contours',im2)


k = cv2.waitKey(0)
if k == 27:         # wait for ESC key to exit
    cv2.destroyAllWindows()
elif k == ord('s'): # wait for 's' key to save and exit
    cv2.imwrite('messigray.png',im2)
    cv2.destroyAllWindows()

1
  • Did my answer solve your problem? Commented Jan 30, 2017 at 20:00

1 Answer 1

1

For extracting the contours using your current code, you could do the following:

_,contours,heirarchy=cv2.findContours(erode,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
im2 =  np.zeros((480,640,3), np.uint8)
cv2.drawContours(im2,contours,-1,(125,125,0),1)

(using RETR_EXTERNAL will provide you with the boundaries of the outside part only, if there are any individual parts made inside those will be excluded so just use RETR_TREE which will provide proper heirarchy so you know you are looking at the inside boundary). you have created empty array im2 of wrong size 500,500 since doing that will exclude some part of the image(will give error when drawing contours outside this). These are minor mistakes.

And now for the main part which was wrong is cv2.drawContours(im2,contours,0,(125,125,0),1) here you are giving array of arrays of the contours in the image and telling it to draw only the first contour(by the 0th element); So what you should do is either draw/process contours one by one using the contour number(1 or 2 or 3 or 4 or... instead of the 0) or first select the contour then draw it like this:

cnt=contour[4]
cv2.drawContours(im2,[cnt],0,(125,125,0),1)

or just draw all of the contours by supplying (-1) instead of (0) like i have shown earlier.

If you want to select some contours and draw them you could just do something like this:

cnt=[contour[2],contour[5],contour[9]]
cv2.drawContours(im2,cnt,-1,(125,125,0),1)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much, Iamnotgoogle! Your defenetally resolved my major issue and I can go further now. My final goal is to build contours around each of four rectangles in the center of the image and calculate the area. After your input I figure out, that I have to invert the picture (build the negative) and apply the "findContour" function.
@guslik the contours with basic findcontours will still be unclear with respect to the exact lines you are searching for, you could try applying filters to detect linear edges before finding contours or check for particular shapes you want. Either that or train opencv with an image containing generated polygons which you would like to detect. (You will need to do something for the polygon with detached line edges). Please let me know if i can help in any way
thank you, i will appreciate any help... I thought, that finding contours polygons on the image provides me with accurate value of their area. i dont know how accurate is this approach, but i wanted to try. the area(s) measurement is a main goal, and if other approach is applicable and simpler or better i am happy to learn ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.