基于Python的OpenCV 学习
第一章、图像的基本操作
00_思维导图
01_cv2.imread
import cv2
image1 = cv2.imread('cat.jpg')
print(image1)
02_cv2.imshow_cv2.waitKey
import cv2
image = cv2.imread('cat.jpg')
cv2.imshow('HelloWorld',image)
cv2.waitKey(0)
03_cv2.destroyAllWindows
import cv2
image1 = cv2.imread('cat.jpg')
image2 = cv2.imread('clahe.jpg')
cv2.imshow('Img1',image1)
cv2.imshow('Im2',image2)
cv2.waitKey(10000)
cv2.destroyAllWindows()
04_img.shape
import cv2
image1 = cv2.imread('cat.jpg')
print(image1.shape)
05_image.size
import cv2
image1 = cv2.imread('cat.jpg')
print(image1.size)
06_(cv2.IMREAD_GRAYSCALE)
import cv2
image1 = cv2.imread('cat.jpg',cv2.IMREAD_GRAYSCALE)
print(image1)
print(image1.shape)
cv2.imshow('Hello',image1)
cv2.waitKey()
cv2.destroyAllWindows()
07_cv2.imwrite
import cv2
image1 = cv2.imread('cat.jpg')
cv2.imwrite('06.jpg',image1)
08_cv2.VideoCapture
import cv2
video1 = cv2.VideoCapture('dpc.mp4')
if video1.isOpened():
open,frame = video1.read()
else:
open = False
cv2.imshow('frame1',frame)
cv2.waitKey()
video1.release()
cv2.destroyAllWindows()
09_cv2.cvtColor
import cv2
video1 = cv2.VideoCapture('dpc.mp4')
open,frame = video1.read()
gray1 = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',gray1)
cv2.waitKey()
cv2.destroyAllWindows()
10_视频播放
import cv2
video1 = cv2.VideoCapture('dpc.mp4')
if video1.isOpened():
open , frame = video1.read()
else:
open = False
while open:
ret , frame = video1.read()
if frame is None:
break
if ret:
cv2.imshow('dpc',frame)
if cv2.waitKey(1) == 27:
break
video1.release()
cv2.destroyAllWindows()
11_改变图片显示区域
import cv2
image1 = cv2.imread('hwc.jpg')
cv2.imshow('hwc',image1[:100,100:200])
cv2.waitKey()
cv2.destroyAllWindows()
12_cv2.split
import cv2
image1 = cv2.imread('hwc.jpg')
b , g , r = cv2.split(image1)
print(b,'\n',g,'\n',r)
13_cv2.merge
import cv2
image1 = cv2.imread('hwc.jpg')
b,g,r = cv2.split(image1)
image2 = cv2.merge((b,g,r))
cv2.imshow('image2',image2)
cv2.waitKey()
cv2.destroyAllWindows()
14_image.copy
import cv2
image1 = cv2.imread('hwc.jpg')
image2 = image1.copy()
image2[:,:,0] = 0
image2[:,:,1] = 0
cv2.imshow('R',image2)
cv2.waitKey()
cv2.destroyAllWindows()
15_cv2.copyMakeBorder
import cv2
import matplotlib.pyplot as plt
image1 = cv2.imread('hwc.jpg')
top,bottom,left,right = (100,100,100,100)
image2 = cv2.copyMakeBorder(image1,top,bottom,left,right,borderType=cv2.BORDER_REPLICATE)
image3 = cv2.copyMakeBorder(image1,top,bottom,left,right,borderType=cv2.BORDER_REFLECT)
image4 = cv2.copyMakeBorder(image1,top,bottom,left,right,borderType=cv2.BORDER_REFLECT_101)
image5 = cv2.copyMakeBorder(image1,top,bottom,left,right,borderType=cv2.BORDER_WRAP)
image6 = cv2.copyMakeBorder(image1,top,bottom,left,right,borderType=cv2.BORDER_CONSTANT,value=0)
plt.subplot(2,3,1),plt.imshow(image1),plt.title('1')
plt.subplot(2,3,2),plt.imshow(image2),plt.title('2')
plt.subplot(2,3,3),plt.imshow(image3),plt.title('3')
plt.subplot(2,3,4),plt.imshow(image4),plt.title('4')
plt.subplot(2,3,5),plt.imshow(image5),plt.title('5')
plt.subplot(2,3,6),plt.imshow(image6),plt.title('6')
plt.show()
cv2.destroyAllWindows()
16
import cv2
image1 = cv2.imread('hwc.jpg')
image1 += 10
cv2.imshow('image1',image1)
cv2.waitKey()
cv2.destroyAllWindows()
17.cv2.add
import cv2
img1 = cv2.imread('hwc.jpg')
img2 = cv2.imread('cat.jpg')
img3 = cv2.add(img1[:200,:200],img2[:200,:200])
cv2.imshow('img3',img3)
cv2.waitKey()
cv2.destroyAllWindows()
18_cv2.resize
import cv2
image1 = cv2.imread('hwc.jpg')
print(image1.shape)
image2 = cv2.resize(image1,(284,330))
print(image2.shape)
cv2.imshow('2',image2)
cv2.waitKey()
cv2.destroyAllWindows()
19_fx_fy
import cv2
image1 = cv2.imread('hwc.jpg')
print(image1.shape)
image2 = cv2.resize(image1,(0,0),fx=2,fy=3)
print(image2.shape)
cv2.imshow('2',image2)
cv2.waitKey()
cv2.destroyAllWindows()
20_cv2.addWeighted
import cv2
img1 = cv2.imread('hwc.jpg')
img2 = cv2.imread('cat.jpg')
img2 = cv2.resize(img2,(330,284))
img3 = cv2.addWeighted(img1,0.5,img2,0.5,0)
cv2.imshow('3',img3)
cv2.waitKey()
cv2.destroyAllWindows()