import cv2
import matplotlib.pyplot as plt
import numpy as np
def cvImgShow(name,img):
# 默认是rgb 读彩色图
# img = cv2.imread("img1.png")
# 读取成灰度图
img = cv2.imread("img1.png",cv2.IMREAD_GRAYSCALE)
print(img.shape)
# 图片的现实 也可以创建多个窗口
cv2.imshow('image',img)
# 等待时间 毫秒级 0表示任意键终止
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('img1_1.png',img)
def cvVideoShow(name,video):
vc = cv2.VideoCapture("video1.mp4")
if vc.isOpened():
open,frame = vc.read()
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(10000):
break
vc.release()
cv2.destroyAllWindows()
def cvSplitImgShow(name,img):
img = cv2.imread("img1.png")
splitImg = img[0:500,0:500]
print(splitImg.shape)
# 图片的现实 也可以创建多个窗口
cv2.imshow('image', splitImg)
# 等待时间 毫秒级 0表示任意键终止
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('splitImg_1.png', splitImg)
def cvSplitImgChannelShow(name,img):
img = cv2.imread("img1.png")
# 拆分通道
# b,g,r = cv2.split(img)
#
# 合并通道
# cv2.imshow("RED",R); #显示三通道的值都为R值时d图片
# cv2.imshow("GREEN",G); #显示三通道的值都为G值时d图片
# cv2.imshow("BLUE",B); #显示三通道的值都为B值时d图片
#
# 当调用 imshow(R)时,是把图像的R,G,B三个通道的值都变为R的值,所以图像的颜色三通道值为(R,R,R)
# 同理 imshow(G)和imshow(B)所显示d图像的颜色通道也依次为(G,G,G)和(B,B,B)。
# 而 当三个通道d值相同时,则为灰度图。
#
# img = cv2.merge((b,g,r))
img[:,:,:0] = 0
img[:,:,:1] = 0
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 2个图片融合
def cvImageFusion():
img1 = cv2.imread("img1.png")
cat = cv2.imread("unnamed.jpg")
print(img1.shape)
cat = cv2.resize(cat,(img1.shape[1],img1.shape[0]))
print(cat.shape)
# res = a*x1 + b*x2 + c 最后一个参数就是偏置项
res = cv2.addWeighted(img1,0.8,cat,0.2,0)
cv2.imshow('image', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
# cvImgShow("img1","img1.png")
# cvVideoShow("video","video1.mp4")
# cvSplitImgShow("img1","img1.png")
cvImageFusion()
计算机视觉 opencv 图像基本操作 图像读取 图像保存 图像切分 图像色道切分 图片融合
最新推荐文章于 2022-07-26 10:07:33 发布