Spaces:
Sleeping
Sleeping
| """ | |
| vCDR (vertical Cup-to-Disc Ratio) Computation | |
| Computes vCDR from fundus images using Hough Circle Transform | |
| """ | |
| import cv2 | |
| import numpy as np | |
| def compute_robust_vcdr(image_path): | |
| """ | |
| Compute vCDR from fundus image | |
| Returns: vCDR value (float between 0 and 1) | |
| """ | |
| try: | |
| # Read image | |
| img = cv2.imread(str(image_path)) | |
| if img is None: | |
| return 0.5 # Default value | |
| # Resize for processing | |
| process_size = 512 | |
| img_resized = cv2.resize(img, (process_size, process_size)) | |
| gray = cv2.cvtColor(img_resized, cv2.COLOR_BGR2GRAY) | |
| blurred = cv2.GaussianBlur(gray, (9, 9), 2) | |
| scale_factor = process_size / 512.0 | |
| # Detect optic disc (larger circle) | |
| circles_disc = cv2.HoughCircles( | |
| blurred, cv2.HOUGH_GRADIENT, dp=1, | |
| minDist=int(100 * scale_factor), | |
| param1=50, param2=30, | |
| minRadius=int(30 * scale_factor), | |
| maxRadius=int(250 * scale_factor) | |
| ) | |
| # Detect optic cup (smaller circle) | |
| circles_cup = cv2.HoughCircles( | |
| blurred, cv2.HOUGH_GRADIENT, dp=1, | |
| minDist=int(50 * scale_factor), | |
| param1=50, param2=15, | |
| minRadius=int(10 * scale_factor), | |
| maxRadius=int(120 * scale_factor) | |
| ) | |
| if circles_disc is not None: | |
| disc_r = circles_disc[0][0][2] | |
| cup_r = circles_cup[0][0][2] if circles_cup is not None else disc_r * 0.3 | |
| vcdr = cup_r / disc_r | |
| return min(max(vcdr, 0.1), 0.9) # Clamp between 0.1 and 0.9 | |
| return 0.5 # Default if detection fails | |
| except Exception as e: | |
| print(f"Error computing vCDR: {e}") | |
| return 0.5 | |