0 目的
- Access pixel values and modify them(获取像素值并修改)
- Access image properties(获取图像属性)
- Set a Region of Interest (ROI)(选择一个感兴趣区域)
- Split and merge images(分离和融合图片)
Almost all the operations in this section are mainly related to Numpy rather than OpenCV.(下面的操作主要和Numpy库有关) A good knowledge of Numpy is required to write better optimized code with OpenCV.
1 获取并修改像素值
加载图片:
import numpy as np
import cv2 as cv
img = cv.imread('messi5.jpg') # 读取图片
You can access a pixel value by its row and column coordinates(通过行列坐标获取像素值). For BGR image, it returns an array of Blue, Green, Red values. For grayscale image, just corresponding intensity is returned.
>>> px = img[100,100]
>>> print( px )
[157 166 200]
# accessing only blue pixel
>>> blue = img[100,100,0]
>>> print( blue )
157
可以通过同样的方式修改像素值:
>>> img[100,100] = [255,255,255]
>>> print( img[100,100] )
[255 255 255]
警告:
Numpy是一个用于快速数组计算的优化库。 因此,简单地访问每个像素值并修改它将非常缓慢,这是不可取的。
上述方法通常用于选择数组的一个区域,比如前5行和后3列。对于单个像素访问,Numpy数组方法array.item()和array.itemset()被认为更好。但是,它们总是返回一个标量,所以如果您想访问所有的B、G、R值,就需要对每个值分别调用array.item()。
更好的像素获取和修改方式:
# accessing RED value
>>> img.item(10,10,2)
59
# modifying RED value
>>> img.itemset((10,10,2),100)
>>> img.item(10,10,2)
100
2 获取图像属性
Image properties include number of rows, columns, and channels; type of image data; number of pixels(包括行数、列数、通道数;图像数据类型;像素个数); etc.
>>> print( img.shape ) # 返回行、列、通道数构成的tuple
(342, 548, 3)
>>> print( img.size ) # 返回像素个数
562248
>>> print( img.dtype ) # 图像数据类型
uint8
3 图片感兴趣区域
ROI is again obtained using Numpy indexing.(通过Numpy索引操作来获取图像感兴趣区域) Here I am selecting the ball and copying it to another region in the image(选择这个足球,并将它复制到图像中的另一个区域:):
>>> ball = img[280:340, 330:390]
>>> img[273:333, 100:160] = ball
4 分离和融合图片
将BGR图像分割为单个通道。加入这些单独的通道来创建BGR映像。你可以简单地这样做:
>>> b,g,r = cv.split(img) # 分离图片的各通道
>>> img = cv.merge((b,g,r)) # 将单独的通道融合为一张图片
或者
>>> b = img[:,:,0]
假设你想把所有的红色像素设为零——你不需要首先分割通道。Numpy索引更快:
>>> img[:,:,2] = 0 # 图片的第三个通道的像素值全部设为0
警告:
cv.split()是一个昂贵的操作(就时间而言)。所以只有在必要的时候才使用它。否则就使用Numpy索引
5 为图像制作边框(填充)
如果你想创建一个图像的边框,比如一个相框,你可以使用cv.copyMakeBorder()。但它在卷积运算、零填充等方面有更多的应用。这个函数接受以下参数:
- src - input image
- top, bottom, left, right - border width in number of pixels in corresponding directions
- borderType - Flag defining what kind of border to be added. It can be following types:
- cv.BORDER_CONSTANT - Adds a constant colored border. The value should be given as next argument.
- cv.BORDER_REFLECT - Border will be mirror reflection of the border elements, like this : fedcba|abcdefgh|hgfedcb
- cv.BORDER_REFLECT_101 or cv.BORDER_DEFAULT - Same as above, but with a slight change, like this : gfedcb|abcdefgh|gfedcba
- cv.BORDER_REPLICATE - Last element is replicated throughout, like this: aaaaaa|abcdefgh|hhhhhhh
- cv.BORDER_WRAP - Can’t explain, it will look like this : cdefgh|abcdefgh|abcdefg
- value - Color of border if border type is cv.BORDER_CONSTANT
下面是代码示例:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
BLUE = [255,0,0]
img1 = cv.imread('opencv-logo.png')
replicate = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REPLICATE)
reflect = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT)
reflect101 = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT_101)
wrap = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_WRAP)
constant= cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_CONSTANT,value=BLUE)
plt.subplot(231),plt.imshow(img1,'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('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()
参考链接:https://docs.opencv.org/4.x/d3/df2/tutorial_py_basic_ops.html