图像处理-基础函数记录
其中,包括数值计算,图像融合,视频、图像播放,图像裁剪等等
代码:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plot
# 定义函数读取图像并显示其信息
def a(filepath,name):
img=cv.imread(filepath)
cv.imshow(name,img)
cv.waitKey(0)
cv.destroyAllWindows()
print(img.shape,img.dtype,img.size)
img1=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
print(img1.shape,img1.dtype,img1.size)
a(r"F:\z\2\u-net-vgg-bmp\0501.bmp",'11')
# 视频读取
def vidle(filepath):
vd=cv.VideoCapture(filepath)
if vd.isOpened():
ret,frame=vd.read()
print(ret,frame)
else:
print("视频未打开!!!!")
while ret :
ret,frame=vd.read()
if frame is None:
break
if ret is True:
gray=cv.cvtColor(frame,cv.COLOR_BGR2GRAY)
cv.imshow('result',gray)
if cv.waitKey(60) & 0xFF=="q":
break
vd.release()
cv.destroyAllWindows()
vidle(r"C:\Users\Windows\Desktop\mda-pc9js2k24f5fd31f.mp4")
#截取图像中部分图像数据
def ROI(filepath):
img=cv.imread(filepath)
img2=img[100:789,154:987]
cv.imshow('result',img2)
cv.waitKey(0)
ROI(r'F:\z\2\u-net-vgg-bmp\0501.bmp')
#图像颜色通道提取
def abb(filepath):
img=cv.imread(filepath)
b,g,r=cv.split(img)#通道拆分
print(b,g,r,b.shape,g.shape,r.shape)
img2=img.copy()
img2[:,:,0]=0#将其他通道数设置为0
img2[:,:,2]=0
cv.imshow('R',img2)#展示r通道的图像
cv.waitKey(0)
img=cv.merge((b,g,r))#通道合并
print(img.shape)
abb(r'F:\z\2\u-net-vgg-bmp\0501.bmp')
#边界填充
def ass(filepath):
img=cv.imread(filepath)
top_size,bottom_size,left_size,right_size=(50,50,50,50)#设置边界扩充大小数据
img1=cv.copyMakeBorder(img, top_size,bottom_size,left_size,right_size,cv.BORDER_REFLECT101)#边界填充
cv.imshow('2',img1)
cv.waitKey(0)
print(img1.shape)
ass(r'F:\z\2\u-net-vgg-bmp\0501.bmp')
#数值计算
def meth(filepath1,filepath2):
img1=cv.imread(filepath1)
img2=cv.imread(filepath2)
img3=img1+img2#图像相同大小才可以相加
print(img3[:5,:,0],img3.shape)
cv.imshow('33',img3)
cv.waitKey(0)
img4=cv.resize(img3,(0,0),fx=0.5,fy=0.5)#(0,0)表示自己设置大小,设置为0时,则用fx,fy倍数关系进行缩放
cv.imshow('44',img4)
cv.waitKey(0)
print(img4.shape)
img5=cv.addWeighted(img1,0.2,img2,0.8,0.3)#土相混合
cv.imshow('55',img5)
cv.waitKey(0)
print(img5.shape)
meth(r'F:\z\2\u-net-vgg-bmp\0501.bmp',r'F:\zhouwang\2\u-net-vgg-bmp\0551.bmp')