Skip to content
Advertisement

Drawing a contour in a specific region of an image and finding the outermost contour in opencv-python

My code selects a frame from a video which is than subtracted with a background frame selected from the same video. It is then converted to grayscale, blurred, and then an image threshold is applied. Then a contour is drawn which outputs this image. However, I would only like to have the outermost contour and also not have any contours drawn above y=500. How can I implement this?

Contour code:

contours, hierarchy = cv2.findContours(tframe,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
image = cv2.drawContours(sampleframe, contours, -1, (0, 255, 0), 2) 

I have tried using cv2.dilate which works given enough iteration to remove internal contours but the iteration causes the contour to be overestimated which is not desired.

Advertisement

Answer

If you filter out the contours by their area size, you can keep the biggest contour. check this link please: https://docs.opencv.org/3.4/dd/d49/tutorial_py_contour_features.html

And for the second problem you can cut your frame on y axis to be lower than 500.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement