def compute_chip(img_fpath, chip_fpath, roi, new_size, preproc_prefs): # Read image img = Image.open(img_fpath) [img_w, img_h] = [gdim - 1 for gdim in img.size] # Ensure ROI is within bounds [roi_x, roi_y, roi_w, roi_h] = [max(0, cdim) for cdim in roi] roi_x2 = min(img_w, roi_x + roi_w) roi_y2 = min(img_h, roi_y + roi_h) # Crop out ROI: left, upper, right, lower raw_chip = img.crop((roi_x, roi_y, roi_x2, roi_y2)) # Scale chip, but do not rotate pil_chip = raw_chip.convert('L').resize(new_size, Image.ANTIALIAS) # Preprocessing based on preferences if preproc_prefs.histeq_bit: pil_chip = histeq(pil_chip) if preproc_prefs.adapt_histeq_bit: pil_chip = Image.fromarray(adapt_histeq(np.asarray(pil_chip))) if preproc_prefs.contrast_stretch_bit: pil_chip = Image.fromarray(contrast_stretch(np.asarray(pil_chip))) if preproc_prefs.autocontrast_bit: pil_chip = ImageOps.autocontrast(pil_chip) if preproc_prefs.bilateral_filt_bit: raise NotImplemented('Bilateral filter implementation removed.') #pil_chip = shiftableBF(pil_chip) # Save chip to disk pil_chip.save(chip_fpath, 'PNG')
def preprocess_chip(am, pil_filt): logdbg('prepocessing') # Convert to grayscale # raw_chip = cm.cx2_raw_chip(6) # --- Resize --- # --- Filters --- #if am.algo_prefs.preproc.histeq_bit : ##pil_filt = ImageOps.equalize(pil_filt) #img_rescale = exposure.equalize_hist(np.asarray(pil_filt)) #pil_filt = Image.fromarray(histeq(np.asarray(pil_filt))).convert('L') if am.algo_prefs.preproc.histeq_bit: from hotspotter.algo.imalgos import histeq logdbg('Equalizing Histogram') pil_filt = histeq(pil_filt) if am.algo_prefs.preproc.adapt_histeq_bit: from hotspotter.algo.imalgos import adapt_histeq logdbg('Adaptive Equalizing Histogram') pil_filt = Image.fromarray(adapt_histeq(np.asarray(pil_filt))) if am.algo_prefs.preproc.contrast_stretch_bit: from hotspotter.algo.imalgos import contrast_stretch logdbg('Stretching Histogram') pil_filt = Image.fromarray(contrast_stretch(np.asarray(pil_filt))) if am.algo_prefs.preproc.autocontrast_bit : logdbg('PIL Autocontrast') pil_filt = ImageOps.autocontrast(pil_filt) if am.algo_prefs.preproc.bilateral_filt_bit : logdbg('O(1) Bilateral Filter Approximation') from hotspotter.tpl.other.shiftableBF import shiftableBF pil_filt = Image.fromarray(shiftableBF(np.asarray(pil_filt))) return pil_filt
def preprocess_chip(am, pil_filt): logdbg('prepocessing') # Convert to grayscale # raw_chip = cm.cx2_raw_chip(6) # --- Resize --- # --- Filters --- #if am.algo_prefs.preproc.histeq_bit : ##pil_filt = ImageOps.equalize(pil_filt) #img_rescale = exposure.equalize_hist(np.asarray(pil_filt)) #pil_filt = Image.fromarray(histeq(np.asarray(pil_filt))).convert('L') if am.algo_prefs.preproc.histeq_bit: from hotspotter.algo.imalgos import histeq logdbg('Equalizing Histogram') pil_filt = histeq(pil_filt) if am.algo_prefs.preproc.adapt_histeq_bit: from hotspotter.algo.imalgos import adapt_histeq logdbg('Adaptive Equalizing Histogram') pil_filt = Image.fromarray(adapt_histeq(np.asarray(pil_filt))) if am.algo_prefs.preproc.contrast_stretch_bit: from hotspotter.algo.imalgos import contrast_stretch logdbg('Stretching Histogram') pil_filt = Image.fromarray(contrast_stretch(np.asarray(pil_filt))) if am.algo_prefs.preproc.autocontrast_bit: logdbg('PIL Autocontrast') pil_filt = ImageOps.autocontrast(pil_filt) if am.algo_prefs.preproc.bilateral_filt_bit: logdbg('O(1) Bilateral Filter Approximation') from hotspotter.tpl.other.shiftableBF import shiftableBF pil_filt = Image.fromarray(shiftableBF(np.asarray(pil_filt))) return pil_filt
def compute_chip(img_fpath, chip_fpath, roi, new_size, preproc_prefs): # Read image img = Image.open(img_fpath) [img_w, img_h] = [ gdim - 1 for gdim in img.size ] # Ensure ROI is within bounds [roi_x, roi_y, roi_w, roi_h] = [ max(0, cdim) for cdim in roi] roi_x2 = min(img_w, roi_x + roi_w) roi_y2 = min(img_h, roi_y + roi_h) # Crop out ROI: left, upper, right, lower raw_chip = img.crop((roi_x, roi_y, roi_x2, roi_y2)) # Scale chip, but do not rotate pil_chip = raw_chip.convert('L').resize(new_size, Image.ANTIALIAS) # Preprocessing based on preferences if preproc_prefs.histeq_bit: pil_chip = histeq(pil_chip) if preproc_prefs.adapt_histeq_bit: pil_chip = Image.fromarray(adapt_histeq(np.asarray(pil_chip))) if preproc_prefs.contrast_stretch_bit: pil_chip = Image.fromarray(contrast_stretch(np.asarray(pil_chip))) if preproc_prefs.autocontrast_bit : pil_chip = ImageOps.autocontrast(pil_chip) if preproc_prefs.bilateral_filt_bit : raise NotImplemented('Bilateral filter implementation removed.') #pil_chip = shiftableBF(pil_chip) # Save chip to disk pil_chip.save(chip_fpath, 'PNG')