像素处理
案例1:首先使用numpy生成一个(8, 8)大小的数组,用来模拟一个黑色图像
1 2 3 4 5 6 7
| import numpy as np import cv2
img = np.zeros((8, 8), dtype=np.uint8) cv2.imshow('one', img) cv2.waitKey() cv2.destroyAllWindows()
|
1 2 3 4
| img[0, 3] = 255 cv2.imshow('one', img) cv2.waitKey() cv2.destroyAllWindows()
|
案例2:取一个灰度图像,并对其像素进行访问、修改
1 2 3 4
| dog = cv2.imread('data/dog.jpg', flags=cv2.IMREAD_GRAYSCALE) cv2.imshow('dog', dog) cv2.waitKey() cv2.destroyAllWindows()
|
1 2
| cc = dog[10:100, 80:100] dog[10:100, 80:100] = 255
|
1 2 3
| cv2.imshow('dog', cc) cv2.waitKey() cv2.destroyAllWindows()
|
案例3:使用Numpy生成三维数组,用来观察三个通道的变化情况
1 2 3 4 5 6 7 8 9
| import numpy as np import cv2 from get_show_img import get_show
blue=np.zeros((300,300,3), dtype=np.uint8) blue[:,:,0]=255 cv2.imshow('blue', blue) cv2.waitKey() cv2.destroyAllWindows()
|
1 2 3 4 5 6 7 8 9
| import numpy as np import cv2
green=np.zeros((300,300,3), dtype=np.uint8) green[:,:,1]=255
get_show(green)
|
1 2 3 4 5 6 7 8 9
| import numpy as np import cv2
red=np.zeros((300,300,3), dtype=np.uint8) red[:,:,2]=255
get_show(red)
|
案例4:使用Numpy生成一个三维数组,用来观察三个通道值的变化情况
1 2 3 4 5 6 7 8 9 10 11
| import numpy as np import cv2
img = np.zeros((300, 300, 3), dtype=np.uint8) img[:,:100,0] = 255 img[:,100:200,1]=255 img[:,200:300,2]=255
get_show(img)
|
案例5:使用Numpy生成一个三维数组,用来模拟一幅BGR的彩色图像,并对其进行访问、修改
1 2
| import numpy as np img = np.zeros((2,4,3), dtype=np.uint8)
|
1 2 3 4 5 6 7 8
| img[0,3] img[1,2,2] img[0,3]=255 img[0,0]=[66,77,88] img[1,1,1]=3 img[1,2,2]=4 img[0,2,0]=5 img
|
array([[[ 66, 77, 88],
[ 0, 0, 0],
[ 5, 0, 0],
[255, 255, 255]],
[[ 0, 0, 0],
[ 0, 3, 0],
[ 0, 0, 4],
[ 0, 0, 0]]], dtype=uint8)
4
案例6:读取一幅彩色图像,并对其像素进行访问修改
1 2 3 4 5 6
| import cv2
img = cv2.imread('data/dog.jpg') cv2.imshow('dog', img) cv2.waitKey() cv2.destroyAllWindows()
|
(300, 534, 3)
1 2 3 4 5 6
| img[0,0] img[0,0,0] img[0,0,1] img[0,0,2] img[50,0] img[100,0]
|
array([63, 61, 83], dtype=uint8)
1 2 3 4
| for i in range(50): for j in range(100): for k in range(3): img[i,j,k] = 255
|
1 2 3
| cv2.imshow('dog', img) cv2.waitKey() cv2.destroyAllWindows()
|
1 2 3
| for i in range(50, 100): for j in range(100,200): img[i,j]=[128, 128, 128]
|
1 2 3
| cv2.imshow('dog', img) cv2.waitKey() cv2.destroyAllWindows()
|
1 2 3
| for i in range(100, 150): for j in range(200,300): img[i,j]=0
|
1 2 3
| cv2.imshow('dog', img) cv2.waitKey() cv2.destroyAllWindows()
|
255
感兴趣区域提取
1 2 3 4 5
| import cv2 img=cv2.imread('data/dog.jpg') cv2.imshow('dog', img) cv2.waitKey() cv2.destroyAllWindows()
|
(300, 534, 3)
1
| a = img[200:300,250:350,0]
|
1 2 3
| cv2.imshow('dog', a) cv2.waitKey() cv2.destroyAllWindows()
|
通道操作
- 在RGB图像中,图像是由R通道,G通道,B通道三通道构成的。
- 需要注意的是,在opencv中,通道是按照B通道,G通道,R通道的顺序存储的
通道拆分
通过索引拆分
(300, 534, 3)
1 2 3
| b=img[:,:,0] g=img[:,:,1] r=img[:,:,2]
|
1 2 3 4
| import numpy as np cv2.imshow('bgr', np.hstack([b,g,r])) cv2.waitKey() cv2.destroyAllWindows()
|
1 2 3
| cv2.imshow('dog', img) cv2.waitKey() cv2.destroyAllWindows()
|
1 2 3
| cv2.imshow('dog', img) cv2.waitKey() cv2.destroyAllWindows()
|
通过函数拆分
1 2 3 4 5 6 7
| import cv2 import numpy as np img=cv2.imread('data/dog.jpg') b,g,r=cv2.split(img) cv2.imshow('dog', np.hstack([b,g,r])) cv2.waitKey() cv2.destroyAllWindows()
|
通道合并
- 通道合并是通道拆分的逆过程,通过合并通道可以将三个通道的灰度图像构成一幅彩色图像。
- cv2.merge()
1 2 3 4
| bgr = cv2.merge([b,g,r]) cv2.imshow('dog', bgr) cv2.waitKey() cv2.destroyAllWindows()
|
1 2 3 4 5
| import matplotlib.pyplot as plt plt.imshow(bgr[:,:,::-1]) plt.xticks([]) plt.yticks([]) plt.show()
|
获取图像属性
- shape:如果是彩色图像,返回行数、列数、通道数的数组;如果是二值图像或者灰度图像,则仅返回行数和列数。
- size:返回图像的像素数目。其值位“行×列×通道数”,灰度图像或者二值图像的通道数为1
- dtype:返回图像的数据类型
(300, 534, 3)
480600
dtype('uint8')