Search code examples
pythonimagepython-2.7opencvthreshold

OpenCV: threshold operation with unexpected result


I read a white picture using OpenCV:

enter image description here

I apply this threshold over it:

import cv2    
# Read image
src = cv2.imread("threshold.png", cv2.CV_LOAD_IMAGE_GRAYSCALE)    
# Set threshold and maxValue
thresh = 0
maxValue = 255    
# Basic threshold example
th, dst = cv2.threshold(src, thresh, maxValue, cv2.THRESH_BINARY);
# Find Contours  
contours, hierarchy=cv2.findContours(dst,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# Draw Contour
cv2.drawContours(dst,countours,-1,(255,255,255),3)
cv2.imshow("Contour",dst)

So according the cv2.THRESH_BINRARY thresholding type, I logically expect a totally white picture. Why ? because I set thresh=0 and maxValue=255, so the result I expect is logic according to the official documentation that says in this case:

enter image description here

But I get a totally black picture as a result (pixels of dst set to 0 even if they are greater than thresh in src picture)

Why this ? What am I misunderstanding ?


Solution

  • From the documentation

    findContours function modifies the source image. So if you want source image even after finding contours, already store it to some other variables.