Skip to content

Update ACRObject.py with phantom rotation detection function. #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions hazenlib/ACRObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,40 @@
# else:
# 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.

Check warning on line 100 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

W293 blank line contains whitespace

Check warning on line 100 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

W293 blank line contains whitespace
Args:

Check warning on line 101 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

W291 trailing whitespace

Check warning on line 101 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

W291 trailing whitespace
img (np.ndarray): pixel array of a DICOM object

Check warning on line 103 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

W293 blank line contains whitespace

Check warning on line 103 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

W293 blank line contains whitespace
Returns:
float: The rotation angle of the phantom wrt the positive x-axis in degrees.
"""

img = (np.maximum(img,0) / img.max()) * 255

Check failure on line 108 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

E231 missing whitespace after ','

Check failure on line 108 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

E231 missing whitespace after ','
img = np.uint8(img)

imgBlur = cv.GaussianBlur(img, (13,13), 0)

Check warning on line 111 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

F821 undefined name 'cv'

Check failure on line 111 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

E231 missing whitespace after ','

Check warning on line 111 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

F821 undefined name 'cv'

Check failure on line 111 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

E231 missing whitespace after ','
canny_edge = cv.Canny(imgBlur, 5, 40)

Check warning on line 112 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

F821 undefined name 'cv'

Check warning on line 112 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

F821 undefined name 'cv'

test_angles = np.linspace(-pi/2, pi/2, 1801, endpoint = False)

Check warning on line 114 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

F821 undefined name 'pi'

Check warning on line 114 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

F821 undefined name 'pi'

Check failure on line 114 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

E251 unexpected spaces around keyword / parameter equals

Check failure on line 114 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

E251 unexpected spaces around keyword / parameter equals

Check warning on line 114 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

F821 undefined name 'pi'

Check warning on line 114 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

F821 undefined name 'pi'

Check failure on line 114 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

E251 unexpected spaces around keyword / parameter equals

Check failure on line 114 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

E251 unexpected spaces around keyword / parameter equals
hough_space, theta, rho = hough_line(canny_edge, test_angles)

Check warning on line 115 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

F821 undefined name 'hough_line'

Check warning on line 115 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

F821 undefined name 'hough_line'
_ , bestTheta, _ = hough_line_peaks(hough_space, theta, rho, min_angle = 10)

Check warning on line 116 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

F821 undefined name 'hough_line_peaks'

Check failure on line 116 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

E203 whitespace before ','

Check failure on line 116 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

E251 unexpected spaces around keyword / parameter equals

Check failure on line 116 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

E251 unexpected spaces around keyword / parameter equals

Check warning on line 116 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

F821 undefined name 'hough_line_peaks'

Check failure on line 116 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

E203 whitespace before ','

Check failure on line 116 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

E251 unexpected spaces around keyword / parameter equals

Check failure on line 116 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

E251 unexpected spaces around keyword / parameter equals
rotation = np.mean(bestTheta[:2]) * 180/pi

Check warning on line 117 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.9)

F821 undefined name 'pi'

Check warning on line 117 in hazenlib/ACRObject.py

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 3.11)

F821 undefined name '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.
Expand Down
Loading