Opencv 图像处理
cmd.exe 安装命令行
》pip install opencv-python
》pip install tensorflow
》pip install keras 必须安装tensorflow基础之上
板块一、
几何图形(cv2 图像处理)
图片导入; |
tu = cv2.imread('image.jpg') cv2.imwrite('newImage.jpg',image) |
封装常用函数 代码块(.py文件) 包含图片旋转代码 |
import cv2 import matplotlib.pyplot as plt import numpy as np #读取图片 def imgRead(src): src = cv2.imread(src) return cv2.cvtColor(src,cv2.COLOR_BGR2RGB) #显示图片 def imgShow(img): plt.imshow(img) plt.axis('off') plt.show() def imgWrite(Img): return cv2.imwrite('newImage.jpg',Img) img = imgRead('face.png') imgShow(img) #imgWrite(img) #看图片的信息 (h,w,t)=img.shape print(img.shape) #得出图片的中心点 cX,cY=(w//2,h//2) #写一个 动作 ,旋转动作 (旋转中心点,旋转角度,缩放的倍数) Action = cv2.getRotationMatrix2D((cX,cY),90,1.0) #外容器执行旋转动作 存储图片 img = cv2.warpAffine(img,Action,(w,h)) imgShow(img) 使用引入格式: from 文件名 import * |
设置图片大小+等比例算法: | imgW = 80 imgH = int(img.shape[0]*imgW/img.shape[1]) img= cv2.resize(img,(imgW,imgH)) show(img) print(img.shape) |
人脸检测方法初学:
#定义人脸检测方法 def check(image): detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') #写入这个 简单的xml文件 rects = detector.detectMultiScale(image,scaleFactor=1.1,minNeighbors=3,minSize=(10,10),flags=cv2.CASCADE_SCALE_IMAGE) for (x,y,w,h) in rects: cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0), 2) img = imread('Solvay.jpg') check(img) show(img) |
板块二、
一、画图
导包 | import numpy as np import cv2 import matplotlib.pyplot as plt |
显示函数 | def show(image): plt.imshow(image) plt.axis('off') plt.show() |
创建一个 300 300 画布 | image = np.zeros((300,300,3),dtype='uint8') |
画线 | green = (0,255,0) cv2.line(image, (0,0), (300,300), green) |
正方形 -1 为填充 | cv2.rectangle(wrap,(3,3),(296,296),(120,120,120),5) |
(cX,cY) = wrap.shape[1]//2,wrap.shape[0]//2 for i in range(0,140,15): cv2.circle(wrap,(cX,cY),i,green,2) cv2.circle(wrap,(cX,cY),15,(255,0,0),-1) show(wrap) |
|
content = np.zeros((300,300,3),dtype='uint8') for z in range(15): #随机的半径 Rban = np.random.randint(0,150) #随机的圆心 Rxin = tuple(np.random.randint(0,300,size=(2))) #随机的颜色 Rcolor = np.random.randint(0,255,size=(3)).tolist() cv2.circle(content,Rxin,Rban,Rcolor,-1) show(content) |
二、翻转
导包不说了 | cv2 matplotlib.pyplot |
水平翻转 1 垂直翻转 0 水平+垂直 -1 |
cv2.finp(image,1) |
三、裁剪
裁剪就是:把图片的某一块坐标数值,存起来显示
avater = image[0:0,200,200] 打印输出
四、图像算术
加像素 | add = np.ones(wrap.shape,dtype='uint8')*100 coo = cv2.add(wrap,add) show(coo) |
|
减像素 | shan = np.ones(wrap.shape,dtype='uint8')*205 cww = cv2.subtract(wrap,shan) show(cww) |
五、按位计算
与 1&1=1,1&0=0,0&1=0,0&0=0 | and = cv2.bitwise_and(box1,box2) | |
或1|1=1,1|0=1,0|1=1,0|0=0 | or = cv2.bitwise_or(box1,box2) | |
异或1^1=0,1^0=1,0^1=1,0^0=0 | xor = cv2.bitwise_xor(box1,box2) | |
非 |
not= cv2.bitwise_not(wrap) |
五、遮挡
#导入一张图片 img = cv2.imread('cat.jpg') #创建一个 跟图片相同大小的容器 wrap = np.zeros(img.shap,dtype='uint8') #创建一个盒子 squen = cv2.rectangle(wrap,(25,25),(250,350),白色,-1) #进行按位计算 得出新的图片 newImg = cv2.bitwise_and(wrap,img) show(newImg) |
|
五、单通道切割
mm = cv2.imread('cat.jpg') show(mm) (R,G,B) = cv2.split(mm) mm.shape meraged = cv2.merge([R,G,B]) show(meraged) cv2.imshow('R',R) cv2.imshow('G',G) cv2.imshow('B',B) cv2.waitKey(0) cv2.destroyAllWindows() |
|
六、图像金字塔
nn = cv2.imread('image.jpg')
|
for u in range(4): nn = cv2.pyrUp(nn) print(nn.shape) show(nn) |
七、拉普拉斯金字塔(提取图像边界轮廓的)
yuan = cv2.imread('image.jpg') |
|
板块三、 形态学(卷积)
一、Erode腐蚀 .erode()
#设置腐蚀 Erision 黑色多一点 白色小时 juxing = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) tuoyuan = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) shizi = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5)) |
imagea = cv2.erode(imga,juxing) 去除白色,黑色多 |
二、Dilate膨胀 .dilate()
#膨胀 dilate 黑色多一点 白色小时 juxing = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) tuoyuan = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) shizi = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5)) |
imagea = cv2.dilate(imga,juxing) 去除白色,黑色多 |
三、Opening开运算
#先腐蚀后膨胀 juxing = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) |
opening= cv2.morphologyEx(imga,cv2.MORPH_OPEN,juxing) 专业去除白色小点 |
四、Closing闭运算 相反
#先膨胀后腐蚀 juxing = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) |
closing= cv2.morphologyEx(imga,cv2.MORPH_CLOSE,juxing) 专业去除嘿色小点 |
五、先开后闭运算
#先膨胀后腐蚀 juxing = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) |
imgb = cv2.imread('image2.jpg') opening= cv2.morphologyEx(imgb,cv2.MORPH_OPEN,juxing) colseing= cv2.morphologyEx(opening,cv2.MORPH_CLOSE,juxing) show(colseing) |
六、梯度运算
juxing = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) |
closing= cv2.morphologyEx(imga,cv2.MORPH_GRADIENT,juxing) 膨胀 - 腐蚀 =出现图像的轮廓线 |
七、白帽运算作用:隐藏的白色显示
# juxing = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) |
closing= cv2.morphologyEx(imga,cv2.MORPH_TopHat,juxing) src - opening |