测试图片自取区
cat.jpg dog.jpg
#首先导入需要的包
import cv2
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
#将图片数据读取到img中,可以看到一张彩色图片是三层的矩阵块
img=cv2.imread('cat.jpg')
print(img)
array([[[127, 152, 196],
[122, 147, 191],
[120, 145, 189],
...,
[113, 137, 183],
[115, 139, 185],
[116, 140, 186]],
[[124, 149, 193],
[120, 145, 189],
[118, 143, 187],
...,
[112, 136, 182],
[114, 138, 184],
[115, 139, 185]],
[[122, 147, 191],
[118, 143, 187],
[116, 141, 185],
...,
[111, 135, 181],
[113, 137, 183],
[114, 138, 184]],
...,
[[177, 200, 232],
[177, 200, 232],
[177, 200, 232],
...,
[186, 207, 235],
[190, 211, 239],
[193, 214, 242]],
[[180, 203, 235],
[180, 203, 235],
[180, 203, 235],
...,
[188, 208, 239],
[191, 211, 242],
[193, 213, 244]],
[[183, 206, 238],
[183, 206, 238],
[183, 206, 238],
...,
[192, 212, 243],
[192, 212, 243],
[193, 213, 244]]], dtype=uint8)
#图像的显示,也可以建立多个窗口
cv2.imshow('image',img)
#等待时间,毫秒级,0按或者任意键终止
cv2.waitKey(0)
cv2.destroyAllWindows()
#封装一个图像显示的方法,将名称和数据放入即可
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#查看图片数据的形状
img.shape
#三层的彩色图片
#(189, 284, 3)
#读取该图片的灰度值
#将图片转化为了灰度图在赋值给img
img=cv2.imread('cat.jpg',cv2.IMREAD_GRAYSCALE)
img
array([[162, 157, 155, ..., 148, 150, 151],
[159, 155, 153, ..., 147, 149, 150],
[157, 153, 151, ..., 146, 148, 149],
...,
[207, 207, 207, ..., 213, 217, 220],
[210, 210, 210, ..., 215, 218, 220],
[213, 213, 213, ..., 219, 219, 220]], dtype=uint8)
#查看一下灰度图的形状
img.shape
#(189, 284)
#显示灰度图
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#保存
#保存成mycat.jpg
cv2.imwrite('mycat.jpg',img)
#True
#查看图片大小
img.size
#导包
import cv2
import matplotlib.pyplot as plt
import numpy as np
#将视频数据读取
vc=cv2.VideoCapture('jiujiu.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(30) & 0xFF==27:
break
vc.release()
cv2.destroyAllWindows()
#导包
import cv2
#封装图像输出函数
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#截取部分图像数据
img=cv2.imread('cat.jpg')
cat=img[0:50,0:50]
cv_show('cat',cat)
#用b,g,r来分割图像的三层
b,g,r=cv2.split(img)
#输出b
print(b)
array([[127, 122, 120, ..., 113, 115, 116],
[124, 120, 118, ..., 112, 114, 115],
[122, 118, 116, ..., 111, 113, 114],
...,
[177, 177, 177, ..., 186, 190, 193],
[180, 180, 180, ..., 188, 191, 193],
[183, 183, 183, ..., 192, 192, 193]], dtype=uint8)
b.shape
#(189, 284)
#将三通道合并
img=cv2.merge((b,g,r))
img.shape
#(189, 284, 3)
#只保留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[:,:,0]=0
cur_img[:,:,0]=0
cv_show('B',cur_img)
#导包
import cv2
import matplotlib.pyplot as plt
#提取图像
img=cv2.imread('cat.jpg')
top_size,bottom_size,left_size,right_size=(50,50,50,50)
#BORDER_REPLICATE:复制法,也就是复制最边缘像素
replicate=cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REPLICATE)
#BORDER_REFLECT:反射法,对图像中的像素在两边进行复制
reflect=cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,cv2.BORDER_REFLECT)
#BORDER_REFLECT_101:反射法,以最边缘的像素为轴,对称
reflect101=cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,cv2.BORDER_REFLECT_101)
#BORDER_WRAP:外包装法
warp=cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,cv2.BORDER_WRAP)
#BORDER_CONSTANT:常量法,常数值填充
constant=cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,cv2.BORDER_CONSTANT,value=0)
#将填充过的图像一并输出,对比
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('REFLECT101')
plt.subplot(235),plt.imshow(warp,'gray'),plt.title('WARP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()
#导包
import cv2
import matplotlib.pyplot as plt
#提取图像数据
img_cat=cv2.imread('cat.jpg')
img_dog=cv2.imread('dog.jpg')
#把img_cat数据加10后赋值给img_cat2
img_cat2=img_cat+10
img_cat[:5,:,0]
array([[127, 122, 120, ..., 113, 115, 116],
[124, 120, 118, ..., 112, 114, 115],
[122, 118, 116, ..., 111, 113, 114],
[122, 118, 116, ..., 111, 112, 114],
[124, 119, 117, ..., 111, 112, 114]], dtype=uint8)
img_cat2[:5,:,0]
array([[137, 132, 130, ..., 123, 125, 126],
[134, 130, 128, ..., 122, 124, 125],
[132, 128, 126, ..., 121, 123, 124],
[132, 128, 126, ..., 121, 122, 124],
[134, 129, 127, ..., 121, 122, 124]], dtype=uint8)
#图像直接相加相当于%256
(img_cat+img_cat2)[:5,:,0]
array([[ 8, 254, 250, ..., 236, 240, 242],
[ 2, 250, 246, ..., 234, 238, 240],
[254, 246, 242, ..., 232, 236, 238],
[254, 246, 242, ..., 232, 234, 238],
[ 2, 248, 244, ..., 232, 234, 238]], dtype=uint8)
#用函数相加封顶255
cv2.add(img_cat,img_cat2)[:5,:,0]
array([[255, 254, 250, ..., 236, 240, 242],
[255, 250, 246, ..., 234, 238, 240],
[254, 246, 242, ..., 232, 236, 238],
[254, 246, 242, ..., 232, 234, 238],
[255, 248, 244, ..., 232, 234, 238]], dtype=uint8)
图像融合的条件是两张图象的形状必须相同。
img_cat.shape
#(189, 284, 3)
img_dog.shape
#(195, 195, 3)
#将img_cat转换为195, 195, 3的格式
img_cat=cv2.resize(img_cat,(195,195))
img_cat.shape
#(195, 195, 3)
#融合
res=cv2.addWeighted(img_cat,0.4,img_dog,0.6,0)
plt.imshow(res)
#把目标值设置为零,进行图片像素缩放
res=cv2.resize(img_dog,(0,0),fx=4,fy=4)
plt.imshow(res)