1.把”jpg”格式转换成”png”格式,并修改图片大小
1.1导入此环节需要的库
from skimage import io,transform,color
import numpy as np
1.2定义函数
def convert_gray(f): #定义函数名为convert_gray
rgb = io.imread(f)
gray = color.rgb2gray(rgb) #将RGB图像转为灰度图像
1.3将灰度图像大小调整为256x256,返回图像
dst = transform.resize(gray,(256,256))
return dst
1.4加载图像路径
img = r"C:\Users\86130\Desktop\210"
str = img +'/*.jpg'
coll = io.ImageCollection(str,load_func = convert_gray)
1.5遍历图像集合的长度,保存图像为.png格式,文件名以0开头并逐渐增加
for i in range(len(coll)):
io.imsave(r"C:\Users\86130\Desktop\210\0"+np.str(i)+'.png',coll[i])
运行结果:
2.自定义缩放图像
2.1自定义缩放图像的方法:transform.resize( )
参数1:图像,参数2:图像自定义缩放的比例,shape[0]:宽×1/2,shape[1]:高×1/2
b = transform.resize(img,(img.shape[0]*0.5,img.shape[1]*0.5))
c= transform.resize(img,(img.shape[0]*1/3,img.shape[1]*1/3)) #同理b
d = transform.resize(img,(img.shape[0]*1/4,img.shape[1]*1/4))
plt.axis(‘on’) #等于on或者不写:显示坐标
运行结果:
2.2输出图像大小
print(img.shape)
运行结果:
2.3按比例缩放图像: transform.rescale
a = transform.rescale(img,0.1) #按比例缩放0.1倍
print(a.shape)
io.imshow(a)
运行结果:
2.4旋转图像:transform.rotate()
2.4.1导包
import matplotlib.pyplot as plt
2.4.2输出图像大小
print(img.shape)
2.4.3将图像旋转60°、30°,逆时针方向,resize = True:自定义缩小图像
img1 = transform.rotate(img,60)
print(img1.shape)
img2 = transform.rotate(img,30,resize = True)
print(img2.shape)
运行结果:
3.gamma调整图像
要点:gamma>1,调暗!!
gamma<1,调亮!!
gam1 = exposure.adjust_gamma(img,5) #调暗
gam2 = exposure.adjust_gamma(img,0.3) #调亮
运行结果:
4.log调整图像
log = exposure.adjust_log(img)
运行结果:
可以看到,使用log调整后的图像比原图像更亮一些
1.将调整后的图像保存,以“wzq.jpg”命名
io.imsave(r"C:\Users\86130\Desktop\210\wzq.jpg",log)
5.分析图像的对比度
5.1判断图像对比度
5.1.1导入exposure模块,读取图片
from skimage import exposure
img = io.imread(r"C:\Users\86130\Desktop\210\wzq.jpg")
5.1.2is_low_contrast函数判断图像的对比度是否低,返回布尔型的值,True代表低,False代表正常/高
result = exposure.is_low_contrast(img)
print(result)
运行结果:
5.2创建数组,对其缩放
5.2.1创建图像数组,然后缩放,类型设置为uint8
img = np.array([51,102,153],dtype = np.uint8)
5.2.2对图像的像素强度进行缩放
mat = exposure.rescale_intensity(img)
print(mat)
5.2.3查看数组的数据类型
print(mat.dtype.name)
5.2.4判断对比度高低
result = exposure.is_low_contrast(mat)
print(result)
运行结果:
5.3将数组转换为浮点型
img = np.array([51,102,153],dtype = np.uint8)
print(img*1.0)
运行结果:
5.4对浮点型数组进行缩放
img = np.array([51,102,153],dtype = np.uint8)
tmp=img*1.0
mat = exposure.rescale_intensity(tmp)
print(mat)
运行结果:
6.对图像画形状
6.1使用draw.line方法对图像画线
hh,ll = draw.line(0,250,1600,250)
#起点:参数1:距x轴的坐标;参数2:距y轴的坐标
#终点:参数3:距x轴的坐标;参数4:距y轴的坐标
6.1.1给线条添加颜色
# img[hh,ll] = 200
draw.set_color(img,[hh,ll],[0,0,255])
可以使用两种方法,1:直接给定0--255的值
2:使用set_color方法
运行结果:
6.2使用draw.disk方法画圆圈
hh,ll = draw.disk((1000,520),50)
#参数1:确定圆圈的中心点,参数2:圆形的半径
运行结果:
6.3使用draw.polygon画多边形
X = np.array([240,400,500,405,240,150]) #顺时针坐标
Y = np.array([50,50,180,320,320,190])
hh,ll = draw.polygon(Y,X)
draw.set_color(img,[hh,ll],[0,0,255])
X存储在x轴的点位。Y存储距x轴的距离的坐标(经过好几次修改,终于得到了六边形)
运行结果:
6.4使用draw.ellipse画椭圆
hh,ll = draw.ellipse(1000,520,50,100)
#参数1、2:确定椭圆的中心点坐标
#参数3:椭圆上下的距离
#参数4:椭圆左右的距离
运行结果: