def stitch(targets,images): mask = rois_mask(targets) # True where image data is gaps_mask = mask==False # True where infill needs to go # compute bounds relative to the camera field (x,y,w,h) = stitched_box(targets) uroi = img_as_float(stitch_raw(targets,images,(x,y,w,h))) # stitch with black infill # step 1: sparsely sample background mostly ignoring blob # compute gradient on both axes k = [[-3,-1,0,1,3], [-3,-1,0,1,3], [-3,-1,0,1,3], [-3,-1,0,1,3]] gy = convolve(uroi,k) gx = convolve(uroi,np.rot90(k)) # ignore all but low-gradient areas bg = (abs(gy+gx) < 0.2) & mask # step 2: remove less contiguous areas filter_size = max(2,int(max(h,w)/200)) mf = minimum_filter(bg*1,filter_size) # step 3: interpolate between samples z = inpaint(uroi*mf,mf==False) # step 4: subsample and re-interpolate to degrade artifacts in fill region random = RandomState(0) (h,w)=z.shape ng = random.rand(h,w) < 0.01 z2 = inpaint(z*ng,ng==False) # step 5: final composite roi = (z2 * gaps_mask) + uroi return (roi * 255).astype(np.uint8), mask
def get_roi_image_from_files(schema_version, adc_path, roi_path, bin_pid, target_no, fast_stitching=False, mask=False, stitch_version=1): to_stitch = app.config[STITCH] if to_stitch: offset=max(1,target_no-1) limit=3 # read three targets, in case we need to stitch else: offset=target_no limit=1 # just read one targets = list(read_targets(adc_path, offset, limit, schema_version)) if len(targets) == 0: # no targets? return Not Found return None # open the ROI file as we may need to read more than one with open(roi_path,'rb') as roi_file: if to_stitch: pairs = list(find_pairs(targets)) # look for stitched pairs else: pairs = targets roi_image = None if len(pairs) >= 1: # found one? (a,b) = pairs[0] # split pair if b[TARGET_NUMBER] == target_no: # second of a pair? return None images = list(read_rois((a,b),roi_file=roi_file)) # read the images if mask: roi_image = stitching.mask((a,b)) elif fast_stitching: roi_image = stitch_raw((a,b), images, background=180) elif stitch_version == 1: (roi_image, mask) = stitch((a,b), images) # stitch them elif stitch_version == 2: (roi_image, mask) = joe_stitch((a,b), images) else: # now check that the target number is correct for target in targets: if target[TARGET_NUMBER] == target_no: images = list(read_rois([target],roi_file=roi_file)) # read the image roi_image = images[0] return roi_image