(十)python语法之图像处理

1.pillow

from PIL import Image

#1 基础
im = Image.open("test.png")
print(im.format, im.size, im.mode) 
# PNG (512,512) RGBA, size=(宽,高)
im.show() 

# 灰度转换
im = im.convert("L")  
# 对每个像素点执行函数运算
im = im.point(lambda i: i * 1.2) 

im = im.resize((128, 128)) # 缩放
im.save("result.png")      # 保存

# 生成JPEG缩略图
im.thumbnail((128, 128))
im.save("result.thumbnail", "JPEG")

#2 裁剪
im = Image.open("test.jpg")
# (左上角x, 左上角y, 右下角x, 右下角y)
box = (100, 100, 400, 400) 
out = im.crop(box)
out.show()

#3 粘贴
im2 = Image.open("test.png")
im2.paste(out, box)                        
im2.show()

#4 旋转
out = im.transpose(Image.FLIP_LEFT_RIGHT)    
out = im.transpose(Image.FLIP_TOP_BOTTOM)
out.show()

out = im.transpose(Image.ROTATE_90)
out = im.transpose(Image.ROTATE_180)
out = im.transpose(Image.ROTATE_270)
out.show()

out = im.rotate(45) 
out.show()

#5 通道分离与合并
im = Image.open("test.jpg")
r, g, b = im.split()               
im = Image.merge("RGB", (b, g, r)) 
im.show()

#6 效果增强
from PIL import ImageEnhance 
from PIL import ImageFilter  

im = Image.open("test.jpg")

# 对比度
enh = ImageEnhance.Contrast(im)
enh.enhance(1.3).show("30% more contrast")

# 滤镜
out = im.filter(ImageFilter.DETAIL)
out.show()

#7 图像阵列
im = Image.open("test.gif")
im.seek(1)  # 跳到下一帧
try:
    while 1:
        im.seek(im.tell()+1)
        im.show()
except EOFError:   
    # 在帧尾时会得到一个EOFError异常
    pass 

#8 与numpy互相转换
a = np.array(Image.open("test.jpg"))
print(a.shape, a.dtype)
b = [255, 255, 255] - a
im = Image.fromarray(b.astype('uint8'))
im.save("test2.jpg")

2.opencv

基础

import cv2 

# 图片读取与显示
img = cv2.imread('test.jpg', 1) 
cv2.namedWindow("test") 
cv2.imshow('test',img) 
cv2.waitKey(0)

# 获取像素值,(高,宽)
(b,g,r) = img[100,200]                         
print(b,g,r)

# 图片ROI
dst = img[200:400,200:400]                     
cv2.imshow('test',dst) 
cv2.waitKey(0)

# 图像加权融合
dst1 = img[200:400,200:400]   
dst2 = img[0:200,0:200]
dst = cv2.addWeighted(dst1,0.5,dst2,0.5,0)
cv2.imshow('test',dst) 
cv2.waitKey(0)

# 灰度转换
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  
cv2.imshow('test',grey) 
cv2.waitKey(0)

# 图片缩放 (宽,高)
dst = cv2.resize(img, (100,200))
cv2.imshow('test',dst) 
cv2.waitKey(0)

# 像素遍历
height   = img.shape[0]
weight   = img.shape[1]
channels = img.shape[2]
for row in range(height):               
    for col in range(weight):           
        for c in range(channels):       
            pv = img[row, col, c] 
            img[row, col, c] = 255 - pv                             
cv2.imshow('test',img) 
cv2.waitKey(0)  

# 图像写入
cv2.imwrite('cv-test.jpg', img) 
# 0-100 jpg压缩比高
cv2.imwrite('cv-test-50.jpg',img,[cv2.IMWRITE_JPEG_QUALITY,50]) 
# 0-9 png压缩比低
cv2.imwrite('cv-test.png',img,[cv2.IMWRITE_PNG_COMPRESSION,0])

