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)
掩码只考虑掩码大于零的原始图像中的像素 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)
|
近期评论