图像阈值处理cv2.threshold()函数(python)

内容

cv2.threshold()函数:在opencv中比较常用,但一开始不是很理解是什么意思。
下面是官方文档中给的解释

Python: cv2.threshold(src, thresh, maxval, type[, dst]) → retval, dst

在其中:

  • src:表示的是图片源
  • thresh:表示的是阈值(起始值)
  • maxval:表示的是最大值
  • type:表示的是这里划分的时候使用的是什么类型的算法,常用值为0(cv2.THRESH_BINARY)

这里写图片描述

  • (x,y)表示的是图像中的坐标
  • INV 表示的是取反
  • 一般的(BINARY)效果是:
    将一个灰色的图片,变成要么是白色要么就是黑色。(大于规定thresh值就是设置的最大值(常为255,也就是白色))
import cv2
import numpy as np

img = np.zeros((200, 200), dtype=np.uint8)
img[50:150, 50:150] = 255  # create a image with black color in the middle and its background is white.
cv2.imshow("Image-Old", img)
ret, thresh = cv2.threshold(img, 127, 255, 0)
cv2.imshow("Image-New", thresh)
cv2.waitKey()
cv2.destroyAllWindows()

注意: 这里的两张图其实是一样的。

猜你喜欢

转载自blog.csdn.net/a19990412/article/details/81172426