阈值化滤波

import cv2
import numpy as np

img = cv2.imread('test.jpg', 0)

# 阈值化
ret,dst = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
cv2.imshow('test',dst) 
cv2.waitKey(0) 
# cv2.THRESH_BINARY_INV
# cv2.THRESH_TRUNC
# cv2.THRESH_TOZERO
# cv2.THRESH_TOZERO_INV

# 自适应阈值化
dst = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
cv2.imshow('test',dst) 
cv2.waitKey(0) 
# cv2.ADAPTIVE_THRESH_GAUSSIAN_C

# OTSU阈值化
ret,dst = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imshow('test',dst) 
cv2.waitKey(0)

# 自定义滤波
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)
cv2.imshow('test',dst) 
cv2.waitKey(0)

# 平均滤波
dst = cv2.blur(img,(5,5))
cv2.imshow('test',dst) 
cv2.waitKey(0)
            
# 高斯均值滤波
dst = cv2.GaussianBlur(img,(5,5),1.5)   
cv2.imshow('test',dst) 
cv2.waitKey(0) 

# 中值滤波
dst = cv2.medianBlur(img,5)
cv2.imshow('test',dst) 
cv2.waitKey(0) 

# 双边滤波
dst = cv2.bilateralFilter(img,15,35,35) 
cv2.imshow('test',dst) 
cv2.waitKey(0)

形态学处理

import cv2

img = cv2.imread('test.jpg', 0)
kernel = np.ones((5,5),np.uint8)

# 腐蚀
dst = cv2.erode(img,kernel,iterations = 1)
cv2.imshow('test',dst) 
cv2.waitKey(0) 

# 膨胀
dst = cv2.dilate(img,kernel,iterations = 1)
cv2.imshow('test',dst) 
cv2.waitKey(0) 

# 开运算
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
cv2.imshow('test',dst) 
cv2.waitKey(0) 

# 闭运算
dst = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imshow('test',dst) 
cv2.waitKey(0) 

# 形态学梯度
dst = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
cv2.imshow('test',dst) 
cv2.waitKey(0) 

# 顶帽
dst = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
cv2.imshow('test',dst) 
cv2.waitKey(0) 

# 黑帽
dst = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
cv2.imshow('test',dst) 
cv2.waitKey(0)

边缘轮廓

import cv2
import numpy as np

img = cv2.imread('test.jpg', 0)

# 边缘检测
dst = cv2.GaussianBlur(img,(3,3),0)        
dst = cv2.Canny(dst,50,50) 
cv2.imshow('test',dst) 
cv2.waitKey(0)

# 轮廓检测
ret, thresh = cv2.threshold(img, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0,255,0), 3)
cv2.imshow('test',img) 
cv2.waitKey(0)

# 轮廓拟合
cnt = contours[0]
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)
cv2.imshow('test',img) 
cv2.waitKey(0)

绘制

import cv2

# 绘制的底板
dst = np.zeros((500,500,3), np.uint8)

# 绘制线段 begin/end/color/line/linetype
cv2.line(dst,(100,20),(400,20),(0,255,0),20,cv2.LINE_AA)    

# 绘制矩形 左上角/右下角/color/line
cv2.rectangle(dst,(30,50),(120,200),(255,0,0),5)  

# 绘制圆形 center/radius/color/line
cv2.circle(dst,(250,100),(50),(0,255,0),2) 

# 绘制椭圆 center/轴/angle/begin/end/color/line              
cv2.ellipse(dst,(256,256),(150,100),0,0,180,(255,255,0),-1) 

# 绘制折线
points = np.array([[150,50],[140,140],[200,170],[250,250],[150,50]],np.int32) 
points = points.reshape((-1,1,2))                                             
cv2.polylines(dst,[points],True,(0,255,255))                                  

# 绘制文字 文字内容/坐标/字体/字体大小/color/line/linetype
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(dst,'xxxx',(100,400),font,1,(200,100,255),2,cv2.LINE_AA) 

