opencv入门6:蒙版-masking

使用蒙板可以让我们只关注感兴趣的图像部分。
掩码的关键点是它们允许我们将计算的重点仅限于感兴趣的图像区域.

详细解释:
https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html?
highlight=bitwise_and#cv2.bitwise_and


蒙板masking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import numpy as np 
import argparse
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-i","--image",required =True, help="Path to the image")
args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Original",image)

mask = np.zeros(image.shape[:2],dtype ="uint8")
(cx,cy) = (image.shape[1]//2,image.shape[0]//2)
cv2.rectangle(mask,(cx-250,cy-150),(cx+200 ,cy+150),255,-1)
cv2.imshow("Mask",mask)

masked = cv2.bitwise_and(image,image,mask=mask)
#bitwise_and方法前两个参数是图像本身
掩码只考虑掩码大于零的原始图像中的像素
cv2.imshow("Mask applied to image",masked)
cv2.waitKey(0)


mask = np.zeros(image.shape[:2], dtype = "uint8")
cv2.circle(mask, (cx, cy), 100, 255, -1)
masked = cv2.bitwise_and(image, image, mask = mask)
cv2.imshow("Mask", mask)
cv2.imshow("Mask Applied to Image", masked)
cv2.waitKey(0)



现实不似你所见。
坚持原创技术分享,您的支持将鼓励我继续创作!