Circle cropping for multiple images: overlapping images, dark images, high-saturation images
00:37 17 Aug 2026

I got some images like that from the Eye disease Classification dataset or EDC dataset availabe on Kaggle to feed MobileNet V3 Large model for classification images:

Image 1

Image 2

image 3

My problem is that I don't know how I can effectively apply circle cropping to images like that because with the code that I am working on do these circle cropping instead:

circle cropping from image 1

circle cropping from image 2

circle cropping from image 3

This is the code that works for the most images of the dataset (or I hope so):

# I put a single image for now, in the future I am going to do it in a iterable way for each image

ruta = "_75_6801378.jpg" 
img = cv2.imread(ruta)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


mask_binary = cv2.inRange(gray, 15, 240)
kernel = np.ones((3,3), np.uint8)
mask_clean = cv2.morphologyEx(mask_binary, cv2.MORPH_CLOSE, kernel)
mask_clean = cv2.morphologyEx(mask_clean, cv2.MORPH_OPEN, kernel)



contours, hierarchy = cv2.findContours(mask_clean, cv2.RETR_EXTERNAL,
                               cv2.CHAIN_APPROX_SIMPLE)

contorno_principal = max(contours, key=cv2.contourArea)

elipse = cv2.fitEllipse(contorno_principal)

mask_perfecta2 = np.zeros(gray.shape, dtype=np.uint8)
cv2.ellipse(mask_perfecta2, elipse, 255, thickness = -1)

cv2.ellipse(img_rgb, elipse, (255,0,0), thickness = 3)

imagen_aislada2 = cv2.bitwise_and(img_rgb, img_rgb, mask = mask_perfecta2)

x,y,w,h = cv2.boundingRect(contorno_principal)

imagen_recortada2 = imagen_aislada2[y: y+h, x:x+w]


plt.figure(figsize=(25,4))

plt.subplot(1,3,1),plt.imshow(mask_perfecta2, cmap='gray')
plt.title('Perfect Mask')

plt.subplot(1,3,2),plt.imshow(img_rgb)


plt.subplot(1,3,3),plt.imshow(imagen_recortada2)
plt.title('Circle-Cropping')
python opencv image-processing