how to select a circular area of an image?

100 views Asked by At

I would like to select only the gray area in the middle of my image ?

I mean my calculations of mean, standard deviation must use values in this area.

I can select columns and rows but I will still have the corners.

import numpy as np
import cv2
import rawpy
import rawpy.enhance
import matplotlib.pyplot as plt
import glob

####################  2023-09-21_16-58-51.894
# Reading a Nikon RAW (NEF) image

init="/media/alexandre/Transcend/Expérience/Ombroscopie/eau/initialisation/2023-09-19_19-02-33.473.nef"
brut="/media/alexandre/Transcend/Expérience/Ombroscopie/eau/DT0.4/2023-09-25_13-26-56.259.nef"
bruit="/media/alexandre/Transcend/Expérience/Ombroscopie/eau/bruit-electronique/2023-09-18_18-59-34.994.nef"
####################
# This uses rawpy library
print("reading init file using rawpy.")
raw_init = rawpy.imread(init)

image_init = raw_init.postprocess(use_camera_wb=True, output_bps=16)
print("Size of init image read:" + str(image_init.shape))

print("reading brut file using rawpy.")
raw_brut = rawpy.imread(brut)
image_brut = raw_brut.postprocess(use_camera_wb=True, output_bps=16)
print("Size of brut image read:" + str(image_brut.shape))


print("reading bruit file using rawpy.")
raw_bruit = rawpy.imread(bruit)
image_bruit = raw_bruit.postprocess(use_camera_wb=True, output_bps=16)
print("Size of bruit image read:" + str(image_bruit.shape))


####################
# (grayscale) OpenCV
init_grayscale = cv2.cvtColor(image_init, cv2.COLOR_RGB2GRAY)
brut_grayscale = cv2.cvtColor(image_brut, cv2.COLOR_RGB2GRAY)
bruit_grayscale = cv2.cvtColor(image_bruit, cv2.COLOR_RGB2GRAY)

print("max brut_grayscle : ", np.max(brut_grayscale))
print("init grayscale type : ", init_grayscale.dtype)
print("brut grayscale type : ", brut_grayscale.dtype)

test = cv2.divide((brut_grayscale-init_grayscale),(init_grayscale))

####################
# Irms, std, mean
intensite_rms = np.sqrt(np.mean(np.square(test)))
print("Intensité RMS de l'image :", intensite_rms)
mean, std_dev = cv2.meanStdDev(test)
print("ecart type de l'image :", std_dev[0][0])
print("Moyenne de l'image :", mean[0][0])
print("variance de l'image :", std_dev[0][0]**2)

####################
# Matplotlib
import matplotlib.pyplot as plt
plt.imshow((test * 65535), cmap='gray')
plt.imshow((brut_grayscale * 65535), cmap='gray')
plt.show()

enter image description here

Do you have any idea to do it ?

1

There are 1 answers

1
murat taşçı On BEST ANSWER
# Step 1: Create an empty mask of the same shape as your image
mask = np.zeros_like(brut_grayscale)

# Step 2: Create a circle in the mask
height, width = mask.shape
center_y, center_x = height // 2, width // 2
radius = min(height, width) // 4  # Adjust the radius as needed

cv2.circle(mask, (center_x, center_y), radius, 1, thickness=-1)

# Step 3: Apply the mask to your image
masked_image = cv2.bitwise_and(brut_grayscale, brut_grayscale, mask=mask)

# Step 4: Perform calculations only on masked region
mean_masked, std_dev_masked = cv2.meanStdDev(brut_grayscale, mask=mask)

print("Mean of masked region:", mean_masked[0][0])
print("Std Dev of masked region:", std_dev_masked[0][0])

# Optionally: Show the masked image
plt.imshow(masked_image, cmap='gray')
plt.show()