How to Mask an image using Numpy/OpenCV?

前端 未结 4 1690
别跟我提以往
别跟我提以往 2020-12-14 22:32

I have an image I load with:

im = cv2.imread(filename)

I want to keep data that is in the center of the image. I created a circle as a mas

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-14 23:11

    Use cv2.bitwise_and and pass the circle as mask.

    im = cv2.imread(filename)
    height,width,depth = im.shape
    circle_img = np.zeros((height,width), np.uint8)
    cv2.circle(circle_img,(width/2,height/2),280,1,thickness=-1)
    
    masked_data = cv2.bitwise_and(im, im, mask=circle_img)
    
    cv2.imshow("masked", masked_data)
    cv2.waitKey(0)
    

提交回复
热议问题