Skip to content

Commit

Permalink
Extended to support 2d masks
Browse files Browse the repository at this point in the history
  • Loading branch information
aditiiyer committed May 22, 2024
1 parent 7e76b4d commit ae2ab93
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions cerr/utils/bbox.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
import numpy as np

def compute_boundingbox(x3D, maskFlag=0):
# Finding the bounding box parameters
def compute_boundingbox(binaryMaskM, is2DFlag=False, maskFlag=0):
# Finding extents of bounding box given a binary mask
# If maskFlag > 0, it is interpreted as a padding parameter
# If sliceWiseFlag is True slicewise extents are returned


maskFlag = int(maskFlag)

iV, jV, kV = np.where(x3D)
minr = np.min(iV)
maxr = np.max(iV)
minc = np.min(jV)
maxc = np.max(jV)
mins = np.min(kV)
maxs = np.max(kV)
if is2DFlag:

iV, jV = np.where(binaryMaskM)
kV = []
minr = np.min(iV)
maxr = np.max(iV)
minc = np.min(jV)
maxc = np.max(jV)
mins = []
maxs = []
else:
iV, jV, kV = np.where(binaryMaskM)
minr = np.min(iV)
maxr = np.max(iV)
minc = np.min(jV)
maxc = np.max(jV)
mins = np.min(kV)
maxs = np.max(kV)

bboxmask = None

if maskFlag != 0:
bboxmask = np.zeros_like(x3D)
bboxmask = np.zeros_like(binaryMaskM)

if maskFlag > 0:
siz = x3D.shape
siz = binaryMaskM.shape
minr -= maskFlag
maxr += maskFlag
if maxr >= siz[0]:
Expand All @@ -33,13 +46,18 @@ def compute_boundingbox(x3D, maskFlag=0):
maxc = siz[1] - 1
if minc < 0:
minc = 0
mins -= maskFlag
maxs += maskFlag
if maxs >= siz[2]:
maxs = siz[2] - 1
if mins < 0:
mins = 0

bboxmask[minr:maxr+1, minc:maxc+1, mins:maxs+1] = 1

else:
mins -= maskFlag
maxs += maskFlag
if maxs >= siz[2]:
maxs = siz[2] - 1
if mins < 0:
mins = 0

if is2DFlag:
bboxmask[minr:maxr+1, minc:maxc+1] = 1
else:
bboxmask[minr:maxr+1, minc:maxc+1, mins:maxs+1] = 1

return minr, maxr, minc, maxc, mins, maxs, bboxmask

0 comments on commit ae2ab93

Please sign in to comment.