1

I am having a image here. The region within the yellow lines is my region of interest, as shown in this image here, which is also one of my objective. Here's my planning / steps:

  1. Denoise, color filtering, masking and Canny edging (DONE)
  2. Coordinates of the edges (DONE)
  3. Select coordinates of certain vertices, for example
  4. Draw polygon with those vertices' coordinates

Here's the code:

import cv2
import numpy as np
from matplotlib import pyplot as plt

frame = cv2.imread('realtest.jpg')

denoisedFrame = cv2.fastNlMeansDenoisingColored(frame, None, 10, 10, 7, 21)

HSVframe = cv2.cvtColor(denoisedFrame, cv2.COLOR_BGR2HSV) 

lower_yellowColor = np.array([15,105,105]) 
upper_yellowColor = np.array([25,255,255])

whiteMask = cv2.inRange(HSVframe, lower_yellowColor, upper_yellowColor) 
maskedFrame = cv2.bitwise_and(denoisedFrame, denoisedFrame, mask=whiteMask)

grayFrame = cv2.cvtColor(maskedFrame, cv2.COLOR_BGR2GRAY)
gaussBlurFrame = cv2.GaussianBlur(grayFrame, (5,5), 0)
edgedFrame = cv2.Canny(grayFrame, 100, 200)

#Coordinates of each white pixels that make up the edges
ans = []

for y in range(0, edgedFrame.shape[0]):
    for x in range(0, edgedFrame.shape[1]):
        if edgedFrame[y, x] != 0:
            ans = ans + [[x, y]]

ans = np.array(ans)

#print(ans.shape)
#print(ans[0:100, :])


cv2.imshow("edged", edgedFrame)

cv2.waitKey(0)
cv2.destroyAllWindows()

As you can see, I have successfully done step number (2) in getting the coordinates of each white pixels that make the edges. Whereas for the next step, step number (3), I am stuck. I have tried the coding here, but getting error that says 'ValueError: too many values to unpack (expected 2)'.

Please help teaching me in finding good vertices for constructing a polygon that is as close to the yellow lines as possible.

2
  • Mask yellow area, find contours of outer bound of yellow area with enough points. if you have too many values to pack, you can say for example instead of a = cv2.funcX() this: a,_ = cv2.funcX(). This is typical problem, if you use BW/GREYSCALE/COLOR images together (their numpy-arrays have different dimensions). Commented May 18, 2018 at 8:45
  • But I can't just throw away the returns by using _, right? Because they may be important vertices? Commented May 18, 2018 at 8:57

1 Answer 1

1

I have split the answer into two parts

Part 1: Finding the good vertices to construct a polygon

The required vertices around an image containing edges can be done using OpenCV's inbuilt cv2.findContours() function. It returns the image with contours, vertices of the contours and the hierarchy of the contours.

One can find vertices of contours in two ways:

  • cv2.CHAIN_APPROX_NONE plots ALL the coordinates(boundary points) on each contour

  • cv2.CHAIN_APPROX_SIMPLE plots ONLY the most necessary coordinates on each contour. It doesn't store all the points. Only the most required coordinates that best represent the contours are stored.

In your case option 2 can be opted. After finding the edges you can do the following:

image, contours, hier = cv2.findContours(edgedFrame, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

contours contains the vertices of every contour in the image edgedFrame

Part 2: Constructing the polygon

Opencv has an in-built function for this as well cv2.convexHull() After finding those points you can draw them using cv2.drawContours().

for cnt in contours:
    hull = cv2.convexHull(cnt)
    cv2.drawContours(frame, [hull], -1, (0, 255, 0), 2)

cv2.imshow("Polygon", frame)

enter image description here

You can obtain a better approximation of the desired edge by doing some more pre-processing while creating the mask

Sign up to request clarification or add additional context in comments.

2 Comments

thanks so much for the crystal clear explanation. Based on the photo you posted, is it possible to remove the smaller rectangle (the inner rectangle)? What method / approach I need to use?
@stacknoflow Yes! After finding the edges in the image edgedFrame , perform some morphological operation (erosion/dilation) such that when you find the contours only the external frame is detected.

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.