OpenCV系列-1.图像基本操作、ROI、边界填充
AI-摘要
JinzAI GPT
AI初始化中...
介绍自己
生成本文简介
推荐相关文章
前往主页
前往tianli博客
1.图像的基本操作
import cv2
import matplotlib.pyplot as plt
import numpy as np
# 图像的基本操作
if __name__ == '__main__':
img = cv2.imread('aa.jpg') # cv2 默认读取格式为GBR
img_color = cv2.imread('bb.jpg', cv2.IMREAD_COLOR) # 读取彩色图片
img_grey = cv2.imread('cc.jpg', cv2.IMREAD_GRAYSCALE) # 读取灰色图片
print(img)
print(type(img))
print(img.size)
print(img.shape)
cv2.imshow('img', img)
cv2.imshow('img', img_color)
cv2.imshow('img', img_grey)
cv2.waitKey(0) # 等待时间 毫秒,0表示任意按键终止
cv2.imwrite('grey.jpg', img_grey) # 图片保存
cv2.destroyAllWindows()
2.视频读取与处理
import cv2
import matplotlib.pyplot as plt
import numpy as np
# 视频读取与处理
if __name__ == '__main__':
vc = cv2.VideoCapture('w1.mp4')
if vc.isOpened:
open, frame = vc.read() # 读取视频的第一帧
else:
open = False
print(open)
while open:
ret, frame = vc.read()
if frame is None:
break
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('grey', gray)
if cv2.waitKey(10) & 0XFF == 27:
break
vc.release()
cv2.destroyAllWindows()
3.ROI (region of interest) 感兴趣的区域
import cv2
import matplotlib.pyplot as plt
import numpy as np
# ROI (region of interest) 感兴趣的区域
if __name__ == '__main__':
img = cv2.imread('aa.jpg')
roi = img[0:100, 0:200] # w, h
b, g, r = cv2.split(img) # 提取颜色通道
print(type(b))
print(b)
print(b.size)
print(b.shape)
img = cv2.merge((b, g, r)) # 重新组合
# 只保留B
b_img = img.copy()
b_img[:, :, 1] = 0
b_img[:, :, 2] = 0
# cv2.imshow('b_img', b_img)
# 只保留G
g_img = img.copy()
g_img[:, :, 0] = 0
g_img[:, :, 2] = 0
cv2.imshow('g_img', g_img)
# 只保留R
r_img = img.copy()
r_img[:, :, 0] = 0
r_img[:, :, 1] = 0
cv2.imshow('r_img', r_img)
# cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
4.边界填充
import cv2
import matplotlib.pyplot as plt
import numpy as np
# 边界填充
if __name__ == '__main__':
img = cv2.imread('aa.jpg')
top_size, bottom_size, left_size, right_size = (50, 50, 50, 50)
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)
wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_CONSTANT, value=0)
'''
BORDER_REPLICATE:复制法
<-- -->
BORDER_REFLECT:反射法,对ROI的两侧像素进行复制,例如:fedcba|abcdefgh|hgfedcb
<-- -->
BORDER_REFLECT_101: 反射法,以最边缘像素为轴,例如:gfedcb|abcdefgh|gfedcba
BORDER_WRAP:外包装法,cdefgh|abcdefgh|abcdefg
BORDER_CONSTANT:常量发,常数值填充
'''
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('REFLECT_101')
plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title('WRAP')
plt.subplot(236), plt.imshow(constant, 'gray'), plt.title('CONSTANT')
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 Jinz
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果