changelog
import numpy as np
from skimage import exposure,data
image =data.camera()*1.0
hist1=np.histogram(image, bins=2) #用 numpy 包计算直方图
hist2=exposure.histogram(image, nbins=2) #用 skimage 计算直方图
print(hist1)
print(hist2)
(array([ 93585, 168559], dtype=int64), array([ 0. , 127.5, 255. ])) (array([ 93585, 168559], dtype=int64), array([ 63.75, 191.25]))
绘图都可以调用 matplotlib.pyplot 库来进行,其中的 hist 函数可以直接绘制直方图。
from skimage import data
import matplotlib.pyplot as plt
img=data.camera()
plt.figure("hist")
arr=img.flatten()
n, bins, patches = plt.hist(arr, bins=256, density=1,edgecolor='None',facecolor='red')
plt.show()
hist 函数调用方式:
n, bins, patches = plt.hist(arr, bins=10, normed=0, facecolor='black', edgecolor='black',alpha=1,histtyp
e='bar')
# hist 的参数非常多,但常用的就这六个,只有第一个是必须的,后面四个可选
# arr: 需要计算直方图的一维数组
# bins: 直方图的柱数,可选项,默认为 10
# normed: 是否将得到的直方图向量归一化。默认为 0 (python 新版使用 density)
# facecolor: 直方图颜色
# edgecolor: 直方图边框颜色
# alpha: 透明度
# histtype: 直方图类型,‘bar’, ‘barstacked’, ‘step’, ‘stepfilled’
# 返回值 :
# n: 直方图向量,是否归一化由参数 normed 设定
# bins: 返回各个 bin 的区间范围
# patches: 返回每个 bin 里面包含的数据,是一个 list
flatten()函数是 numpy 包里面的,用于将二维数组序列化成一维数组。
是按行序列,如
mat=[[1 2 3
4 5 6]]
经过 mat.flatten()后,就变成了
mat=[1 2 3 4 5 6]
一般来说直方图都是征对灰度图的,如果要画 rgb 图像的三通道直方图,实际上就是三 个直方图的叠加。
from skimage import data
import matplotlib.pyplot as plt
img=data.cat()
ar=img[:,:,0].flatten()
plt.hist(ar, bins=256, density=1,facecolor='r',edgecolor='r')
ag=img[:,:,1].flatten()
plt.hist(ag, bins=256, density=1, facecolor='g',edgecolor='g')
ab=img[:,:,2].flatten()
plt.hist(ab, bins=256, density=1, facecolor='b',edgecolor='b')
plt.show()
如果一副图像的像素占有很多的灰度级而且分布均匀,那么这样的图像往往有高对比度和多变的灰度色调。直方图均衡化就是一种能仅靠输入图像直方图信息自动达到这种效果的变换函数。它的基本思想是对图像中像素个数多的灰度级进行展宽,而对图像中像素个数少的灰度进行压缩,从而扩展取值的动态范围,提高了对比度和灰度色调的变化,使图像更加清晰。
from skimage import data,exposure
import matplotlib.pyplot as plt
img=data.moon()
plt.figure("hist",figsize=(8,8))
arr=img.flatten()
plt.subplot(221)
plt.imshow(img,plt.cm.gray) #原始图像
plt.subplot(222)
plt.hist(arr, bins=256, density=1,edgecolor='None',facecolor='red') #原始图像直方图
img1=exposure.equalize_hist(img)
arr1=img1.flatten()
plt.subplot(223)
plt.imshow(img1,plt.cm.gray) #均衡化图像
plt.subplot(224)
plt.hist(arr1, bins=256, density=1,edgecolor='None',facecolor='red') #均衡化直方图
plt.show()
filters.sobel(image, mask=None)
filters.roberts(img)
filters.scharr(img)
filters.prewitt(img)
feature.canny(image,sigma=1.0)可以修改 sigma 的值来调整效果
from skimage import data,filters
import matplotlib.pyplot as plt
img = data.camera()
edges = filters.sobel(img)
plt.imshow(edges,plt.cm.gray)
plt.show()
from skimage import data,filters,feature
import matplotlib.pyplot as plt
img = data.camera()
edges1 = feature.canny(img) #sigma=1
edges2 = feature.canny(img,sigma=3) #sigma=3
plt.figure('canny',figsize=(8,8))
plt.subplot(121)
plt.imshow(edges1,plt.cm.gray)
plt.subplot(122)
plt.imshow(edges2,plt.cm.gray)
plt.show()
from skimage import data,filters
import matplotlib.pyplot as plt
img = data.camera()
filt_real, filt_imag = filters.gabor(img,frequency=0.6)
plt.figure('gabor',figsize=(8,8))
plt.subplot(221)
plt.title('filt_real f0.6')
plt.imshow(filt_real,plt.cm.gray)
plt.subplot(222)
plt.title('filt-imag f0.6')
plt.imshow(filt_imag,plt.cm.gray)
filt_real, filt_imag = filters.gabor(img,frequency=0.1)
plt.subplot(223)
plt.title('filt_real f0.1')
plt.imshow(filt_real,plt.cm.gray)
plt.subplot(224)
plt.title('filt-imag f0.1')
plt.imshow(filt_imag,plt.cm.gray)
plt.show()
多维的滤波器,是一种平滑滤波,可以消除高斯噪声。
调用函数为:skimage.filters.gaussian_filter(image, sigma)
通过调节 sigma 的值来调整滤波效果,sigma 越大,过滤后的图像越模糊。
from skimage import data,filters
import matplotlib.pyplot as plt
img = data.astronaut()
edges1 = filters.gaussian(img,sigma=0.4) #sigma=0.4
edges2 = filters.gaussian(img,sigma=5) #sigma=5
plt.figure('gaussian',figsize=(8,8))
plt.subplot(121)
plt.imshow(edges1,plt.cm.gray)
plt.subplot(122)
plt.imshow(edges2,plt.cm.gray)
plt.show()
d:\Applications\miniconda\envs\dip\lib\site-packages\skimage\_shared\utils.py:348: RuntimeWarning: Images with dimensions (M, N, 3) are interpreted as 2D+RGB by default. Use `multichannel=False` to interpret as 3D image with last dimension of length 3. return func(*args, **kwargs)
from skimage import data,filters
import matplotlib.pyplot as plt
from skimage.morphology import disk
img = data.camera()
edges1 = filters.median(img,disk(5))
edges2= filters.median(img,disk(9))
plt.figure('median',figsize=(8,8))
plt.subplot(121)
plt.imshow(edges1,plt.cm.gray)
plt.subplot(122)
plt.imshow(edges2,plt.cm.gray)
plt.show()
上边所举的例子都是进行全部边缘检测,有些时候我们只需要检测水平边缘,或垂直边 缘,就可用下面的方法。
水平边缘检测:sobel_h, prewitt_h, scharr_h
垂直边缘检测: sobel_v, prewitt_v, scharr_v
from skimage import data,filters
import matplotlib.pyplot as plt
img = data.camera()
edges1 = filters.sobel_h(img)
edges2 = filters.sobel_v(img)
plt.figure('sobel_v_h',figsize=(8,8))
plt.subplot(121)
plt.title('Horizontal')
plt.imshow(edges1,plt.cm.gray)
plt.subplot(122)
plt.title('Vertical')
plt.imshow(edges2,plt.cm.gray)
plt.show()
可使用 Roberts 的十字交叉核来进行过滤,以达到检测交叉边缘的目的。这些交叉边缘 实际上是梯度在某个方向上的一个分量。
其中一个核:
0 1
-1 0
对应的函数:
roberts_neg_diag(image)
from skimage import data,filters
import matplotlib.pyplot as plt
img =data.camera()
dst =filters.roberts_neg_diag(img)
plt.figure('filters',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.subplot(122)
plt.title('filted image')
plt.imshow(dst,plt.cm.gray)
plt.show()
另外一个核:
1 0
0 -1
对应函数为:roberts_pos_diag(image)
from skimage import data,filters
import matplotlib.pyplot as plt
img =data.camera()
dst =filters.roberts_pos_diag(img)
plt.figure('filters',figsize=(8,8))
plt.subplot(121)
plt.title('origin image')
plt.imshow(img,plt.cm.gray)
plt.subplot(122)
plt.title('filted image')
plt.imshow(dst,plt.cm.gray)
plt.show()