代码之家  ›  专栏  ›  技术社区  ›  user4584333

了解cv2.threshold()函数

  •  1
  • user4584333  · 技术社区  · 10 年前

    我运行以下代码:

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

    在这两张图片上(我显示原始图片和结果图片):

    图片1:

    enter image description here

    图片2:

    enter image description here

    结果1:

    enter image description here

    结果2:

    enter image description here

    我的问题:

    在里面 结果1 , threshold() 做了我所期望的。

    但为什么 结果2 那个绿色的广场在哪里?根据我对 阈值() 函数时,只能显示绿色圆圈。这是为什么?我对这个功能有什么不理解?

    1 回复  |  直到 10 年前
        1
  •  3
  •   Falko    10 年前

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

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