drawContour doesn't do what I want
Question:
I am trying really hard to find contours in this image:
here is my code:
import numpy as np
import cv2
im = cv2.imread('test.jpg')
##imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.namedWindow('gray',cv2.WINDOW_NORMAL)
cv2.imshow('gray',im)
cv2.waitKey()
ret, thresh = cv2.threshold(imgray, 0, 255,
cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_, contours, _ = cv2.findContours(thresh, cv2.RETR_LIST,
cv2.CHAIN_APPROX_NONE)
print ("number of countours detected before filtering %d -> "%len(contours))
new = np.zeros(im.shape)
new = cv2.drawContours(im,contours,-1,(0,0,255))
cv2.namedWindow('Display',cv2.WINDOW_NORMAL)
cv2.imshow('Display',new)
cv2.waitKey()
Problems: 1. the produced image is always completely black and it never shows anything, just pure black image. by my description you can realize i am new to image processing. i am trying to show the body contour in red color but nothing appears.
I am using open cv 3.4.0 and python 3.6
any suggestions / solutions are welcome, thank you.
Answers:
Instead of using cv2.RETR_LIST
, you must use cv2.RETR_EXTERNAL
.
cv2.RETR_LIST
is used when you want to list all the contours in your image.
- Sometimes there might be contours within some contours. The ones on the outside are called parent, while the ones on the inside are called child. When you only want these parent contours use
cv2.RETR_EXTERNAL
.
You can find more about this on THIS LINK
I am trying really hard to find contours in this image:
here is my code:
import numpy as np
import cv2
im = cv2.imread('test.jpg')
##imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.namedWindow('gray',cv2.WINDOW_NORMAL)
cv2.imshow('gray',im)
cv2.waitKey()
ret, thresh = cv2.threshold(imgray, 0, 255,
cv2.THRESH_BINARY+cv2.THRESH_OTSU)
_, contours, _ = cv2.findContours(thresh, cv2.RETR_LIST,
cv2.CHAIN_APPROX_NONE)
print ("number of countours detected before filtering %d -> "%len(contours))
new = np.zeros(im.shape)
new = cv2.drawContours(im,contours,-1,(0,0,255))
cv2.namedWindow('Display',cv2.WINDOW_NORMAL)
cv2.imshow('Display',new)
cv2.waitKey()
Problems: 1. the produced image is always completely black and it never shows anything, just pure black image. by my description you can realize i am new to image processing. i am trying to show the body contour in red color but nothing appears.
I am using open cv 3.4.0 and python 3.6
any suggestions / solutions are welcome, thank you.
Instead of using cv2.RETR_LIST
, you must use cv2.RETR_EXTERNAL
.
cv2.RETR_LIST
is used when you want to list all the contours in your image.- Sometimes there might be contours within some contours. The ones on the outside are called parent, while the ones on the inside are called child. When you only want these parent contours use
cv2.RETR_EXTERNAL
.
You can find more about this on THIS LINK