基于python的opencv项目实战P2

图片的读取、保存、基本属性的查看、颜色通道提取、边界填充、伸缩;两图片的融合;读取视频
摘要由CSDN通过智能技术生成

02-图像基本操作

在这里插入图片描述p.s. 图像lena的来源

“A Note on Lena” ( 发表于 IEEE TRANSACTIONS ON IMAGE PROCESSING. VOL. 5. NO. 1. JANUARY 1996 )
“I think it is safe to assume that the Lena image became a standard in our “industry” for two reasons. First, the image contains a nice mixture of detail, flat regions, shading, and texture that do a good job of testing various image processing algorithms. It is a good test image! Second, the Lena image is a picture of an attractive woman. It is not surprising that the (mostly male) image processing research community gravitated toward an image that they found attractive. ”

图片的读取:
import cv2 #opencv读取格式RGB
import matplotlib.pyplot as plt #绘图展示
import numpy as np #数值计算工具包

get_ipython().run_line_magic('matplotlib','inline') #直接展示 魔法指令 notebook特有

img = cv2.imread('dog.jpg')
#图像显示,也可以创建多个窗口
cv2.imshow('image',img)


#等待时间毫秒级,2000ms,0表示任意键终止
cv2.waitKey(2000)
cv2.destroyAllWindows()
img.shape #获取图像长宽 h w c(BGR三色图)
img = cv2.imread('dog.jpg',cv2.IMREAD_GRAYSCALE)#cv2.IMREAD_COLOR:彩色图像; cv2.IMREAD_GRAYSCALE:灰度图像
img = cv2.imread('dog.jpg',cv2.IMREAD_GRAYSCALE)
#保存
cv2.imwrite('mydog.png',img)
type(img)#查看底层

img.size#像素值

img.dtype#数据类型
视频的读取:
  • cv2.VideoCapture可以捕获摄像头,用数字控制不同设备
  • 如果是视频文件,直接指定好路径
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(200) & 0xFF == 27:#退出键27
            break
vc.release()
cv2.destroyAllWindows()
#截取部分图像数据
def cv_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey(0)    #等待时间毫秒级,2000ms,0表示任意键终止
    cv2.destroyAllWindows()
#上面已经定义过

img = cv2.imread('dog.jpg')
dog = img[0:200,0:200]
cv_show('dog',dog)
颜色通道的提取
b,g,r = cv2.split(img)

#只保留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)
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) #常数为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') #反射法 dcba|abcd|dcba
plt.subplot(234), plt.imshow(reflect101, 'gray'), plt.title('REFLECT_101') #反射法 bcd|abcd|cba
plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title('WRAP') #反射法 bcd|abcd|abc
plt.subplot(236), plt.imshow(constant, 'gray'), plt.title('CONSTANT')
plt.show()
图像融合
img_cat.shape
img_dog.shape

img_cat = cv2.resize(img_cat,(220,160)) # 重置图像大小,确保图像大小一致
img_cat.shape

res = cv2.addWeighted(img_cat,0.4,img_dog,0.6,0) #0是亮度级提量,偏置量
plt.imshow(res)
图像重置
img_cat = cv2.resize(img_cat,(220,160)) #重置图像大小

res = cv2.resize(img,(0,0), fx = 1.5, fy = 1)
plt.imshow(res)#图像伸缩
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值