目录

一、Opencv简介

二、安装Opencv

三、Opencv图像的基本操作


一、Opencv简介

OpenCV是一个基于Apache2.0许可(开源)发行的跨平台计算机视觉和机器学习软件库,可以运行在Linux、Windows、Android和Mac OS操作系统上。它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。OpenCV用C++语言编写,它具有C ++,Python,Java和MATLAB接口,并支持Windows,Linux,Android和Mac OS,OpenCV主要倾向于实时视觉应用,并在可用时利用MMX和SSE指令, 如今也提供对于C#、Ch、Ruby,GO的支持。(百度百科)

二、安装Opencv

首先安装numpy,在cmd中输入命令:

pip install numpy

安装opencv-python,在cmd输入命令:

pip install opencv-python

安装opencv-contrib-python,在cmd输入命令:

pip install opencv-contrib-python

opencv-python和opencv-contrib-python的版本要一致。

三、Opencv图像的基本操作

(1)读取图片

import cv2 as cv
# 图片展示函数
def show(name, img):
    cv.imshow(name, img)
    cv.waitKey(0)

# 读取图片
img = cv.imread(r'../de/1_Handshaking_Handshaking_1_314.jpg',0)    # 0表示灰度图像,不写表示BRG彩色图像
img_1 = cv.imread(r'../de/4_Dancing_Dancing_4_517.jpg')
show("hand", img)
show("dance", img_1)

(2)读取视频

import cv2 as cv
vc = cv.VideoCapture(0)  # 读取视频,0表示读取摄像头视频,或 “视频文件地址”
if vc.isOpened():       # 判断是否读取成功
    open, frame = vc.read()   # open返回True or False,frame返回帧
else:
    open = False

while(open):
    ret, frame = vc.read()
    if frame is None:      # 读到最后一帧跳出循环
        break
    if ret == True:

        cv.imshow("man", frame)
        if cv.waitKey(100) & 0xFF == 27:   # 等待100毫秒读取下一帧或按ESC退出
            break

(3)图片截取ROI

import cv2 as cv

# 图片展示函数
def show(name, img):
    cv.imshow(name, img)
    cv.waitKey(0)

img = cv.imread(r'../de/1_Handshaking_Handshaking_1_314.jpg',0)    # 0表示灰度图像,不写表示BRG彩色图像
img_1 = cv.imread(r'../de/4_Dancing_Dancing_4_517.jpg')

img1 = img[100:300, 500:700]
show("img", img1)

b, g, r = cv.split(img)    # 颜色通道提取,opencv读取的图片是BRG
print(b)
print(b.shape)
B = img[:, :, 0]
G = img[:, :, 1]
R = img[:, :, 2]

if b.any() == B.any():
    print("这两种方式是一样的。")

img = cv.merge((B, G, R))    # 将三通道组合在一起
show("man", img)

img_b = img.copy()  # 只保留B通道
img_b[:, :, 1] = 0
img_b[:, :, 2] = 0
show('B', img_b)

img_g = img.copy()  # 只保留G通道
img_g[:, :, 0] = 0
img_g[:, :, 2] = 0
show('G', img_g)

img_r = img.copy()  # 只保留R通道
img_r[:, :, 0] = 0
img_r[:, :, 1] = 0
show('R', img_r)

(4)边界填充

import cv2 as cv
from matplotlib import pyplot as plt

img = cv.imread(r'../de/1_Handshaking_Handshaking_1_314.jpg')  

top_size, bottom_size, left_size, right_size = (100, 100, 100, 100)  # 设置上下左右填充的大小
replicate = cv.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv.BORDER_REPLICATE)  # BORDER_REPLICATE,复制法,复制边缘的像素
reflect = cv.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv.BORDER_REFLECT)  # 反射法,对图像的四周进行反射,比如:fedcba[abcdef]fedcba
reflect_101 = cv.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv.BORDER_REFLECT_101)  # 反射法,以最边缘的像素为轴,对称,fedcb[abcdef]edcba
wrap = cv.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv.BORDER_WRAP)  # 外包装法,abcdef[abcdef]abcdef
constant = cv.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv.BORDER_CONSTANT, value=0)  # 常量法,用value的值进行填充
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(reflect_101, '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()

 (5)图像的数值操作

import cv2 as cv
from matplotlib import pyplot as plt


# 图片展示函数
def show(name, img):
    cv.imshow(name, img)
    cv.waitKey(0)


# 读取图片
img = cv.imread(r'../de/1_Handshaking_Handshaking_1_314.jpg') 
img_1 = cv.imread(r'../de/4_Dancing_Dancing_4_517.jpg')
img1 = img + 10    # 所有像素都加10
print(img[0:10, 0:10, 0])
print(img1[0:10, 0:10, 0])

sum = img+img1       # 像素值和与255取余,两个图片相加要保证shape相同
print(sum[0:10, 0:10, 0])

sum = cv.add(img, img1)  # 像素值之和最大取255
print(sum[0:10, 0:10, 0])

print(img_1.shape)
img = cv.resize(img, (0, 0), fx=2, fy=2)  # 将img的shape分别扩大fx,fy倍
img = cv.resize(img, (1024, 576))  # 将img的shape修改为(576, 1024)
print(img_1.shape)
print(img.shape)
res = cv.addWeighted(img, 0.4, img_1, 0.6, 0)  # 将两张图片融合
show("res", res)

Logo

更多推荐