Python opencv drawContours shows nothing

I followed the manual on this page, but nothing seems to happen when the line cv2.drawContours(im,contours,-1,(0,255,0),3) . I expected to see star.jpg with a green outline, as shown in the manual. Here is my code:

 import numpy as np import cv2 im = cv2.imread('C:\Temp\ip\star.jpg') print im.shape #check if the image is loaded correctly imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(imgray,127,255,0) contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(im,contours,-1,(0,255,0),3) pass 

There are no error messages. star.jpg is a star from the above web page. I am using opencv version 2.4.8 and Python 2.7.

Should drawContours display an image on my screen? If so, what did I do wrong? If not, how can I show the image?

thanks

Edit:

Adding the following lines will show the image:

 cv2.imshow("window title", im) cv2.waitKey() 

waitKey () is necessary, otherwise the window will simply show a gray background. According to this post , this is because waitKey () tells it to start processing the WM_PAINT event.

+13
python opencv
source share
4 answers

I had the same problem. I believe the problem is that the main image is 1-channel, not 3-channel. Therefore, you need to set the color so that it is non-zero in the first element (for example, (255,0,0)).

+11
source share

I had the same problem too. The fact is that this is visible, but too dark for our eyes. Solution: change the color from (0.255.0) (for some strange reason, I also gave the exact same color!) To (128.255.0) (or some brighter color)

+7
source share

You need to do something:

 cv2.drawContours(im,contours,-1,(255,255,0),3) cv2.imshow("Keypoints", im) cv2.waitKey(0) 
+7
source share

I assume your original image is in a gray bit plane. Since your bit plane is gray, not BGR, and therefore the outline is not displayed. Because it is slightly black and gray that you cannot distinguish. Here's a simple solution to [Convert Bit Plane]:

 im=cv2.cvtColor(im,cv2.COLOR_GRAY2BGR) cv2.drawContours(im,contours,-1,(0,255,0),3) 
0
source share

All Articles