OpenCV-图像基本操作 图像截取特定区域、颜色通道提取、边界填充、数值计算、图像融合
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
免费下载资源
·
将图片转换为像素的矩阵
图片
from matplotlib import pyplot as plt 的基本用法简介
import cv2
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
img=cv2.imread('man.jpg')
img
img
array([[[ 2, 2, 2],
[ 2, 2, 2],
[ 2, 2, 2],
...,
[ 20, 20, 6],
[ 19, 19, 5],
[ 27, 27, 13]],
[[ 2, 2, 2],
[ 2, 2, 2],
[ 2, 2, 2],
...,
[ 15, 15, 9],
[ 11, 11, 5],
[ 6, 7, 0]],
[[ 2, 2, 2],
[ 2, 2, 2],
[ 2, 2, 2],
...,
[ 6, 6, 6],
[ 4, 4, 4],
[ 7, 7, 7]],
...,
[[196, 209, 217],
[198, 211, 219],
[201, 214, 222],
...,
[240, 239, 241],
[236, 235, 237],
[239, 238, 240]],
[[196, 209, 217],
[198, 211, 219],
[200, 213, 221],
...,
[244, 243, 245],
[238, 237, 239],
[239, 238, 240]],
[[198, 210, 216],
[199, 211, 217],
[197, 211, 217],
...,
[236, 238, 238],
[233, 235, 235],
[237, 239, 239]]], dtype=uint8)
cv2.imshow('image',image)
cv2.waitKey(0)
cv2.destoryAllWindows()
image.shape
import cv2
image=cv2.imread('man.jpg',cv2.IMREAD_GRAYSCALE)#灰度图像,
image
#保存
cv2.imwrite('huiman.jpg',image)
type(image)
image.size
image.dtype
视频
import cv2
vc = cv2.VideoCapture('scenery.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(10)&0xFF==27:#括号中数字越大,视频播放速度越慢。0xFF==27表示按ESC后退出视频播放
break
vc.release()
cv2.destoryAllWindows()
图像截取特定的区域
这是数组类型, 0-200 指定切片,把想要的区域选择出来
import cv2
def cv_show(name,image):
cv2.imshow(name,image)
cv2.waitKey(0)
cv2.destoryAllWindows()
image = cv2.imread('man.jpg')
man = image[0:200,0:200]
cv_show('man',man)
颜色通道提取
import cv2
def cv_show(name,image):
cv2.imshow(name,image)
cv2.waitKey(0)
cv2.destoryAllWindows()
image = cv2.imread('man.jpg')
man = image[200:630,250:450]
cv_show('man',man)
B,G,R分离
import cv2
image = cv2.imread('man.jpg')
b,g,r=cv2.split(image)
import cv2
import numpy as np
image = cv2.imread('man.jpg')
(B,G,R) = cv2.split(image)
image = cv2.merge([B,G,R])
cv2.imshow('image',image)
cv2.imshow("Red",R)
cv2.imshow("Green",G)
cv2.imshow("Blue",B)
cv2.waitKey(0)
#只保留R
#只保留R
def cv_show(name,image):
cv2.imshow(name,image)
cv2.waitKey(0)
cv2.destoryAllWindows()
cur_img = image.copy()
cur_img[:,:,0] = 0
cur_img[:,:,1] = 0
cv_show('R',cur_img)
#只保留B
def cv_show(name,image):
cv2.imshow(name,image)
cv2.waitKey(0)
cv2.destoryAllWindows()
cur_img = image.copy()
cur_img[:,:,2] = 0
cur_img[:,:,1] = 0
cv_show('B',cur_img)
#只保留G
def cv_show(name,image):
cv2.imshow(name,image)
cv2.waitKey(0)
cv2.destoryAllWindows()
cur_img = image.copy()
cur_img[:,:,0] = 0
cur_img[:,:,2] = 0
cv_show('G',cur_img)
边界填充
import cv2
import numpy as np
img=cv2.imread('man.jpg')
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,borderType=cv2.BORDER_REFLECT)
reflect101=cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REFLECT101)
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=0)#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('REFLECT101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANTL')
plt.show()
# cv2.BORDER_REPLICATE复制法,复制最边缘像素
# cv2.BORDER_REFLECT反射法,对感兴趣的图像中的像素在两边进行入职
# cv2.BORDER_REFLECT101反射法,以最边缘像素为轴,对称 cv2.BORDER_WRAP外包装法
# cv2.BORDER_CONSTANT常良法
数值计算
img_queen = cv2.imread('queen.jpg')
img_man = cv2.imread('man.jpg')
img_man2 =img_man + 10 #在图像中每个像素点+10,
img_man[:5,:,0]
img_man2[:5,:,0]
(img_man + img_man2)[:5,:,0]
cv2.add(img_man,img_man2)[:5,:,0]
图像融合
res =cv2.addWeighted(img_man,0.4,img_queen,0.6,0.4)
plt.imshow(res)
img_man=cv2.resize(img_man,(640,640))
img_man.shape
res = cv2.resize(img,(0,0),fx=3,fy=1)
plt.imshow(res)
res = cv2.resize(img,(0,0),fx=1,fy=3)
plt.imshow(res)
res = cv2.resize(img,(0,0),fx=5,fy=5)
plt.imshow(res)
GitHub 加速计划 / opencv31 / opencv
150
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
96d6395a
Add include chrono gstreamersource.cpp 3 天前
fe649f4a - 4 天前
更多推荐
已为社区贡献2条内容
所有评论(0)