在skimage或纯python中的图像阈值

时间:2016-11-21 22:53:26

标签: python image-processing scipy computer-vision scikit-image

在一个代码段中,open cv使用

import cv2
img = cv2.threshold(img, 0.5, 1., cv2.THRESH_BINARY)[1].astype(np.uint8)

在skimage或纯Python中,是否有任何有效的方法或现有功能可以实现与上述开放cv(cv2)用法相同的目标?

2 个答案:

答案 0 :(得分:0)

skimage中,您应该使用skimage.filters来使用所有可用的阈值相关功能。

from skimage import data
from skimage.filters import try_all_threshold

img = data.page()

fig, ax = try_all_threshold(img, figsize=(10, 8), verbose=False)
plt.show()

访问THIS PAGE

关键是,在OpenCV中只涉及一个功能。在skimage中,您必须明确导入filter模块 才能执行阈值操作。

*您还可以使用PIL(Python Imaging Library)执行阈值操作。

答案 1 :(得分:0)

这是仅使用Python阈值的一种方法(除了读取,写入和查看结果外)

输入:

enter image description here

import cv2

# read image as grayscale
img = cv2.imread('lena.jpg',0)

# threshold
img_thresh = img
img_thresh[ img < 128 ] = 0
img_thresh[ img_thresh >= 128 ] = 255

# view result
cv2.imshow("threshold", img_thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save result
cv2.imwrite("lena_threshold.jpg", img_thresh)


enter image description here