print("Processing cell ",obj1+1," of ",wsh.max()," for oversegmentation.")
maskobj1=dilobjs[obj1,:,:]
dil1=dil_objs[obj1,:,:]
ifnp.sum(maskobj1)>0:#maskobj1 can be empty because in the loop, maskobj2 can be deleted if it is joined with a (previous) maskobj1
ifnp.sum(dil1)>0:#dil1 can be empty because in the loop, maskobj2 can be deleted if it is joined with a (previous) maskobj1
objcounter=objcounter+1
maskoriobj1=oriobjs[obj1,:,:]
orig1=objs[obj1,:,:]
forobj2inrange(obj1+1,wsh.max()):
maskobj2=dilobjs[obj2,:,:]
dil2=dil_objs[obj2,:,:]
if (np.sum(maskobj2)>0and#maskobj1 and 2 can be empty because joined with maskobj2's and then maskobj2's deleted (set to zero)
(((objcoords[obj1,0]-2<objcoords[obj2,0]andobjcoords[obj1,1]+2>objcoords[obj2,0])or# do the bounding boxes overlap? plus/minus 2 pixels to allow for bad bounding box measurement
border=maskobj1*maskobj2#intersection of two masks constitutes a border
# borderarea = np.sum(border)
# borderpred = border * pred
# borderheight = np.sum(borderpred)
borderprednonzero=pred[np.nonzero(border)]# all the prediction values inside the border area
sortborderprednonzero=sorted(borderprednonzero)# sort the values
borderprednonzeroarea=len(borderprednonzero)# how many values are there?
quartborderarea=round(borderprednonzeroarea/4)# take one fourth of the values. there is some subtlety about how round() rounds but doesn't matter
topborderpred=sortborderprednonzero[quartborderarea:]# take top 3/4 of the predictions
topborderheight=np.sum(topborderpred)# sum over top 3/4 of the predictions
topborderarea=len(topborderpred)# area of 3/4 of predictions. In principle equal to 3/4 of borderprednonzeroarea but because of strange rounding, will just measure again
iftopborderarea>8:# SJR: Not only must borderarea be greater than 0 but also have a little bit of border to go on.
iftopborderheight/topborderarea>0.99:# SJR: We are really deep inside a cell, where the prediction is =1. Won't use: borderheight/borderarea > 0.95. Redundant.
#print("--")
#print(objcounter)
#wsh=np.where(wsh==obj2+1, obj1+1, wsh)
maskoriobj1=np.uint8(np.multiply((maskoriobj1>0)|(oriobjs[obj2,:,:]>0),1))#have to do boolean then integer just to do an 'or'
dilobjs[obj1,:,:]=np.uint8(np.multiply((maskobj1>0)|(maskobj2>0),1))#have to do boolean then integer just to do an 'or'
if (do_box_overlap(obj_coords[obj1,:],obj_coords[obj2,:])
andnp.sum(dil2)>0):
border=dil1*dil2
border_pred=pred[border]
# Border is too small to be considered
iflen(border_pred)<32:
continue
# Sum of top 25% of predicted border values
q75=np.quantile(border_pred,.75)
top_border_pred=border_pred[border_pred>q75]
top_border_height=top_border_pred.sum()
top_border_area=len(top_border_pred)
# borderprednonzero = pred[np.nonzero(border)] # all the prediction values inside the border area
# sortborderprednonzero = sorted(borderprednonzero) # sort the values
# borderprednonzeroarea = len(borderprednonzero) # how many values are there?
# quartborderarea = round(borderprednonzeroarea/4) # take one fourth of the values. there is some subtlety about how round() rounds but doesn't matter
# topborderpred = sortborderprednonzero[quartborderarea:] # take top 3/4 of the predictions
# topborderheight = np.sum(topborderpred) # sum over top 3/4 of the predictions
# topborderarea = len(topborderpred) # area of 3/4 of predictions. In principle equal to 3/4 of borderprednonzeroarea but because of strange rounding, will just measure again
# if topborderheight/topborderarea > 0.99 : # SJR: We are really deep inside a cell, where the prediction is =1. Won't use: borderheight/borderarea > 0.95. Redundant.
# #print("--")
# #print(objcounter)
# #wsh=np.where(wsh==obj2+1, obj1+1, wsh)
# maskoriobj1 = np.uint8(np.multiply((maskoriobj1 > 0) | (oriobjs[obj2,:,:] > 0),1)) #have to do boolean then integer just to do an 'or'
# dilobjs[obj1,:,:] = np.uint8(np.multiply((maskobj1 > 0) | (maskobj2 > 0),1)) #have to do boolean then integer just to do an 'or'