From 08f3ff2dc698293a06af310ae7627ac9626f7519 Mon Sep 17 00:00:00 2001 From: nathancrossley Date: Fri, 8 Nov 2024 14:21:13 +0000 Subject: [PATCH 1/2] Update ACRObject.py Updated with new rotation function for testing --- hazenlib/ACRObject.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/hazenlib/ACRObject.py b/hazenlib/ACRObject.py index 90f7371e..207a8ec2 100644 --- a/hazenlib/ACRObject.py +++ b/hazenlib/ACRObject.py @@ -95,6 +95,31 @@ def order_phantom_slices(self, dcm_list): # else: # print("LR orientation swap not required.") + def calculate_rotation(img): + img = (np.maximum(img,0) / img.max()) * 255 + img = np.uint8(img) + + imgBlur = cv.GaussianBlur(img, (13,13), 0) + canny_edge = cv.Canny(imgBlur, 5, 40) + + test_angles = np.linspace(-pi/2, pi/2, 1801, endpoint = False) + hough_space, theta, rho = hough_line(canny_edge, test_angles) + _ , bestTheta, _ = hough_line_peaks(hough_space, theta, rho, min_angle = 10) + rotation = np.mean(bestTheta[:2]) * 180/pi + + # Transform to be angle wrt positive x axis. + rotation = 90 - (rotation + 180) + + # Select positive angle that is less than 360 deg. + while rotation < 0: + rotation += 180 + + if rotation > 360: + rotation -= 360 + + # Angle in degrees from positive x-axis. + return rotation + @staticmethod def determine_rotation(img): """Determine the rotation angle of the phantom using edge detection and the Hough transform. From 60f41560b0a2dc0221d1ec837d1be3d2f45acf64 Mon Sep 17 00:00:00 2001 From: nathancrossley Date: Wed, 4 Dec 2024 14:02:45 +0000 Subject: [PATCH 2/2] Update ACRObject.py Updated with docstrings. --- hazenlib/ACRObject.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/hazenlib/ACRObject.py b/hazenlib/ACRObject.py index 207a8ec2..6f51121e 100644 --- a/hazenlib/ACRObject.py +++ b/hazenlib/ACRObject.py @@ -96,6 +96,15 @@ def order_phantom_slices(self, dcm_list): # print("LR orientation swap not required.") def calculate_rotation(img): + """Calculate the rotation angle of the phantom using a Gaussian blur, Canny edge detection and the Hough Transform. + + Args: + img (np.ndarray): pixel array of a DICOM object + + Returns: + float: The rotation angle of the phantom wrt the positive x-axis in degrees. + """ + img = (np.maximum(img,0) / img.max()) * 255 img = np.uint8(img)