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
#2
Not too sure what you wish to achieve.I assume you want to get a circular section from your original image on a white background.

I usually do things in Idle, so I don't use .imshow() because that often causes problems. I just save each step as an image and have a look.

I started with an image I made for another question, a white die on a pink background showing the number 5, then got a circular section from the image on a black background, then changed black to white.

This is following an answer on stackoverflow about using cv.bitwise_and(), but also demonstrates the use of cv.bitwise_or()

import cv2 as cv
import numpy as np

source = 'cv2/images/steps/five.png'
destination = 'cv2/images/steps/' 

# Read image 283 x 283 x 3 uint8
die = cv.imread(source) # (283, 283, 3)
# if the image does not exist cv2 won't tell you, so check that before proceeding
assert die is not None, "file could not be read, check with os.path.exists()"
die.shape # (283, 283, 3)

# Create white image, 3 channels 283 x 283 x 3 uint8
w_3c = np.full_like(die, fill_value=(255,255,255))
cv.imwrite(destination + 'white.png', w_3c) # have a look, just a white square

# Define disc elements
centre = (die.shape[1]//2, die.shape[0]//2)
radius = int(min(centre) * .9)

# Create white disk, 3 channels 283 x 283 x 3 uint8
d_3c = np.zeros_like (die[:,:], dtype='uint8')
cv.circle(d_3c, centre, radius, [255]*3, thickness=cv.FILLED)
cv.imwrite(destination + 'white_disk.png', d_3c) # a white disc on black background

# create a single channel 283 x 283  uint8
d_1c = d_3c[:,:,0]
cv.imwrite(destination + 'd_1c.png', d_1c) # white disk on black background
# Extract pixels disk using white disk single channel as mask
masked = cv.bitwise_and(die, w_3c, mask=d_1c)
cv.imwrite(destination + 'masked_pic.png', masked) # have a look
# 'masked_pic.png' is more or less what you want, but with a black background
# Add white background
d_3c_i = ~d_3c
# this produces a circular section of the original pic on a white background
final = cv.bitwise_or(die, d_3c_i)
cv.imwrite(destination + 'final_output.png', final) # have a look
Reply
#3
The bitwise operation is performed on the pixels, not the pixel colors. The color setting for your circular mask is irrelevant, as long as it is not 0. If the mask pixel color is 0, the masked_img pixel color is 0. If the mask pixel color is not zero the masked_img pixel color is the img pixel color.
Reply
#4
I understand it now. Thank you for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  with open context inside of a recursive function billykid999 1 1,317 May-23-2023, 02:37 AM
Last Post: deanhystad
  How to Properly Open a Serial Port in a Function bill_z 2 8,310 Jul-22-2021, 12:54 PM
Last Post: bill_z
  replacing the open function Skaperen 2 3,393 Jan-27-2019, 02:45 AM
Last Post: Skaperen
  What default directory does "open" function draw from? Athenaeum 4 4,946 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