1.图像的读取
- cv2.IMREAD_COLOR:彩色图像
- cv2.IMREAD_GRAYSCALE:灰度图像
1.1 图像的读取(默认为彩色图像)
import cv2 #opencv读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np
# 默认读取彩色图像
img1 = cv2.imread('cat.jpg')
# 显示图像的数据结构
img1.shape
(414, 500, 3)
1.2 图像的显示
用cv2的API
#定义显示图片的函数
def cv2_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0) #等待关闭时间,0代表按下任意键关闭
cv2.destroyAllWindows()
#打开新的窗口显示(显示的顺序为GBR)
cat = cv2_show('cat',img1)
原图.PNG
matplotlib读取
# 直接在jupyter中显示(显示的顺序为RGB)
plt.imshow(img1)
output_8_1.png
# 读取灰度图像
img2=cv2.imread('cat.jpg',cv2.IMREAD_GRAYSCALE)
# 灰度图像没有三通道
img2.shape
(414, 500)
# 显示读取的灰度图像
cat_gray=cv2_show('cat_gray',img2)
灰度图.PNG
1.3 图像的属性
# 图像的大小
img2.size
207000
# 图像的数据类型为无符号整型
img2.dtype
dtype('uint8')
1.4 图像的保存
# 图像的保存,保存成功为True
cv2.imwrite('mycat.png',img2)
True
2.图像的基本操作
2.1 截取部分图像数据
img = cv2.imread('cat.jpg')
img_slice = img[0:200,0:300]
cv2_show('img_slice',img_slice)
图片的截取.PNG
2.2 颜色通道提取
b,g,r = cv2.split(img)
b
array([[142, 146, 151, ..., 156, 155, 154],
[108, 112, 118, ..., 155, 154, 153],
[108, 110, 118, ..., 156, 155, 154],
...,
[162, 157, 142, ..., 181, 170, 149],
[140, 147, 139, ..., 169, 125, 106],
[154, 154, 121, ..., 183, 128, 127]], dtype=uint8)
b.shape
(414, 500)
img=cv2.merge((b,g,r))
img.shape
(414, 500, 3)
# 只保留R
cur_img = img.copy()
cur_img[:,:,0] = 0
cur_img[:,:,1] = 0
cv2_show('R',cur_img)
提取R通道.PNG
2.3 边界填充
- 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 = (50,50,50,50)
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()
output_26_0.png
2.4 数值计算
img_cat=cv2.imread('cat.jpg')
img_dog=cv2.imread('dog.jpg')
img_cat2= img_cat +10
img_cat[:5,:,0]
array([[142, 146, 151, ..., 156, 155, 154],
[108, 112, 118, ..., 155, 154, 153],
[108, 110, 118, ..., 156, 155, 154],
[139, 141, 148, ..., 156, 155, 154],
[153, 156, 163, ..., 160, 159, 158]], dtype=uint8)
img_cat2[:5,:,0]
array([[152, 156, 161, ..., 166, 165, 164],
[118, 122, 128, ..., 165, 164, 163],
[118, 120, 128, ..., 166, 165, 164],
[149, 151, 158, ..., 166, 165, 164],
[163, 166, 173, ..., 170, 169, 168]], dtype=uint8)
#相当于% 256
(img_cat + img_cat2)[:5,:,0]
array([[ 38, 46, 56, ..., 66, 64, 62],
[226, 234, 246, ..., 64, 62, 60],
[226, 230, 246, ..., 66, 64, 62],
[ 32, 36, 50, ..., 66, 64, 62],
[ 60, 66, 80, ..., 74, 72, 70]], dtype=uint8)
#直接相加,大于255则为255
cv2.add(img_cat,img_cat2)[:5,:,0]
array([[255, 255, 255, ..., 255, 255, 255],
[226, 234, 246, ..., 255, 255, 255],
[226, 230, 246, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255]], dtype=uint8)
2.5 图像融合
# 维度不同,不能融合
img_cat + img_dog
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-22-9ef138040847> in <module>
1 # 维度不同,不能融合
----> 2 img_cat + img_dog
ValueError: operands could not be broadcast together with shapes (414,500,3) (429,499,3)
img_cat.shape
(414, 500, 3)
# 将两者维度转化为相同的
img_dog = cv2.resize(img_dog, (500, 414))
img_dog.shape
(414, 500, 3)
# 给两者赋完权重融合
res = cv2.addWeighted(img_cat, 0.5, img_dog, 0.5, 0)
plt.imshow(res)
output_37_1.png
2.6 图像拉伸缩放操作
res = cv2.resize(img, (0, 0), fx=4, fy=4)
plt.imshow(res)
output_39_1.png
res = cv2.resize(img, (0, 0), fx=1, fy=3)
plt.imshow(res)
output_40_1.png
网友评论