赞
踩
scikit-image是基于scipy的一款图像处理包,它将图片作为numpy数组进行处理,正好与matlab一样。这里选择Skimage模块进行数字图像处理。
程序自带图片:
skimage程序自带了一些示例图片,如果我们不想从外部读取图片,就可以直接使用这些示例图片:
#显示上面图片可用如下代码,图片名对应的就是函数名。
from skimage import io, data
img=data.lena()
io.imshow(img)
以上内容来自于:https://blog.csdn.net/weixin_41500849/article/details/80366991
本文重点学习了1.调整图像对比度,2.旋转图片,3. 图片缩放,4.直方图均衡化等操作。
# 1.调整图片对比度 from skimage import io,data, exposure, img_as_float import matplotlib.pyplot as plt #显示功能 #filename = 'Thin layer/0.jpg' # 图片文件的路径 #image = io.imread(filename) # 使用imread读取图像,当使用imread时需要调用io image = img_as_float(data.page()) #data.moon指的是调用skimage里面的月亮照片 gam1= exposure.adjust_gamma(image,3)#调暗 gam2= exposure.adjust_gamma(image, 0.5) #调亮 plt.figure('adjust_gamma',figsize=(8,8)) #设置图片大小 plt.subplot(131) #131代表显示的位置(130-134) plt.title('origin image') #显示标题 plt.imshow(image,plt.cm.gray) # 使用灰度方式显示图片 plt.axis('off') #131列结束 plt.subplot(132) #同131列一致 plt.title('gamma=3') plt.imshow(gam1,plt.cm.gray) plt.axis('off') #132列结束 plt.subplot(133) #同131列一致 plt.title('gamma=0.5') plt.imshow(gam2,plt.cm.gray) plt.axis('off') #133列结束 plt.show() #显示总图片 io.imshow(gam1) #io读入图片 io.imsave('0.jpg',gam1) #保存图片,moon.jpg指的是保存路径和名称,gam1指的是需要保存的数组变量
# 2.旋转图片 from skimage import transform,data import matplotlib.pyplot as plt img = data.camera() img1=transform.rotate(img, 60) #旋转90度,不改变大小 img2=transform.rotate(img, 30,resize=True) #旋转30度,同时改变大小 plt.subplot(131) plt.title('rotate=0.6') plt.imshow(img1,plt.cm.gray) plt.axis('off') plt.subplot(132) plt.title('rotate=0.3') plt.imshow(img2,plt.cm.gray) plt.axis('off') plt.show()
#3.图片缩放 from skimage import transform,data import matplotlib.pyplot as plt img = data.camera() plt.subplot(131) plt.title('image') plt.imshow(img,plt.cm.gray) plt.axis('off') print(img.shape) #图片原始大小 img1=transform.rescale(img, [0.5,0.25]) #缩小为原来图片行数一半,列数四分之一 plt.subplot(132) plt.title('image1') plt.imshow(img1,plt.cm.gray) plt.axis('off') print(transform.rescale(img, [0.5,0.25]).shape) img2=transform.resize(img, (80, 60)) #缩小为原来图片行数一半,列数四分之一 plt.subplot(133) plt.title('image2') plt.imshow(img2,plt.cm.gray) plt.axis('off') plt.show()
如果一副图像的像素占有很多的灰度级而且分布均匀,那么这样的图像往往有高对比度和多变的灰度色调。直方图均衡化就是一种能仅靠输入图像直方图信息自动达到这种效果的变换函数。它的基本思想是对图像中像素个数多的灰度级进行展宽,而对图像中像素个数少的灰度进行压缩,从而扩展取值的动态范围,提高了对比度和灰度色调的变化,使图像更加清晰。
#4.直方图均衡化 from skimage import io,data,exposure import matplotlib.pyplot as plt #img=data.moon() #使用数据库里面自带的图片 filename = 'Thin layer/0.jpg' # 图片文件的路径 img = io.imread(filename) # 使用imread读取图像,当使用imread时需要调用io plt.figure("Thin layer",figsize=(8,8)) #调整图片尺寸 arr=img.flatten() ##默认按行的方向降维 plt.subplot(221) plt.imshow(img,plt.cm.gray) #原始图像 plt.subplot(222) plt.hist(arr, bins=100,edgecolor='None',facecolor='red') #原始图像直方图 # 当bin为整数时,则等于柱子的个数,有bin + 1个边(256+1)。 # edgecolor:是柱子边界的颜色。 # facecolor: 是柱子的颜色。 img1=exposure.equalize_hist(img) #进行直方图均衡化 arr1=img1.flatten() #返回数组折叠成一维的副本 plt.subplot(223) plt.imshow(img1,plt.cm.gray) #均衡化图像 plt.subplot(224) plt.hist(arr1, bins=100,edgecolor='None',facecolor='blue') #均衡化直方图 plt.show() io.imshow(img1) #io读入图片 io.imsave('1.jpg',img1) #保存图片,moon.jpg指的是保存路径和名称,gam1指的是需要保存的数组变量
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。