1.概念:從一幅圖像中利用閾值分割出我們需要的物體部分(當然這裏的物體可以是一部分或者整體)。這樣的圖像分割方法是基於圖像中物體與背景之間的灰度差異,而且此分割屬於像素級的分割。
2.代碼實現:
from skimage import data,filters
import matplotlib.pyplot as plt
import cv2
# image = data.camera()
image=cv2.imread('./test.jpg',1)
thresh = filters.threshold_otsu(image) #返回一個閾值
dst =(image <= thresh)*1.0 #根據閾值進行分割
plt.figure('thresh',figsize=(8,8))
plt.subplot(121)
plt.title('original image')
plt.imshow(image,plt.cm.gray)
plt.subplot(122)
plt.title('binary image')
plt.imshow(dst,plt.cm.gray)
plt.show()
3.算法效果:
參考博客:http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/threshold/threshold.html