图像显示
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destoryAllWindows()
cv2.imwrite('iamgeName.png',img)
img.shape
img.size
img.dtype
数据读取–视频
vc=cv2.VideCapture('test.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(10)&0xFF==27:
break
vc.release()
cv2.destoryAllWindows()
截取部分图像数据
img=cv2.imread('image.jpg')
cat=img[0:200,0:200]
cv.show('cat',cat)
颜色通道提取
b,g,r=cv2.split(img)
img=cv2.merge((b,g,r))
img.shape
cur_img=img.cpy()
cur_img[:,:,0]=0
cur_img[:,:,1]=0
cv_show('R',cur_img)
cur_img=img.cpy()
cur_img[:,:,0]=0
cur_img[:,:,2]=0
cv_show('G',cur_img)
cur_img=img.cpy()
cur_img[:,:,1]=0
cur_img[:,:,2]=0
cv_show('B',cur_img)
边界填充
top_size,botm_size,left_size,right_size=(50,50,50,50)
replicate=cv2.copyMakeBorder(img,top_size,botm_size,left_size,right_size,borderType=cv2.BORDER_REPLICATE)
reflect=cv2.copyMakeBorder(img,top_size,botm_size,left_size,right_size,cv2.BORDER_REFLECT)
reflect01=cv2.copyMakeBorder(img,top_size,botm_size,left_size,right_size,cv2.BORDER_REFLECT_101)
wrap=cv2.copyMakeBorder(img,top_size,botm_size,left_size,right_size,cv2.BORDER_WRAP)
constant=cv2.copyMakeBorder(img,top_size,botm_size,left_size,right_size,cv2.BORDER_CONSTANT,value=0)
数值计算
img_a=cv2.read('a.jpg')
img_b=cv2.read('b.jpg')
img_a2=img_a+10
(img_a+imga2)[:5,:,0]
CV2.add(img_a,img_a2)[:5,:,0]
图像融合
cv2.resize(img_a,size(img_b))
res=cv2.addWeighted(img_a,0.4,img_b,0.6,0)
plt.imhow(res)
形态学-腐蚀操作
img=cv2.imread('test01.jpg')
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destoryAllWindows()
kernel=np.ones((5,5),np.unit8)
erosion=cv2.erode(img,kernel,iteration=1)
cv2.imshow('erosion',erosion)
cv2.waitKey(0)
cv2.destoryAllWindows()
膨胀操作
img=cv2.imread('test01.jpg')
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destoryAllWindows()
kernel=np.ones((3,3),np.unit8)
dige_dilate=cv2.dilate(img,kernel,iteration=1)
cv2.imshow('dilate',dige_dilate)
cv2.waitKey(0)
cv2.destoryAllWindows()
开运算与闭运算
img=cv2.imread('test01.jpg')
kernel=np.ones((3,3),np.unit8)
opening=cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)
cv2.imshow('opening',opening)
cv2.waitKey(0)
cv2.destoryAllWindows()
img=cv2.imread('test01.jpg')
kernel=np.ones((3,3),np.unit8)
closing=cv2.morphologyEx(img,cv2.MORPH_close,kernel)
cv2.imshow('closing',closing)
cv2.waitKey(0)
cv2.destoryAllWindows()
梯度运算
img=cv2.imread('test01.jpg')
kernel=np.ones((3,3),np.unit8)
dige_dilate=cv2.dilate(img,kernel,iteration=5)
erosion=cv2.erode(img,kernel,iteration=5)
res=np.hstack((dige_dilate,erosion))
cv2.imshow('res',res)
cv2.waitKey(0)
cv2.destoryAllWindows()
gradient=cv2.morphologyEx(img,cv2.MORPH_gradient,kernel)
cv2.imshow('gradient',gradient)
cv2.waitKey(0)
cv2.destoryAllWindows()
礼帽与黑帽
img=cv2.imread('test01.jpg')
kernel=np.ones((3,3),np.unit8)
tophat=cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)
cv2.imshow('tophat',tophat)
cv2.waitKey(0)
cv2.destoryAllWindows()
img=cv2.imread('test01.jpg')
kernel=np.ones((3,3),np.unit8)
blackhat=cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel)
cv2.imshow('blackhat',blackhat)
cv2.waitKey(0)
cv2.destoryAllWindows()