使用版本
- python:3.8
- opencv-python: 4.5.4.58
像素操作,BGR编码的数据格式,BGR通道分离等操作。
img = cv2.imread('../img/meinv.png')
print(img)
px = img[100, 100]
print(px)
#像素 BGR(Blue, Green, Red values) [157 166 200]
print(px)
# accessing only blue pixel
blue = img[100, 100, 0]
print(blue)
# accessing RED value
print(img.item(10, 10, 2))
# modifying RED value
img.itemset((10, 10, 2), 100)
print(img.item(10, 10, 2))
# shape(340, 353, 3)如果是灰度图,则shape只会显示行列
print(img.shape)
# Total number of pixels
print(img.size)
# Image datatype
print(img.dtype)
操作图片的部分区域。例如识别图片的人脸部分后,然后用人脸部分去识别眼睛,可以提升准确度。
# Image ROI(region of images)
part = img[200:300, 200:300]
# 移花接木
img[0:100, 50:150] = part
cv2.imwrite('../res/meinv_1.png', img)
通道分离
b,g,r = cv2.split(img)
通道合并
img = cv2.merge((b,g,r))
split太消耗性能,所以还有另外的方式,通过numpy去获取通道
b = img[:,:,0]
#or make all the red pixels to zero,
img[:,:,2] = 0
cv2.imwrite('../res/meinv_2.png', img)
Bitwise Operations 位运算
"""
This includes bitwise AND, OR, NOT and XOR operations. They will be highly useful while extracting
any part of the image (as we will see in coming chapters), defining and working with non-rectangular ROI etc.
Below we will see an example on how to change a particular region of an image.
I want to put OpenCV logo above an image. If I add two images, it will change color.
If I blend it, I get an transparent effect. But I want it to be opaque. If it was a rectangular region,
I could use ROI as we did in last chapter. But OpenCV logo is a not a rectangular shape.
"""
# Load two images
img1 = cv2.imread('../img/test1.jpg')
img2 = cv2.imread('../img/opencv-logo.png')
# I want to put logo on top-left corner, So I create a ROI
rows, cols, channels = img2.shape
roi = img1[0:rows, 0:cols ]
# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)
# Now black-out the area of logo in ROI
img1_bg = cv2.bitwise_and(roi, roi, mask = mask_inv)
# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2, img2, mask = mask)
# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg, img2_fg)
img1[0:rows, 0:cols ] = dst
cv2.imwrite('../res/bit.png', img1)
原图
结果
数字图像处理中的掩膜的概念是借鉴于PCB制版的过程,在半导体制造中,许多芯片工艺步骤采用光刻技术,用于这些步骤的图形“底片”称为掩膜(也称作“掩模”),其作用是:在硅片上选定的区域中对一个不透明的图形模板遮盖,继而下面的腐蚀或扩散将只影响选定的区域以外的区域。
图像掩膜与其类似,用选定的图像、图形或物体,对处理的图像(全部或局部)进行遮挡,来控制图像处理的区域或处理过程。
数字图像处理中,掩模为二维矩阵数组,有时也用多值图像,图像掩模主要用于:
①提取感兴趣区,用预先制作的感兴趣区掩模与待处理图像相乘,得到感兴趣区图像,感兴趣区内图像值保持不变,而区外图像值都为0。
②屏蔽作用,用掩模对图像上某些区域作屏蔽,使其不参加处理或不参加处理参数的计算,或仅对屏蔽区作处理或统计。
③结构特征提取,用相似性变量或图像匹配方法检测和提取图像中与掩模相似的结构特征。
④特殊形状图像的制作。
可以用cv2.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:
- cv2.BORDER_ConSTANT - Adds a constant colored border. The value should be given as next argument.
- cv2.BORDER_REFLECT - Border will be mirror reflection of the border elements, like this : fedcba|abcdefgh|hgfedcb
- cv2.BORDER_REFLECT_101 or cv2.BORDER_DEFAULT - Same as above, but with a slight change, like this : gfedcb|abcdefgh|gfedcba
- cv2.BORDER_REPLICATE - Last element is replicated throughout, like this: aaaaaa|abcdefgh|hhhhhhh
- cv2.BORDER_WRAP - Can’t explain, it will look like this : cdefgh|abcdefgh|abcdefg
- value - Color of border if border type is cv2.BORDER_CONSTANT
https://gitee.com/carlzhangweiwen/python-opencv-learn
https://opencv24-python-tutorials.readthedocs.io/en/latest/py_tutorials/py_core/py_basic_ops/py_basic_ops.html#basic-ops
opencv 用到的图片都在这里:https://github.com/opencv/opencv/tree/master/samples/data


![[opencv-python]学习基础操作 [opencv-python]学习基础操作](http://www.mshxw.com/aiimages/31/423627.png)
