Skip to content

Commit

Permalink
Fix findContours call for recent versions of opencv
Browse files Browse the repository at this point in the history
  • Loading branch information
iimog committed Aug 31, 2022
1 parent da49225 commit f5ab9f6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions common/cardiac_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def determine_aha_coordinate_system(seg_sa, affine_sa):
rv = get_largest_cc(rv).astype(np.uint8)

# Extract epicardial contour
_, contours, _ = cv2.findContours(cv2.inRange(epi, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contours, _ = cv2.findContours(cv2.inRange(epi, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
epi_contour = contours[0][:, 0, :]

# Find the septum, which is the intersection between LV and RV
Expand Down Expand Up @@ -417,11 +417,11 @@ def evaluate_wall_thickness(seg_name, output_name_stem, part=None):
# Extract endocardial contour
# Note: cv2 considers an input image as a Y x X array, which is different
# from nibabel which assumes a X x Y array.
_, contours, _ = cv2.findContours(cv2.inRange(endo, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contours, _ = cv2.findContours(cv2.inRange(endo, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
endo_contour = contours[0][:, 0, :]

# Extract epicardial contour
_, contours, _ = cv2.findContours(cv2.inRange(epi, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contours, _ = cv2.findContours(cv2.inRange(epi, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
epi_contour = contours[0][:, 0, :]

# Smooth the contours
Expand Down Expand Up @@ -612,7 +612,7 @@ def extract_myocardial_contour(seg_name, contour_name_stem, part=None, three_sli
lv_centre = np.dot(affine, np.array([cx, cy, z, 1]))[:3]

# Extract epicardial contour
_, contours, _ = cv2.findContours(cv2.inRange(epi, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contours, _ = cv2.findContours(cv2.inRange(epi, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
epi_contour = contours[0][:, 0, :]
epi_contour = approximate_contour(epi_contour, periodic=True)

Expand Down Expand Up @@ -666,7 +666,7 @@ def extract_myocardial_contour(seg_name, contour_name_stem, part=None, three_sli
# Extract endocardial contour
# Note: cv2 considers an input image as a Y x X array, which is different
# from nibabel which assumes a X x Y array.
_, contours, _ = cv2.findContours(cv2.inRange(endo, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contours, _ = cv2.findContours(cv2.inRange(endo, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
endo_contour = contours[0][:, 0, :]
endo_contour = approximate_contour(endo_contour, periodic=True)

Expand Down Expand Up @@ -1187,11 +1187,11 @@ def extract_la_myocardial_contour(seg_la_name, seg_sa_name, contour_name):
# Extract endocardial contour
# Note: cv2 considers an input image as a Y x X array, which is different
# from nibabel which assumes a X x Y array.
_, contours, _ = cv2.findContours(cv2.inRange(endo, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contours, _ = cv2.findContours(cv2.inRange(endo, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
endo_contour = contours[0][:, 0, :]

# Extract epicardial contour
_, contours, _ = cv2.findContours(cv2.inRange(epi, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contours, _ = cv2.findContours(cv2.inRange(epi, 1, 1), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
epi_contour = contours[0][:, 0, :]

# Record the points located on the mitral valve plane.
Expand Down
4 changes: 2 additions & 2 deletions common/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ def distance_metric(seg_A, seg_B, dx):
# The distance is defined only when both contours exist on this slice
if np.sum(slice_A) > 0 and np.sum(slice_B) > 0:
# Find contours and retrieve all the points
_, contours, _ = cv2.findContours(cv2.inRange(slice_A, 1, 1),
contours, _ = cv2.findContours(cv2.inRange(slice_A, 1, 1),
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
pts_A = contours[0]
for i in range(1, len(contours)):
pts_A = np.vstack((pts_A, contours[i]))

_, contours, _ = cv2.findContours(cv2.inRange(slice_B, 1, 1),
contours, _ = cv2.findContours(cv2.inRange(slice_B, 1, 1),
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
pts_B = contours[0]
Expand Down

0 comments on commit f5ab9f6

Please sign in to comment.