学过的知识,会过时、会遗忘,但在努力过程中学会的处事态度和做事习惯,都会留在骨子里,变成我们的一部分。
Open Source Computer Vision Library,OpenCV于1999年由Intel建立,如今由Willow Garage提供支持。OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、MacOS操作系统上。它轻量而且高效——由一系列 C 函数和少量C++类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。
pip install opencv-python -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install opencv-contrib-python -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip install pytesseract -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
import cv2 as cv
def get_image_info(image):
print(type(image)) #
print(image.shape) # 高度 宽度 通道数
print(image.size) # 像素大小
print(image.dtype) # 数据类型
src = cv.imread(r'D:\python\pycharm2020\test\004.jpg')
cv.imshow("input image", src)
get_image_info(src)
cv.waitKey(0)
cv.destroyAllWindows()
运行结果如下:
<class 'numpy.ndarray'>
(500, 500, 3)
750000
uint8
import cv2 as cv
def read_video():
cap = cv.VideoCapture(r'D:\beauty\video\test.mp4')
while True:
ret, frame = cap.read()
if ret == False:
break
cv.imshow('video', frame)
cv.waitKey(20)
read_video()
import cv2 as cv
# 调用笔记本内置镜头
cap = cv.VideoCapture(0, cv.CAP_DSHOW)
while True:
ret, frame = cap.read()
frame = cv.flip(frame, 1)
cv.imshow('video', frame) # 显示镜头捕获的每一帧
if cv.waitKey(100) & 0xff == ord('q'): # 按q退出
break
cap.release()
cv.destroyAllWindows()
释放摄像头对象时错误:
SourceReaderCB::~SourceReaderCB terminating async callback
解决方法:
cap = cv.VideoCapture(0, cv.CAP_DSHOW)
import cv2 as cv
def color_space_transform(img):
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('gray', gray)
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
cv.imshow('hsv', hsv)
hls = cv.cvtColor(img, cv.COLOR_BGR2HLS)
cv.imshow('hls', hls)
YCrCb = cv.cvtColor(img, cv.COLOR_BGR2YCrCb)
cv.imshow('YCrCb', YCrCb)
yuv = cv.cvtColor(img, cv.COLOR_BGR2YUV)
cv.imshow('yuv', yuv)
src = cv.imread(r'D:\python\pycharm2020\test\004.jpg')
src = cv.resize(src, None, fx=0.5, fy=0.5)
cv.imshow('src', src)
color_space_transform(src)
cv.waitKey(0)
cv.destroyAllWindows()
函数的参数意义:第一个参数为原数组,可以为单通道,多通道。第二个参数为下界,第三个参数为上界
mask = cv2.inRange(hsv, lower_blue, upper_blue)
而在lower_blue~upper_blue之间的值变成255 (255代表白色)
即:opencv的inRange函数可提取特定颜色,使特定颜色变为白色,其他颜色变为黑色,从而实现图像的二值化处理。
HSV颜色对应的RGB分量范围表如下:(这里是三通道的)
测试所用图像如下:
追踪绿色,代码如下:
import cv2 as cv
import numpy as np
def tracking_colors(image):
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
# 追踪绿色
lower_hsv = np.array([35, 43, 46])
upper_hsv = np.array([77, 255, 255])
mask = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv)
cv.imshow('mask', mask)
cv.waitKey(0)
cv.destroyAllWindows()
src = cv.imread(r'D:\python\pycharm2020\test\001.jpg')
tracking_colors(src)
运行效果如下:
import cv2 as cv
import numpy as np
def tracking_colors(image):
hsv = cv.cvtColor(image, cv.COLOR_BGR2HSV)
# 追踪蓝色
lower_hsv = np.array([100, 43, 46])
upper_hsv = np.array([124, 255, 255])
mask = cv.inRange(hsv, lowerb=lower_hsv, upperb=upper_hsv)
cv.imshow('mask', mask)
cv.waitKey(0)
cv.destroyAllWindows()
src = cv.imread(r'D:\python\pycharm2020\test\001.jpg')
tracking_colors(src)
通道分离与合并
import cv2 as cv
src = cv.imread(r'D:\python\pycharm2020\test\004.jpg')
src = cv.resize(src, None, fx=0.5, fy=0.5)
b, g, r = cv.split(src) # 分离
cv.imshow('blue', b)
cv.imshow('green', g)
cv.imshow('red', r)
cv.imshow('src', src)
cv.waitKey(0)
cv.destroyAllWindows()
运行效果如下:
import cv2 as cv
src = cv.imread(r'D:\python\pycharm2020\test\004.jpg')
b, g, r = cv.split(src)
src[:, :, 2] = 0 # 修改了的图片
cv.imshow('changed src', src)
src1 = cv.merge((b, g, r)) # 合并得到原来的图片
cv.imshow('merged src', src1)
cv.waitKey(0)
运行效果如下:
算数运算:
像素的算术运算涉及加减乘除等基本运算(要进行算术运算,两张图片的shape必须一样)
实例如下:
import cv2 as cv
def add_demo(m1, m2): # 像素的加运算
dst = cv.add(m1, m2)
cv.imshow("add_demo", dst)
def subtract_demo(m1, m2): # 像素的减运算
dst = cv.subtract(m1, m2)
cv.imshow("subtract_demo", dst)
def divide_demo(m1, m2): # 像素的除法运算
dst = cv.divide(m1, m2)
cv.imshow("divide_demo", dst)
def multiply_demo(m1, m2): # 像素的乘法运算
dst = cv.multiply(m1, m2)
cv.imshow("multiply_demo", dst)
src1 = cv.imread(r'D:\python\pycharm2020\test\007.png')
src2 = cv.imread(r'D:\python\pycharm2020\test\008.png')
cv.imshow('image1', src1)
cv.imshow('image2', src2)
# 像素的算术运算(加、减、乘、除) 两张图片必须shape一致
add_demo(src1, src2)
subtract_demo(src1, src2)
divide_demo(src1, src1)
multiply_demo(src1, src2)
cv.waitKey(0)
cv.destroyAllWindows()
运行效果如下:
像素的逻辑运算:
像素的逻辑运算涉及与、或、非、异或等基本运算(要进行逻辑运算,两张图片的shape必须一样)
实例如下:
import cv2 as cv
def and_demo(m1, m2): # 与运算 每个像素点每个通道的值按位与
dst = cv.bitwise_and(m1, m2)
cv.imshow("and_demo", dst)
def or_demo(m1, m2): # 或运算 每个像素点每个通道的值按位或
dst = cv.bitwise_or(m1, m2)
cv.imshow("or_demo", dst)
def not_demo(m1): # 非运算 每个像素点每个通道的值按位取反
dst = cv.bitwise_not(m1)
cv.imshow("not_demo", dst)
src1 = cv.imread(r'D:\python\pycharm2020\test\007.png')
src2 = cv.imread(r'D:\python\pycharm2020\test\008.png')
cv.imshow('image1', src1)
cv.imshow('image2', src2)
# 像素的逻辑运算(与、或、非) 两张图片必须shape一致
and_demo(src1, src2)
or_demo(src1, src2)
not_demo(src1)
cv.waitKey(0)
cv.destroyAllWindows()
运行效果如下:
简单测试如下:
import cv2 as cv
import numpy as np
def adjust_brightness_image(image, c, b): # 第2个参数rario为对比度 第3个参数b为亮度
height, width, channels = image.shape
blank = np.zeros([height, width, channels], image.dtype) # 新建的一张全黑图片和img1图片shape类型一样,元素类型也一样
dst = cv.addWeighted(image, c, blank, 1 - c, b)
cv.imshow("adjust_contrast_brightness", dst)
src = cv.imread(r'D:\python\pycharm2020\test\004.jpg')
cv.imshow("first", src)
# 调节图片对比度和亮度 contrast and brightness (1.2, 100) (2.5, 0) (0.5 10)
adjust_brightness_image(src, 1.2, 100)
cv.waitKey(0)
cv.destroyAllWindows()
运行效果如下: