cv2.imread(‘filepath’,flags):读入图像。
alpha通道:以后再说
上代码
import cv2 #opencv读取的格式是BGR(不是RGB格式)
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
img = cv2.imread('cat.jpg') #读取图片信息
img
array([[[184, 196, 200],
[184, 196, 200],
[184, 196, 200],
…,
[201, 203, 204],
[201, 203, 204],
[203, 202, 204]],
[[184, 196, 200],
[184, 196, 200],
[184, 196, 200],
…,
[201, 203, 204],
[201, 203, 204],
[204, 203, 205]],
[[185, 196, 200],
[185, 196, 200],
[185, 196, 200],
…,
[202, 204, 205],
[202, 204, 205],
[204, 203, 205]],
…,
[[203, 205, 205],
[203, 205, 205],
[203, 205, 205],
…,
[226, 225, 227],
[226, 225, 227],
[225, 224, 226]],
[[204, 206, 206],
[204, 206, 206],
[204, 206, 206],
…,
[226, 225, 227],
[226, 225, 227],
[225, 224, 226]],
[[201, 203, 203],
[202, 204, 204],
[202, 204, 204],
…,
[228, 226, 226],
[228, 226, 226],
[229, 226, 228]]], dtype=uint8)
代码举例
#图像的显示,也可以创建多个窗口
cv2.imshow ('image',img) #第一个参数是窗口的名字,其次为图像。
# cv2.imshow('image2',img) #创建多个窗口
#等待时间,毫秒级,0表示 任意键 终止
cv2.waitKey(10000)
#删除任何我们建立的窗口
cv2.destroyAllWindows()
# cv2.destroyWindow('image') #删除特定的窗口,在括号内输入想删除的窗口名
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
想代码整洁,就设个函数呗,框起来就好啦
#执行上述三个函数的函数窗口
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img.shape
(225, 225, 3)
上面讲过cv2.imread的flags,那cv2.IMREAD_GRAYSCALE就可以读取灰度图像了,既然没有RGB,所以就二维了呗
#灰度图的读取做法
img = cv2.imread('cat.jpg',cv2.IMREAD_GRAYSCALE)
img
array([[196, 196, 196, …, 203, 203, 203],
[196, 196, 196, …, 203, 203, 204],
[196, 196, 196, …, 204, 204, 204],
…,
[205, 205, 205, …, 226, 226, 225],
[206, 206, 206, …, 226, 226, 225],
[203, 204, 204, …, 226, 226, 227]], dtype=uint8)
img.shape
(225, 225)
#图像的显示,也可以创建多个窗口
cv2.imshow ('image',img)
#等待时间,毫秒级,0表示任意键终止
cv2.waitKey(10000)
cv2.destroyAllWindows()
cv2.imwrite("mycat.png",img)
True
type(img) #ndarray格式
numpy.ndarray
img.size #size:像素点个数
50625
img.dtype #数据类型
dtype(‘uint8’)
img = cv2.imread('cat.jpg')
plt.imshow(img,cmap = 'gray',interpolation='bicubic')
plt.xticks([]),plt.yticks([])
plt.show()
img_2 = img[:,:,[2,1,0]]
plt.imshow(img_2)
视频由图象组成,成为帧,想必都知道。
运行一下,不要被自己的大脸吓到哦(不是在说我自己
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# import numpy as np
# import cv2
# cap = cv2.VideoCapture(0)
# # Define the codec and create VideoWriter object
# fourcc = cv2.cv.FOURCC(*'XVID')
# out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
# while(cap.isOpened()):
# ret, frame = cap.read()
# if ret==True:
# frame = cv2.flip(frame,0)
# # write the flipped frame
# out.write(frame)
# cv2.imshow('frame',frame)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
# else:
# break
# # Release everything if job is finished
# cap.release()
# out.release()
# cv2.destroyAllWindows()
vc = cv2.VideoCapture('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(1) & 0xFF == 27: #waitkey数值越大,播放速度越慢
break
vc.release()
cv2.destroyAllWindows()
可自己截取图片的部分
img = cv2.imread('cat.jpg')
cat = img[0:200,0:20]
cv_show('cat',cat)
b,g,r = cv2.split(img)
b
array([[184, 184, 184, …, 201, 201, 203],
[184, 184, 184, …, 201, 201, 204],
[185, 185, 185, …, 202, 202, 204],
…,
[203, 203, 203, …, 226, 226, 225],
[204, 204, 204, …, 226, 226, 225],
[201, 202, 202, …, 228, 228, 229]], dtype=uint8)
r.shape
(225, 225)
img = cv2.merge((b,g,r))
img.shape
(225, 225, 3)
R:G:B分别对应0:1:2
将其余两项设置为零即可
#只保留R通道
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,1] = 0
cv_show('R',cur_img)
#只保留G通道
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,2] = 0
cv_show('G',cur_img)
#只保留B通道
cur_img = img.copy()
cur_img[:,:,1] = 0
cur_img[:,:,2] = 0
cv_show('B',cur_img)
看代码注释,有讲解
top_size, bottom_size, left_size, right_size = (50,50,50,50) #上下左右填充值
#复制法,复制最边缘像素
replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType = cv2.BORDER_REPLICATE)
#反射法,对感兴趣的图像中的像素在两边进行赋值。例如: fedcba|abcdefgh|hgfedc
reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType = cv2.BORDER_REFLECT)
#反射法,以最边缘像素为轴,对称。例如:gfedcb|abcdefgh|gfedcba
reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType = cv2.BORDER_REFLECT_101)
#外包装法。例如:bcdefh|abcdefgh|abcdefg
wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType = cv2.BORDER_WRAP)
#常量法,常数值填充
constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType = cv2.BORDER_CONSTANT, value = 700)#需设置value值,选择常数填充
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()
img_cat = cv2.imread('cat.jpg')
img_dog = cv2.imread('dog.jpg')
img_cat2 = img_cat + 10 #所有像素值加10
img_cat[:5,:,0]
array([[184, 184, 184, …, 201, 201, 203],
[184, 184, 184, …, 201, 201, 204],
[185, 185, 185, …, 202, 202, 204],
[185, 185, 185, …, 202, 202, 205],
[185, 185, 185, …, 203, 203, 206]], dtype=uint8)
img_cat2[:5,:,0]
array([[194, 194, 194, …, 211, 211, 213],
[194, 194, 194, …, 211, 211, 214],
[195, 195, 195, …, 212, 212, 214],
[195, 195, 195, …, 212, 212, 215],
[195, 195, 195, …, 213, 213, 216]], dtype=uint8)
(img_cat + img_cat2)[:5,:,0] # 和 对256取余(像素值范围为0~255)
array([[122, 122, 122, …, 156, 156, 160],
[122, 122, 122, …, 156, 156, 162],
[124, 124, 124, …, 158, 158, 162],
[124, 124, 124, …, 158, 158, 164],
[124, 124, 124, …, 160, 160, 166]], dtype=uint8)
cv2.add(img_cat,img_cat2)[:5,:,0] # add()函数,越界 取最大值
array([[255, 255, 255, …, 255, 255, 255],
[255, 255, 255, …, 255, 255, 255],
[255, 255, 255, …, 255, 255, 255],
[255, 255, 255, …, 255, 255, 255],
[255, 255, 255, …, 255, 255, 255]], dtype=uint8)
img_cat + img_dog #不可操作,两图像shape值不同,无法相加
ValueError Traceback (most recent call last)
in
----> 1 img_cat + img_dog #不可操作,两图像shape值不同,无法相加
ValueError: operands could not be broadcast together with shapes (225,225,3) (678,1024,3)
img_cat.shape
(225, 225, 3)
img_dog = cv2.resize(img_dog, (225,225))
img_dog.shape
(225, 225, 3)
融合一下
res = cv2.addWeighted(img_cat, 0.7, img_dog, 0.3, 2)
plt.imshow(res)
害 挺 好 看,是吧~
res = cv2.resize(img,(0,0),fx = 1, fy = 3)
plt.imshow(res)
res = cv2.resize(img,(0,0),fx = 3, fy = 1)
plt.imshow(res)
就是这样了,可能还会补充,欢迎讨论。