OpenCV_Python 入门
创建/显示窗口
方法 |
说明 |
参数 |
返回 |
namedWindow |
创建一个窗口 |
窗口名称,WINDOW_NORMAL(窗口属性) |
|
resizeWindow |
设置窗口大小 |
窗口名称,宽,高 |
|
imshow |
显示窗口 |
窗口名称,要显示的图像 |
|
destroyAllWindows |
关闭所有窗口 |
|
|
waitKey |
延时监听键盘按下 |
0无限 / 单位毫秒 |
键盘按下键的ASCII码 |
python
复制代码
import cv2
cv2.namedWindow("img",cv2.WINDOW_NORMAL)
cv2.resizeWindow("img",800,600)
while True:
cv2.imshow("img",0)
key = cv2.waitKey(0)
if key & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
显示图片
方法 |
说明 |
参数 |
返回 |
imread |
读取图片 |
路径,IMREAD_GRAYSCALE(灰)/IMREAD_COLOR(彩)/其它 |
读取到的图片资源 |
python
复制代码
import cv2
while True:
# cv2.imshow("img",cv2.imread("../1.jpg",cv2.IMREAD_COLOR))
cv2.imshow("img",cv2.imread("../1.jpg",cv2.IMREAD_GRAYSCALE))
key = cv2.waitKey(0)
if key & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
保存图片
方法 |
说明 |
参数 |
返回 |
imwrite |
保存图片 |
保存的文件名称,图片资源 |
|
python
复制代码
import cv2
img = cv2.imread("../1.jpg",cv2.IMREAD_COLOR)
while True:
cv2.imshow("img",img)
key = cv2.waitKey(0)
if key & 0xFF == ord('q'):
break
elif key & 0xFF == ord('s'):
cv2.imwrite("11.png",img)
cv2.destroyAllWindows()
视频采集
方法 |
说明 |
参数 |
返回 |
VideoCapture |
摄像机 |
0(开启本机摄像头) |
|
VideoCapture.read |
获取视频帧 |
|
ret(读取到帧为true),frame(视频帧) |
VideoCapture.release |
释放资源 |
|
|
python
复制代码
import cv2
cv2.namedWindow("frame",cv2.WINDOW_NORMAL)
cv2.resizeWindow("frame",800,600)
cap = cv2.VideoCapture(0)
while True:
ret,frame = cap.read()
cv2.imshow('frame',frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
读取视频文件
方法 |
说明 |
参数 |
返回 |
VideoCapture |
摄像机 |
本机视频文件路径 |
|
python
复制代码
import cv2
cv2.namedWindow("frame",cv2.WINDOW_NORMAL)
cv2.resizeWindow("frame",800,600)
cap = cv2.VideoCapture("../2.mp4")
while True:
ret,frame = cap.read()
cv2.imshow('frame',frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
视频录制
方法 |
说明 |
参数 |
返回 |
VideoWriter_fourcc |
编码器 |
*MJPG / 其它 |
|
VideoWriter |
视频输出 |
文件名称,编码器,帧数,(宽,高) |
|
VideoWriter.write |
视频写入 |
帧 |
|
VideoWriter.release |
释放资源 |
|
|
python
复制代码
import cv2
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
vw = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
cv2.namedWindow("frame",cv2.WINDOW_NORMAL)
cv2.resizeWindow("frame",800,600)
cap = cv2.VideoCapture(0)
while True:
ret,frame = cap.read()
cv2.imshow('frame',frame)
vw.write(frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
cap.release()
vw.release()
cv2.destroyAllWindows()
python
复制代码
import cv2
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
vw = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
cv2.namedWindow("frame",cv2.WINDOW_NORMAL)
cv2.resizeWindow("frame",640,480)
cap = cv2.VideoCapture(0)
while cap.isOpened(): # 判断摄像头是否为打开状态
ret,frame = cap.read()
if not ret: break # 判断帧是否打开
cv2.imshow('frame',frame)
# 窗口撑开后重新设置
cv2.resizeWindow("frame", 640, 480)
vw.write(frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
cap.release()
vw.release()
cv2.destroyAllWindows()
鼠标控制
方法 |
说明 |
参数 |
返回 |
setMouseCallback |
监听鼠标 |
窗口名称,监听事件,额外参数 |
|
python
复制代码
import cv2
# 鼠标操作 x y 组合操作 额外参数
def mouse_callback(event, x, y, flags, param):
print(event, x, y, flags, param)
cv2.namedWindow("img",cv2.WINDOW_NORMAL)
cv2.resizeWindow("img",800,600)
cv2.setMouseCallback("img",mouse_callback,"123")
img = cv2.imread("../1.jpg")
while True:
cv2.imshow("img",img)
key = cv2.waitKey(0)
if key & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
TrackBar 滑块
方法 |
说明 |
参数 |
返回 |
createTrackbar |
创建 |
滑块名称,窗口名称,value 当前值,count 最大值,callback 回调,额外参数 |
|
getTrackbarPos |
获取 |
滑块名称,窗口名称 |
当前值 |
python
复制代码
import cv2
import numpy as np
def callback():
pass
cv2.namedWindow("img",cv2.WINDOW_NORMAL)
cv2.createTrackbar("R","img",0,255,callback)
cv2.createTrackbar("G","img",0,255,callback)
cv2.createTrackbar("B","img",0,255,callback)
img = np.zeros((480,640,3),np.uint8)
while True:
r = cv2.getTrackbarPos("R","img")
g = cv2.getTrackbarPos("G","img")
b = cv2.getTrackbarPos("B","img")
img[:] = [b,g,r]
cv2.imshow("img",img)
key = cv2.waitKey(10)
if key & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
色彩
- opencv 采用 BGR 格式的颜色显示
- HSV 色相 / 饱和度 / 明度
- HSL 色相 / 饱和度 / 明度
- 色彩空间转换
方法 |
说明 |
参数 |
返回 |
cvtColor |
色彩转换 |
获取到的图片资源,色彩转换 |
|
python
复制代码
import cv2
def callback():
pass
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
cv2.resizeWindow('image',800,600)
img = cv2.imread("../1.jpg",1)
colorspaces = [cv2.COLOR_BGR2RGB,cv2.COLOR_BGR2BGRA,
cv2.COLOR_BGR2GRAY,cv2.COLOR_BGR2HSV_FULL,
cv2.COLOR_BGR2YUV]
cv2.createTrackbar('curcolor','image',0,len(colorspaces)-1,callback)
while True:
v = cv2.getTrackbarPos('curcolor','image')
# 颜色空间转换
cvt_img = cv2.cvtColor(img,colorspaces[v])
cv2.imshow('image',cvt_img)
key = cv2.waitKey(10)
if key & 0xFF == ord('q'):
break
cv2.destroyAllWindows()
Numpy 基本操作
方法 |
参数 |
说明 |
array |
[] |
|
zeros |
(行,列,通道数),类型 |
值为0 |
ones |
(行,列,通道数),类型 |
值为1 |
full |
(行,列,通道数),值,类型 |
|
identity |
3 |
三行三列 |
eye |
3,5,k=1 |
三行五列, 从第 1-1列开始 |
python
复制代码
import numpy as np
a = np.array([1,2,3])
b = np.array([[1,2,3],[4,5,6]])
print(a)
print(b)
c = np.zeros((8,8,3),np.uint8)
print(c)
d = np.ones((8,8,3),np.uint8)
print(d)
e = np.full((8,8,3),10,np.uint8)
print(e)
f = np.identity(4)
print(f)
y = np.eye(5,7,k=1)
print(y)
- 赋值 [y,x]
- 赋值 [y,x,channel]
python
复制代码
import numpy as np
import cv2
img = np.zeros((640,480,3),np.uint8)
# 检索
print(img[100,100])
count = 0
while count < 100:
# 赋值 通道 0,1,2
# img[count,100,0] = 255
img[count, 100] = [0,0,255]
count = count+1
cv2.imshow("img",img)
key = cv2.waitKey(0)
if key & 0xFF == ord('q'):
cv2.destroyAllWindows()
- ROI 获取子矩阵 [y1:y2,x1:x2]
- [:,:] 整个矩阵 [:]
python
复制代码
import numpy as np
import cv2
img = np.zeros((640,480,3),np.uint8)
roi = img[100:200,100:200]
img[:] = [255,0,0]
roi[:,:] = [0,0,255]
roi[10:20,10:20] = [255,255,255]
cv2.imshow("img",img)
key = cv2.waitKey(0)
if key & 0xFF == ord('q'):
cv2.destroyAllWindows()
Mat
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
# 浅拷贝
img2 = img
# 深拷贝
img3 = img.copy()
img[10:100,10:100] = [0,0,255]
cv2.imshow('img',img)
cv2.imshow('img2',img2)
cv2.imshow('img3',img3)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
# shape 高,长度,通道
print(img.shape)
# 图像占用多大空间
# 高度 * 长度 * 通道数
print(img.size)
# 图像中每个元素的位深
print(img.dtype)
python
复制代码
import numpy as np
import cv2
img = np.zeros((480,640,3),np.uint8)
# 分割
b,g,r = cv2.split(img)
b[10:100,10:100] = 255
g[10:100,10:100] = 255
# 合并
img2 = cv2.merge([b,g,r])
cv2.imshow("img",img)
cv2.imshow("b",b)
cv2.imshow("g",g)
cv2.imshow("img2",img2)
cv2.waitKey(0)
图形绘制
python
复制代码
import numpy as np
import cv2
img = np.zeros((480,640,3),np.uint8)
# 画线 坐标点 x,y
# 起始点 结束点 bgr 线条宽 线型:平滑
cv2.line(img,(10,20),(300,400),(0,0,255),2,16)
# 绘制矩形
# 图像 起始点 结束点 bgr 线条宽 -1为填充
cv2.rectangle(img, (100, 120), (160, 200), (0, 255, 0), 2)
cv2.rectangle(img, (180, 300), (160, 200), (0, 255, 0), -1)
# 圆
# 图像 圆点 半径 bgr 线条宽 -1为填充
cv2.circle(img,(300,300), 30, (255,0,0), 5)
cv2.circle(img,(360,360), 30, (255,0,0), -1)
# 椭圆
# 图像 圆点 长宽一半 起始角度 终止角度 角度 bgr 线条宽 -1为填充
cv2.ellipse(img,(300,420), (100,30),0,0,360, (255,0,0), 5)
cv2.ellipse(img,(300,420), (100,30),0,0,360, (255,0,0), -1)
pts = np.array([(300,10),(150,100),(450,100)],np.int32)
# 多边形
# 图像 点集 是否闭合 bgr 线条宽
cv2.polylines(img,[pts],True,(0,255,0))
# 多边形填充
# 图像 点集 bgr
cv2.fillPoly(img,[pts],(0,255,0))
# 文本
# 图像 文本 起始点 字体 字号 brg
cv2.putText(img,"Hello World!!!",(100,100),cv2.FONT_HERSHEY_PLAIN,3,(255,255,255))
cv2.imshow("img",img)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
# 通过鼠标进行图形的绘制
# 1. l键 画线
# 2. r键 画矩形
# 3. c键 画圆
current = 0
startpos = (0,0)
def mouse_callback(event, x, y, flags, param):
# print(event, x, y, flags, param)
global current,startpos
if event & cv2.EVENT_LBUTTONDOWN == cv2.EVENT_LBUTTONDOWN:
startpos = (x,y)
elif event & cv2.EVENT_LBUTTONUP == cv2.EVENT_LBUTTONUP:
if current == 0:
cv2.line(img,startpos,(x,y),(0,0,255))
elif current == 1:
cv2.rectangle(img,startpos,(x,y),(0,0,255))
elif current == 2:
a = (x-startpos[0])
b = (y-startpos[1])
r = (a**2 + b**2)**0.5
cv2.circle(img,startpos,int(r),(0,0,255))
else:
print("无其它")
cv2.namedWindow("img",cv2.WINDOW_NORMAL)
cv2.setMouseCallback("img",mouse_callback,"123")
img = np.zeros((480,640,3),np.uint8)
while True:
cv2.imshow("img",img)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
elif key == ord('l'):
current =0
elif key == ord('r'):
current =1
elif key == ord('c'):
current =2
cv2.destroyAllWindows()
图像运算
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
img2 = np.ones(img.shape,np.uint8) * 50
# 加 原始图像 相加的图像
img11 = cv2.add(img, img2)
# 减 原始图像 相减的图像
img22 = cv2.subtract(img, img2)
# 乘 原始图像 相乘的图像
img33 = cv2.multiply(img, img2)
# 除 原始图像 相除的图像
img44 = cv2.divide(img, img2)
# 溶合 原始图像 原始图像权重 溶合的图像 溶合的图像权重 静态权重
img55 = cv2.addWeighted(img,0.1, img2,0.9,0)
cv2.imshow('img', img)
cv2.imshow('img11', img11)
cv2.imshow('img22', img22)
cv2.imshow('img33', img33)
cv2.imshow('img44', img44)
cv2.imshow('img55', img55)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img1 = np.zeros((200,200),np.uint8)
img1[50:150,50:150] = 255
# 非运算
img11 = cv2.bitwise_not(img1)
img2 = np.zeros((200,200),np.uint8)
img22 = np.zeros((200,200),np.uint8)
img2[20:120,20:120] = 255
img22[80:150,80:150] = 255
# 与运算
# img222 = cv2.bitwise_and(img2,img22)
# 或运算
# img222 = cv2.bitwise_or(img2,img22)
# 异或运算
img222 = cv2.bitwise_xor(img2,img22)
cv2.imshow('img1',img1)
cv2.imshow('img11',img11)
cv2.imshow('img222',img222)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
# roi 获取图标在原始图像的位置
roi = img[0:200,0:200]
# 图标创建
logo = np.zeros((200,200, 3), np.uint8)
logo[20:120,20:120] = [0,0,255]
logo[80:180,80:180] = [0,255,0]
# 创建一个与图标相同的图像
# 使用非运算取相反的图像
# 图标在原始图像的位置与当前获取相反图标进行与运算得到原始图像中显示图标的位置
# logo与原始图像中显示图标相加得到原始图像显示logo的图像
mask = np.zeros((200,200), np.uint8)
mask[20:120,20:120] = 255
mask[80:180,80:180] = 255
m = cv2.bitwise_not(mask)
tmp = cv2.bitwise_and(roi,roi,mask=m)
dst = cv2.add(tmp,logo)
# 更改原始图像显示logo位置的图像
img[0:200,0:200] = dst
cv2.imshow('logo', dst)
cv2.waitKey(0)
图像变换
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
# 缩放
# 图像 (长,宽) fx 长缩放比例 fy 宽缩放比例 interpolation 插值算法
img1 = cv2.resize(img, (224, 224))
img2 = cv2.resize(img, None, fx=0.6, fy=0.6, interpolation=cv2.INTER_AREA)
img3 = cv2.resize(img, None, fx=1.6, fy=1.6, interpolation=cv2.INTER_AREA)
cv2.imshow('image', img)
cv2.imshow('image1', img1)
cv2.imshow('image2', img2)
cv2.imshow('image3', img3)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
# 翻转
# 图像 0 上下翻转 >0 左右翻转 <0 上下左右翻转
img1 = cv2.flip(img, 0)
img2 = cv2.flip(img, 1)
img3 = cv2.flip(img, -1)
cv2.imshow('image', img)
cv2.imshow('image1', img1)
cv2.imshow('image2', img2)
cv2.imshow('image3', img3)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
# 旋转
img1 = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
cv2.imshow('image', img1)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
h,w,ch = img.shape
print(h,w)
# 平移
# M = np.float32([[1,0,100],[0,1,200]])
# 中心点 旋转角度(逆时针) 缩放
# M = cv2.getRotationMatrix2D((w/2,h/2),15,1.0)
src = np.float32([[200,150],[400,150],[400,500]])
dst = np.float32([[100,200],[300,250],[150,600]])
M = cv2.getAffineTransform(src,dst)
# 修改新图的尺寸dsize(w/2,h/2)
img2 = cv2.warpAffine(img,M,(w,h))
cv2.imshow('img',img)
cv2.imshow('img2',img2)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
src = np.float32([[10,300],[100,300],[20,500],[150,450]])
dst = np.float32([[0,0],[120,0],[0,300],[120,300]])
M = cv2.getPerspectiveTransform(src,dst)
img1 = cv2.warpPerspective(img,M,(300,120))
cv2.imshow('img',img)
cv2.imshow('img1',img1)
cv2.waitKey(0)
图像滤波
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
kernel = np.ones((5,5),np.uint32) / 25
dst = cv2.filter2D(img,-1,kernel)
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
dst = cv2.blur(img,(5,5))
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
dst = cv2.GaussianBlur(img,(5,5),sigmaX=1)
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
dst = cv2.medianBlur(img,5)
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
dst = cv2.bilateralFilter(img,5,20,50)
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('./12.png')
# 索贝尔算子x方向边缘
dst = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)
# 索贝尔算子y方向边缘
dst1 = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)
dst2 = cv2.add(dst,dst1)
cv2.imshow('img',img)
cv2.imshow('dst',dst2)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('./12.png')
# 沙尔算子x方向边缘
dst = cv2.Scharr(img,cv2.CV_64F,1,0)
# 沙尔算子y方向边缘
dst1 = cv2.Scharr(img,cv2.CV_64F,0,1)
dst2 = cv2.add(dst,dst1)
cv2.imshow('img',img)
cv2.imshow('dst',dst2)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('./12.png')
# 拉普拉斯算子边缘,需先进行降噪
dst = cv2.Laplacian(img,cv2.CV_64F,ksize=5)
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
边缘检测
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
# Canny
dst = cv2.Canny(img,100,200)
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey(0)
形态学图像处理
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
img1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,img2 = cv2.threshold(img1,180,255,cv2.THRESH_BINARY)
cv2.imshow('img',img)
cv2.imshow('img1',img1)
cv2.imshow('img2',img2)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('../1.jpg')
img1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst = cv2.adaptiveThreshold(img1,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,3,0)
cv2.imshow('img',img)
cv2.imshow('img1',img1)
cv2.imshow('img2',dst)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('./img.png')
# kernel = np.ones((3,3),np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
# 腐蚀
img1 = cv2.erode(img,kernel,iterations = 1)
# 膨胀
img2 = cv2.dilate(img1,kernel,iterations = 1)
cv2.imshow('img',img)
cv2.imshow('img1',img1)
cv2.imshow('img2',img2)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('./img_1.png')
img3 = cv2.imread('./img_3.png')
img6 = cv2.imread('./img_2.png')
imgd = cv2.imread('./img_4.png')
# kernel = np.ones((3,3),np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(7,7))
# 开运算
img2 = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
# 闭运算
img33 = cv2.morphologyEx(img3, cv2.MORPH_CLOSE, kernel)
# 梯度
img66 = cv2.morphologyEx(img6, cv2.MORPH_GRADIENT, kernel)
# 顶帽
imgdd = cv2.morphologyEx(imgd, cv2.MORPH_TOPHAT, cv2.getStructuringElement(cv2.MORPH_RECT,(19,19)))
# 黑帽
imgh = cv2.morphologyEx(img3, cv2.MORPH_BLACKHAT, kernel)
cv2.imshow('img2',img2)
cv2.imshow('img33',img33)
cv2.imshow('img66',img66)
cv2.imshow('imgdd',imgdd)
cv2.imshow('imgh',imgh)
cv2.waitKey(0)
图像轮廓
python
复制代码
import numpy as np
import cv2
img = cv2.imread('./img_5.png')
# 转为单通道
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret,img1 = cv2.threshold(gray,150,255,cv2.THRESH_BINARY)
# 查找轮廓
contours,hierarchy = cv2.findContours(img1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓
cv2.drawContours(img,contours,-1,(0,0,255),1)
# 计算面积
area = cv2.contourArea(contours[0])
print(area)
# 计算周长
len = cv2.arcLength(contours[0],True)
print(len)
cv2.imshow('img', img)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('./img_6.png')
# 转为单通道
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret,img1 = cv2.threshold(gray,150,255,cv2.THRESH_BINARY)
# 查找轮廓
contours,hierarchy = cv2.findContours(img1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓
cv2.drawContours(img,contours,0,(255,0,0),1)
# 多边形逼近
approx = cv2.approxPolyDP(contours[0],20,True)
# 多边形凸包
hull = cv2.convexHull(contours[0])
def drawShape(img, approx):
i = 0
while i < len(approx):
if i == len(approx) - 1:
x, y = approx[i][0]
x1, y1 = approx[0][0]
cv2.line(img, (x, y), (x1, y1), (0, 0, 255), 1)
else:
x, y = approx[i][0]
x1, y1 = approx[i + 1][0]
cv2.line(img, (x, y), (x1, y1), (0, 0, 255), 1)
i = i+1
drawShape(img,approx)
drawShape(img,hull)
cv2.imshow('img', img)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img = cv2.imread('./img_7.png')
# 转为单通道
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化
ret,img1 = cv2.threshold(gray,150,255,cv2.THRESH_BINARY)
# 查找轮廓
contours,hierarchy = cv2.findContours(img1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# 最小外接矩形
r = cv2.minAreaRect(contours[1])
box = cv2.boxPoints(r)
box = np.int8(box)
cv2.drawContours(img,[box],0,(0,0,255),2)
# 最大外接矩形
x,y,w,h = cv2.boundingRect(contours[1])
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow("img",img)
cv2.waitKey(0)
特征点检测
- 哈里斯角点检测
- Shi-Tomasi 角点检测
- SIFT
- SURF
- ORB
python
复制代码
import numpy as np
import cv2
img = cv2.imread('./12.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# dst = cv2.cornerHarris(gray, 2, 3, 0.04)
# img[dst>0.01*dst.max()] = [0,0,255]
# corners = cv2.goodFeaturesToTrack(gray, 1000, 0.01, 10)
# corners = np.int8(corners)
# for i in corners:
# x,y = i.ravel()
# cv2.circle(img,(x,y),3,(255,0,0),-1)
# sift = cv2.SIFT.create()
# kp,des = sift.detectAndCompute(gray, None)
# cv2.drawKeypoints(gray,kp,img)
orb = cv2.ORB.create()
kp,des = orb.detectAndCompute(gray,None)
cv2.drawKeypoints(gray,kp,img)
cv2.imshow("img",img)
cv2.waitKey(0)
特征匹配
python
复制代码
import numpy as np
import cv2
img1 = cv2.imread('./img.png')
img2 = cv2.imread('./img_1.png')
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT.create()
kp1,des1 = sift.detectAndCompute(gray1, None)
kp2,des2 = sift.detectAndCompute(gray2, None)
bf = cv2.BFMatcher(cv2.NORM_L1)
matches = bf.match(des1, des2)
img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches, None, flags=2)
cv2.imshow("img",img3)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
img1 = cv2.imread('./img.png')
img2 = cv2.imread('./img_1.png')
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT.create()
kp1,des1 = sift.detectAndCompute(gray1, None)
kp2,des2 = sift.detectAndCompute(gray2, None)
flann = cv2.FlannBasedMatcher( dict(trees=5,algorithm=1),dict(checks=50))
matches = flann.knnMatch(des1, des2,k=2)
good = []
for i,(m,n) in enumerate(matches):
if m.distance < 0.7*n.distance:
good.append(m)
ret = cv2.drawMatchesKnn(img1,kp1,img2,kp2,[good],None)
cv2.imshow("img",ret)
cv2.waitKey(0)
图像分割
python
复制代码
import numpy as np
import cv2
img = cv2.imread("./img.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
kernel = np.ones((3,3),np.uint8)
open1 = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel,iterations=2)
bg = cv2.dilate(open1,kernel,iterations=2)
dist = cv2.distanceTransform(open1,cv2.DIST_L2,5)
re1t,fg = cv2.threshold(dist,0.7*dist.max(),255,cv2.THRESH_BINARY)
fg = np.uint8(fg)
unknow = cv2.subtract(bg,fg)
ret3,marker = cv2.connectedComponents(fg)
marker = marker + 1
marker[unknow == 255] = 0
result = cv2.watershed(img,marker)
img[result == -1] = [0,0,255]
cv2.imshow("img",img)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
class App:
rect = (0,0,0,0)
flag_rect = False
startX = 0
startY = 0
def onmouse(self,event,x,y,flags,param):
print("onmouse")
if event == cv2.EVENT_LBUTTONDOWN:
self.flag_rect = True
self.startX = x
self.startY = y
elif event == cv2.EVENT_LBUTTONUP:
self.flag_rect = False
cv2.rectangle(self.img,(self.startX,self.startY),(x,y),(0,0,255),2)
self.rect = (min(self.startX,x),min(self.startY,y),abs(self.startX - x),abs(self.startY - y))
print("左键抬起")
elif event == cv2.EVENT_MOUSEMOVE:
if self.flag_rect:
self.img = self.img2.copy()
cv2.rectangle(self.img,(self.startX,self.startY),(x,y),(255,0,0),2)
print("移动")
def run(self):
print("run")
cv2.namedWindow('input')
cv2.setMouseCallback('input',self.onmouse)
self.img = cv2.imread('./img.png')
self.img2 = self.img.copy()
self.mask = np.zeros(self.img.shape[:2],np.uint8)
self.output = np.zeros(self.img.shape,np.uint8)
while 1:
cv2.imshow('input',self.img)
cv2.imshow('output',self.output)
k = cv2.waitKey(100)
if k == 27:
break
if k == ord('g'):
bgmodel = np.zeros((1,65),np.float64)
fgmodel = np.zeros((1,65),np.float64)
cv2.grabCut(self.img2,self.mask,self.rect,bgmodel,fgmodel,1,cv2.GC_INIT_WITH_RECT)
mask2 = np.where((self.mask == 1)|(self.mask == 3),255,0).astype('uint8')
self.output = cv2.bitwise_and(self.img2,self.img2,mask=mask2)
App().run()
python
复制代码
import numpy as np
import cv2
img = cv2.imread("./img.png")
img2 = cv2.pyrMeanShiftFiltering(img, 20, 30)
img3 = cv2.Canny(img2, 150, 300)
contours,_ = cv2.findContours(img3, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0,0,255), 2)
cv2.imshow("img", img)
cv2.imshow("img2", img2)
cv2.imshow("img3", img3)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
cap = cv2.VideoCapture('../2.mp4')
mog = cv2.createBackgroundSubtractorMOG2()
while cap.isOpened():
ret, frame = cap.read()
fgmask = mog.apply(frame)
cv2.imshow('frame', fgmask)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
图像修复
python
复制代码
import numpy as np
import cv2
img = cv2.imread("./img_1.png")
mask = cv2.imread("./img_2.png",0)
mask = cv2.resize(mask,(img.shape[1],img.shape[0]))
print(img.shape)
print(mask.shape)
dst = cv2.inpaint(img,mask,3,cv2.INPAINT_TELEA)
cv2.imshow("img",dst)
cv2.waitKey(0)
哈尔级联法
人脸识别
python
复制代码
import numpy as np
import cv2
# 创建haar级联器
facer = cv2.CascadeClassifier(
'D:\SoftWare\py\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml'
)
img = cv2.imread('./img_3.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = facer.detectMultiScale(gray,1.1,5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow('img', img)
cv2.waitKey(0)
python
复制代码
import numpy as np
import cv2
# 创建haar级联器
facer = cv2.CascadeClassifier(
'D:\SoftWare\py\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml'
)
img = cv2.VideoCapture(0)
while True:
ret,frame = img.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = facer.detectMultiScale(gray,1.1,5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow('img', frame)
k = cv2.waitKey(10)
if k == 27:
break
img.release()
cv2.destroyAllWindows()
python
复制代码
import numpy as np
import cv2
# 眼
facer = cv2.CascadeClassifier(
'D:\SoftWare\py\Lib\site-packages\cv2\data\haarcascade_eye.xml'
)
img = cv2.imread('./img_3.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = facer.detectMultiScale(gray,1.1,5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow('img', img)
cv2.waitKey(0)