opencv入门6:蒙版-masking

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


opencv入门6:蒙版-masking_第1张图片
图·1
opencv入门6:蒙版-masking_第2张图片
"图·2"
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)

现实不似你所见。
更多文章请关注我的博客:https://harveyyeung.github.io

你可能感兴趣的:(opencv入门6:蒙版-masking)