图像处理—OpenCV相关简单操作
引言
本篇博客只简单介绍下Python中openCV库中对图像的简单处理及相关操作,介绍常用API的使用,方便自己查阅和向读者简单介绍下。但是读者了解但不应局限于此,图像操作对面向API的话,我们很容易就能实现对图像的操作,我们更应该去学习图像操作的原理,无非就是对图像矩阵中像素的一系列的操作,学习这些才能让我们更清楚的去了解图像。
1. openCV库的安装
最方便的是使用PIP安装会自动下载相关依赖类库
pip install opencv-python
原图像:
2. 图像的读取显示
图像读进来是一个三维的矩阵(WEIGHT,HEIGHT,KERNELL)分别是宽高通道数,如果时灰度图,通道数为1;如果时彩色图通道数为3。
注:openCV读进来的图像KERNELL是以BGR的方式。而其他的比如PIL,tf.image是RGB方式读取。openCV中也提供了相关的API去转换。
定义一下路径:
path = './dog.jpg'
# 1. 引入OpenCV 2.API 3.stop
import cv2
img = cv2.imread(path,0) # 路径 0灰度1彩色
cv2.imshow('zuer',img) # name win img
cv2.waitKey(0)
3. 图像的写入
import cv2
img = cv2.imread(path,1)
cv2.imwrite('./new.jpg', img)
import cv2
img = cv2.imread(path,1)
cv2.imwrite('./new.jpg', img, [cv2.IMWRITE_JPEG_QUALITY , 50])
# jpg 有损压缩 0-100 png 无损压缩 0-9
下面代码是在图上面画了一条线
import cv2
img = cv2.imread(path, 1)
(b, g, r) = img[200, 200]
for i in range(300):
img[10+i,400] = (255,0,0)
cv2.imshow('image', img)
cv2.waitKey(0)
4. 图像的尺寸变化
import cv2
img = cv2.imread(path, 1)
imginfo = img.shape
height = imginfo[0]
width = imginfo[1]
mode = imginfo[2]
# 放大 缩小 等比例 非等比例
dstWidth = int(width * 0.3)
dstHeight = int(height * 0.3)
# 最近临域插值 双线性插值 像素关系重采样 立方插值 其他API中常用的也是这几种
dst = cv2.resize(img, (dstWidth, dstHeight))
cv2.imshow('erke', dst)
cv2.waitKey(0)
5. 图像的仿射变换
在图像上选取点做对应的映射变换
import cv2
import numpy as np
img = cv2.imread(path)
imginfo = img.shape
height = imginfo[0]
width = imginfo[1]
matSrc = np.float32([[0,0],[0,height-1],[width-1, 0]])
matDst = np.float32([[50,50],[0,height-80],[width-100,0]]) #采用x,y坐标选取点
matAffine = cv2.getAffineTransform(matSrc, matDst)
dst = cv2.warpAffine(img, matAffine, (400, 400))
cv2.imshow('dst', dst)
cv2.waitKey(0)
6. 图像的旋转
# 在一定空间商做旋转
import cv2
import numpy as np
img = cv2.imread(path)
imginfo = img.shape
height = imginfo[0]
width = imginfo[1]
matRotate = cv2.getRotationMatrix2D((width * 0.5, height * 0.5),45,0.7)
dst = cv2.warpAffine(img, matRotate, (460, 460))
cv2.imshow('dst', dst)
cv2.waitKey()
7. 颜色空间转换
import cv2
import numpy as np
img = cv2.imread(path, 1)
imginfo = img.shape
height = imginfo[0]
width = imginfo[1]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #颜色空间转换
dst = np.zeros((height, width, 1),np.uint8)
for i in range(height):
for j in range(width):
dst[i,j] = 255 - gray[i,j]
cv2.imshow('dst', dst)
cv2.waitKey(0)
8. 颜色反转
import cv2
import numpy as np
img = cv2.imread(path, 1)
imginfo = img.shape
height = imginfo[0]
width = imginfo[1]
dst = np.zeros((height, width, 3),np.uint8)
for i in range(height):
for j in range(width):
(b,g,r) = img[i,j]
dst[i,j] = (255-b,255-g,255-r)
cv2.imshow('dst', dst)
cv2.waitKey(0)
9. 马赛克
import cv2
img = cv2.imread(path)
imginfo = img.shape
height = imginfo[0]
width = imginfo[1]
for m in range(100,150):
for n in range(100,150):
if m%10==0 and n%10==0:
for i in range(m,m+10):
for j in range(n,n+10):
img[i,j] = img[m,n]
cv2.imshow('马赛克', img)
cv2.waitKey(0)
10. 毛玻璃
import cv2
import numpy as np
import random
img = cv2.imread(path, 1)
imginfo = img.shape
height = imginfo[0]
width = imginfo[1]
dst = np.zeros((height, width, 3), np.uint8)
mm = 6
for m in range(height - mm):
for n in range(width - mm):
index = random.randint(0,mm)
dst[m,n] = img[m + index, n + index]
cv2.imshow('dst',dst)
cv2.waitKey(0)
11. 图像融合
import cv2
import numpy as np
img0 = cv2.imread('./zuer.jpg', 1)
img1 = cv2.imread('./erke.jpg', 1)
height = 660
width = 460
origin = 20
img0ROI = img0[20:height + 20, 20:width + 20]
img1ROI = img1[150:height + 150, 120:width + 120]
print(img0ROI.shape, img1ROI.shape)
dst = np.zeros((height, width, 3), np.uint8)
dst = cv2.addWeighted(img0ROI,0.5,img1ROI,0.5,0)
cv2.imshow('dst', dst)
cv2.waitKey(0)
12. 边缘检测
import cv2
import numpy as np
img = cv2.imread('./erke.jpg', 1)
cv2.imshow('src', img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.GaussianBlur(gray, (3,3), 0)
dst = cv2.Canny(img, 50, 50)
cv2.imshow('dst', dst)
cv2.waitKey(0)
13. 浮雕效果
import cv2
import numpy as np
img = cv2.imread('./erke.jpg', 1)
imginfo = img.shape
height = imginfo[0]
width = imginfo[1]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
dst = np.zeros((height, width, 1), np.uint8)
for i in range(0, height):
for j in range(0, width-1):
grayP0 = int(gray[i,j])
grayP1 = int(gray[i,j+1])
newP = grayP0 - grayP1 + 150
if newP > 255:
newP = 255
if newP < 0:
newP = 0
dst[i,j] = newP
cv2.imshow('dst',dst)
cv2.waitKey(0)
14. 颜色映射
import cv2
import numpy as np
img = cv2.imread(path,1)
cv2.imshow('src', img)
imginfo = img.shape
height = imginfo[0]
width = imginfo[1]
dst = np.zeros((height,width,3), np.uint8)
for i in range(height):
for j in range(width):
(b,g,r) = img[i,j]
b = b*1.5
g = g*1.3
if b > 255:
b = 255
if g > 255:
g = 255
dst[i,j] = (b,g,r)
cv2.imshow('dst', dst)
cv2.waitKey(0)
15. 几何图形绘制
import cv2
import numpy as np
newImageInfo = (500,500,3)
dst = np.zeros(newImageInfo, np.uint8)
cv2.line(dst, (100,100),(400,400),(0,255,255))
cv2.line(dst, (100,200),(400,200),(0,255,255), 20)
cv2.line(dst, (100,200),(400,200),(0,255,255), 20, cv2.LINE_8)
cv2.imshow('dst', dst)
cv2.waitKey(0)
16. 彩色图片均衡化
import cv2
import numpy as np
def ImageHist(image, type):
color = (255,255,255)
windowName = 'Gray'
if type == 31:
color =(255,0,0)
windowName = 'B Hist'
elif type == 32:
color = (0,255,0)
windowName = 'G Hist'
elif type == 33:
color = (0,0,255)
windowName = 'R Hist'
hist = cv2.calcHist([image],[0],None,[256], [0.0, 255.0])
minV,maxV,minL,maxL = cv2.minMaxLoc(hist)
histImg = np.zeros((256,256,3),np.uint8)
for h in range(256):
intenNormal = int(hist[h]/maxV*255)
cv2.line(histImg,(h,255),(h,255-intenNormal),color)
cv2.imshow(windowName,histImg)
return histImg
img = cv2.imread(path,1)
channels = cv2.split(img)
for i in range(0,3):
ImageHist(channels[i], 31 + i)
cv2.waitKey(0)
17. 直方图均衡化
# 灰度图片
import cv2
import numpy as np
img = cv2.imread(path,1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('src', gray)
dst = cv2.equalizeHist(gray)
cv2.imshow('dst', dst)
cv2.waitKey(0)
#彩色图片
import cv2
import numpy as np
img = cv2.imread(path,1)
cv2.imshow('src', img)
(b,g,r) = cv2.split(img)
bH = cv2.equalizeHist(b)
gH = cv2.equalizeHist(g)
rH = cv2.equalizeHist(r)
result = cv2.merge((bH,gH,rH))
cv2.imshow('dst', result)
cv2.waitKey(0)
18. 亮度增强 美白
# p = p + 50
import cv2
import numpy as np
img = cv2.imread('./zuer.jpg', 1)
imginfo = img.shape
height = imginfo[0]
width = imginfo[1]
cv2.imshow('src', img)
dst = np.zeros((height,width,3), np.uint8)
degree = 36
for i in range(height):
for j in range(width):
(b,g,r) = img[i,j]
bb = b + degree
gg = g + degree
rr = r + degree
if bb > 255:
bb = 255
if gg > 255:
gg = 255
if rr > 255:
rr = 255
dst[i,j] = (bb,gg,rr)
cv2.imshow('dst', dst)
cv2.waitKey(0)
19. 双边滤波
import cv2
import numpy as np
img = cv2.imread('./zuer.jpg', 1)
cv2.imshow('src', img)
dst = cv2.bilateralFilter(img, 15,35,35)
cv2.imshow('dst', dst)
cv2.waitKey(0)