OpenCV:具有意外结果的阈值操作
OpenCV: threshold operation with unexpected result
我用OpenCV读了一张白图:
我在上面应用了这个阈值:
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)
所以根据 cv2.THRESH_BINRARY
阈值类型,我在逻辑上期望全白图片。为什么 ?因为我设置了 thresh=0
和 maxValue=255
,所以我期望的结果是根据官方文档在这种情况下所说的逻辑:
但结果我得到一张全黑的图片(dst
的像素设置为 0
,即使它们大于 src
图片中的 thresh
)
这是为什么?我误会了什么?
findContours function modifies the source image. So if you want source
image even after finding contours, already store it to some other
variables.
我用OpenCV读了一张白图:
我在上面应用了这个阈值:
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)
所以根据 cv2.THRESH_BINRARY
阈值类型,我在逻辑上期望全白图片。为什么 ?因为我设置了 thresh=0
和 maxValue=255
,所以我期望的结果是根据官方文档在这种情况下所说的逻辑:
但结果我得到一张全黑的图片(dst
的像素设置为 0
,即使它们大于 src
图片中的 thresh
)
这是为什么?我误会了什么?
findContours function modifies the source image. So if you want source image even after finding contours, already store it to some other variables.