0. 配置openCV
用pip即可.
这是open CV版本的Hello World
import cv2 as cv
src = cv.imread(r"F:\JupyterNotebook\ComputerVision\lena.png")
cv.namedWindow("Lena",cv.WINDOW_AUTOSIZE)
cv.imshow("Lena",src)
cv.waitKey(0)
cv.destroyAllWindows()
1. 概论
- 什么是图像?
- 什么是数字图像?
- 图像有什么属性?
- 通道数目
- 高和宽
- 像素数据
- 图像类型
有图有真相,一图胜千言.
2. 图像的加载和保存
import cv2 as cv
src = cv.imread(r"F:\JupyterNotebook\ComputerVision\lena.png")
cv.namedWindow("Lena",cv.WINDOW_AUTOSIZE)
cv.imshow("Lena",src)
cv.imwrite(r'F:\JupyterNotebook\ComputerVision\GrayLena.png',gray)
cv.waitKey(0)
cv.destroyAllWindows()
图像的属性获取
def get_image_info(image):
print(type(image))
print(image.shape)
print(image.size)
print(image.dtype)
pixel_data = np.array(image)
print(pixel_data)
#Output:
#----------Hello Python----------
#
#(512, 512, 3)
#786432
#uint8
#786432=521*521*3
#还有一幅图
调用计算机自带的摄像头
def vedio_demo():
capture = cv.VideoCapture(0)
while(True):
ret ,frame = capture.read()
frame = cv.flip(frame,1)
cv.imshow("vedio",frame)
c=cv.waitKey(50)
if c==27:
break
3. Numpy和图像处理
data\dtype\size\shape\len
属性读取
def access_pixels(image):
print(image.shape)
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
print("width : %s,height : %s channels : %s"%(width,height,channels))
获取像素点
def access_pixels(image):
print(image.shape)
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
print("width : %s,height : %s channels : %s"%(width,height,channels))
for row in range(height):
for col in range(width):
for c in range(channels):
pv = image[row,col,c]
image[row,col,c] = 255 - pv
cv.imshow("pixels_demo",image)
#这里实测512*512的图像跑了1.63s
暴力创建数字图像
def create_image():
"""
img = np.zeros([400,400,3],np.uint8)
img[: , : , 1]=np.ones([400,400])*255
#Ta 绿 了
cv.imshow("new image",img);
"""
"""
img = np.zeros([400,400,1],np.uint8)
img[: , : , 0]=np.ones([400,400])*127
#创建灰度图
cv.imshow("new image",img);
"""
ml=np.ones([3,3],np.float32)
ml.fill(122.388)
print(ml)
#当心Overflow
#dst = cv.bitwise_not(image)#这里调用C跑得非常快
4. 色彩空间01
- 什么是色彩空间?
- 常见的色彩空间有哪些?
- RGB
- HSV
- HIS
- YCrCb
- YUV
- 如何实现不同色彩空间转换?
注意HSV这里用一个uint8可以存储的180把标准的360标准化.HSV对不同色彩表现比较好
YUV:Android开发,由硬件性能所限.
色彩空间之间的相互转换
最常见的是
- HSV和RGB
- YUV和RGB
def color_space_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow("gray",gray)
hsv = cv.cvtColor(image,cv.COLOR_BGR2HSV)
cv.imshow("hsv",hsv)
yuv = cv.cvtColor(image,cv.COLOR_BGR2YUV)
cv.imshow("yuv",yuv)
Ycrcb = cv.cvtColor(image,cv.COLOR_BGR2YCrCb)
cv.imshow("Ycrcb",Ycrcb)
5. 色彩空间02
注意到HSV的取值:
- H:0-180
- S:0-255
- V:0-255
视频获取
def extrace_object_demo():
capture=cv.VideoCapture("F:\JupyterNotebook\ComputerVision\Ko.mp4")
while(True):
ret, frame = capture.read()
if ret == False:
break;
cv.imshow("video",frame)
c = cv.waitKey(40)
if c==27:
break
过滤颜色
def extrace_object_demo():
capture=cv.VideoCapture("F:\JupyterNotebook\ComputerVision\Ko.mp4")
while(True):
ret, frame = capture.read()
if ret == False:
break;
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
lower_hsv = np.array([37,43,46])
upper_hsv = np.array([77,255,255])
#注意看HSV和RGB转换表
mask = cv.inRange(hsv,lowerb=lower_hsv,upperb=upper_hsv)
cv.imshow("video",frame)
cv.imshow("mask",mask)#二值化并且采集绿色.
c = cv.waitKey(40)
if c==27:
break
分离与合并
b,g,r =cv.split(src)
#以下为其用法
cv.imshow("blue",b)
cv.imshow("green",b)
cv.imshow("red",b)
#以下为其合并
src[:,:,2]=0
src = cv.merge([b,g,r])
cv.imshow("Lena",src)
基于python3的Opencv图像处理教程(从零到实践)
NumPy官方网站