Python Forum
Open CV cv2.bitwise_and() function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Open CV cv2.bitwise_and() function
#1
My question is about python and Open CV. Here is my code:
import cv2
import numpy as np
 
# read image
img = cv2.imread(r"C:\Users\asus\Desktop\20230104200344_6edae.thumb.1000_0.jpg")
# Create a mask the same size as the image
mask = np.zeros(img.shape[:2], dtype=np.uint8)
# Create a circular mask with a radius of 100 and the center at the center of the image.
mask = cv2.circle(mask, (img.shape[1]//2, img.shape[0]//2), 100, 100, -1)
# Perform bitwise operations on the image and mask
masked_img = cv2.bitwise_and(img, img, mask=mask)
 
cv2.imshow('image', img)
cv2.imshow('mask', mask)
cv2.imshow('masked_image', masked_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
I want to ask about this line particularly:mask = cv2.circle(mask, (img.shape[1]//2, img.shape[0]//2), 100, 100, -1).It creates a grey circle on a black background. What if I change it to mask = cv2.circle(mask, (img.shape[1]//2, img.shape[0]//2), 100, 255, -1) so it creates a *white *circle. My question is, when performing bitwise and, I can see why a white color (R=255,G=255,B=255) can be translated to (11111111,11111111,11111111) so it can be used to preserve the orginal color. However, if it is grey,(R=100, G=100, B=100), namely (1100100,1100100,1100100), I can't really see why it preserves the color. Please help and name any of my misunderstanding.
Reply


Messages In This Thread
Open CV cv2.bitwise_and() function - by rickyw2777 - Feb-01-2025, 06:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  with open context inside of a recursive function billykid999 1 1,342 May-23-2023, 02:37 AM
Last Post: deanhystad
  How to Properly Open a Serial Port in a Function bill_z 2 8,411 Jul-22-2021, 12:54 PM
Last Post: bill_z
  replacing the open function Skaperen 2 3,410 Jan-27-2019, 02:45 AM
Last Post: Skaperen
  What default directory does "open" function draw from? Athenaeum 4 4,964 Oct-07-2017, 06:15 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020