opencv
图像 RGB
opencv读取的格式是BGR
基本用法
读取图片
import cv2
img = cv2.imread(r'C:\Users\Administrator\Pictures\951c76ee-469e-4084-96db-3d66e5250299.jpg')
print(img)
print(img.size)
print(img.shape)
print('---'*30)
img1 = cv2.imread(r'C:\Users\Administrator\Pictures\951c76ee-469e-4084-96db-3d66e5250299.jpg', flags=cv2.IMREAD_GRAYSCALE)
print(img1)
# cv2.imshow('a', img1)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
print(img1.shape)
print(img1.dtype)
print(img1.size)
读取视频
import cv2
vc = cv2.VideoCapture(r'wfjr.mp4')
# # 检查是否打开正确
# if vc.isOpened():
# open, frame = vc.read() # 正常读取open为true
# else:
# open = False
while open:
ret, frame = vc.read()
if frame is None:
break
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('result', gray)
if cv2.waitKey(10) & 0xFF == 27: # 27为EXC退出
break
vc.release()
cv2.destroyAllWindows()
ROI区域【截图部分区域】
# 截图部分区域
import cv2
img = cv2.imread(r'C:\Users\Administrator\Pictures\951c76ee-469e-4084-96db-3d66e5250299.jpg')
people = img[0:200, 0:200]
cv2.imshow('people', people)
cv2.waitKey(0)
cv2.destroyAllWindows()
颜色通道提取
# 颜色通道提取
import cv2
img = cv2.imread(r'C:\Users\Administrator\Pictures\951c76ee-469e-4084-96db-3d66e5250299.jpg')
b, g, r = cv2.split(img)
print(b)
print(b.shape) # (412, 278)
# 组合
img2 = cv2.merge((b, g, r))
print(img2.shape) # (412, 278, 3)
# 只保留R[其它两个与这个同理] 图片RGB opencv【BGR】
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,1] = 0
cv2.imshow('R', cur_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
边界填充
暂时略
图像融合
暂略