理解 cv2.threshold() 函数

Understanding cv2.threshold() function

我运行这个代码:

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()

以上2张图(我展示的是原图和结果图):

图片1:

图片二:

结果 1 :

结果二:

我的问题:

结果 1 中,threshold() 符合我的预期。

但是为什么在结果 2 中有那个绿色方块?根据我对threshold()函数的理解,只需要显示绿色圆圈即可。为什么是这样 ?我对这个功能有什么不理解的?

OpenCV 将所有白色像素视为前景,将黑色像素视为背景。绿色轮廓可视化检测到的前景。

如果你想突出黑色圆圈,你需要事先反转图像。或者,您可以使用阈值类型 "THRESH_BINARY_INV" (http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=threshold#threshold).