数据读取-图像
  • cv2.IMREAD_COLOR:彩色图像
  • cv2.IMREAD_GRAYSCALE:灰度图像
import cv2 #opencv读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np 
%matplotlib inline 

img=cv2.imread('data/cat.jpg')
img

#图像的显示,也可以创建多个窗口
cv2.imshow('image',img) 
#等待时间,毫秒级,0表示任意键终止
cv2.waitKey(0) 
cv2.destroyAllWindows()
def cv_show(name,img):
    cv2.imshow(name,img) 
    cv2.waitKey(0) 
    cv2.destroyAllWindows()
    

image-20200509122050266

#灰度
img=cv2.imread('data/cat.jpg',cv2.IMREAD_GRAYSCALE)
img
#图像的显示,也可以创建多个窗口
cv2.imshow('image',img) 
# 等待时间,毫秒级,0表示任意键终止
cv2.waitKey(10000) 
cv2.destroyAllWindows()

image-20200509130325514

数据读取-视频
  • cv2.VideoCapture可以捕获摄像头,用数字来控制不同的设备,例如0,1。
  • 如果是视频文件,直接指定好路径即可。
#vc = cv2.VideoCapture('data/test.mp4')
vc = cv2.VideoCapture(0)

检查是否打开正确

if vc.isOpened(): 
	oepn, frame = vc.read() # open表示是否打开图片 frame表示读取到的每一帧图片
    
else:
	open = False
while open:
    ret, frame = vc.read()
    print(ret,":",frame)
    if frame is None:
        break
    if ret == True:
        gray = cv2.cvtColor(frame,  cv2.COLOR_BGR2GRAY)
        cv2.imshow('result', gray)
        # 0xFF == 27参考 https://blog.csdn.net/hao5119266/article/details/104173400
        if cv2.waitKey(10) & 0xFF == 27:
            break
vc.release()
cv2.destroyAllWindows()

截取部分图像数据

img=cv2.imread('data/cat.jpg')
cat=img[0:400,0:200] 
cv_show('cat',cat)

#cv2.imshow('image',cat) 
##等待时间,毫秒级,0表示任意键终止
#cv2.waitKey(0) 
#cv2.destroyAllWindows()

image-20200509122136874

颜色通道提取
img=cv2.merge((b,g,r))
img.shape
cv_show('cat',img)

#只保留R

cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,1] = 0
cv_show('R',cur_img)

image-20200509122149882

#只保留G
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,2] = 0
cv_show('G',cur_img)

image-20200509122200950

#只保留B

cur_img = img.copy()
cur_img[:,:,1] = 0
cur_img[:,:,2] = 0
cv_show('B',cur_img)

image-20200509122212526

边界填充

参数

  • BORDER_REPLICATE:复制法,也就是复制最边缘像素。
  • BORDER_REFLECT:反射法,对感兴趣的图像中的像素在两边进行复制例如:fedcba|abcdefgh|hgfedcb
  • BORDER_REFLECT_101:反射法,也就是以最边缘像素为轴,对称,gfedcb|abcdefgh|gfedcba
  • BORDER_WRAP:外包装法cdefgh|abcdefgh|abcdefg
  • BORDER_CONSTANT:常量法,常数值填充。
top_size,bottom_size,left_size,right_size = (150,150,150,150)

replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv2.BORDER_CONSTANT, value=0)
import matplotlib.pyplot as plt
plt.subplot(231), plt.imshow(img, 'gray'), plt.title('ORIGINAL')
plt.subplot(232), plt.imshow(replicate, 'gray'), plt.title('REPLICATE')
plt.subplot(233), plt.imshow(reflect, 'gray'), plt.title('REFLECT')
plt.subplot(234), plt.imshow(reflect101, 'gray'), plt.title('REFLECT_101')
plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title('WRAP')
plt.subplot(236), plt.imshow(constant, 'gray'), plt.title('CONSTANT')

plt.show()

image-20200509122227039

数值计算
img_cat=cv2.imread('data/cat.jpg')
img_dog=cv2.imread('data/dog.jpg')

img_cat2= img_cat +10 
img_cat[:5,:,0]

cv_show('img',img_cat)

image-20200509122238236

img_cat2[:5,:,0]
cv_show('img',img_cat2)

image-20200509130621035

#相当于% 256
(img_cat + img_cat2)[:5,:,0] 

cv2.add(img_cat,img_cat2)[:5,:,0]

image-20200509130643544

图像融合
img_dog = cv2.resize(img_dog, (500, 414))
img_dog.shape
res = cv2.addWeighted(img_cat, 0.5, img_dog, 0.5, 0)
plt.imshow(res)

image-20200509122305683

res = cv2.resize(img, (0, 0), fx=4, fy=4)
plt.imshow(res)

image-20200509130732605

res = cv2.resize(img, (0, 0), fx=1, fy=3)
plt.imshow(res)

image-20200509130741181