skimage
start
from skimage import io,data
chelsea = data.chelsea()
- 从skimage的data图片库数据库中导入一张图片
io.imshow(chelsea)
from skimage import color
gray_cat = color.rgb2gray(chelsea)
io.imshow(gray_cat)
chelsea.shape, gray_cat.shape
- ((300, 451, 3), (300, 451))
- 由三通道变成了单通道
hsv_cat = color.rgb2hsv(chelsea)
io.imshow(hsv_cat)
chelsea[0,0], hsv_cat[0,0]
- (array([143, 120, 104], dtype=uint8),
array([0.06837607, 0.27272727, 0.56078431])) - 直接对三通道分别做处理
- 取值范围由0~256 jpg 变成了0~1 png
from skimage import filters
二值化操作(灰度只有1,0)
t = filters.threshold_otsu(gray_cat)
for i in range(300):
for j in range(451):
if gray_cat[i, j] <= t:
gray_cat[i, j] = 0
else:
gray_cat[i, j] = 1
io.imshow(gray_cat)
- threshold_otsu(gray_cat)
Return threshold value based on Otsu’s method.
根据Otsu方法返回一个值
- 图像需是单通道
验证码
cap = io.imread('capcha.jpg')
io.imshow(cap)
cap = color.rgb2gray(cap) # 单通道
w,h = cap.shape
for i in range(w):
for j in range(h):
if cap[i, j] <= 0.1: # 找到北京噪声
cap[i, j] = 1 # 将噪声设成白色
io.imshow(cap)
t = filters.threshold_otsu(cap)
display(t) # 0.464490456495098
for i in range(w):
for j in range(h):
if cap[i, j] <= t:
cap[i, j] = 0
else:
cap[i, j] = 1
io.imshow(cap)
- t值太小,未完全区分
cap = io.imread('capcha.jpg')
cap = color.rgb2gray(cap)
w,h = cap.shape
for i in range(w):
for j in range(h):
if cap[i, j] <= 0.1:
cap[i, j] = 1
t = filters.threshold_otsu(cap)
t = (t+1)/2
t = (t+1)/2
display(t) # 0.8661226141237746
for i in range(w):
for j in range(h):
if cap[i, j] <= t:
cap[i, j] = 0
else:
cap[i, j] = 1
io.imshow(cap)
- 手动增大一下