赞
踩
cv2.IMRED_COLOR
:彩色图像cv2.IMREAD_GRAYSCALE
: 灰度图像import cv2 import numpy as np path = "test.png" # 测试图像 img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) # 默认是彩色图像,可以使用灰度图像 # numpy生成测试图像 # 图像读取函数 def cv_imshow(name, img): # 图像显示,也可以创建多个窗口 cv2.imshow(name, img) # 等待时间,毫秒级,0表示任意终止 cv2.waitKey(0) cv2.destroyAllWindows() # 保存图像 cv2.imwrite('mytest.png', img) # 保存成功会返回值 # 图像类型 type(img) # numpy.ndarray # 图像像素点 img.size # 图像存储类型 img.dtype # dtype('uint8') # 截取图像--使用索引形式即可 img[0:200, 0:200] # 颜色通道提取 b, g, r = cv2.split(img) # 合并 img = cv2.merge((b, g, r)) # 只保留单通道 cur_img = img.copy() cur_img[:,:,0] = 0 # B 通道置为0 cur_img[:,:.1] = 0 # G 通道置为0
边界填充
# 边界填充
top_size, bottom_size, left_size, right_size = (200,200,200,200)
replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REPLICATE) # 复制法,复制边缘像素
reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REFLECT) # 反射法,对感兴趣的图像中的像素两边进行复制
reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REFLECT_101) # 反射法,以最边缘像素为轴 gfedcb|abcdefgh|gfedcba
warp = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_WRAP) # 外包装法 cdefgh|abcdefgh|abcdefg
constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_CONSTANT, value=0) # 常量法,常数值填充
plt.subplot(231), plt.imshow(img, 'gray'), plt.title('ORIGINAL')
plt.subplot(232), plt.imshow(replicate, 'gray'), plt.title('replicate')
plt.subplot(233), plt.imshow(reflect, 'gray'), plt.title('reflect')
plt.subplot(234), plt.imshow(reflect101, 'gray'), plt.title('reflect101')
plt.subplot(235), plt.imshow(warp, 'gray'), plt.title('warp')
plt.subplot(236), plt.imshow(constant, 'gray'), plt.title('constant')
数值计算
img+10 # 图像每个位置+10
img+img # 相当于(img+img)%256 相同shape对应位置相加
cv2.add(img, img) # 相当于img+img
图像融合
# 两种图像img1(640, 640, 3) img2(320, 320, 3)
cv2.resize(img1, (320, 320)) # resize图像
# cv2.resize(img1, (0, 0), fx=0.5, fy=0.5) # 对图像x,y变成原来的0.5
cv2.addWeighted(img1, 0.4, img2, 0.5, 0) # 0.4*img1 + 0.5*img2 + 0
图像阈值
ret, dst = cv2.threshold(src, thresh, maxval, type)
ret, thresh1 = cv2.threshold(img1, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img1, 127, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img1, 127, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img1, 127, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img1, 127, 255, cv2.THRESH_TOZERO_INV)
titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [img1, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
plt.subplot(2, 3, i+1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
图像滤波
# 均值滤波 # 简单的平均卷积 blur = cv2.blur(img, (3,3)) # 方框滤波 # 基本和均值一样,可以选择归一化 box = cv2.boxFilter(img1, -1, (3,3), normalize=True) # noremalize=True 卷积除以个数,noremal=False 只是卷积求和 # 高斯滤波 # 同一中心点根据距离不同参数的比例不同 gaussian = cv2.GaussianBlur(img1, (5,5), 1) # 中值滤波 # 相当于用中值代替 median = cv2.medianBlur(img, 5) res = np.hstack((blur, gaussian, median)) cv_imshow('res', res)
形态学-腐蚀操作
kernel = np.ones((5,5), np.uint8)
erosion = cv2.erode(img, kernel, iterations=1) # iterations 腐蚀次数
形态学-膨胀操作
kernel = np.ones((3, 3), np.uint8)
dige_dilate = cv2.dilate(img, kernel, iterations=1)
开运算与闭运算
# 开:先腐蚀,再膨胀
# 将毛刺去掉
kernel = np.ones((5,5), np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
# 闭: 先膨胀,再腐蚀
# 扩张毛刺
kernel = np.ones((5,5), np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
梯度运算
# 梯度=膨胀-腐蚀
kernel = np.ones((7,7), np.uint8)
gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel) # 原图-腐蚀,默认iteration = 1
礼帽与黑帽
# 礼帽
kernel = np.ones((5,5), np.uint8)
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
# 黑帽
tophat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)
图像梯度-Sobel算子
dst = cv2.Sobel(src, ddepth, dx, dy, ksize)
ddepth
: 图像的深度dx和dy分别表示水平和竖直方向
ksize
是Sobel
算子的大小sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3) # 水平梯度
# 白到黑是正数,黑到白是负数,负数会被截断成0,所以要取绝对值,方法如下:
sobelx = cv2.convertScaleAbs(sobelx)
sobelx = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3) # 垂直梯度
# 白到黑是正数,黑到白是负数,负数会被截断成0,所以要取绝对值,方法如下:
sobelx = cv2.convertScaleAbs(sobelx)
# 分别计算完x, y求和
sobelxy = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
# 可以直接计算,但是不建议,效果不如分开合起来计算的好
sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 1, ksize=3) # 水平垂直梯度
图像梯度-Scharr算子
scharrx = cv2.Scharr(img, cv2.CV_64F, 1, 0)
scharry = cv2.Scharr(img, cv2.CV_64F, 0, 1)
scharrx = cv2.covertScaleAbs(scharrx)
scharry = cv2.covertScaleAbs(scharry)
scharrxy = cv2.addWeighted(scharrx, 0.5, scharry, 0.5, 0)
图像梯度-laplacian算子
laplacian = cv2..Laplaciap(img, cv2.CV_64F)
laplacian = cv2.converScaleAbs(laplacian)
1. 高斯滤波器
H
=
[
0.0924
0.1192
0.0924
0.1192
0.1538
0.1192
0.0924
0.1192
0.0924
]
<
−
−
−
归一化处理
H =
e
=
H
∗
A
=
[
h
11
h
12
h
13
h
21
h
22
h
23
h
31
h
32
h
33
]
∗
[
a
b
c
d
e
f
g
h
i
]
=
s
u
m
(
[
a
×
h
11
b
×
h
12
c
×
h
13
d
×
h
21
e
×
h
22
f
×
h
23
g
×
h
31
h
×
h
32
i
×
h
33
]
)
e = H*A =
2. 梯度和方向(Sobel)
G
=
G
x
2
+
G
y
2
G=\sqrt{G_x^2+G_y^2}
G=Gx2+Gy2
θ
=
a
r
c
t
a
n
(
G
y
G
x
)
\theta=arctan(\frac{G_y}{G_x})
θ=arctan(GxGy)
S
x
=
[
−
1
0
1
−
2
0
2
−
1
0
1
]
S
y
=
[
1
2
1
0
0
0
−
1
−
2
−
1
]
S_x =
G
x
=
S
x
∗
A
=
[
−
1
0
1
−
2
0
2
−
1
0
1
]
∗
[
a
b
c
d
e
f
h
g
i
]
=
s
u
m
(
[
−
a
0
c
−
2
d
0
2
f
−
g
0
i
]
)
G_x = S_x*A=
G
y
=
S
y
∗
A
=
[
1
2
1
0
0
0
−
1
−
2
−
1
]
∗
[
a
b
c
d
e
f
h
g
i
]
=
s
u
m
(
[
a
2
b
c
0
0
0
−
g
−
2
h
−
i
]
)
G_y = S_y*A=
3. 非极大值抑制
4. 双阈值检测
v = cv2.Canny(img, 80, 150) # minval = 80, maxval = 150 值越大对于边缘特征提取越细致,信息点过滤越多
高斯金字塔:向上采样方法(放大)
[
1
4
4
16
]
−
−
>
[
1
0
4
0
0
0
0
0
4
0
16
0
0
0
0
0
]
up = cv2.pyrUp(img)
down = cv2.pyrDown(img)
拉普拉斯金字塔
L
i
=
G
i
−
P
y
r
U
P
(
P
y
r
D
o
w
n
(
G
i
)
)
L_i=G_i-PyrUP(PyrDown(G_i))
Li=Gi−PyrUP(PyrDown(Gi))
img - cv2.pyrUp(cv2.pyrDown(img))
cv2.findContours(img, mode, method)
mode
:罗阔检索模式
method
:轮廓逼近方法
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
draw_img = img.copy() # 不拷贝会在img上留下轮廓痕迹
res = cv2.drawContours(draw_img, contours, -1, (0, 0, 255), 2) # -1表示所有轮廓目标,可以0,1,2...对应各种目标
轮廓特征
cnt = contours[0]
# 面积
cv2.contourArea(cnt)
# 周长,True表示闭合的
cv2.arcLength(cnt, True)
轮廓近似
以直代曲
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) cnt = contours[0] # 1. 根据图像形状进行轮廓拟合 epsilon = 0.1*cv2.arcLength(cnt, True) # 周长作为阈值,两点之前使用直线代替的阈值,阈值越小,线段越短,整体性越差 approx = cv2.approxPolyDP(cnt, epsilon, True) draw_img = img.copy() res = cv2.drawContours(draw_img, [approx], -1, (0,0,255), 2) cv_show('res', res) # 2. 根据边界矩形进行轮廓拟合 x, y, w, h = cv2.boundingRect(cnt) rec_img = cv2.rectangle(draw_img, (x,y), (x+w, y+h), (0, 255, 0), 2) cv_show('img', rec_img) area = cv2.contourArea(cnt) rect_area = w*h extent = float(area) / rect_area print(‘轮廓面积与边界矩形比’, extent) # 3. 外接圆 (x, y), radius = cv2.minEnclosingCircle(cnt) center = (int(x), int(y)) radius = int(radius) rad_img = cv2.circle(draw_img, center, radius, (0, 255, 0), 2) cv_show('img', rad_img)
模板匹配和卷积原理很像,模板在原图像上从原点开始华东,计算模板与(图像被模板覆盖的地方)的差别程度,这个差别程度的计算方法在opencv里有6种,然后将每次计算的结果放在一个矩阵里,作为结果输出。假设原图形是AxB大小,而模板是axb大小,则输出结果的矩阵是(A-a+1)x(B-b+1)
img = cv2.imread('', 0) template = cv2.imread('', 0) h, w = template.shape[:2] methods = ['cv2.TM_CCOEFF', ‘cv2.TM_CCOEFF_NORMED’, ‘cv2.TM_CCORR’, 'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED'] res = cv2.matchTemplate(img, template, 1, cv2.TM_SQDIFF) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) for meth in methods: img2 = img.copy() # 匹配方法的真值 method = eval(meth) print(method) res = cv2.matchTemplate(img, template, 1, method) # 如果是平方差匹配TM_SQDIFF或归一化平方差匹配TM_SQDIFF_NORMED,取最小值 if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: top_left = min_loc else: top_left = max_loc bottom_right = (top_left[0] + w, top_left[1] + h) # 画矩形 cv2.rectangle(img2, top_left, bottom_right, 255, 2) plt.subplot(121), plt.imshow(res, cmap='gray') plt.xticks([]), plt.yticks([]) # 隐藏坐标轴 plt.subplot(122), plt.imshow(img2, cmap='gray') plt.xticks([]), plt.yticks([]) # 隐藏坐标轴 plt.suptitle(meth) plt.show()
匹配多个目标
img = cv2.imread('', 0)
template = cv2.imread('', 0)
h, w = template.shape[:2]
res = cv2.matchTemplate(img, template, 1, cv2.TM_CCOEFF_NORMED)
threshhold = 0.8
# 取匹配程度大于80%的坐标
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
bottom_right = (pt[0] + w, pt[1] + h)
cv2.rectangle(img, pt, bottom_right, (0, 0, 255), 2)
cv_show('img_rgb', img)
cv2.calcHist(images, channels, mask, histSize, ranges)
hist_0 = cv2.calcHist([img], [0], None, [256], [0, 256])
hist_1 = cv2.calcHist([img], [1], None, [256], [0, 256])
hist_2 = cv2.calcHist([img], [2], None, [256], [0, 256])
hist.shape
plt.hist(img_nly.ravel(), 256)
plt.show()
# 或者
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])
mask操作
w, h, _ = img.shape mask = np.zeros(img.shape[:2], np.uint8) mask[w//2-500:w//2+500, h//2-300:h//2+300] = 255 cv_imshow('mask', mask) mask_img = cv2.bitwise_and(img_nly, img_nly, mask=mask) # 与操作 cv_imshow("mask_img", mask_img) hist_full = cv2.calcHist([img], [0], None, [256], [0,256]) hist_mask = cv2.calcHist([img], [0], mask, [256], [0,256]) plt.subplot(221, plt.imshow(img, 'img')) plt.subplot(222, plt.imshow(mask, 'img')) plt.subplot(223, plt.imshow(mask_img, 'img')) plt.subplot(224, plt.plot(hist_full), plt.plot(hist_mask)) plt.xlim([0,256]) plt.show()
直方图均衡化
针对全图进行均衡化
# 原图直方图查看 plt.hist(img_nly.ravel(), 256) plt.show() # 单通道直方图均衡化 equ = cv2.equalizeHist(img_nly[:,:,0]) plt.hist(equ.ravel(), 256) plt.show() # 3通道直方图均衡化 B, G, R = cv2.split(img) output_B = cv2.equalizeHist(B) output_G = cv2.equalizeHist(G) output_R = cv2.equalizeHist(R) equ = cv2.merge((output_B, output_G, output_R)) plt.hist(equ.ravel(), 256) plt.show() # 结果对比 res = np.hstack((img, equ)) cv_imshow('res', res)
自适应直方图均衡化
分块均衡化
# 实例化直方图
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
# 对三通道进行直方图均衡化
B, G, R = cv2.split(img)
res_clahe_B = clahe.apply(B)
res_clahe_G = clahe.apply(G)
res_clahe_R = clahe.apply(R)
res_clahe = cv2.merge((res_clahe_B, res_clahe_G, res_clahe_R))
res = np.hstack((img_nly, equ, res_clahe))
cv_imshow('img', res)
时域和频域的转换
https://zhuanlan.zhihu.com/p/19763358
傅里叶变换作用
滤波
低通滤波器:只保留低频,会使得图像模糊
高通滤波器:只保留高频,会使得图像细节增强
opencv中主要使用cv2.dft()和cv2.idft(),输入图像需要先转换成np.float32格式。
得到的结果中频率为0的部分会在左上角,通常要转换到中心位置,可以通过shift变换来实现。
cv2.dft()返回的结果是双通道的(实部,虚部),通常还需要转换成图像格式才能展示(0,255)。
img_float32 = np.float32(img[:,:,0])
dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT) # 傅里叶变换
dft_shift = np.fft.fftshift(dft) # 移动
magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0], dft_shift[:,:,1])) # magnitude实部和虚部调整
plt.subplot(121), plt.imshow(img_nly, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.show()
img_float32 = np.float32(img[:,:,0]) dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) rows, cols = img_nly.shape[:2] crow, ccol = int(rows/2), int(cols/2) # 低通滤波 mask = np.zeros((rows, cols, 2), np.uint8) mask[crow-30:crow+30, ccol-30: ccol+30] = 1 # IDFT fshift = dft_shift*mask f_ishift = np.fft.ifftshift(fshift) img_back = cv2.idft(f_ishift) img_back = cv2.magnitude(img_back[:,:,0], img_back[:,:,1]) plt.subplot(121), plt.imshow(img_nly, cmap='gray') plt.title('Input Image'), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(img_back, cmap='gray') plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) plt.show()
cv2.VideoCapture可以捕获摄像头,用数字来控制不同的设备,例如0,1
如果是视频文件,直接指定好路径即可
cv2.COLOR_BGR2RGB
将BGR格式转换成RGB格式
cv2.COLOR_BGR2GRAY
将BGR格式转换成灰度图片
cv2.COLOR_BGR2BGRA
将BGR格式转换成BGR格式,cv2显示正常
cv2.COLOR_BGR2HSV
将BGR格式转换成HSV格式
import cv2 import numpy as np vc = cv2.VideoCapture('test.mp4') # 打开视频 # vc = cv2.VideoCapture(0) # 打开摄像头0 # 判断是否正确读取视频 if vc.isOpened(): open, fram = vc.read() else: open = False # 播放视频 while open: ret, frame = vc.read() if frame is None: break if ret: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('result', gray) if cv2.waitKey(10) & 0xFF == 27: # 27退出键 break vc.release() cv2.destroyAllWindows()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。