Reputation:
I run this code:
import cv2
import numpy as np
from matplotlib import pyplot as plt
im=cv2.imread('1.jpg')
#mask=np.zeros(img.shape[:2],np.uint8)
imgray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh=cv2.threshold(imgray,200,200,200)
countours,hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im,countours,-1,(0,255,0),3)
cv2.imshow("begueradj",im)
cv2.waitKey(0)
cv2.destroyAllWindows()
Over these 2 pictures (I display original and result pictures):
Picture 1:
Picture 2:
Result 1 :
Result 2:
My problem:
In Result 1, threshold()
did what I expected.
But why in Result 2 there is that green square ? According to my understanding of the threshold()
function, only the green circle must be showed. Why is this ? What am I not understanding with this function ?
Upvotes: 2
Views: 6305
Reputation: 17877
OpenCV treats all white pixels as foreground and the black ones as background. The green contours visualize the detected foreground.
If you wish to highlight the black circle, you'd need to invert the image beforehand. Alternatively you can use the threshold type "THRESH_BINARY_INV" (http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=threshold#threshold).
Upvotes: 4