def cutouts_plot(outpath, base_name, order_num, obj, flat, top_trace, bot_trace, trace): pl.figure('traces', facecolor='white', figsize=(8, 5)) pl.cla() pl.suptitle('order cutouts, {}, order {}'.format(base_name, order_num), fontsize=14) pl.set_cmap('Blues_r') obj_plot = pl.subplot(2, 1, 1) try: obj_plot.imshow(exposure.equalize_hist(obj)) except: obj_plot.imshow(obj) obj_plot.plot(np.arange(1024), top_trace, 'y-', linewidth=1.5) obj_plot.plot(np.arange(1024), bot_trace, 'y-', linewidth=1.5) obj_plot.plot(np.arange(1024), trace, 'y-', linewidth=1.5) obj_plot.set_title('object') # obj_plot.set_ylim([1023, 0]) obj_plot.set_xlim([0, 1023]) flat_plot = pl.subplot(2, 1, 2) try: flat_plot.imshow(exposure.equalize_hist(flat)) except: flat_plot.imshow(flat) flat_plot.plot(np.arange(1024), top_trace, 'y-', linewidth=1.5) flat_plot.plot(np.arange(1024), bot_trace, 'y-', linewidth=1.5) flat_plot.plot(np.arange(1024), trace, 'y-', linewidth=1.5) flat_plot.set_title('flat') # flat_plot.set_ylim([1023, 0]) flat_plot.set_xlim([0, 1023]) pl.tight_layout() pl.savefig(constructFileName(outpath, base_name, order_num, 'cutouts.png')) pl.close()
def sparect_plot(outpath, base_name, order_num, obj, flat): pl.figure('spatially rectified', facecolor='white', figsize=(8, 5)) pl.cla() pl.suptitle('spatially rectified, {}, order {}'.format(base_name, order_num), fontsize=14) pl.set_cmap('Blues_r') obj_plot = pl.subplot(2, 1, 1) try: obj_plot.imshow(exposure.equalize_hist(obj)) except: obj_plot.imshow(obj) obj_plot.set_title('object') # obj_plot.set_ylim([1023, 0]) obj_plot.set_xlim([0, 1023]) flat_plot = pl.subplot(2, 1, 2) try: flat_plot.imshow(exposure.equalize_hist(flat)) except: flat_plot.imshow(flat) flat_plot.set_title('flat') # flat_plot.set_ylim([1023, 0]) flat_plot.set_xlim([0, 1023]) pl.tight_layout() pl.savefig(constructFileName(outpath, base_name, order_num, 'sparect.png')) pl.close()
def traces_plot(outpath, base_name, order_num, obj, flat, top_trace, bot_trace): pl.figure('traces', facecolor='white', figsize=(8, 5)) pl.cla() pl.suptitle('order edge traces, {}, order {}'.format(base_name, order_num), fontsize=14) pl.set_cmap('Blues_r') pl.rcParams['ytick.labelsize'] = 8 obj_plot = pl.subplot(1, 2, 1) obj_plot.imshow(exposure.equalize_hist(obj)) obj_plot.plot(np.arange(1024), top_trace, 'y-', linewidth=1.5) obj_plot.plot(np.arange(1024), bot_trace, 'y-', linewidth=1.5) obj_plot.set_title('object') obj_plot.set_ylim([1023, 0]) obj_plot.set_xlim([0, 1023]) flat_plot = pl.subplot(1, 2, 2) flat_plot.imshow(exposure.equalize_hist(flat)) flat_plot.plot(np.arange(1024), top_trace, 'y-', linewidth=1.5) flat_plot.plot(np.arange(1024), bot_trace, 'y-', linewidth=1.5) flat_plot.set_title('flat') flat_plot.set_ylim([1023, 0]) flat_plot.set_xlim([0, 1023]) pl.tight_layout() pl.savefig(constructFileName(outpath, base_name, order_num, 'traces.png')) pl.close()
def tops_bots_plot(outpath, base_name, tops, bots): pl.figure('edges', facecolor='white', figsize=(8, 5)) pl.cla() pl.suptitle('top and bottom order edges, {}'.format(base_name), fontsize=14) # pl.set_cmap('Blues_r') pl.rcParams['ytick.labelsize'] = 8 obj_plot = pl.subplot(1, 2, 1) try: obj_plot.imshow(exposure.equalize_hist(tops)) except: obj_plot.imshow(tops) obj_plot.set_title('top edges') obj_plot.set_ylim([1023, 0]) obj_plot.set_xlim([0, 1023]) flat_plot = pl.subplot(1, 2, 2) try: flat_plot.imshow(exposure.equalize_hist(bots)) except: flat_plot.imshow(bots) flat_plot.set_title('bottom edges') flat_plot.set_ylim([1023, 0]) flat_plot.set_xlim([0, 1023]) pl.tight_layout() pl.savefig(constructFileName(outpath, base_name, None, 'top_bot_edges.png')) pl.close()
def align_img(image_name, loss_fn=sum_of_squared_diff, max_disp = 15, big=False, name=None, hist_eq = False): b,g,r = get_bgr(image_name) if hist_eq == True: b, g, r = exposure.equalize_hist(b), exposure.equalize_hist(g), exposure.equalize_hist(r) print("Aligning green and blue: ") ag = align(g, b, loss_fn=loss_fn, max_disp = max_disp, big=big, name=name) print("Aligning blue and red: ") ar = align(r, b, loss_fn=loss_fn, max_disp = max_disp, big=big, name=name) # create a color image im_out = np.dstack([ar, ag, b]) plt.show() # save the image iname = image_name.split('.') iname[-1] = 'jpg' image_name = '.'.join(iname) fname = 'out_' + image_name skio.imsave(fname, im_out) # display the image skio.imshow(im_out) plt.show() skio.show()
def test_equalize_masked(): img = skimage.img_as_float(test_img) mask = np.zeros(test_img.shape) mask[50:150, 50:250] = 1 img_mask_eq = exposure.equalize_hist(img, mask=mask) img_eq = exposure.equalize_hist(img) cdf, bin_edges = exposure.cumulative_distribution(img_mask_eq) check_cdf_slope(cdf) assert not (img_eq == img_mask_eq).all()
def slidingWindow(P,inX=3,outX=32,inY=3,outY=64,maxM=50,norm=True): """ Enhance the constrast Cut off extreme values and demean the image Utilize scipy convolve2d to get the mean at a given pixel Remove local mean with inner exclusion region Args: P: 2-d numpy array image inX: inner exclusion region in the x-dimension outX: length of the window in the x-dimension inY: inner exclusion region in the y-dimension outY: length of the window in the y-dimension maxM: size of the output image in the y-dimension norm: boolean to cut off extreme values Returns: Q: 2-d numpy contrast enhanced """ Q = P.copy() m, n = Q.shape Q = exposure.equalize_hist(Q.astype('Float32'), nbins = 65) Q = detrend(Q.astype('Float32'), axis = 1) Q = detrend(Q.astype('Float32'), axis = 0) Q = wiener(Q.astype('Float32'), 4) return Q[:maxM,:]
def twoDimOrderPlot(outpath, base_name, title, obj_name, base_filename, order_num, data, x): pl.figure('2d order image', facecolor='white', figsize=(8, 5)) pl.cla() pl.title(title + ', ' + base_name + ", order " + str(order_num), fontsize=14) pl.xlabel('wavelength($\AA$)', fontsize=12) pl.ylabel('row (pixel)', fontsize=12) #pl.imshow(img, aspect='auto') #pl.imshow(data, vmin=0, vmax=1024, aspect='auto') pl.imshow(exposure.equalize_hist(data), origin='lower', extent=[x[0], x[-1], 0, data.shape[0]], aspect='auto') # from matplotlib import colors # norm = colors.LogNorm(data.mean() + 0.5 * data.std(), data.max(), clip='True') # pl.imshow(data, norm=norm, origin='lower', # extent=[x[0], x[-1], 0, data.shape[0]], aspect='auto') pl.colorbar() pl.set_cmap('jet') # pl.set_cmap('Blues_r') fn = constructFileName(outpath, base_name, order_num, base_filename) pl.savefig(fn) log_fn(fn) pl.close() # np.save(fn[:fn.rfind('.')], data) return
def mirror_mirror(): pca, clf = gen_classifier() cam = cv2.VideoCapture(0) last_emo = [] while True: ret, img = cam.read() if not ret: continue gray = cv2.cvtColor(img, cv.CV_BGR2GRAY) faces = detect_and_scale_face(gray) if not faces: continue for scaled in faces: test_x = exposure.equalize_hist(scaled.reshape((1, -1))) face_pca = pca.transform(test_x) pred = int(clf.predict(face_pca)[0]) last_emo.append(pred) if len(last_emo) == 4: last_emo.pop(0) if last_emo[0] != OTHER_LABEL and \ np.all(np.array(last_emo) == last_emo[0]): sb.call(['say', TARGET_NAMES[pred]])
def preprocessing(image): # Contrast stretching #TODO TEST #p2, p98 = np.percentile(image, (2, 98)) #image_rescale = exposure.rescale_intensity(image, in_range=(p2, p98)) gray = rgb2gray(image) return exposure.equalize_hist(gray)
def preproc(self, img, size, pixel_spacing, equalize=True, crop=True): """crop center and resize""" # TODO: this is stupid, you could crop out the heart # But should test this if img.shape[0] < img.shape[1]: img = img.T # Standardize based on pixel spacing img = transform.resize(img, (int(img.shape[0]*(1.0/np.float32(pixel_spacing[0]))), int(img.shape[1]*(1.0/np.float32(pixel_spacing[1]))))) # we crop image from center short_egde = min(img.shape[:2]) yy = int((img.shape[0] - short_egde) / 2) xx = int((img.shape[1] - short_egde) / 2) if crop: crop_img = img[yy : yy + short_egde, xx : xx + short_egde] # resize to 64, 64 resized_img = transform.resize(crop_img, (size, size)) else: resized_img = img #resized_img = gaussian_filter(resized_img, sigma=1) #resized_img = median_filter(resized_img, size=(3,3)) if equalize: resized_img = equalize_hist(resized_img) resized_img = adjust_sigmoid(resized_img) resized_img *= 255. return resized_img.astype("float32")
def alineacion(image,rgbCopia,escala): centerPoint=(140,165) extents=15 #if escala>1: # extents=extents*escala cropImg=np.empty((extents*2,extents*2,3),dtype=float) cropSizes=[] cropSizes.append([0,0,0,0]) cropImg[:,:,0]=cropImage(image[:,:,0],centerPoint,extents) for i in range(image.shape[2]-1): cropImg[:,:,i+1]=cropImage(image[:,:,i+1],centerPoint,extents*escala)[::escala,::escala] cropImg[:,:,i+1]=cropImg[:,:,i+1]-np.mean(cropImg[:,:,i+1]) channelR=cropImg[:,:,0] for i in range(2): channelRGradient=np.gradient(channelR) cropGradient=np.gradient(cropImg[:,:,i+1]) #correlation=correlationMatrix(channelR,cropImg[:,:,i+1]) correlation=correlationMatrix(channelR,cropImg[:,:,i+1]) correlation=correlation-np.mean(correlation) print "Filtro size ",correlation.shape position=np.where(correlation==np.amax(correlation)) despVector=[position[0][0]-correlation.shape[0]/2,position[1][0]-correlation.shape[1]/2] fila=despVector[0] columna=despVector[1] print "fila",fila print "columna", columna generateNewRGB(i,rgbCopia,fila,columna) rgbCopia=equalize_hist(rgbCopia) rgbCopia=rgbCopia*255/np.max(rgbCopia) return rgbCopia
def compute(self, src): image = img_as_ubyte(src) # denoise image denoised = denoise_tv_chambolle(image, weight=0.05) denoised_equalize= exposure.equalize_hist(denoised) # find continuous region (low gradient) --> markers markers = rank.gradient(denoised_equalize, disk(5)) < 10 markers = ndi.label(markers)[0] # local gradient gradient = rank.gradient(denoised, disk(2)) # labels labels = watershed(gradient, markers) # display results fig, axes = plt.subplots(2,3) axes[0, 0].imshow(image)#, cmap=plt.cm.spectral, interpolation='nearest') axes[0, 1].imshow(denoised, cmap=plt.cm.spectral, interpolation='nearest') axes[0, 2].imshow(markers, cmap=plt.cm.spectral, interpolation='nearest') axes[1, 0].imshow(gradient, cmap=plt.cm.spectral, interpolation='nearest') axes[1, 1].imshow(labels, cmap=plt.cm.spectral, interpolation='nearest', alpha=.7) plt.show()
def loadDataMontgomery(df, path, im_shape): """Function for loading Montgomery dataset""" X, y = [], [] for i, item in df.iterrows(): img = img_as_float(io.imread(path + item[0])) gt = io.imread(path + item[1]) l, r = np.where(img.sum(0) > 1)[0][[0, -1]] t, b = np.where(img.sum(1) > 1)[0][[0, -1]] img = img[t:b, l:r] mask = gt[t:b, l:r] img = transform.resize(img, im_shape) img = exposure.equalize_hist(img) img = np.expand_dims(img, -1) mask = transform.resize(mask, im_shape) mask = np.expand_dims(mask, -1) X.append(img) y.append(mask) X = np.array(X) y = np.array(y) X -= X.mean() X /= X.std() print '### Data loaded' print '\t{}'.format(path) print '\t{}\t{}'.format(X.shape, y.shape) print '\tX:{:.1f}-{:.1f}\ty:{:.1f}-{:.1f}\n'.format(X.min(), X.max(), y.min(), y.max()) print '\tX.mean = {}, X.std = {}'.format(X.mean(), X.std()) return X, y
def visualize_depth_image(data): data[data == 0.0] = np.nan maxdepth = np.nanmax(data) mindepth = np.nanmin(data) data = data.copy() data -= mindepth data /= (maxdepth - mindepth) gray = np.zeros(list(data.shape) + [3], dtype=data.dtype) data = (1.0 - data) gray[..., :3] = np.dstack((data, data, data)) # use a greenish color to visualize missing depth gray[np.isnan(data), :] = (97, 160, 123) gray[np.isnan(data), :] /= 255 gray = exposure.equalize_hist(gray) # set alpha channel gray = np.dstack((gray, np.ones(data.shape[:2]))) gray[np.isnan(data), -1] = 0.5 return gray * 255
def hsi_equalize_hist(): image=data.astronaut() h=color.rgb2hsv(image) h[:,:,2]=exposure.equalize_hist(h[:,:,2]) image_equal=color.hsv2rgb(h) io.imshow(image_equal) io.imsave('astronautequal.png',image_equal)
def test_equalize_ubyte(): with expected_warnings(['precision loss']): img = skimage.img_as_ubyte(test_img) img_eq = exposure.equalize_hist(img) cdf, bin_edges = exposure.cumulative_distribution(img_eq) check_cdf_slope(cdf)
def histograms_of_stuff(cls, ref): # First we show the image, and its histogram neg = 1. - ref figs = [] f1, axs1 = plt.subplots(2, 2, figsize=(8, 8)) for i, im in enumerate(((ref, 'tire'), (neg, 'negative'))): axs1[i,0].imshow(im[0], cmap='gray') axs1[i,1].hist(im[0].ravel(), 256, range=(0., 1.), fc='k', ec='k') axs1[i,0].set_title(im[1]) axs1[i,1].set_title(im[1] + ': histogram') figs.append(f1) # Some gamma correction ... gammas = (0.5, 1.3) f2, axs2 = plt.subplots(2, 2, figsize=(8,8)) for i, gamma in enumerate(gammas): x = ref ** gamma axs2[i,0].imshow(x, cmap='gray') axs2[i,1].hist(x.ravel(), 256, range=(0.,1.), fc='k', ec='k') for z in axs2[i]: z.set_title('gamma = ' + str(gamma)) figs.append(f2) # Histogram Equalization f3, axs3 = plt.subplots(1, 2, figsize=(8, 4)) eq_ref = exposure.equalize_hist(ref) axs3[0].imshow(eq_ref, cmap='gray') axs3[1].hist(eq_ref.ravel(), 256, range=(0.,1.), fc='k', ec='k') for x in axs3: x.set_title('Histogram Equalization') figs.append(f3) return figs
def _equalizeHistogram(img): ''' histogram equalisation not bounded to int() or an image depth of 8 bit works also with negative numbers ''' #to float if int: intType = None if 'f' not in img.dtype.str: TO_FLOAT_TYPES = { np.dtype('uint8'):np.float16, np.dtype('uint16'):np.float32, np.dtype('uint32'):np.float64, np.dtype('uint64'):np.float64} intType = img.dtype img = img.astype(TO_FLOAT_TYPES[intType], copy=False) #get image deph DEPTH_TO_NBINS = {np.dtype('float16'):256, #uint8 np.dtype('float32'):32768, #uint16 np.dtype('float64'):2147483648} #uint32 nBins = DEPTH_TO_NBINS[img.dtype] #scale to -1 to 1 due to skikit-image restrictions mn, mx = np.amin(img), np.amax(img) if abs(mn) > abs(mx): mx = mn img /= mx img = exposure.equalize_hist(img, nbins=nBins) img *= mx if intType: img = img.astype(intType) return img
def extract_sift(image,lm=None, shape=[200,300], fix_points='outer',ttype='affine'): if lm==None:lm = landmarks(image) if np.any(np.isnan(lm)): return np.nan*np.ones([out_shape,out_shape,image.shape[2]]).astype(np.float16), np.nan*np.zeros_like(lm) dst = mean_face[:,p[fix_points]] dst = dst-dst.mean(1)[:,None] dst = dst/np.abs(dst).max() dst *=shape[0]/2 dst +=shape[1]/2 print(dst.min()) print(dst.max()) src = lm[:,p[fix_points]] tform = transform.estimate_transform(ttype, src.T,dst.T) lm_reg = tform(lm.T).T image = transform.warp(image,inverse_map=tform.inverse,output_shape=[shape[1],shape[1]]) image = exposure.equalize_hist(image,mask=image!=0) S = 12 for l1,l2 in lm_reg.T: x = np.arange(l2-S,l2+S) y = np.arange(l1-S,l1+S) for x_ in x: for y_ in y: image[x_,y_,0]=255 return image, lm_reg
def equalize(inputs): """equalize the data""" new_data = [] for i in inputs: new_i = exposure.equalize_hist(i) new_data.append(new_i) return np.array(new_data)
def make_lungs(): path = '/path/to/JSRT/All247images/' for i, filename in enumerate(os.listdir(path)): img = 1.0 - np.fromfile(path + filename, dtype='>u2').reshape((2048, 2048)) * 1. / 4096 img = exposure.equalize_hist(img) io.imsave('/path/to/JSRT/new/' + filename[:-4] + '.png', img) print 'Lung', i, filename
def transform(self, Xb, yb): Xb, yb = super(EqualizeHistBatchIteratorMixin, self).transform(Xb, yb) Xb_transformed = np.asarray([ [equalize_hist(img_ch) for img_ch in img] for img in Xb ]) Xb_transformed = Xb_transformed.astype(Xb.dtype) return Xb_transformed, yb
def classify_emotions(pca, clf, faces): emotions = [] for scaled in faces: test_x = exposure.equalize_hist(scaled.reshape((1, -1))) face_pca = pca.transform(test_x) emotions.append(int(clf.predict(face_pca)[0])) return emotions
def add_auto_masks_area(areaFile,addMasks=False): from skimage.morphology import binary_dilation, binary_erosion, disk from skimage import exposure from skimage.transform import hough_circle from skimage.morphology import convex_hull_image from skimage.feature import canny from skimage.draw import circle_perimeter import h5py MASKs = np.zeros(areaFile.attrs['ROI_patches'].shape) if 'ROI_masks' in (areaFile.attrs.iterkeys()): print 'Masks have already been created' awns = raw_input('Would you like to redo them from scratch: (answer y/n): ') else: awns = 'y' if awns=='y': MASKs = np.zeros(areaFile.attrs['ROI_patches'].shape) for i in range(areaFile.attrs['ROI_patches'].shape[2]): patch = areaFile.attrs['ROI_patches'][:,:,i] tt0 = exposure.equalize_hist(patch) tt = 255*tt0/np.max(tt0) thresh = 1*tt>0.3*255 thresh2 = 1*tt<0.1*255 tt[thresh] = 255 tt[thresh2] = 0 edges = canny(tt, sigma=2, low_threshold=20, high_threshold=30) try_radii = np.arange(3,5) res = hough_circle(edges, try_radii) ridx, r, c = np.unravel_index(np.argmax(res), res.shape) r, c, try_radii[ridx] image = np.zeros([20,20,4]) cx, cy = circle_perimeter(c, r, try_radii[ridx]+2) try: image[cy, cx] = (220, 80, 20, 1) original_image = np.copy(image[:,:,3]) chull = convex_hull_image(original_image) image[chull,:] = (255, 0, 0, .4) except: pass MASKs[:,:,i] = (1*image[:,:,-1]>0) if addMasks: areaFile.attrs['ROI_masks'] = MASKs else: pass return np.array(MASKs)
def getImage(): fn = r'/home/pi/tmp.jpg' proc = subprocess.Popen('raspistill -o %s -w 640 -h 480 -n -t 3' % (fn), shell=True, stderr=subprocess.STDOUT) proc.wait() im = io.imread(fn, as_grey=True) im = exposure.equalize_hist(im) return skimage.img_as_ubyte(im)
def post_process(img): """ Post-processes the results using skimage filters """ filt_1 = ski_filter.hprewitt(img) filt_2 = exposure.equalize_hist(filt_1) return filt_2
def boost_contrast_global(image): """Returns a new image with contrast boosted globally. Args: image: assumed to be grayscale. """ glob = exposure.equalize_hist(image) * 255 return exposure.rescale_intensity(image)
def visualize_derivatives(image): laplacian = scipy.ndimage.filters.laplace(image) lhist = mean_center( blur(exposure.equalize_hist(unitize(laplacian)),1)) plt.imshow(lhist, origin='lower',interpolation='nearest',cmap='gray',extent=(0,64,)*2) plt.title('Laplacian') return gradient, laplacian
def preprocess(self, regions): # decoder expects input shape [samples, 1, 64, 64] crop = CropTransformation(translation=0, crop_shape=(64, 64)) cropped_rois = crop(regions) if self.uses_hist_equalization: cropped_rois = np.stack([equalize_hist(roi) for roi in cropped_rois]) cropped_rois = 2 * cropped_rois - 1 return cropped_rois
def histeq(I): return equalize_hist(I)
def order_tracer(dir_name, file_name, X_half_width, step, min_height, aperture, adaptive, view): poly_order = 2 # order of polynomial fit #read image hdulist = pyfits.open(dir_name.joinpath(file_name)) ordim = hdulist[0].data.copy() hdulist.close() trace_im = np.zeros((ordim.shape[0], ordim.shape[1]), dtype=np.float) # Search for local maxima in the cross sections and construct the mask of orders. 1 - max, 0 - rest of pixels for x_t in range(X_half_width, ordim.shape[1] - X_half_width, X_half_width): slic = np.median(ordim[:, x_t - X_half_width:x_t + X_half_width - 1], 1) peaks_coord, _ = find_peaks(slic, height=min_height, width=2) trace_im[peaks_coord, x_t] = 1 # Now rearrange the data for the futher clustering ones = np.array(np.where(trace_im == 1)) ord_mask = ones.T model = AgglomerativeClustering(n_clusters=None, affinity='euclidean', linkage='single', compute_full_tree=True, distance_threshold=5) model.fit(ord_mask) labels = model.labels_ n_clusters = len(list(set(labels))) order_tab = [] # output table of traces width_tab = [] # output table of sizes center_x = ordim.shape[1] / 2 - 1 for i in range(n_clusters): if len(ord_mask[labels == i, 1]) >= (ordim.shape[1] / X_half_width - 2) * 0.5: cheb_coef = chebfit(ord_mask[labels == i, 1], ord_mask[labels == i, 0], poly_order) cheb_coef = np.insert(cheb_coef, 0, chebval(center_x, cheb_coef)) order_tab.append(cheb_coef) order_tab = np.asarray(order_tab) # convert list into array order_tab = order_tab[order_tab[:, 0].argsort()] # Sort array by ascending order_tab[:, 0] = np.arange( len(order_tab[:, 0]), dtype=int) # insert the number of order in the 0th column n_orders = int(order_tab[-1, 0]) + 1 print(f"Found {n_orders} orders") logging.info(f"Found {n_orders} orders") # Recentering orders and the final tracing #get points for order tracing, start from center column of image n_points = np.floor((ordim.shape[1] / 2 - X_half_width) / step) print(f"{1+2*n_points} points in each order for fitting") logging.info(f"{1+2*n_points} points in each order for fitting") trace_x = np.arange(ordim.shape[1] // 2 - X_half_width - n_points * step, ordim.shape[1] - step, step, dtype=int) x_coord = np.arange(ordim.shape[1]) orders = [] for i in range(n_orders): print(f"Re-trace order {i}") logging.info(f"Re-trace order {i}") xfit = [] centr = [] width = [] for x in trace_x: xt = np.arange(x - step, x + step, 1, dtype=int) yc = chebval(x, order_tab[i, 1:]) if yc > 0 and yc + X_half_width + 2 < ordim.shape[0]: yy = np.arange(yc - X_half_width, yc + X_half_width + 2, 1, dtype=int) prof = np.median(ordim[yy[0]:yy[-1] + 1, xt], axis=1) if prof.shape[0] != 0 and max( prof ) > 20.: # Fit only well-exposured part of the order # Re-fit the cross-sections moffat = lambda x, A, B, C, D, x0: A * (1 + ( (x - x0) / B)**2)**(-C) + D Y = yy - yc p0 = np.array([max(prof), 3.0, 3.0, 0.0, 3.0]) try: popt, pcov = curve_fit(moffat, Y, prof, p0, maxfev=10000) except RuntimeError: pass else: fwhm = 2 * popt[1] * np.sqrt(2**(1 / (popt[2])) - 1) if np.isfinite(fwhm) and fwhm > 1 and fwhm < 10: xfit.append(x) centr.append(popt[4] + yc) width.append(fwhm) med_fwhm = np.median(width) # print(f"Order {i}, median FWHM: {med_fwhm:.2f}, mean FWHM: {np.mean(width):.2f}") print(f"Fit {len(xfit)} points, median FWHM {med_fwhm:.3f}") logging.info(f"Fit {len(xfit)} points, median FWHM {med_fwhm:.3f}") coef_center = chebfit(xfit, centr, poly_order) coef_width = chebfit(xfit, width, poly_order) ## Check the limits of the orders if adaptive: width = chebval(x_coord, coef_width) else: width = np.repeat(med_fwhm, ordim.shape[1]) if np.min(chebval(x_coord, coef_center) - width) < 1. or np.max(chebval(x_coord, coef_center) + width) >= ordim.shape[0] - 1: print(f"Skip incomplete order #{i}") logging.info(f"Skip incomplete order #{i}") else: width_tab.append(width) orders.append(coef_center) # Output data width_tab = np.asarray(width_tab) orders = np.asarray(orders) text_file = open(dir_name.joinpath('temp', 'traces.txt'), "w") # Save results for i in range(orders.shape[0]): text_file.write("Order" + '\t' + str(i) + '\n') for j in x_coord: text_file.write( format('%.2f' % chebval(j, orders[i, :])) + '\t' + format('%.2f' % width[j]) + '\n') text_file.close() print(f"Data saved to {dir_name.joinpath('temp/traces.txt')}") logging.info(f"Data saved to {dir_name.joinpath('temp/traces.txt')}") # Display the results fig = plt.figure(figsize=(15, 15 / (ordim.shape[1] / ordim.shape[0])), tight_layout=True) ax0 = fig.add_subplot(1, 1, 1) ax0.imshow(equalize_hist(ordim), cmap='gist_gray') ax0.set_xlabel("CCD X") ax0.set_ylabel("CCD Y") for i in range(orders.shape[0]): ax0.plot(x_coord, chebval(x_coord, orders[i, :]) + aperture * width_tab[i, :], 'b-', lw=0.4) ax0.plot(x_coord, chebval(x_coord, orders[i, :]) - aperture * width_tab[i, :], 'r-', lw=0.4) ax0.text(x_coord[15], chebval(x_coord[15], orders[i, :]), i + 1, color='k', backgroundcolor='yellow', fontsize=8) plt.gca().invert_yaxis() fig.savefig(dir_name.joinpath('orders_map.pdf'), dpi=350) if view: plt.show() return None
""" Equalizing the histogram of an image ===================================== Histogram equalizing makes images have a uniform histogram. """ from skimage import data, exposure import matplotlib.pyplot as plt camera = data.camera() camera_equalized = exposure.equalize_hist(camera) plt.figure(figsize=(7, 3)) plt.subplot(121) plt.imshow(camera, cmap='gray', interpolation='nearest') plt.axis('off') plt.subplot(122) plt.imshow(camera_equalized, cmap='gray', interpolation='nearest') plt.axis('off') plt.tight_layout() plt.show()
def plot_visualization(vid, vid_orig, vis_dir, plot_img=False, write_video=True): def crop_center(img, cropx, cropy): y, x, _ = img.shape startx = x // 2 - (cropx // 2) starty = y // 2 - (cropy // 2) return img[starty:starty + cropy, startx:startx + cropx, :] def plot_img_and_hist(image, axes, bins=256): #Plot an image along with its histogram and cumulative histogram. image = img_as_float(image) ax_img, ax_hist = axes ax_cdf = ax_hist.twinx() # Display image ax_img.imshow(image, cmap=plt.cm.gray) ax_img.set_axis_off() ax_img.set_adjustable('box-forced') # Display histogram ax_hist.hist(image.ravel(), bins=bins, histtype='step', color='black') ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0)) ax_hist.set_xlabel('Pixel intensity') ax_hist.set_xlim(0, 1) ax_hist.set_yticks([]) # Display cumulative distribution img_cdf, bins = exposure.cumulative_distribution(image, bins) ax_cdf.plot(bins, img_cdf, 'r') ax_cdf.set_yticks([]) return ax_img, ax_hist, ax_cdf def PCA(data): m, n = data.shape[0], data.shape[1] #print(m, n) mean = np.mean(data, axis=0) data -= np.tile(mean, (m, 1)) # calculate the covariance matrix cov = np.matmul(np.transpose(data), data) evals, evecs = np.linalg.eigh(cov) # sort eigenvalue in decreasing order idx = np.argsort(evals)[::-1] evecs = evecs[:, idx] evals = evals[idx] #print(evals) evecs = evecs[:, 0] return np.matmul(data, evecs), evals[0] / sum(evals) width, height = 112, 112 video_histeq = [] for i in range(vid.shape[0]): frame = crop_center(vid[i], 112, 112) frame = np.reshape(frame, (112 * 112, 3)) frame, K = PCA(frame) frame = np.reshape(frame, (112, 112)) max, min = np.max(frame), np.min(frame) frame = ((frame - min) / (max - min) * 255).astype('uint8') # Contrast stretching p2, p98 = np.percentile(frame, (2, 98)) img_rescale = exposure.rescale_intensity(frame, in_range=(p2, p98)) # Equalization img_eq = exposure.equalize_hist(frame) video_histeq.append(img_eq) # # Adaptive Equalization # img_adapteq = exposure.equalize_adapthist(frame, clip_limit=0.03) if plot_img: # Display results fig = plt.figure(figsize=(12, 16)) axes = np.zeros((4, 3), dtype=np.object) axes[0, 0] = fig.add_subplot(4, 3, 1) for j in range(1, 3): axes[0, j] = fig.add_subplot(4, 3, 1 + j, sharex=axes[0, 0], sharey=axes[0, 0]) for j in range(3, 12): axes[j // 3, j % 3] = fig.add_subplot(4, 3, 1 + j) ax_img, ax_hist, ax_cdf = plot_img_and_hist(frame, axes[0:2, 0]) ax_img.set_title('PCA on 3 channels ({:.4f})'.format(K)) y_min, y_max = ax_hist.get_ylim() ax_hist.set_ylabel('Number of pixels') ax_hist.set_yticks(np.linspace(0, y_max, 5)) ax_img, ax_hist, ax_cdf = plot_img_and_hist( img_rescale, axes[0:2, 1]) ax_img.set_title('Contrast stretching') ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq, axes[0:2, 2]) ax_img.set_title('Histogram equalization') #ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_adapteq, axes[0:1, 3]) #ax_img.set_title('Adaptive equalization') ax_cdf.set_ylabel('Fraction of total intensity') ax_cdf.set_yticks(np.linspace(0, 1, 5)) print(vid_orig[j].shape) frame_downsample_crop = crop_center(vid_orig[j], 112, 112) frame = crop_center(vid[j], 112, 112) axes[2, 0].imshow(frame_downsample_crop.astype('uint8')) axes[2, 0].set_title('Dowmsampled') frame_scaled_joint = linear_scaling( frame, vid, video_linear_scaling=True, joint_channels=True).astype('uint8') axes[2, 1].imshow(frame_scaled_joint.astype('uint8')) axes[2, 1].set_title('Joint Scaling') frame_scaled_separate = linear_scaling( frame, vid, video_linear_scaling=True, joint_channels=False).astype('uint8') axes[2, 2].imshow(frame_scaled_separate.astype('uint8')) axes[2, 2].set_title('Separate Scaling') for j in range(frame.shape[2]): axes[3, j].imshow(frame[:, :, j], cmap=plt.get_cmap('jet')) axes[3, j].set_title('Channel{}'.format(j)) # prevent overlap of y-axis labels fig.tight_layout() plt.savefig('{}/vis_{}.png'.format(vis_dir, i)) plt.close() if write_video: fourcc = cv2.VideoWriter_fourcc(*'XVID') # Be sure to use lower case output = "{}/hist_eq.avi".format(vis_dir) out = cv2.VideoWriter(output, fourcc, 10.0, (width, height), False) vid = np.multiply(np.asarray(video_histeq), 255).astype('uint8') print(vid.shape) print(output) for i in range(vid.shape[0]): frame = vid[i] #frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) #frame = frame.reshape(112, 112, 3) # print(frame) out.write(frame) out.release() cv2.destroyAllWindows()
def equalize_histogram(img): return (equalize_hist(img) * 255)
def track_digits(self, old_candidates, new_frame, show_idx): # Prepare the new frame frame_o = img_as_float(new_frame) gray_o = rgb2grey(frame_o) gray = equalize_hist(gray_o) new_candidates = [] i = 0 roi_img = self.preprocess_roi(rescale(gray, 0.5)) # Try to locate each candidate for c in old_candidates: # Extract the area where the candidate can now be i += 1 h, w = c.image.shape if h < 10 or w < 10: new_candidates.append(c) c.lost_for += 1 continue rx, ry, rw, rh = self.get_roi( c.rect, (new_frame.shape[1], new_frame.shape[0])) rx = int(rx / 2) ry = int(ry / 2) rw = int(rw / 2) rh = int(rh / 2) roi = roi_img[ry:ry + rh, rx:rx + rw] if min(roi.shape) <= 0: new_candidates.append(c) c.lost_for += 1 continue if self.show_debug_images and i - 1 == show_idx: cv2.imshow('roi', img_as_ubyte(roi)) # Get digit rects in roi rects = self.rotrects_from_image( roi, 2, True, frame_o.shape[0] * frame_o.shape[1]) cands = [] for rect in rects: transl_rect = ((rect[0][0] + 2 * rx, rect[0][1] + 2 * ry), rect[1], rect[2]) cands.append(transl_rect) if len(cands) == 0: # If there are no candidates we lost the digit c.lost_for += 1 new_candidates.append(c) else: # If there are several candidates we need to choose the best one # Calculate metrics filt_cands = [] for r in cands: # Dimensions difference dw = abs(max(r[1]) - max(c.dimensions)) dh = abs(min(r[1]) - min(c.dimensions)) ds = math.sqrt(dw * dw + dh * dh) # Eucledian distance dx = abs(r[0][0] - c.rect[0][0]) dy = abs(r[0][1] - c.rect[0][1]) dp = math.sqrt(dx * dx + dy * dy) # Angular difference, unused # da = abs(r[2] - c.rect[2]) # Filter based on heuristics if ds < MAX_SIZE_DIFF_FACTOR * np.average( c.dimensions) and dp < MAX_POS_DIFF: filt_cands.append((r, dp)) # If there are no candidates now we lost the digit best_score = -1 if not filt_cands: new_candidates.append(c) c.lost_for += 1 continue # Try to find the best one max_dp = max([dp for _, dp in filt_cands]) + 1 for r, dp in filt_cands: cropped = self.crop_rotrect(r, gray_o) if cropped is None or min(cropped.shape) <= 5: continue # Crop, calculate structural similarity cropped = resize(cropped, (h, w)) ssim = compare_ssim(cropped, c.orig_image) # Create an aggregated score score = (1.0 - pow(dp / max_dp, POS_WEIGHT_ALPHA)) * ssim if score > best_score: best_score = score best_rect = r best_image = cropped # Choose the candidate with the best score if there is one if best_score < 0: new_candidates.append(c) c.lost_for += 1 else: cand = DigitCandidate(best_rect, best_image) cand.dimensions = c.dimensions cand.conf = c.conf cand.guess = c.guess new_candidates.append(cand) return new_candidates
def equalize(image_array): image_equalized = ex.equalize_hist(image_array) return image_equalized
# Fit CP tensor decomposition (two times). rank = 10 repeats = 10 all_models = [] all_obj = [] for repeat in tqdm(range(repeats)): U = tt.cp_als(all_data_long, rank=rank, verbose=False) all_models.append(U) all_obj.append(U.obj) U = all_models[np.argmin(all_obj)] ## We should be able to see differences in tastes by using distance matrices on trial factors trial_factors = U.factors.factors[-1] trial_distances = dist_mat(trial_factors, trial_factors) plt.figure() plt.imshow(exposure.equalize_hist(trial_distances)) # pca on trial factors and plot by taste trial_labels = np.sort([0, 1, 2, 3, 4, 5] * 32) reduced_trials_pca = pca(n_components=2).fit_transform(trial_factors) plt.scatter(reduced_trials_pca[:, 0], reduced_trials_pca[:, 1], c=trial_labels) # tsne on trial factors and plot by taste X_embedded = tsne(n_components=2, perplexity=40).fit_transform(trial_factors) plt.figure() plt.scatter(X_embedded[:, 0], X_embedded[:, 1], c=trial_labels) # Compare the low-dimensional factors from the two fits. fig, _, _ = tt.plot_factors(U.factors)
def cache(self, path): As = [ os.path.join(path, p) for p in os.listdir(path) if p.startswith('A') and p.endswith(self.ext) ] Bs = [ os.path.join(path, p) for p in os.listdir(path) if p.startswith('B') and p.endswith(self.ext) ] LRs = [ os.path.join(path, p) for p in os.listdir(path) if p.startswith('LR') and p.endswith(self.ext) ] if os.path.exists(os.path.join('mask_A' + self.ext)): m = np.array(Image.open(os.path.join('mask_A' + self.ext))) m = np.expand_dims(m, axis=2) if m.ndim == 2 else m maskA = m < m.max() / 2 else: maskA = None if os.path.exists(os.path.join('mask_B' + self.ext)): m = np.array(Image.open(os.path.join('mask_B' + self.ext))) m = np.expand_dims(m, axis=2) if m.ndim == 2 else m maskB = m < m.max() / 2 else: maskB = None ImgAs, PathAs, ImgBs, PathBs, ImgLRs, PathLRs = [], [], [], [], [], [] for p in As: try: img = np.array(Image.open(p)) img = np.expand_dims(img, axis=2) if img.ndim == 2 else img if maskA: img[maskA] = img.min() ImgAs.append(img) PathAs.append(p) except KeyboardInterrupt: raise except Exception as e: print('error when reading file ', p) assert len(ImgAs) > 0, 'no file found for "A"' for p in Bs: try: img = np.array(Image.open(p)) img = np.expand_dims(img, axis=2) if img.ndim == 2 else img if maskB: img[maskB] = img.min() ImgBs.append(img) PathBs.append(p) except KeyboardInterrupt: raise except Exception as e: print('error when reading file ', p) for p in LRs: try: img = np.array(Image.open(p)) assert img.ndim == 2 if self.drift_correction: import imreg_dft as ird from skimage import exposure b = ImgBs[0][:, :, 0] b = exposure.equalize_hist(b) b = scipy.ndimage.filters.gaussian_filter(b, sigma=(6, 6)) b = scipy.misc.imresize(b, img.shape[:2]) ts = ird.translation(b, img) tvec = ts["tvec"].round(4) # the Transformed IMaGe. img = ird.transform_img(img, tvec=tvec) img = scipy.misc.imresize(img, ImgBs[0].shape[:2]) img = np.expand_dims(img, axis=2) ImgLRs.append(img) PathLRs.append(p) except KeyboardInterrupt: raise except Exception as e: print('error when reading file ', p) import traceback, sys traceback.print_exc(file=sys.stdout) self.__cache[path] = { 'A': ImgAs, 'B': ImgBs, 'LR': ImgLRs, 'path': path, 'pathA': PathAs, 'pathB': PathBs, 'pathLR': PathLRs } return True
from matplotlib import pyplot as plt import cv2 import numpy as np from skimage import exposure image = cv2.imread("../fig/Fig1.jpg") # image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) plt.figure() plt.title("Original Image") plt.imshow(image) # Histogram hist1 = np.histogram(image, bins=2) # Calculate histogram with numpy hist2 = exposure.histogram(image, nbins=2) # Calculate histogram with skimage plt.figure("Grayscale Histogram") arr = image.flatten() n, bins, patches = plt.hist(arr, bins=256, normed=1, facecolor='black', edgecolor='black') # Histogram Equalization img1 = exposure.equalize_hist(image) arr1 = img1.flatten() plt.figure("Image after Histogram Equalization") plt.imshow(img1, plt.cm.gray) # Image after Histogram Equalization plt.figure("Histogram Equalization") plt.hist(arr1, bins=256, normed=1, edgecolor='None', facecolor='red') plt.show()
def get_section_stats(bd, section_rows, section_cols, parameter_object, section_counter): """ Split section into chunks and process features at each scale Args: bd (ndarray): The section array. section_rows (int) section_cols (int) parameter_object (class object) Returns: List of computed features for each scale, for each statistic. """ if parameter_object.trigger in ['pantex', 'lac']: out_d_range = (0, 31) else: out_d_range = (0, 255) # Scale the data to an 8-bit range. if (bd.dtype != 'uint8') and (parameter_object.trigger not in parameter_object.spectral_indices): bd = np.uint8( rescale_intensity(bd, in_range=(parameter_object.image_min, parameter_object.image_max), out_range=out_d_range)) # Apply histogram equalization. if parameter_object.trigger != 'dmp': if parameter_object.equalize: bd = equalize_hist(bd, nbins=256) elif parameter_object.equalize_adapt: bd = equalize_adapthist(bd, kernel_size=(int(section_rows / 128), int(section_cols / 128)), clip_limit=.05, nbins=256) if parameter_object.equalize or parameter_object.equalize_adapt: bd = np.uint8( rescale_intensity(bd, in_range=(0., 1.), out_range=(0, 255))) # Remove image noise. if parameter_object.smooth > 0: bd = np.uint8( cv2.bilateralFilter(bd, parameter_object.smooth, .1, .1)) # elif parameter_object.trigger == 'lbp': # # if parameter_object.visualize: # bdOrig = bd.copy() # # elif parameter_object.trigger == 'hough': # # # for display (testing) purposes only # if parameter_object.visualize: # bdOrig = bd.copy() # # # test canny and hough lines # if parameter_object.visualize: # # # for display purposes only # bdOrig = bd.copy() # # test_plot(bd, bdOrig, parameter_object.trigger, parameter_object) # Get the row and column section chunk indices. # chunk_indices = get_chunk_indices(section_rows, # section_cols, # parameter_object.block, # parameter_object.chunk_size, # parameter_object.scales[-1]) func_dict = dict( dmp=dict(name='Differential Morphological Profiles', args=dict()), evi2=dict(name='Two-band Enhanced Vegetation Index', args=dict()), fourier=dict(name='Fourier transfrom', args=dict()), gabor=dict(name='Gabor filters', args=dict()), gndvi=dict(name='Green Normalized Difference Vegetation Index', args=dict()), grad=dict(name='Gradient magnitude', args=dict()), hog=dict(name='Histogram of Oriented Gradients', args=dict()), lac=dict(name='Lacunarity', args=dict(lac_r=parameter_object.lac_r)), lbp=dict(name='Local Binary Patterns', args=dict()), lbpm=dict(name='Local Binary Patterns moments', args=dict()), lsr=dict(name='Line support regions', args=dict()), mean=dict(name='Mean', args=dict()), ndvi=dict(name='Normalized Difference Vegetation Index', args=dict()), pantex=dict(name='PanTex', args=dict(weight=parameter_object.weight)), orb=dict(name='Oriented FAST and Rotated BRIEF key points', args=dict()), saliency=dict(name='Image saliency', args=dict()), seg=dict(name='Segmentation', args=dict()), sfs=dict(name='Structural Feature Sets', args=dict(sfs_threshold=parameter_object.sfs_threshold, sfs_skip=parameter_object.sfs_skip))) for idx in parameter_object.spectral_indices: if idx not in func_dict: func_dict[idx] = {'name': idx, 'args': {}} logger.info(' Processing {} for section {:,d} of {:,d} ...'.format( func_dict[parameter_object.trigger]['name'], section_counter, parameter_object.n_sects)) other_args = func_dict[parameter_object.trigger]['args'] if parameter_object.trigger in parameter_object.spectral_indices: trigger = 'mean' else: trigger = parameter_object.trigger return call_func(bd, parameter_object.block, parameter_object.scales, parameter_object.scales[-1], trigger, **other_args)
def test_equalize_float(): img = skimage.img_as_float(test_img) img_eq = exposure.equalize_hist(img) cdf, bin_edges = exposure.cumulative_distribution(img_eq) check_cdf_slope(cdf)
def test_equalize_uint8_approx(): """Check integer bins used for uint8 images.""" img_eq0 = exposure.equalize_hist(test_img_int) img_eq1 = exposure.equalize_hist(test_img_int, nbins=3) np.testing.assert_allclose(img_eq0, img_eq1)
img_cdf, bins = exposure.cumulative_distribution(img, bins) ax_cdf.plot(bins, img_cdf, 'r') ax_cdf.set_yticks([]) return ax_img, ax_hist, ax_cdf # Load an example image img = data.moon() # Contrast stretching p2, p98 = np.percentile(img, (2, 98)) img_rescale = exposure.rescale_intensity(img, in_range=(p2, p98)) # Equalization img_eq = exposure.equalize_hist(img) # Adaptive Equalization img_adapteq = exposure.equalize_adapthist(img, clip_limit=0.03) # Display results fig, axes = plt.subplots(nrows=2, ncols=4, figsize=(8, 5)) ax_img, ax_hist, ax_cdf = plot_img_and_hist(img, axes[:, 0]) ax_img.set_title('Low contrast image') y_min, y_max = ax_hist.get_ylim() ax_hist.set_ylabel('Number of pixels') ax_hist.set_yticks(np.linspace(0, y_max, 5)) ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale, axes[:, 1])
from tabulate import tabulate import matplotlib.pyplot as plt import time from pylab import savefig text = '----------REPORT----------' ####################################################################################################################### # Import data # ####################################################################################################################### print '--Importing data \n' data = pickle.load(open("data/groundTruth.pkl", "rb")) img = data['image'] img = exposure.equalize_hist(img) mask = data['mask'] ####################################################################################################################### # Building training and test sets # ####################################################################################################################### print '--Building sets \n' h, w = img.shape test_size = 0.85 text += '\n\n---Parameters--' text += '\n-test_size = %s' % (test_size) img_train = img[:, :w * (1 - test_size)] mask_train = mask[:, :w * (1 - test_size)]
# 25 XP # Import the required Scikit-image module for contrast. # Instructions 2/4 # 25 XP # Show the histogram from the original x-ray image chest_xray_image, using the hist() function. # Instructions 3/4 # 25 XP # Use histogram equalization on chest_xray_image to obtain the improved image and load it as xray_image_eq. # Instructions 4/4 # 25 XP # Show the resulting improved image xray_image_eq. # Import the required module (Instruction 1) from skimage import exposure # Show original x-ray image and its histogram (Instruction 2) show_image(chest_xray_image, 'Original x-ray') plt.title('Histogram of image') plt.hist(chest_xray_image.ravel(), bins=256) plt.show() # Use histogram equalization to improve the contrast (Instruction 3) xray_image_eq = exposure.equalize_hist(chest_xray_image) # Show the resulting image (Instruction 4) show_image(xray_image_eq, 'Resulting image')
def dicom_to_array(img_rows, img_cols): TrainData_dict = scipy.io.loadmat('../data/train.mat') TrainMask_dict = scipy.io.loadmat('../data/trainseg.mat') ValidateData_dict = scipy.io.loadmat('../data/validate.mat') ValidateMask_dict = scipy.io.loadmat('../data/validateseg.mat') TestData_dict = scipy.io.loadmat('../data/test.mat') TestMask_dict = scipy.io.loadmat('../data/testseg.mat') TrainData = TrainData_dict['image_all'] TrainMask = TrainMask_dict['seg_all'] ValidateData = ValidateData_dict['image_all'] ValidateMask = ValidateMask_dict['seg_all'] TestData = TestData_dict['image_all'] TestMask = TestMask_dict['seg_all'] d1 = TrainData.shape[0] d2 = ValidateData.shape[0] d3 = TestData.shape[0] imgs1 = np.zeros( [d1, img_rows, img_cols]) imgs1_seg = np.zeros( [d1, img_rows, img_cols]) imgs2 = np.zeros( [d2, img_rows, img_cols]) imgs2_seg = np.zeros( [d2, img_rows, img_cols]) imgs3 = np.zeros( [d3, img_rows, img_cols]) imgs3_seg = np.zeros( [d3, img_rows, img_cols]) for kk in range(0,d1): #for train img = TrainData[kk,:,:] TrainData[kk,:,:] = equalize_hist( img.astype(int) ) imgs1[kk,:,:] = resize( TrainData[kk,:,:], (img_rows, img_cols), preserve_range=True) imgs1_seg[kk,:,:] = resize( TrainMask[kk,:,:], (img_rows, img_cols), preserve_range=True) for kk in range(0,d2): #for validate img = ValidateData[kk,:,:] ValidateData[kk,:,:] = equalize_hist( img.astype(int) ) imgs2[kk,:,:] = resize( ValidateData[kk,:,:], (img_rows, img_cols), preserve_range=True) imgs2_seg[kk,:,:] = resize( ValidateMask[kk,:,:], (img_rows, img_cols), preserve_range=True) for kk in range(0,d3): #for test img = TestData[kk,:,:] TestData[kk,:,:] = equalize_hist( img.astype(int) ) imgs3[kk,:,:] = resize( TestData[kk,:,:], (img_rows, img_cols), preserve_range=True) imgs3_seg[kk,:,:] = resize( TestMask[kk,:,:], (img_rows, img_cols), preserve_range=True) TrainData = np.concatenate(imgs1, axis=0).reshape(-1, img_rows, img_cols, 1) TrainMask = np.concatenate(imgs1_seg, axis=0).reshape(-1, img_rows, img_cols, 1) ValidateData = np.concatenate(imgs2, axis=0).reshape(-1, img_rows, img_cols, 1) ValidateMask = np.concatenate(imgs2_seg, axis=0).reshape(-1, img_rows, img_cols, 1) TestData = np.concatenate(imgs3, axis=0).reshape(-1, img_rows, img_cols, 1) TestMask = np.concatenate(imgs3_seg, axis=0).reshape(-1, img_rows, img_cols, 1) np.save('../data/train.npy', TrainData) np.save('../data/train_masks.npy', TrainMask) np.save('../data/validate.npy', ValidateData) np.save('../data/validate_masks.npy', ValidateMask) np.save('../data/test.npy', TestData) np.save('../data/test_masks.npy', TestMask)
def test_equalize_ubyte(): img = util.img_as_ubyte(test_img) img_eq = exposure.equalize_hist(img) cdf, bin_edges = exposure.cumulative_distribution(img_eq) check_cdf_slope(cdf)
inverse_thresh = np.invert(thresh_planes) fig = plt.figure(figsize=(6, 6)) plt.imshow(inverse_thresh, "gray") plt.show() # %% # Contrast stretching p2 = np.percentile(image, 2) p98 = np.percentile(image, 98) image_ct = exposure.rescale_intensity(image, in_range=(p2, p98)) # Histogram Equalization image_eq = exposure.equalize_hist(image) # Show the images fig = plt.figure(figsize=(20, 12)) # Subplot for original image a = fig.add_subplot(3, 3, 1) imgplot = plt.imshow(image) a.set_title('Original') # Subplot for contrast stretched image a = fig.add_subplot(3, 3, 2) imgplot = plt.imshow(image_ct) a.set_title('Contrast Stretched') # Subplot for equalized image
from matplotlib import pyplot as plt from skimage import exposure from utils import show_image, show_histogram chest_xray_img = plt.imread('images/chest_xray.png') show_image(chest_xray_img, "Chest x-ray") show_histogram(chest_xray_img.ravel()) chest_xray_img_eq = exposure.equalize_hist(chest_xray_img) show_image(chest_xray_img_eq, 'Resulting image')
show_plane(c, gamma_high[32], title=f'Gamma = {gamma_high_val}') plot_hist(d, data) plot_hist(e, gamma_low) plot_hist(f, gamma_high) # sphinx_gallery_thumbnail_number = 4 ##################################################################### # `Histogram # equalization <https://en.wikipedia.org/wiki/Histogram_equalization>`_ # improves contrast in an image by redistributing pixel intensities. The most # common pixel intensities get spread out, increasing contrast in low-contrast # areas. One downside of this approach is that it may enhance background # noise. equalized_data = exposure.equalize_hist(data) display(equalized_data) ##################################################################### # As before, if we have a Jupyter kernel running, we can explore the above # slices interactively. explore_slices(equalized_data) ##################################################################### # Let us now plot the image histogram before and after histogram equalization. # Below, we plot the respective cumulative distribution functions (CDF). _, ((a, b), (c, d)) = plt.subplots(nrows=2, ncols=2, figsize=(16, 8))
def plot_visualization_frame(eval_vis_dir, frame, name): def crop_center(img, cropx, cropy): y, x, _ = img.shape startx = x // 2 - (cropx // 2) starty = y // 2 - (cropy // 2) return img[starty:starty + cropy, startx:startx + cropx, :] def plot_img_and_hist(image, axes, bins=256): #Plot an image along with its histogram and cumulative histogram. image = img_as_float(image) ax_img, ax_hist = axes ax_cdf = ax_hist.twinx() # Display image ax_img.imshow(image, cmap=plt.cm.gray) ax_img.set_axis_off() ax_img.set_adjustable('box-forced') # Display histogram ax_hist.hist(image.ravel(), bins=bins, histtype='step', color='black') ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0)) ax_hist.set_xlabel('Pixel intensity') ax_hist.set_xlim(0, 1) ax_hist.set_yticks([]) # Display cumulative distribution img_cdf, bins = exposure.cumulative_distribution(image, bins) ax_cdf.plot(bins, img_cdf, 'r') ax_cdf.set_yticks([]) return ax_img, ax_hist, ax_cdf frame = crop_center(frame, 112, 112) frame = frame[:, :, 0] max, min = np.max(frame), np.min(frame) frame = ((frame - min) / (max - min) * 255).astype('uint8') # Contrast stretching p2, p98 = np.percentile(frame, (2, 98)) img_rescale = exposure.rescale_intensity(frame, in_range=(p2, p98)) # Equalization img_eq = exposure.equalize_hist(frame) # Display results fig = plt.figure(figsize=(12, 8)) axes = np.zeros((2, 3), dtype=np.object) axes[0, 0] = fig.add_subplot(2, 3, 1) for i in range(1, 3): axes[0, i] = fig.add_subplot(2, 3, 1 + i, sharex=axes[0, 0], sharey=axes[0, 0]) for i in range(3, 6): axes[i // 3, i % 3] = fig.add_subplot(2, 3, 1 + i) ax_img, ax_hist, ax_cdf = plot_img_and_hist(frame, axes[0:2, 0]) ax_img.set_title('Feature map') y_min, y_max = ax_hist.get_ylim() ax_hist.set_ylabel('Number of pixels') ax_hist.set_yticks(np.linspace(0, y_max, 5)) ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale, axes[0:2, 1]) ax_img.set_title('Contrast stretching') ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq, axes[0:2, 2]) ax_img.set_title('Histogram equalization') ax_cdf.set_ylabel('Fraction of total intensity') ax_cdf.set_yticks(np.linspace(0, 1, 5)) fig.tight_layout() plt.savefig(os.path.join(eval_vis_dir, '{}.png'.format(name))) plt.close()
def tile_alt_imshow(img_arrays, heat_maps=None, labels=None, titles=None, label_choice=1, width=30, height=30, save_it=None, h_slot=None, w_slot=None, CAM=None, cmap='seismic', alpha=0.3, vmin=None, vmax=None, colorbar=False, prob_array=None, force_single_channel=False): plot_heat_map = None #scaled_img_arrays = rescale_img(img_arrays) scaled_img_arrays = img_arrays if len(img_arrays.shape) == 4: img_n, img_h, img_w, _ = img_arrays.shape else: img_n, img_h, img_w = img_arrays.shape if h_slot is None: h_slot = int(math.ceil(np.sqrt(img_n))) if w_slot is None: w_slot = int(math.ceil(np.sqrt(img_n))) fig, axes = plt.subplots(h_slot, w_slot, figsize=(width, height)) fig.subplots_adjust(hspace=0.1, wspace=0) for ax, i in zip(axes.flatten(), range(img_n)): #img = rescale_img(scaled_img_arrays[i]) img = scaled_img_arrays[i] #p2, p98 = np.percentile(img, (2, 98)) #img = exposure.rescale_intensity(img, in_range=(p2, p98)) img = exposure.equalize_hist(img) if labels is not None: c_labels = resize_label_array(labels[i], (img_h, img_w)) img *= np.expand_dims(c_labels[..., label_choice], axis=2) if force_single_channel: cxr = ax.imshow(img[..., 0], cmap='gray') else: cxr = ax.imshow(img, 'gray') if prob_array is not None: c_prob = prob_array[i].round(2) c_text = 'Pelvis: {0:.2f}\nHip: {1:.2f}\nFail: {2:.2f}\nCXR: {3:.2f}'.format( c_prob[0], c_prob[1], c_prob[2], c_prob[3]) ax.text(10, 30, c_text, bbox={'facecolor': 'white', 'pad': 2}) if heat_maps is not None: resized_map = transform.resize(heat_maps[i], (224, 224), mode='reflect', anti_aliasing=True) plot_heat_map = ax.imshow(resized_map, cmap=cmap, alpha=alpha, vmin=vmin, vmax=vmax) if CAM is not None: resized_map = transform.resize(CAM[i], (224, 224), mode='reflect', anti_aliasing=True) resized_map = rescale_img(resized_map) plot_heat_map = ax.imshow(resized_map, cmap=cmap, alpha=alpha, vmin=vmin, vmax=vmax) if titles is not None: ax.set_title(titles[i], color='red') ax.axis('off') if colorbar: fig.colorbar(plot_heat_map) if save_it is not None: plt.savefig(save_it, dpi=300, format='png') else: plt.show()
def order_location_plot(outpath, obj_base_name, flat_base_name, flat_img, obj_img, orders): pl.figure('orders', facecolor='white', figsize=(8, 5)) pl.cla() pl.suptitle('order location and identification', fontsize=14) pl.set_cmap('Blues_r') pl.rcParams['ytick.labelsize'] = 8 obj_plot = pl.subplot(1, 2, 1) try: obj_plot.imshow(exposure.equalize_hist(obj_img)) except BaseException: obj_plot.imshow(obj_img) obj_plot.set_title('object ' + obj_base_name) obj_plot.set_ylim([1023, 0]) obj_plot.set_xlim([0, 1023]) flat_plot = pl.subplot(1, 2, 2) try: flat_plot.imshow(exposure.equalize_hist(flat_img)) except BaseException: flat_plot.imshow(flat_img) flat_plot.set_title('flat ' + flat_base_name) flat_plot.set_ylim([1023, 0]) flat_plot.set_xlim([0, 1023]) for order in orders: obj_plot.plot(np.arange(1024), order.flatOrder.topEdgeTrace, 'k-', linewidth=1.0) obj_plot.plot(np.arange(1024), order.flatOrder.botEdgeTrace, 'k-', linewidth=1.0) obj_plot.plot(np.arange(1024), order.flatOrder.smoothedSpatialTrace, 'y-', linewidth=1.0) obj_plot.text(10, order.flatOrder.topEdgeTrace[0] - 10, str(order.flatOrder.orderNum), fontsize=10) flat_plot.plot(np.arange(1024), order.flatOrder.topEdgeTrace, 'k-', linewidth=1.0) flat_plot.plot(np.arange(1024), order.flatOrder.botEdgeTrace, 'k-', linewidth=1.0) flat_plot.plot(np.arange(1024), order.flatOrder.smoothedSpatialTrace, 'y-', linewidth=1.0) flat_plot.text(10, order.flatOrder.topEdgeTrace[0] - 10, str(order.flatOrder.orderNum), fontsize=10) pl.tight_layout() pl.savefig(constructFileName(outpath, obj_base_name, None, 'traces.png')) pl.close()
def hist_equalize(img): return exposure.equalize_hist(img)
def time_equalize_hist(self): # Run 10x to average out performance # note that this is not needed as asv does this kind of averaging by # default, but this loop remains here to maintain benchmark continuity for i in range(10): result = exposure.equalize_hist(self.image)
xmin, xmax = dtype_range[image.dtype.type] ax_hist.set_xlim(xmin, xmax) # Display cumulative distribution img_cdf, bins = exposure.cumulative_distribution(image, bins) ax_cdf.plot(bins, img_cdf, 'r') return ax_img, ax_hist, ax_cdf # Load an example image img = img_as_ubyte(data.moon()) # Global equalize img_rescale = exposure.equalize_hist(img) # Equalization selem = disk(30) img_eq = rank.equalize(img, selem=selem) # Display results fig = plt.figure(figsize=(8, 5)) axes = np.zeros((2, 3), dtype=np.object) axes[0, 0] = plt.subplot(2, 3, 1) axes[0, 1] = plt.subplot(2, 3, 2, sharex=axes[0, 0], sharey=axes[0, 0]) axes[0, 2] = plt.subplot(2, 3, 3, sharex=axes[0, 0], sharey=axes[0, 0]) axes[1, 0] = plt.subplot(2, 3, 4) axes[1, 1] = plt.subplot(2, 3, 5) axes[1, 2] = plt.subplot(2, 3, 6)
# We compare here how the global histogram equalization is applied locally. # # The equalized image [2]_ has a roughly linear cumulative distribution # function for each pixel neighborhood. The local version [3]_ of the # histogram equalization emphasizes every local gray-level variations. # # .. [2] http://en.wikipedia.org/wiki/Histogram_equalization # .. [3] http://en.wikipedia.org/wiki/Adaptive_histogram_equalization from skimage import exposure from skimage.filters import rank noisy_image = img_as_ubyte(data.camera()) # equalize globally and locally glob = exposure.equalize_hist(noisy_image) * 255 loc = rank.equalize(noisy_image, disk(20)) # extract histogram for each image hist = np.histogram(noisy_image, bins=np.arange(0, 256)) glob_hist = np.histogram(glob, bins=np.arange(0, 256)) loc_hist = np.histogram(loc, bins=np.arange(0, 256)) fig, axes = plt.subplots(3, 2, figsize=(10, 10)) ax = axes.ravel() ax[0].imshow(noisy_image, interpolation='nearest', cmap=plt.cm.gray) ax[0].axis('off') ax[1].plot(hist[1][:-1], hist[0], lw=2) ax[1].set_title('Histogram of gray values')
def __getitem__(self, idx): self.clone = False self.orig_noise = False self.binary_orig = False self.binary_mask = False self.binary_ones_pow = False self.binary_orig_pow = False file_name = self.file_names[idx] if self.working_dir is not None: file = os.path.join(self.working_dir, file_name) else: file = file_name sample, _ = self.t_spectrogram(file) sample_spec = sample.clone() # Data augmentation if self.augmentation: sample_spec = self.t_amplitude(sample_spec) sample_spec = self.t_pitchshift(sample_spec) sample_spec = self.t_timestretch(sample_spec) sample_orca_detect = sample_spec.clone() sample_orca_detect = self.t_compr_a(sample_orca_detect) sample_orca_detect = self.t_norm(sample_orca_detect) sample_spec = self.sp.detect_strong_spectral_region(sample_orca_detect, sample_spec).unsqueeze(dim=0) sample_spec_ncmpr = sample_spec.clone() sample_spec = self.t_compr_f(sample_spec) # input not compressed, but 0/1 normalized for binary cases binary_input_not_cmpr_not_norm = sample_spec_ncmpr.clone() binary_input = self.t_compr_a(binary_input_not_cmpr_not_norm) binary_input = self.t_norm(binary_input) # frequency compressed, to amplitude and normalized ground truth ground_truth = sample_spec.clone() ground_truth = self.t_compr_a(ground_truth) ground_truth = self.t_norm(ground_truth) # ARTF PART distribution_idx = random.randint(0, 9) if distribution_idx != 4: sample_spec = self.t_compr_a(sample_spec) if distribution_idx == 0: if self.random: gaus_stdv = float(random.randint(0, 25)) else: gaus_stdv = self.gaus_stdv distribution = torch.distributions.normal.Normal(torch.tensor(self.gaus_mean), torch.tensor(gaus_stdv)).sample( sample_shape=torch.Size([128, 256])).squeeze(dim=-1) elif distribution_idx == 1: if self.random: df = float(random.randint(0, 30)) else: df = self.df distribution = torch.distributions.chi2.Chi2(torch.tensor(df)).sample( sample_shape=torch.Size([128, 256])).squeeze(dim=-1) elif distribution_idx == 2: if self.random: p_lambda = float(random.randint(0, 30)) else: p_lambda = self.poisson_lambda distribution = torch.distributions.poisson.Poisson(torch.tensor(p_lambda)).sample( sample_shape=torch.Size([128, 256])).squeeze(dim=-1) elif distribution_idx == 3: if self.random: e = round(random.uniform(0.05, 0.15), 2) else: e = self.exp_e distribution = torch.distributions.exponential.Exponential(torch.tensor(e)).sample( sample_shape=torch.Size([128, 256])).squeeze(dim=-1) elif distribution_idx == 4: if not self.random: self.t_addnoise.min_snr = self.orig_noise_value self.t_addnoise.max_snr = self.orig_noise_value self.orig_noise = True elif distribution_idx == 5: # histogram equalization is always constant - no probabilistic effects! self.clone = True elif distribution_idx == 6: self.binary_orig = True elif distribution_idx == 7: if self.random: bin_pow = round(random.uniform(1.3, 2.7), 2) else: bin_pow = self.bin_pow self.binary_ones_pow = True elif distribution_idx == 8: if self.random: bin_pow = round(random.uniform(1.3, 2.7), 2) else: bin_pow = self.bin_pow self.binary_orig_pow = True elif distribution_idx == 9: self.binary_mask = True if self.orig_noise: # Add original noise to the sample sample_spec_n, _ = self.t_addnoise(sample_spec) sample_spec_n = self.t_compr_a(sample_spec_n) sample_spec_n = self.t_norm(sample_spec_n) elif self.clone: sample_spec_n = self.t_compr_a(sample_spec_ncmpr) sample_spec_n = self.t_norm(sample_spec_n) sample_spec_n = self.sp.search_maxima_spec(sample_spec_n, radius=2) sample_spec_n = torch.tensor(exposure.equalize_hist(np.nan_to_num(sample_spec_n.squeeze(dim=0).numpy())), dtype=torch.float) sample_spec_n = self.t_compr_f(sample_spec_n.unsqueeze(dim=0)) elif self.binary_orig: # amplitude and normalized sample_spec_n = binary_input.clone() binary_mask = self.sp.create_mask(trainable_spectrogram=sample_spec_n).unsqueeze(dim=0) ground_truth = binary_input * binary_mask ground_truth = self.t_compr_f(ground_truth) sample_spec_n = self.t_compr_f(binary_input_not_cmpr_not_norm) sample_spec_n = self.t_compr_a(sample_spec_n) sample_spec_n = self.t_norm(sample_spec_n) elif self.binary_ones_pow: sample_spec_n = binary_input.clone() binary_mask = self.sp.create_mask(trainable_spectrogram=sample_spec_n).unsqueeze(dim=0) ground_truth = binary_input + binary_mask ground_truth[ground_truth >= 1.0] = 1.0 ground_truth = ground_truth.pow(bin_pow) ground_truth = self.t_compr_f(ground_truth) sample_spec_n = self.t_compr_f(binary_input_not_cmpr_not_norm) sample_spec_n = self.t_compr_a(sample_spec_n) sample_spec_n = self.t_norm(sample_spec_n) elif self.binary_orig_pow: sample_spec_n = binary_input.clone() binary_mask = self.sp.create_mask(trainable_spectrogram=sample_spec_n).unsqueeze(dim=0) ground_truth = binary_input + binary_mask ground_truth[ground_truth >= 1.0] = 0.0 ground_truth = ground_truth.pow(bin_pow) ground_truth = ground_truth + (sample_spec_n * binary_mask) ground_truth = self.t_compr_f(ground_truth) sample_spec_n = self.t_compr_f(binary_input_not_cmpr_not_norm) sample_spec_n = self.t_compr_a(sample_spec_n) sample_spec_n = self.t_norm(sample_spec_n) elif self.binary_mask: sample_spec_n = binary_input.clone() binary_mask = self.sp.create_mask(trainable_spectrogram=sample_spec_n).unsqueeze(dim=0) ground_truth = binary_mask ground_truth = self.t_compr_f(ground_truth) sample_spec_n = self.t_compr_f(binary_input_not_cmpr_not_norm) sample_spec_n = self.t_compr_a(sample_spec_n) sample_spec_n = self.t_norm(sample_spec_n) else: sample_spec_n = sample_spec + distribution sample_spec_n = self.t_norm(sample_spec_n) label = self.load_label(file) label["ground_truth"] = ground_truth label["file_name"] = label["file_name"].replace(label["file_name"].rsplit("/", 1)[1], str(distribution_idx)+"_"+label["file_name"].rsplit("/", 1)[1]) return sample_spec_n, label
def composite_fig(data, bands, title, scalebar_color=None, fig_name=None, max_size=16, hist_str=None, \ v_min = None, v_max = None): """ Description: Create a three band (one time) composite figure ----- Input: data: one time xarray.Dataset containing the three bands mentionned in bands. bands: bands to be used in the composite (RGB order). title: prefix of the figure title. scalebar_color (OPTIONAL): scalebar color (https://matplotlib.org/examples/color/named_colors.html) fig_name (OPTIONAL): file name (including extension) to save the figure (show only if not added to input). max_size (OPTIONAL, default 16): maximum size of the figure (either horizontal or vertical). hist_str: (OPTIONAL): histogram stretch type (['contr','eq','ad_eq']). Cannot be used with v_min, v_max options. v_min (OPTIONAL, default minimum value): minimum value to display. Cannot be used with hist_str option. v_max (OPTIONAL, default maximum value): maximum value to display. Cannot be used with hist_str option. Output: figure. """ # check options combination assert not((hist_str is not None) and (v_min is not None or v_max is not None)) , \ 'hist_str option cannot be used with v_min, vmax options' assert not((v_min is not None) ^ (v_max is not None)), \ 'v_min option requires v_max option, and inverserly' if v_min is not None: assert v_min < v_max, 'v_min value must be lower than v_max' # Create a copy to unlink from original dataset rgb = data.copy(deep=True) height, width, orient, posit = fig_aspects(rgb.sizes, max_size) rgb = np.stack([rgb[bands[0]], rgb[bands[1]], rgb[bands[2]]]) # perform stretch on each band for b in range(3): # https://scikit-image.org/docs/dev/auto_examples/color_exposure/plot_equalize.html # Contrast stretching if hist_str == 'contr': p2, p98 = np.nanpercentile(rgb[b], (2, 98)) rgb[b] = exposure.rescale_intensity(rgb[b], in_range=(p2, p98)) # Equalization if hist_str == 'eq': rgb[b] = exposure.equalize_hist(rgb[b]) # Adaptive Equalization if hist_str == 'ad_eq': rgb[b] = exposure.equalize_adapthist(rgb[b], clip_limit=0.03) rgb = np.stack(rgb, axis=-1) # normalize between 0 and 1 if v_min is None: rgb = (rgb - np.nanmin(rgb)) / (np.nanmax(rgb) - np.nanmin(rgb)) else: rgb = (rgb - v_min) / (v_max - v_min) # Start plotting the figure plt.close('all') fig, ax = plt.subplots() fig.set_size_inches(width, height) im = ax.imshow(rgb, vmin=0, vmax=1) # add a scalebar if required if scalebar_color: ax.add_artist(create_scalebar(data, ax, scalebar_color)) # ticks moved 1 pixel inside to guarantee they are displayed plt.yticks([rgb.shape[0] - 1, 1], xtrms_format(data.latitude.values)) plt.xticks([1, rgb.shape[1] - 1], xtrms_format(data.longitude.values)) plt.title(title, weight='bold', fontsize=16) fig.tight_layout() if fig_name: plt.savefig(fig_name, dpi=150) display( HTML("""<a href="{}" target="_blank" >View and download {}</a>""". format(fig_name, basename(fig_name)))) else: plt.show() plt.close()