cv2.imshow('test',dst) 
cv2.waitKey(0)

直方图

import cv2
import numpy as np        

grey = cv2.imread('test.jpg', 0)
img = cv2.imread('test.jpg', 1)

# 直方图绘制
color = ('b','g','r')
for i,col in enumerate(color):
    histr = cv2.calcHist([img],[i],None,[256],[0,256])
    plt.plot(histr,color = col)
    plt.xlim([0,256])
plt.show()

# 灰度直方图均衡化
dst = cv2.equalizeHist(grey) 
cv2.imshow('test',dst) 
cv2.waitKey(0)

# 彩色直方图均衡化
(b,g,r) = cv2.split(img)    
bH = cv2.equalizeHist(b)
gH = cv2.equalizeHist(g)
rH = cv2.equalizeHist(r)
dst = cv2.merge((bH,gH,rH)) 
cv2.imshow('test',dst) 
cv2.waitKey(0)

# YUV直方图均衡化
imgYUV = cv2.cvtColor(img,cv2.COLOR_BGR2YCrCb)
channelYUV = cv2.split(imgYUV)
channelYUV[0] = cv2.equalizeHist(channelYUV[0])
channels = cv2.merge(channelYUV)
dst = cv2.cvtColor(channels,cv2.COLOR_YCrCb2BGR)
cv2.imshow('test',dst) 
cv2.waitKey(0)

几何变换

import cv2
import numpy as np

img = cv2.imread('test.jpg', 1)
height = img.shape[0]
width = img.shape[1]

# 图片缩放
matScale = np.float32([[0.5,0,0],[0,0.5,0]])
dst = cv2.warpAffine(img,matScale,(int(width/2),int(height/2)))
cv2.imshow('test',dst) 
cv2.waitKey(0)

# 仿射变换
# 左上角 左下角 右上角
matSrc = np.float32([[0,0],[0,height-1],[width-1,0]]) 
matDst = np.float32([[50,50],[300,height-200],[width-300,100]])
matAffine = cv2.getAffineTransform(matSrc,matDst)
dst = cv2.warpAffine(img,matAffine,(width,height))
cv2.imshow('test',dst) 
cv2.waitKey(0)

# 图片旋转
# center/angle/scale
matRotate = cv2.getRotationMatrix2D((height*0.5,width*0.5),45,1)
dst = cv2.warpAffine(img,matRotate,(height,width))
cv2.imshow('test',dst) 
cv2.waitKey(0)

# 透视变换
pts1 = np.float32([[200,200],[500,200],[200,500],[500,500]])
pts2 = np.float32([[0,0],[300,0],[0,300],[300,300]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,M,(300,300))
cv2.imshow('test',dst) 
cv2.waitKey(0)

# 图片镜像
img = cv2.imread('test.jpg',1)
cv2.imshow('src',img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
newImgInfo = (height*2,width,deep)
dst = np.zeros(newImgInfo,np.uint8)#uint8 
for i in range(0,height):
    for j in range(0,width):
        dst[i,j] = img[i,j]
        dst[height*2-i-1,j] = img[i,j]
for i in range(0,width):
    dst[height,i] = (0,0,255)#BGR
cv2.imshow('dst',dst)
cv2.waitKey(0)

# 图片移位1
img = cv2.imread('test.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]

matShift = np.float32([[1,0,100],[0,1,200]])      
dst = cv2.warpAffine(img,matShift,(height,width))
cv2.imshow('dst',dst)
cv2.waitKey(0)

# 图片移位2
dst = np.zeros(img.shape,np.uint8)
for i in range(0,height):
    for j in range(0,width-100):
        dst[i,j+100]=img[i,j]
cv2.imshow('image',dst)
cv2.waitKey(0)
posted @ 2020-08-20 17:43  qxcheng  阅读(290)  评论(0编辑  收藏  举报