def compute_binary_diff_image(self, new_image): """ Compute an Otsu-thresholded image corresponding to the absolute difference between the empty chessboard image and the current image. """ adj_start_image = exposure.adjust_gamma( color.rgb2gray(self.empty_chessboard_image), 0.1) # gamma values have a strong impact on classification adj_image = exposure.adjust_gamma(color.rgb2gray(new_image), 0.1) diff_image = exposure.adjust_gamma(np.abs(adj_image - adj_start_image), 0.3) return diff_image > threshold_otsu(diff_image)
def image_prep(img): # apply filters for better contrast and noise removal img_gamma = adjust_gamma(img, 0.7) img_median = median(img_gamma, disk(1)) # apply threshold val = threshold_otsu(img_median) img_otsu = img_median > val # label image regions label_image = label(img_otsu) candidates = [] for region in regionprops(label_image): minr, minc, maxr, maxc = region.bbox if (maxr - minr > maxc - minc): candidates.append(region) # find numbers areas = [] for candidate in candidates: areas.append(candidate.area) areas.sort() areas.reverse() n = 1 v = [] for candidate in candidates: if (candidate.area == areas[0] or candidate.area == areas[1] or candidate.area == areas[2]): v.append(candidate.image) imsave('num%d.png' % n, candidate.image) n += 1 return v
def gamma_correction(img=None, gamma=1., gain=1.): r''' Gamma correction or power law transform. It can be expressed as: .. math:: I_{out} = gain \times {I_{in}} ^ {\gamma} Adjusts contrast without changing the shape of the histogram. For the values .. :math:`\gamma > 1` : Histogram shifts towards left (darker) .. :math:`\gamma < 1` : Histogram shifts towards right (lighter) Parameters ---------- img : array_like Single image as numpy array or multiple images as array-like object gamma : float Non-negative real number gain : float Multiplying factor References ---------- .. [1] http://scikit-image.org/docs/dev/auto_examples/color_exposure/plot_log_gamma.html # noqa .. [2] http://en.wikipedia.org/wiki/Gamma_correction ''' img_out = exposure.adjust_gamma(img, gamma, gain) return img_out
def gen_thumbs(dirname, key='/*/*decon.tif', where='host', level=2, figsize=6, redo=True, gamma=1.0, **kwargs): ''' Main function to generate and save thumbnail pngs ''' # load data data = load_data(dirname, key) if data: # can clean the dirnames here foldername = os.path.abspath(dirname).split(os.path.sep)[-level] if where == 'host': save_name = 'Thumbs ' + foldername + '.png' elif where == 'in folder': save_name = os.path.abspath( os.path.join(dirname, 'Thumbs ' + foldername + '.png')) else: save_name = os.path.abspath( os.path.join(where, 'Thumbs ' + foldername + '.png')) if not redo and os.path.exists(save_name): print(save_name, "already exists, skipping") return dirname + os.path.sep + key data = {clean_dirname(k, figsize): adjust_gamma(abs(v), gamma) for k, v in data.items()} fig, ax = display_grid(data, figsize=figsize, **kwargs) # make the layout 'tight' fig.tight_layout() # save the figure print('Saving', save_name, 'on', os.getpid(), '...') fig.savefig(save_name, bbox_inches='tight') print('finished saving', save_name) # mark data for gc del data return dirname + os.path.sep + key
def image_generate(img, ZCA_array) : timg = np.copy(img) timg = timg.transpose(1, 2, 0) # transpose to use skimage augnum = random.randrange(0, 5) if augnum==0 or augnum==1 : # change nothing pass elif augnum==2 : # horizontal flip for j in range(3) : timg[:,:,j] = np.fliplr(timg[:,:,j]) elif augnum==3 : # random rotation of -15~15 degrees angle = random.random()*30-15 timg = transform.rotate(timg/256.0, angle) elif augnum==4 : # gamma correction (luminance adjust) - random gamma 0.7~1.3 gamma = random.random()*0.6+0.7 timg = exposure.adjust_gamma(timg/256.0, gamma) timg = timg.transpose(2, 0, 1) # GCN, ZCA for i in range(3) : timg[i,:,:] -= np.mean(timg[i,:,:]) timg[i,:,:] /= np.std(timg[i,:,:]) timg[i,:,:] = np.dot(ZCA_array, timg[i,:,:].reshape(1024, 1)).reshape(32, 32) return timg
def save_segmented_image(self, filepath_image, modality='t1c', show=False): ''' Creates an image of original brain with segmentation overlay and save it in ./predictions INPUT (1) str 'filepath_image': filepath to test image for segmentation, including file extension (2) str 'modality': imaging modality to use as background. defaults to t1c. options: (flair, t1, t1c, t2) (3) bool 'show': If true, shows output image. defaults to False. OUTPUT (1) if show is True, shows image of segmentation results (2) if show is false, returns segmented image. ''' modes = {'flair': 0, 't1': 1, 't1c': 2, 't2': 3} segmentation = self.predict_image(filepath_image, show=False) print 'segmentation = ' + str(segmentation) img_mask = np.pad(segmentation, (16, 16), mode='edge') ones = np.argwhere(img_mask == 1) twos = np.argwhere(img_mask == 2) threes = np.argwhere(img_mask == 3) fours = np.argwhere(img_mask == 4) test_im = io.imread(filepath_image) test_back = test_im.reshape(5, 216, 160)[modes[modality]] # overlay = mark_boundaries(test_back, img_mask) gray_img = img_as_float(test_back) # adjust gamma of image image = adjust_gamma(color.gray2rgb(gray_img), 0.65) sliced_image = image.copy() red_multiplier = [1, 0.2, 0.2] yellow_multiplier = [1, 1, 0.25] green_multiplier = [0.35, 0.75, 0.25] blue_multiplier = [0, 0.25, 0.9] print str(len(ones)) print str(len(twos)) print str(len(threes)) print str(len(fours)) # change colors of segmented classes for i in xrange(len(ones)): sliced_image[ones[i][0]][ones[i][1]] = red_multiplier for i in xrange(len(twos)): sliced_image[twos[i][0]][twos[i][1]] = green_multiplier for i in xrange(len(threes)): sliced_image[threes[i][0]][threes[i][1]] = blue_multiplier for i in xrange(len(fours)): sliced_image[fours[i][0]][fours[i][1]] = yellow_multiplier #if show=True show the prediction if show: print 'Showing...' io.imshow(sliced_image) plt.show() #save the prediction print 'Saving...' try: mkdir_p('./predictions/') io.imsave('./predictions/' + os.path.basename(filepath_image) + '.png', sliced_image) print 'prediction saved.' except: io.imsave('./predictions/' + os.path.basename(filepath_image) + '.png', sliced_image) print 'prediction saved.'
def __prepare_image(image): """Prepare image for comparison""" image = rgb2gray(image) image = adjust_gamma(image) image = pyramid_reduce(image, downscale=8) with warnings.catch_warnings(): warnings.simplefilter("ignore") image = img_as_ubyte(image) return image
def S2_image_to_rgb(self, rgb_bands=("B11", "B08", "B03"), rgb_gamma=(1.0, 1.0, 1.0),hist_chop_off_fraction=0.01, output_size=None,max_hist_pixel=1000**2,resample_order=3): if output_size is None: if self.target_resolution is None: raise ValueError("output_size=None is only allowed for target_resolution != None") else: output_shape = list(self.final_shape) else: output_shape = [output_size,output_size] rgb_type = np.uint8 S2_rgb = np.zeros(output_shape + [len(rgb_bands),],dtype=rgb_type) if self.unit == "reflectance": bins = np.linspace(0.0,1.0,100 / 2.0) elif self.unit == "dn": bins = np.linspace(0,10000,100 / 2.0) for i_rgb, (band, gamma) in enumerate(zip(rgb_bands, rgb_gamma)): if self.target_resolution is None: data = self.data[band] else: i_band = self.band_list.index(band) data = self.data[:,:,i_band] if self.bad_data_value is np.NAN: bf = data[:,:][np.isfinite(data[:,:])] else: bf = data[:,:][data[:,:] == self.bad_data_value] pixel_skip = np.int(np.floor(bf.shape[0] / max_hist_pixel) + 1) bf = bf[::pixel_skip] hh, xx = np.histogram(bf, bins=bins,normed=False) bb = 0.5 * (xx[1:] + xx[:-1]) hist_chop_off = hist_chop_off_fraction * np.sum(hh) / len(bins) lim = (lambda x: (np.min(x), np.max(x)))(bb[hh > hist_chop_off]) zoom_factor = np.array(output_shape) / np.array(data[:,:].shape) zm = np.nan_to_num(np.array(data[:, :],dtype=np.float32)) if (zoom_factor != [1.0,1.0]).all(): self.logger.info("Resample band for RGB image: %i,%s,zoom:%.2f" % (i_rgb, band,zoom_factor[0])) zm = zoom(input=zm,zoom=zoom_factor,order=resample_order) bf = rescale_intensity(image=zm,in_range=lim,out_range=(0.0, 255.0)) S2_rgb[:, :, i_rgb] = np.array(bf,dtype=rgb_type) self.logger.info("Rescale band for RGB image: %i,%s,(%.2f,%.2f)->(0,256), zoom:%.2f" % (i_rgb, band, lim[0], lim[1],zoom_factor[0])) if gamma != 0.0: S2_rgb[:, :, i_rgb] = np.array( adjust_gamma(np.array(S2_rgb[:, :, i_rgb], dtype=np.float32),gamma),dtype=rgb_type) return S2_rgb
def make_square(img): height, width = img.shape max_side = max([height, width]) copy = np.zeros(shape=(max_side, max_side), dtype=np.uint8) copy.fill(255) for i in range(height): for j in range(width): copy[i][j] = img[i][j] # increase contrast a bit with a gamma correction copy = exposure.adjust_gamma(copy, gamma=1.8) return copy
def transform(self, Xb, yb): Xb, yb = super(AdjustGammaBatchIteratorMixin, self).transform(Xb, yb) Xb_transformed = Xb.copy() if self.adjust_gamma_p > 0: random_idx = get_random_idx(Xb, self.adjust_gamma_p) for i in random_idx: gamma = choice(self.adjust_gamma_chocies) Xb_transformed[i] = adjust_gamma( Xb[i].transpose(1, 2, 0), gamma=gamma ).transpose(2, 0, 1) return Xb_transformed, yb
def call(self, image, saliency_image): img_resize = resize(image, saliency_image.shape) saliency_range = max(0.15, saliency_image.max() - saliency_image.min()) saliency_norm = (saliency_image - saliency_image.min()) / saliency_range saliency_gamma = adjust_gamma(saliency_norm, gamma=self.gamma) cmap = matplotlib.cm.get_cmap('viridis') cmap_hsv = rgb2hsv(cmap(saliency_gamma)[:, :, :3]) hsv = np.stack([ cmap_hsv[:, :, 0], saliency_gamma, img_resize ], axis=-1) return hsv2rgb(hsv)
def save_segmented_image(self, index, test_img, save=False): """ Creates an image of original brain with segmentation overlay :param index: index of image to save :param test_img: filepath to test image for segmentation, including file extension :param save: If true, shows output image. (defaults to False) :return: if show is True, shows image of segmentation results if show is false, returns segmented image. """ segmentation = self.predict_image(test_img) img_mask = np.pad(segmentation, (16, 16), mode='edge') ones = np.argwhere(img_mask == 1) twos = np.argwhere(img_mask == 2) threes = np.argwhere(img_mask == 3) fours = np.argwhere(img_mask == 4) test_im = mpimg.imread(test_img).astype('float') test_back = rgb2gray(test_im).reshape(5, 216, 160)[-2] # overlay = mark_boundaries(test_back, img_mask) gray_img = img_as_float(test_back) # adjust gamma of image image = adjust_gamma(color.gray2rgb(gray_img), 0.65) sliced_image = image.copy() red_multiplier = [1, 0.2, 0.2] yellow_multiplier = [1, 1, 0.25] green_multiplier = [0.35, 0.75, 0.25] blue_multiplier = [0, 0.25, 0.9] # change colors of segmented classes for i in xrange(len(ones)): sliced_image[ones[i][0]][ones[i][1]] = red_multiplier for i in xrange(len(twos)): sliced_image[twos[i][0]][twos[i][1]] = green_multiplier for i in xrange(len(threes)): sliced_image[threes[i][0]][threes[i][1]] = blue_multiplier for i in xrange(len(fours)): sliced_image[fours[i][0]][fours[i][1]] = yellow_multiplier if save: try: mkdir_p('./results/') io.imsave('./results/result' + '_' + str(index) + '.png', sliced_image) except: io.imsave('./results/result' + '_' + str(index) + '.png', sliced_image) else: return sliced_image
def test_adjust_gamma_less_one(): """Verifying the output with expected results for gamma correction with gamma equal to half""" image = np.arange(0, 255, 4, np.uint8).reshape(8,8) expected = np.array([[ 0, 31, 45, 55, 63, 71, 78, 84], [ 90, 95, 100, 105, 110, 115, 119, 123], [127, 131, 135, 139, 142, 146, 149, 153], [156, 159, 162, 165, 168, 171, 174, 177], [180, 183, 186, 188, 191, 194, 196, 199], [201, 204, 206, 209, 211, 214, 216, 218], [221, 223, 225, 228, 230, 232, 234, 236], [238, 241, 243, 245, 247, 249, 251, 253]], dtype=np.uint8) result = exposure.adjust_gamma(image, 0.5) assert_array_equal(result, expected)
def test_adjust_gamma_greater_one(): """Verifying the output with expected results for gamma correction with gamma equal to two""" image = np.arange(0, 255, 4, np.uint8).reshape(8,8) expected = np.array([[ 0, 0, 0, 0, 1, 1, 2, 3], [ 4, 5, 6, 7, 9, 10, 12, 14], [ 16, 18, 20, 22, 25, 27, 30, 33], [ 36, 39, 42, 45, 49, 52, 56, 60], [ 64, 68, 72, 76, 81, 85, 90, 95], [100, 105, 110, 116, 121, 127, 132, 138], [144, 150, 156, 163, 169, 176, 182, 189], [196, 203, 211, 218, 225, 233, 241, 249]], dtype=np.uint8) result = exposure.adjust_gamma(image, 2) assert_array_equal(result, expected)
def augment_image(img): """ Augment an image using a combination of lightening, darkening, rotation and mirror images. :params img: Image as numpy array :return: array of augmented images """ augmented_images = [] augmented_images.append(np.fliplr(img)) for g in [0.45, 0.65, 0.85, 1.25, 1.5, 2]: new_img = exposure.adjust_gamma(img, gamma=g) augmented_images.append(new_img) augmented_images.append(np.fliplr(new_img)) new_img = transform.rotate(img, 180) augmented_images.append(new_img) augmented_images.append(np.fliplr(new_img)) return np.array(augmented_images)
def show_segmented_image(self, test_img, modality='t1c', show = False): ''' Creates an image of original brain with segmentation overlay INPUT (1) str 'test_img': filepath to test image for segmentation, including file extension (2) str 'modality': imaging modelity to use as background. defaults to t1c. options: (flair, t1, t1c, t2) (3) bool 'show': If true, shows output image. defaults to False. OUTPUT (1) if show is True, shows image of segmentation results (2) if show is false, returns segmented image. ''' modes = {'flair':0, 't1':1, 't1c':2, 't2':3} segmentation = self.predict_image(test_img, show=False) img_mask = np.pad(segmentation, (16,16), mode='edge') ones = np.argwhere(img_mask == 1) twos = np.argwhere(img_mask == 2) threes = np.argwhere(img_mask == 3) fours = np.argwhere(img_mask == 4) test_im = io.imread(test_img) test_back = test_im.reshape(5,240,240)[-2] # overlay = mark_boundaries(test_back, img_mask) gray_img = img_as_float(test_back) # adjust gamma of image image = adjust_gamma(color.gray2rgb(gray_img), 0.65) sliced_image = image.copy() red_multiplier = [1, 0.2, 0.2] yellow_multiplier = [1,1,0.25] green_multiplier = [0.35,0.75,0.25] blue_multiplier = [0,0.25,0.9] # change colors of segmented classes for i in xrange(len(ones)): sliced_image[ones[i][0]][ones[i][1]] = red_multiplier for i in xrange(len(twos)): sliced_image[twos[i][0]][twos[i][1]] = green_multiplier for i in xrange(len(threes)): sliced_image[threes[i][0]][threes[i][1]] = blue_multiplier for i in xrange(len(fours)): sliced_image[fours[i][0]][fours[i][1]] = yellow_multiplier if show: io.imshow(sliced_image) plt.show() else: return sliced_image
def image_transformation(X, method_type='blur', **kwargs): # https://www.kaggle.com/tomahim/image-manipulation-augmentation-with-skimage q = kwargs['percentile'] if 'percentile' in kwargs else (0.2, 99.8) angle = kwargs['angle'] if 'angle' in kwargs else 60 transformation_dict = { 'blur': normalize(ndimage.uniform_filter(X)), 'invert': normalize(util.invert(X)), 'rotate': rotate(X, angle=angle), 'rescale_intensity': _rescale_intensity(X, q=q), 'gamma_correction': exposure.adjust_gamma(X, gamma=0.4, gain=0.9), 'log_correction': exposure.adjust_log(X), 'sigmoid_correction': exposure.adjust_sigmoid(X), 'horizontal_flip': X[:, ::-1], 'vertical_flip': X[::-1, :], 'rgb2gray': skimage.color.rgb2gray(X) } return transformation_dict[method_type]
def enhance_patches(_img, _patches, _gamma=0.2): """ Emphasize a set of rectangular regions from an image. The original image is altered such that the regions of interest appear enhanced. :param _img: numpy.ndarray original image :param _patches: list list of 4-elements vectors indicating the coordinates of patches of interest :param _gamma: float gamma adjustment for "background" image :return: """ _res = adjust_gamma(_img, _gamma) for p in _patches: _res[p[0]:p[1], p[2]:p[3]] = _img[p[0]:p[1], p[2]:p[3]] return _res
def transform_features(data): with open('transformed_test_inputs.csv', 'wb') as csvfile: d = get_circle_filter(48,48) # writer = csv.writer(csvfile) # writer.writerow(["Id", "Prediction"]) out_data = [] i = 1 bar = pyprind.ProgBar(len(data), title="Transforming raw features") for row in data: # from (2304,) to (48,48) because i wrote the functions for 2d , can optimize later m = a2m(row) dm = deskew(apply_filter(exposure.adjust_gamma(m,0.4), d)) dm = dm-get_dilated(dm)*0.5 out_data.append(dm.flatten()) # print len(dm.flattenedten()) r = "%s," % str(i) for j in dm.flatten(): r += "%s," % str(j) csvfile.write(r[:-1] + "\n") i += 1 bar.update() return out_data
Y = [] for fname in image_file_list[k:k+n]: label = label_dict[fname.split('.')[0]] cur_img = imread(folder+'/'+fname , as_grey=True) cur_img = 1 - cur_img # randomly add samples r_for_eq = random() if r_for_eq<0.3: cur_img = equalize_adapthist(cur_img,ntiles_x=5,ntiles_y=5,clip_limit=0.1) if 0.3<r_for_eq<0.4: cur_img = adjust_sigmoid(cur_img,cutoff=0.5, gain=10, inv=False) if 0.5<r_for_eq<0.6: cur_img = adjust_gamma(cur_img,gamma=0.5, gain=1) X.append([cur_img.tolist()]) label_vec = [0]*5 label_vec[label] = 1 """ label_vec = [0]*3 if label == 0 or label == 1: label_vec[0] = 1 elif label == 2 or label == 3 : label_vec[1] = 1 else: label_vec[2] = 1 """
def do_darken(pic): return exposure.adjust_gamma(pic,1.3)
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 # Load an example image img = data.moon() # Gamma gamma_corrected = exposure.adjust_gamma(img, 2) # Logarithmic logarithmic_corrected = exposure.adjust_log(img, 1) # 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) ax_img, ax_hist, ax_cdf = plot_img_and_hist(img, axes[:, 0])
def main(): p = opt.ArgumentParser(description=""" Classifies regions of an image (based on SURF) using a pre-built model (codebook). """) p.add_argument('in_file', action='store', help='image file name') p.add_argument('out_file', action='store', help='file to store the resulting classification') p.add_argument('model', action='store', help='file containing the model') p.add_argument('-a', '--annot', action='store', help='annotation file name', default=None) p.add_argument('-t', '--threshold', action='store', type=int, default=5000, help='Hessian threshold for SURF features.') p.add_argument('-x', action='store', help='image name with patches classified', default=None) p.add_argument('-v', '--verbose', action='store_true', help='verbose?') args = p.parse_args() th = args.threshold if args.verbose: print("Image:", args.in_file) img = cv2.imread(args.in_file) mask = None with ModelPersistence(args.model, 'r', format='pickle') as mp: codebook = mp['codebook'] Xm = mp['shift'] Xs = mp['scale'] standardize = mp['standardize'] avg_dist = mp['avg_dist_to_centroid'] sd_dist = mp['stddev_dist_to_centroid'] if args.annot is not None: coords = np.fromfile(args.annot, dtype=int, sep=' ') # x y - values coords = np.reshape(coords, (coords.size/2, 2), order='C') # get the bounding box: xmin, ymin = coords.min(axis=0) xmax, ymax = coords.max(axis=0) img = img[ymin:ymax+3, xmin:xmax+3, :] # keep only the region of interest if args.verbose: print("\t...building mask") mask = np.zeros(img.shape[0:2], dtype=np.uint8) r, c = skimage.draw.polygon(coords[:,1]-ymin, coords[:,0]-xmin) # adapt to new image... mask[r,c] = 1 # everything outside the region is black if args.verbose: print("\t...H&E extraction") img_h, _ = rgb2he(img, normalize=True) # get the H- component img_h = equalize_adapthist(img_h) img_h = rescale_intensity(img_h, out_range=(0,255)) # make sure the dtype is right for image and the mask: OpenCV is sensitive to data type img_h = img_h.astype(np.uint8) if mask is not None: img_h *= mask if args.verbose: print("\t...feature detection and computation") feat = cv2.xfeatures2d.SURF_create(hessianThreshold=th) keyp, desc = feat.detectAndCompute(img_h, mask) if args.verbose: print("\t...", str(len(keyp)), "features extracted") X = np.hstack(desc) X = np.reshape(X, (len(desc), desc[0].size), order='C') if standardize: # make sure each variable (column) is mean-centered and has unit standard deviation X = (X - Xm) / Xs if args.verbose: print("\t...classification") # instead of the following, allow for "no label": y0 = codebook.predict(X).tolist() y = np.zeros(X.shape[0], dtype=np.int) - 1 d = np.zeros((X.shape[0], codebook.cluster_centers_.shape[0])) for k in range(0, codebook.cluster_centers_.shape[0]): d[:, k] = np.linalg.norm(X - codebook.cluster_centers_[k, :], axis=1) for i in range(0, d.shape[0]): # find the closest centroid among those that have a distance < 3*SD j = np.where(d[i, :] < avg_dist + 3.0*sd_dist)[0] if np.any(j): y[i] = j[np.argmin(d[i, j])] # the label of the closest centroid #if np.any(y < 0): # y = y[y >= 0] if args.verbose: print("\t...of", str(X.shape[0]), "patches,", str(y.size), "where assigned a label") with open(args.out_file, mode='w') as fout: for k in range(len(y)): s = '\t'.join([str(np.round(keyp[k].pt[0])), str(np.round(keyp[k].pt[1])), str(np.round(keyp[k].size)), str(y[k]), str(y0[k])]) + '\n' fout.write(s) if args.x is not None: # construct a representation of the image based on the class labels img = adjust_gamma(img, 0.2) # dim the image for k in range(len(y)): x, y = keyp[k].pt x = int(np.round(x)) y = int(np.round(y)) r = int(np.round(keyp[k].size)) img[y-int(r/2):y+int(r/2), x-int(r/2):x+int(r/2), :] = (10, (10+2*k)%256, k%256) cv2.imwrite(args.x, img)
def do_composite(image, bg_fname, sigma, motion, noise, gamma): """Composite a background image onto the foreground. Args: image: original image, floating point [0,1]. bg_fname: background image file name, None or empty string if none. sigma: blur in pixels; single value or range. motion: 4-tuple <min_pix_move, max_pix_move, min_deg_angle, max_deg angle>. noise: pixel noise in the range 0-255, either single value or range. gamma: gamma correction to be applied to image, 0 for none. Returns: Updated image. """ def make_random(x): # Arg x can be list, tuple, numpy.ndarray if isinstance(x, list) or isinstance(x, tuple): x = np.array(x) assert isinstance( x, np.ndarray), 'Argument to do_composite must be list or array' if x.size == 0: return None elif x.size == 1: return x[0] else: return random.uniform(x[0], x[1]) if motion[1]: image = do_motion_blur(image, motion[:2], motion[2:]) ys, xs, _ = image.shape if bg_fname: scene = read_image(bg_fname) * (1.0 / 255.0) if random.choice([True, False]): scene = np.flipud(scene) if random.choice([True, False]): scene = np.fliplr(scene) yss, xss = scene.shape[0], scene.shape[1] assert yss > ys, 'Background image must be larger than training image' assert xss > xs, 'Background image must be larger than training image' # Adjust gamma of image. gamma = make_random(gamma) if gamma is not None: image[:, :, :3] = adjust_gamma(image[:, :, :3], gamma) # Add noise to object. noise = make_random(noise) if noise is not None: image[:, :, :3] += np.random.randn(ys, xs, 3) * noise * 0.5 / 255. np.clip(image, 0.0, 1.0, image) # Cut out ellipse where image alpha is 0. if bg_fname: ul_y = random.randint(0, yss - ys - 1) ul_x = random.randint(0, xss - xs - 1) scene_crop = scene[ul_y:ul_y + ys, ul_x:ul_x + xs, :] mask = image[:, :, 3] rgbmask = np.stack([mask, mask, mask], axis=2) image[:, :, :3] = scene_crop * (1.0 - rgbmask) + image[:, :, :3] * rgbmask else: image[image[:, :, 3] == 0, 0:3] = 0.5 # Add gaussian blur and noise. sigma = make_random(sigma) im = np.copy(image[:, :, :3]) # Need copy to preserve float32 gaussian(image[:, :, :3], sigma, multichannel=True, output=im) # Add noise to whole scene, after blur. if noise is not None: im[:, :, :3] += np.random.randn(ys, xs, 3) * noise * 0.5 / 255. np.clip(im, 0.0, 1.0, im) return im
def test_adjust_gamma_neggative(): image = np.arange(0, 255, 4, np.uint8).reshape((8, 8)) with testing.raises(ValueError): exposure.adjust_gamma(image, -1)
from skimage import feature from skimage import draw from skimage import util from skimage import color from skimage import morphology from skimage import filters from skimage import measure from skimage import transform from skimage import exposure filename = 'AmorpHole.jpg' image = io.imread(filename) grey = color.rgb2grey(image) grey = exposure.equalize_adapthist(exposure.adjust_gamma(grey)) # grey = exposure.equalize_adapthist(exposure.adjust_gamma(grey)) grey = grey > 0.5 # grey = util.invert(grey) # def getHoleImage(hole_coords): # hole_image = numpy.zeros((image_height,image_width)) # for coord in hole_coords: # hole_image[coord[1],coord[0]] = 1 # return ndi.binary_fill_holes(morphology.binary_closing(hole_image))
def bleach_plot2(k, v, bg=100.0, num_tiles=16, gamma=0.5, dt=1): """Plot bleaching of image timeseries Parameters ---------- k : str Title string v : ndarray (t, y, x) Image stack (x must y) bg : float Background counts num_tiles : int Number of tiles per image side """ nt, ny, nx = v.shape assert ny == nx, "data isn't square" # make figure fig, axs = plt.subplots(1, 3, figsize=(9, 3)) (ax, ax_k, ax_i) = axs fig.suptitle(k, y=1.05) # split the image img_split = split_img(v, nx // num_tiles) # sum kinetics, convert to float bg = np.asarray(bg) bg.shape = (-1, 1, 1) kinetics = (img_split * 1.0 - bg).sum((2, 3)) # anything that's negative mask with an nan kinetics[kinetics < 0] = np.nan # normalize all the kinetics with the max one norm_kinetics = kinetics / np.max(kinetics[:, np.newaxis], -1) kin_img = np.ones_like(img_split)[:, 0, ...] * norm_kinetics[:, -1, np.newaxis, np.newaxis] # start plotting # plot kinetics, color by amount of bleaching and set alpha to initial intensity for trace, cpoint in zip(norm_kinetics, scale(norm_kinetics[:, -1])): # make sure the point is not an nan if np.isfinite(cpoint): ax.loglog(np.arange(len(trace)) * dt, trace, c=plt.get_cmap("spring")(cpoint)) # label stuff ax.set_title("Bleaching Kinetics") ax.set_xlabel("Time (s)") ax.set_ylabel("Relative Intensity (a.u.)") ax.tick_params() # calculate the max image v_max = v.max(0) # plot color coded kinetics ax_k.matshow(combine_img(kin_img), cmap="spring", zorder=0) # plot image with alpha over it ax_k.matshow(v_max, cmap=greys_alpha_cm, norm=LogNorm(), zorder=1) # plot raw image with gamma adjustment for reference ax_i.matshow(adjust_gamma(v_max, gamma), cmap="Greys_r") # remove grids for ax in (ax_i, ax_k): ax.grid("off") ax.axis("off") # set titles ax_i.set_title("Image") ax_k.set_title("Bleaching Map") fig.tight_layout() return fig, axs
# There are plenty of ways of combining the different grey-level images into a # false-color representation (it's a bit of an art!). This is a very simple # pipeline, that mainly tweaks intensities, and that does nothing fancy along # the lines of denoising, sharpening, etc. import numpy as np import matplotlib.pyplot as plt from skimage import img_as_float, io, exposure ic = io.ImageCollection('m8_050507_*.png') ic = [img_as_float(img) for img in ic] H, B, G, R, L = ic H = exposure.adjust_sigmoid(H, cutoff=0.05, gain=35) L = exposure.adjust_sigmoid(L, cutoff=0.05, gain=15) R = exposure.adjust_gamma(R, 0.1) # Merge R, G, B channels out = np.dstack((H, L, R)) out = exposure.adjust_gamma(out, 2.1) io.imsave('m8_recon.png', out) f, ax = plt.subplots() ax.imshow(out) plt.show()
def generator(self): rand_index = np.arange(len(self.image_list)) np.random.shuffle(rand_index) for index in range(len(self.image_list)): image_path = self.image_list[rand_index[index]] mask_path = self.mask_list[rand_index[index]] # image_path = os.path.join(self.data_dir, file_basename_image) if image_path.split('/')[-1].split('_')[0] == 'n': label = np.array([0.0]) else: label = np.array([1.0]) # label = image_path.split('/') # label = label[-2] # label = label_dict[label] image, mask = self.read_data(image_path, mask_path) # image = tf.cast(image, tf.float32) # mask = tf.cast(mask, tf.float32) image = image / 255 mask = mask / 255 # mask = scalar2onehot(mask) # image = (np.array(image[:, :, np.newaxis])) # mask = np.array(mask[:, :, np.newaxis]) # if self.__Param["mode"] == "train_decision" or self.__Param["mode"] == "train_segmentation": # aug_random = np.random.uniform() # if aug_random > 0.6: # expo = np.random.choice([0.7, 0.8, 0.9, 1.1, 1.2, 1.3]) # image = exposure.adjust_gamma(image, expo) # angle=np.random.randint(1,72)*5 # image=rotate(image,angle) ######################################################################################### # if IMAGE_MODE == 0: # expanding dimension is needed only in mono mode if self.__Param["mode"] == "train_decision" or self.__Param["mode"] == "train_segmentation": aug_random = np.random.uniform() if aug_random > 0.7: # adjust_gamma if np.random.uniform() > 0.7: expo = np.random.choice([0.7, 0.8, 0.9, 1.1, 1.2, 1.3]) image = exposure.adjust_gamma(image, expo) # flip if np.random.uniform() > 0.7: aug_seed = np.random.randint(-1, 2) image = cv2.flip(image, aug_seed) mask = cv2.flip(mask, aug_seed) # # rotate # if np.random.uniform() > 0.7: # angle = np.random.randint(-5, 5) # image = rotate(image, angle) # mask = rotate(mask, angle) # GassianBlur if np.random.uniform() > 0.7: image = cv2.GaussianBlur(image, (5, 5), 0) # # shift # if np.random.uniform() > 0.7: # dx = np.random.randint(-5, 5) # width*5% # dy = np.random.randint(-5, 5) # Height*10% # rows, cols = image.shape[:2] # M = np.float32([[1, 0, dx], [0, 1, dy]]) # (x,y) -> (dx,dy) # image = cv2.warpAffine(image, M, (cols, rows)) # mask = cv2.warpAffine(mask, M, (cols, rows)) # mask = self.ImageBinarization(mask) if IMAGE_MODE == 0: # expanding dimention is needed only in mono mode image = image[:,:,0] image = (np.array(image[:, :, np.newaxis])) mask = (np.array(mask[:, :, np.newaxis])) # needed only when label is mono # image = (np.array(image[:, :, np.newaxis])) ########################################################### yield image, mask, label, image_path
io.imsave(os.path.join("/data/images", new_name), new_img) # Jitter by -10 in RGB new_img = jitter_rgb(img, -10) new_name = im_name + "_p4" + ".jpg" new_image_list.append(new_name + " " + label) io.imsave(os.path.join("/data/images", new_name), new_img) # Equalize histogram new_img = exposure.equalize_hist(img) new_name = im_name + "_p5" + ".jpg" new_image_list.append(new_name + " " + label) io.imsave(os.path.join("/data/images", new_name), new_img) # Lighten 1 new_img = exposure.adjust_gamma(img, gamma=0.5) new_name = im_name + "_p6" + ".jpg" new_image_list.append(new_name + " " + label) io.imsave(os.path.join("/data/images", new_name), new_img) # Lighten 2 new_img = exposure.adjust_gamma(img, gamma=0.65) new_name = im_name + "_p7" + ".jpg" new_image_list.append(new_name + " " + label) io.imsave(os.path.join("/data/images", new_name), new_img) # Lighten 3 new_img = exposure.adjust_gamma(img, gamma=0.85) new_name = im_name + "_p8" + ".jpg" new_image_list.append(new_name + " " + label) io.imsave(os.path.join("/data/images", new_name), new_img)
def color_erode(img_data, color_data, is_alpha, transparent, noise, img_type): blacks = rgb2gray(img_data) blacks = blacks < 0.2 original_image = img_data img_data = color_data img_data = exposure.adjust_gamma(color_data, 1) grayscale = rgb2gray(img_data) original_grayscale = grayscale print(grayscale[0, 0]) print(img_data[0, 0]) #grayscale += 9 grayscale = 10 * np.round_(grayscale, 1) grayscale = grayscale.astype(int) grayscale += blacks if is_alpha and transparent: major_alpha, a = alpha_test(is_alpha, color_data) if major_alpha: alpha_mask = a else: print("Not enough transparency, falling back to white threshold") smoothness = 2 alpha_mask = smooth(grayscale, smoothness) alpha_mask = invert(alpha_mask) else: smoothness = 2 alpha_mask = smooth(grayscale, smoothness) alpha_mask = invert(alpha_mask) label_image = label(grayscale) img_data = grayscale new_arr = np.zeros(img_data.shape) for region in regionprops(label_image): if region.area >= 3: #gives us square area in original image region is inside minr, minc, maxr, maxc = region.bbox #use the offset to get the position of the region and rip it from the image img_slice = region.image if is_alpha and transparent: img_slice *= alpha_mask[minr:maxr, minc:maxc] if img_data.shape[1] < 2880: print(img_data.shape[0]) print("Image under 2880px wide, resizing to avoid artifacts") img_slice = rescale(img_slice, 2, anti_aliasing=False) #now erode it and stick it in the new array selem = disk(1) eroded_slice = binary_erosion(img_slice, selem) if img_data.shape[1] < 2880: eroded_slice = rescale(eroded_slice, 0.5, anti_aliasing=False) new_arr[minr:maxr, minc:maxc] += eroded_slice #new_arr[minr:maxr, minc:maxc] += img_slice vor_arr = np.pad(new_arr, pad_width=10, mode='constant', constant_values=1) #medial axis skeleton, to get point location and thickness skel, distance = medial_axis(new_arr, return_distance=True) dist_on_skel = distance * skel #Voronoi diagram from Harris edges, to avoid concave shapes h_smoothness = 0 label_image2 = harris_voronoi(new_arr, vor_arr, h_smoothness, dist_on_skel, is_alpha) lst = pts_to_list(dist_on_skel, label_image2, noise, img_type, color_data) return lst
def image_augmentation(image, segment, policy): # # TODO melanoma area mask or replacement # # 30% room in if 'enhence' in policy and np.random.rand() > 0.7: # print("room in") # # find object border h, w, _ = image.shape locy, locx = np.where(segment == 255) objl, objr, obju, objd = min(locx), max(locx), min(locy), max(locy) # # (objl + objr) / 2, (obju + objd) / 2, r - l, d - u cropl_max = (3 * objl + objr) / 4. cropr_min = (3 * objr + objl) / 4. cropu_max = (3 * obju + objd) / 4. cropd_min = (3 * objd + obju) / 4. mu_l, sigma_l = 2 * cropl_max / 3., cropl_max / 6. mu_r, sigma_r = (2 * cropr_min + w) / 3., (w - cropr_min) / 6. mu_u, sigma_u = 2 * cropu_max / 3., cropu_max / 8. mu_d, sigma_d = (2 * cropd_min + h) / 3., (h - cropd_min) / 8. cropl = np.random.normal(mu_l, sigma_l) cropr = np.random.normal(mu_r, sigma_r) cropu = np.random.normal(mu_u, sigma_u) cropd = np.random.normal(mu_d, sigma_d) cropl = np.clip(cropl, 0, cropl_max).astype(int) cropr = np.clip(cropr, cropr_min, w).astype(int) cropu = np.clip(cropu, 0, cropu_max).astype(int) cropd = np.clip(cropd, cropd_min, h).astype(int) # print("image", h, w, "obj", objl, objr, obju, objd, 'crop_m', cropl_max, cropr_min, cropu_max, cropd_min, "crop", cropl, cropr, cropu, cropd) image = image[cropu:cropd, cropl:cropr] # io.imshow(image) # io.show() segment = segment[cropu:cropd, cropl:cropr] # print(image.shape, segment.shape) # # 30% exposure if np.random.rand() > 0.7: # print("exposure") # # Brightness adjust image = exposure.adjust_gamma(image, np.random.normal(1.0, 0.1), np.random.normal(1.0, 0.1)) image = exposure.adjust_log(image, np.random.normal(1.0, 0.1)) image = exposure.adjust_sigmoid(image, np.random.normal(.25, 0.1), 10 * np.random.normal(1.0, 0.1)) image = img_as_ubyte(image) # # 30% rotation if np.random.rand() > 0.7: # print("rotation") image = Image.fromarray(image) image.rotate(60 * np.random.normal(1.0, 0.2)) image = np.asarray(image) # # 30% random noise if np.random.rand() > 0.7: # print("noise") image = util.random_noise(image, mean=0., var=5e-4 * np.random.normal(1.0, 0.1)) image = img_as_ubyte(image) # # 30% padding if np.random.rand() > 0.7: # print("padding") p = 20 th, tw, _ = image.shape tmp_img = np.zeros((th + 2 * p, tw + 2 * p, 3)) tmp_img[p:th + p, p:tw + p] = image tmp_segment = np.zeros((th + 2 * p, tw + 2 * p)) tmp_segment[p:th + p, p:tw + p] = segment else: tmp_img = image tmp_segment = segment # io.imshow(tmp_img) # io.show() # io.imshow(tmp_segment) # io.show() return tmp_img, tmp_segment
def gamma(image): return exposure.adjust_gamma(image, gamma=0.5, gain=1)
def recovery_plot(k, v, bg=100, p0=(-0.1, 100, 1), num_tiles=16): """Plot the recovery of intensity Parameters ---------- k : str Figure title v : nd.array (t, y, x) Assumes an image stack bg : numeric Background for image data p0 : tuple Initial guesses for exponential decay num_tiles : int The number of sub images to calculate Returns ------- fig : figure handle axs : axes handles""" # make figure fig, axs = plt.subplots(1, 4, figsize=(4 * 3.3, 3), gridspec_kw=dict(width_ratios=(1, 1.3, 1, 1))) (ax, ax_k, ax_h, ax_i) = axs my_shape = v.shape fig.suptitle(k, y=1.05) # split the image img_split = split_img(v, v.shape[-1] // num_tiles) * 1.0 - bg # sum kinetics, convert to float kinetics = img_split.mean((2, 3)) norm_kinetics = kinetics / kinetics[:, np.newaxis].max(-1) xdata = np.arange(my_shape[0]) * 0.1 * 46 ks = np.array( [curve_fit(exp, xdata, kin, p0=p0)[0][1] for kin in norm_kinetics]) kin_img = np.ones_like(img_split)[:, 0, ...] * ks[:, np.newaxis, np.newaxis] # plot kinetics, color by amount of bleaching and set alpha to initial intensity for trace, cpoint in zip(norm_kinetics, scale(ks)): if np.isfinite(cpoint): ax.plot(xdata, trace, c=plt.get_cmap("spring")(cpoint)) # start plotting ax.set_title("Bleaching Kinetics") ax.set_xlabel("J/cm$^2$") ax.set_ylabel("Relative Intensity (a.u.)") ax.tick_params() img = ax_k.matshow(combine_img(kin_img), cmap="spring") cb = plt.colorbar(img, ax=ax_k, aspect=10, use_gridspec=True) cb.set_label("Decay constant (J/cm$^2$)") ax_k.matshow(v.max(0), cmap=greys_alpha_cm, norm=LogNorm()) ax_i.matshow(adjust_gamma(v.max(0), 0.25), cmap="Greys_r") ax_h.hist(ks, bins=int(np.sqrt(ks.size))) ax_h.set_title("Median = {:.0f}".format(np.median(ks))) for ax in (ax_i, ax_k): ax.grid("off") ax.axis("off") ax_i.set_title("Image") ax_k.set_title("Bleaching Map") fig.tight_layout() return fig, axs
def __call__(self, sample): sigma = np.random.uniform(self.sigmas[0], self.sigmas[1]) sample = exposure.adjust_gamma(self.norm(sample), sigma) return sample
def plot_false_RGB(im1, im2, im3, savedir, gamma=0.5, perlow=5, perhigh=98, tag='', savefig=True): """ Make false color RGB from 3 bands of images and plot Parameters ---------- im1: numpy array b1 image, 2d im2: numpy array b2 image, 2d im3: 2d array b3 image, 3d gamma: float gamma correction value perlow: int or float lower bound for percentile for imshow constrast stretch perhigh: int or float uppper bound for percentile for imshow constrast stretch savedir: str where to save plots tag: str e.g., 'train' or 'test' savefig: bool save plot or show """ # Normalize band im3_norm = normalize(im3) im2_norm = normalize(im2) im1_norm = normalize(im1) false_RGB = np.dstack((im1_norm, im2_norm, im3_norm)) plt.figure() plt.hist(false_RGB.flatten()) if savefig: plt.savefig(savedir + tag + '_falsergb_hist.png') else: plt.show() plt.close() # scale up contrast from skimage import exposure plow, phigh = np.percentile(false_RGB, (perlow, perhigh)) rgbStretched = exposure.rescale_intensity( false_RGB, in_range=(plow, phigh)) # Perform contrast stretch on RGB range rgbStretched = exposure.adjust_gamma(rgbStretched, gamma) # Perform Gamma Correction # imshow fig = plt.figure() ax = plt.Axes(fig, [0, 0, 1, 1]) ax.set_axis_off() fig.add_axes(ax) # Plot a natural color RGB ax.imshow(rgbStretched, interpolation='bilinear', alpha=0.9) if savefig: plt.savefig(savedir + tag + '_falsergb.png') else: plt.show() plt.close()
def test_negative(): image = np.arange(-10, 245, 4).reshape((8, 8)).astype(np.double) with testing.raises(ValueError): exposure.adjust_gamma(image)
# Display cumulative distribution img_cdf, bins = exposure.cumulative_distribution(image, bins) ax_cdf.plot(bins, img_cdf, 'r',lw=3) ax_cdf.set_yticks([]) return ax_img, ax_hist, ax_cdf ### Load example image # Load an example image img = data.moon() ### Gamma corrected with $\gamma=2$ gamma_corrected = exposure.adjust_gamma(img, 2) ### Logarithmic correction logarithmic_corrected = exposure.adjust_log(img, 1) ### Display results side by side 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)
def gamma_correction(image: ndarray): return adjust_gamma(image, gamma=0.4, gain=0.9)
# # data = exposure.rescale_intensity(data, in_range=(np.min(data)*100, np.max(data)*100)) # seed = np.copy(fdata) # seed[1:-1, 1:-1] = fdata.min() # fdata = fdata - reconstruction(seed, fdata, method='dilation') # fdata = filters.gaussian_filter(fdata, sigma=5) # seed = None # # seed = np.copy(fdata) # seed[1:-1, 1:-1] = fdata.min() # fdata = fdata - reconstruction(seed, fdata, method='dilation') # seed = None # scipy.misc.imsave(oname, fdata) #data = np.flipud(data) #fdata = np.flipud(fdata) fdata = exposure.adjust_gamma(fdata, 5) # p1, p2 = np.percentile(fdata, (2, 98)) fdata = exposure.rescale_intensity(fdata, in_range=(p1, p2)) fdata -= fdata.min() fdata /= fdata.max() if show_plots: plt.imshow(fdata, cmap="gray") plt.show() #thresh = threshold_otsu(fdata) thresh = threshold_minimum(fdata) #bin = fdata > thresh*1.70
def test_adjust_gamma_zero(): """White image should be returned for gamma equal to zero""" image = np.random.uniform(0, 255, (8, 8)) result = exposure.adjust_gamma(image, 0) dtype = image.dtype.type assert_array_equal(result, dtype_range[dtype][1])
def img_to_list(img_data, num, noise, img_type, transparent, smoothness): #smoothness = 5 h_smoothness = smoothness * 1.5 is_alpha = False width, height = img_data.size img_data = np.array(img_data) img_data = np.array(img_data) color_data = img_data.copy() color_data = exposure.adjust_gamma(color_data, 2) lst = [] #strip colors try: img_data = rgb2gray(rgba2rgb(img_data)) is_alpha = True except ValueError: img_data = rgb2gray(img_data) ''' #smooth and threshold Line Art to make sure it isn't basically empty smooth_intensity = 0 #don't want to smooth the image for the panic test if it's too small if width >= 700 and height >= 700: smooth_intensity = 2 blank_test = smooth(img_data, smooth_intensity) #keep it from panicking if the image is blank if is_blank(blank_test): print("blank") return ''' if img_type == "SHADING": lst = shading_to_list(img_data, color_data, is_alpha) if img_type == "COLOR": lst = color_erode(img_data, color_data, is_alpha, transparent, noise, img_type) if img_type == "LINEART": #make a padded version so the Voronoi output looks better vor_arr = np.pad(img_data, pad_width=10, mode='constant', constant_values=1) if is_alpha and transparent: major_alpha, a = alpha_test(is_alpha, color_data) if major_alpha: img_data = a else: print( "Not enough transparency, falling back to white threshold") img_data = smooth(img_data, smoothness) img_data = invert(img_data) else: img_data = smooth(img_data, smoothness) img_data = invert(img_data) #medial axis skeleton, to get point location and thickness skel, distance = medial_axis(img_data, return_distance=True) dist_on_skel = distance * skel #Voronoi diagram from Harris edges, to avoid concave shapes label_image = harris_voronoi(img_data, vor_arr, h_smoothness, dist_on_skel, is_alpha) lst = pts_to_list(dist_on_skel, label_image, noise, img_type, color_data) return lst
im_path = pair[0] full_im_name = re.findall(r'\/(.+)', im_path)[0] im_name = re.findall(r'(\d+)\.', full_im_name)[0] # Add original image to list of augmented images #Load the image img = io.imread(os.path.join(image_path, im_path)) #Save the original image io.imsave(os.path.join(image_out, full_im_name), img) new_image_list.append(full_im_name + " " + label) io.imsave(os.path.join(image_out, "m_" + full_im_name), np.fliplr(img)) new_image_list.append("m_" + full_im_name + " " + label) #Perform transformations # Lighten 1 & mirror image new_img = exposure.adjust_gamma(img, gamma=0.45) new_name = im_name + "_p1" + ".jpg" new_image_list.append(new_name + " " + label) io.imsave(os.path.join(image_out, new_name), new_img) new_name = im_name + "_p1m" + ".jpg" new_image_list.append(new_name + " " + label) io.imsave(os.path.join(image_out, new_name), np.fliplr(new_img)) # Lighten 2 & mirror image new_img = exposure.adjust_gamma(img, gamma=0.65) new_name = im_name + "_p2" + ".jpg" new_image_list.append(new_name + " " + label) io.imsave(os.path.join(image_out, new_name), new_img) new_name = im_name + "_p2m" + ".jpg" io.imsave(os.path.join(image_out, new_name), np.fliplr(new_img))
def do_brighten(pic): return exposure.adjust_gamma(pic,0.7)
def evaluate(test_data, mask): with tf.Graph().as_default() as g: y_ = tf.placeholder(tf.float32, shape=[None, 6, 117, 120, 2], name='y-label') mask_p = tf.placeholder(tf.complex64, shape=[None, 6, 117, 120], name='mask') kspace_p = tf.placeholder(tf.complex64, shape=[None, 6, 117, 120], name='kspace') y = inference.inference(mask_p, kspace_p, None) loss = tf.reduce_mean( tf.reduce_mean(tf.squared_difference(y, y_), [1, 2, 3, 4])) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state(model_save_path) saver = tf.train.Saver() test_case = 'show image' if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) if __name__ == '__main__': if test_case == 'check_loss': count = 0 for ys in train.iterate_minibatch(test_data, batch_size, shuffle=True): xs_l, kspace_l, mask_l, ys_l = train.prep_input( ys, mask) loss_value, y_pred = sess.run([loss, y], feed_dict={ y_: ys_l, mask_p: mask_l, kspace_p: kspace_l }) print("The loss of No.{} test data = {}".format( count + 1, loss_value)) y_c = real2complex(y_pred) xs_c = real2complex(xs_l) base_mse, test_mse, base_psnr, \ test_psnr, base_ssim, test_ssim = performance(xs_c, y_c, ys) print("test loss:\t\t{:.6f}".format(loss_value)) print("test psnr:\t\t{:.6f}".format(test_psnr)) print("base psnr:\t\t{:.6f}".format(base_psnr)) print("base mse:\t\t{:.6f}".format(base_mse)) print("test mse:\t\t{:.6f}".format(test_mse)) print("base ssim:\t\t{:.6f}".format(base_ssim)) print("test ssim:\t\t{:.6f}".format(test_ssim)) count += 1 elif test_case == 'show image': final_result_path = '/media/keziwen/86AA9651AA963E1D/Tensorflow/MyDeepMRI-KI_Net V2/for KI/final results' figure_save_path = join(final_result_path, 'KI vs KI_with_KLoss(e-1)', 'KI') if not os.path.isdir(figure_save_path): os.makedirs(figure_save_path) order = 79 ys = test_data[order] ys = ys[np.newaxis, :] xs_l, kspace_l, mask_l, ys_l = train.prep_input( ys, mask) time_start = time.time() loss_value, y_pred = sess.run([loss, y], feed_dict={ y_: ys_l, mask_p: mask_l, kspace_p: kspace_l }) time_end = time.time() y_pred = real2complex(y_pred) xs = real2complex(xs_l) if order == 0: order_x = 100 elif order == 1: order_x = 60 elif order == 2: order_x = 85 elif order == 6: order_x = 40 else: order_x = 55 #order_x = 55 # (order, order_x): (0, 100), (1, 60), (6, 40), (7, 55) ys_t = ys[:, :, order_x, :] y_pred_t = y_pred[:, :, order_x, :] xs_t = xs[:, :, order_x, :] xs_t_error = ys_t - xs_t y_pred_error = ys_t - y_pred_t base_mse, test_mse, base_psnr,\ test_psnr, base_ssim, test_ssim = performance(xs, y_pred, ys) print("test time:\t\t{:.6f}".format(time_end - time_start)) print("test loss:\t\t{:.6f}".format(loss_value)) print("test psnr:\t\t{:.6f}".format(test_psnr)) print("base psnr:\t\t{:.6f}".format(base_psnr)) print("base mse:\t\t{:.6f}".format(base_mse)) print("test mse:\t\t{:.6f}".format(test_mse)) print("base ssim:\t\t{:.6f}".format(base_ssim)) print("test ssim:\t\t{:.6f}".format(test_ssim)) mask_shift = mymath.fftshift(mask, axes=(-1, -2)) gamma = 1 plt.figure(1) plt.subplot(221) plt.imshow( exposure.adjust_gamma(np.abs(ys[0][0]), gamma), plt.cm.gray) plt.title('ground truth') plt.subplot(222) plt.imshow( exposure.adjust_gamma(abs(mask_shift[0][0]), gamma), plt.cm.gray) plt.title('mask') plt.subplot(223) plt.imshow(exposure.adjust_gamma(abs(xs[0][0]), gamma), plt.cm.gray) plt.title("undersampling") plt.subplot(224) plt.imshow( exposure.adjust_gamma(abs(y_pred[0][0]), gamma), plt.cm.gray) plt.title("reconstruction") plt.savefig( join(figure_save_path, 'test%s.png' % order)) plt.figure(2) plt.imshow( exposure.adjust_gamma(np.abs(ys[0][0]), gamma), plt.cm.gray) plt.title('ground truth') plt.savefig(join(figure_save_path, 'gr%s.png' % order)) plt.figure(3) plt.imshow(exposure.adjust_gamma(abs(xs[0][0]), gamma), plt.cm.gray) plt.title("undersampling") plt.savefig( join(figure_save_path, 'under%s.png' % order)) plt.figure(4) plt.imshow( exposure.adjust_gamma(abs(y_pred[0][0]), gamma), plt.cm.gray) plt.title("reconstruction") plt.savefig( join(figure_save_path, 'recon%s.png' % order)) plt.figure(5) plt.imshow( exposure.adjust_gamma( abs(np.abs(ys[0][0]) - abs(y_pred[0][0])), gamma)) plt.title("error") plt.savefig( join(figure_save_path, 'error%s.png' % order)) plt.figure(6) plt.subplot(511) plt.imshow(np.abs(ys_t[0]), plt.cm.gray) plt.title("gnd_t_y") plt.subplot(512) plt.imshow(np.abs(xs_t[0]), plt.cm.gray) plt.title("under_t_y") plt.subplot(513) plt.imshow(np.abs(xs_t_error[0])) plt.title("under_t_y_error") plt.subplot(514) plt.imshow(np.abs(y_pred_t[0]), plt.cm.gray) plt.title("recon_t_y") plt.subplot(515) plt.imshow(np.abs(y_pred_error[0])) plt.title("recon_t_y_error") plt.savefig(join(figure_save_path, 't_y%s.png' % order)) train_plot = np.load( join(project_root, 'models/%s' % model_file, 'train_plot.npy')) validate_plot = np.load( join(project_root, 'models/%s' % model_file, 'validate_plot.npy')) [ num_train_plot, ] = train_plot.shape [ num_validate_plot, ] = validate_plot.shape x1 = np.arange(1, num_train_plot + 1) x2 = np.arange(1, num_validate_plot + 1) plt.figure(7) l1, = plt.plot(x1, train_plot) l2, = plt.plot(x2, validate_plot) plt.legend(handles=[ l1, l2, ], labels=['train loss', 'validation loss'], loc=1) plt.xlabel('epoch') plt.ylabel('loss') plt.title('loss') if not os.path.exists( join(figure_save_path, 'loss.png')): plt.savefig(join(figure_save_path, 'loss.png')) #plt.show() #elif test_case == "Save image": else: print("No checkpoint file found")
def read(self, name, flatten=True): img = img_as_ubyte(imread(name, flatten)) cropped = img[55:150, 175:300] cropped = exposure.adjust_gamma(cropped) return cropped
from PIL import Image from skimage import data, exposure, img_as_float import os import numpy as np dirname = os.path.dirname(os.path.abspath(__file__)) dirnames = [os.path.join(dirname, 'train'), os.path.join(dirname, 'train')] for dirname in dirnames: for i in range(2000): im = Image.open(dirname + '/%d/%d_1.png' % (i, i)) gam1 = exposure.adjust_gamma(im, 2) gam2 = exposure.adjust_gamma(im, 0.5) im_Contrast = skimage.exposure.rescale_intensity(im) gam1.save(dirname + '/%d/%d_2.png' % (i, i)) gam2.save(dirname + '/%d/%d_3.png' % (i, i)) im_Contrast.save(dirname + '/%d/%d_4.png' % (i, i)) im_noise = im rows, cols, dims = im_noise.shape for j in range(500): x = np.random.randint(0, rows) y = np.random.randint(0, cols) im_noise[x, y, :] = 255 im_noise.save(dirname + '/%d/%d_5.png' % (i, i)) im_transpose = im.transpose(Image.FLIP_LEFT_RIGHT) im_transpose.save(dirname + '/%d/%d_6.png' % (i, i)) gam1 = exposure.adjust_gamma(im_transpose, 2) gam2 = exposure.adjust_gamma(im_transpose, 0.5) im_Contrast = skimage.exposure.rescale_intensity(im_transpose) gam1.save(dirname + '/%d/%d_7.png' % (i, i))
# brightens or darkens an image. A power-law transform, where `gamma` denotes # the power-law exponent, is applied to each pixel in the image: `gamma < 1` # will brighten an image, while `gamma > 1` will darken an image. def plot_hist(ax, data, title=None): # Helper function for plotting histograms ax.hist(data.ravel(), bins=256) ax.ticklabel_format(axis="y", style="scientific", scilimits=(0, 0)) if title: ax.set_title(title) gamma_low_val = 0.5 gamma_low = exposure.adjust_gamma(data, gamma=gamma_low_val) gamma_high_val = 1.5 gamma_high = exposure.adjust_gamma(data, gamma=gamma_high_val) _, ((a, b, c), (d, e, f)) = plt.subplots(nrows=2, ncols=3, figsize=(12, 8)) show_plane(a, data[32], title='Original') show_plane(b, gamma_low[32], title=f'Gamma = {gamma_low_val}') 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
def imrandgammaadj(im, gammarange): gamma = float(np.random.randint(gammarange[0], gammarange[1])) / 100.0 return xpsr.adjust_gamma(im, gamma)
def mobile_chioce(img_path,save_path,choice_num,image_Var): ''' :param img_path: 图片路径 :param save_path: 图片保存路径 :param choice_num: 选项个数 :param image_Var: 图片模糊度,可设置(10,20。。。),值越大,过滤掉的模糊图片越多 :return: 识别出来的图片,对应的out,不能识别的模糊图片 ''' choice_set = [(0, 0, 42, 32), (42, 0, 84, 32), (84, 0, 126, 32), (126, 0, 168, 32) , (168, 0, 210, 32), (210, 0, 252, 32), (252, 0, 294, 32)] all_output=[] all_image=[] unknown_img = [] all_lable=[] for file in os.listdir(img_path): if file == '.DS_Store': os.remove(img_path+'/'+file) else: img=cv2.imread(img_path + '/' + file) imageVar = cv2.Laplacian(img, cv2.CV_64F).var() #拉普拉斯求图像的模糊程度 normalizedImg = np.zeros((32, 168)) normalizedImg = cv2.normalize(img, normalizedImg, 255.0, 0, cv2.NORM_MINMAX, cv2.CV_8U) all_choice_picel = [] all_out = [] all_crop_img = [] if imageVar<image_Var: unknown_img.append(img) else: if image_Var<imageVar < 100: gam1 = exposure.adjust_log(normalizedImg) #增加图像对比度和亮度(log) # NpKernel = np.uint8(np.ones((3, 3))) # erosion = cv2.dilate(gam1, NpKernel) erosion_gray=cv2.cvtColor(gam1,cv2.COLOR_BGR2GRAY) _, erosion = cv2.threshold(erosion_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) for ii in range(choice_num): #对图像进行腐蚀 erosion_img = erosion_gray[choice_set[ii][1]:choice_set[ii][3], choice_set[ii][0]:choice_set[ii][2]] _, erosion_cropimg1 = cv2.threshold(erosion_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) erosion_cropimg2=erosion[choice_set[ii][1]:choice_set[ii][3], choice_set[ii][0]:choice_set[ii][2]] if np.mean(erosion_cropimg1) > np.mean(erosion_cropimg2): ok_img = erosion_cropimg1 else: ok_img = erosion_cropimg2 all_crop_img.append(ok_img.reshape([32, 42, 1])) # cv2.imwrite(save_path+'/'+file+'_{}.jpg'.format(ii),ok_img) pixels = ok_img.reshape([-1]) #计算黑色像素点的数量 pixel_num = 0 for pixel in pixels: if pixel == 0: pixel_num += 1 all_choice_picel.append(pixel_num) # cv2.waitKey(0) else: bright = brightness(img_path + '/' + file) #计算图片的亮度值 gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) bright_img2 = exposure.adjust_gamma(gray_img, 0.3) #增加图片的亮度(gamma) _1, img2 = cv2.threshold(bright_img2, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) #img2图像整体OTSU for ii in range(choice_num): crop_img=gray_img[choice_set[ii][1]:choice_set[ii][3],choice_set[ii][0]:choice_set[ii][2]] crop_img2=img2[choice_set[ii][1]:choice_set[ii][3],choice_set[ii][0]:choice_set[ii][2]] crop_img = exposure.adjust_gamma(crop_img, 0.3) tret2, crop_img = cv2.threshold(crop_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) #crop_img图像局部OTSU if bright>190: #图片亮度比较大的时候采用局部阈值分割的结果 ok_img=crop_img else: #亮度较暗采用黑色像素点较少的图片 if np.mean(crop_img)>np.mean(crop_img2): ok_img=crop_img else: ok_img =crop_img2 # cv2.imwrite(save_path + '/' + file + '_{}.jpg'.format(ii), ok_img) all_crop_img.append(ok_img.reshape([32,42,1])) pixels=ok_img.reshape([-1]) pixel_num=0 for pixel in pixels: if pixel==0: pixel_num += 1 all_choice_picel.append(pixel_num) picel_scale = np.array(all_choice_picel) / max(all_choice_picel) #计算所得的每个选项的比值 #塞选数据: image,cls=clas_md(np.array(all_crop_img)/255-0.5) #得到深度学习分类结果 deeplearning_out=np.where(cls==1)[0] for index in range(len(all_choice_picel)): if picel_scale[index] > 0.55 and cls[index]==1: all_out.append(index) if len(all_out) > 1: #根据选项比值第二次塞选过滤 r_scale = 1 - np.array(picel_scale) all_out=np.where(r_scale < 0.4)[0] all_output.append(all_out) all_image.append(img) label=file.split('.')[0].split('_')[1:] all_lable.append(label) #保存数据 # if len(all_out) == 0: # misc.imsave(save_path + '/' + str(cc) + '_X.jpg',img) # else: # name = '' # for c in all_out: # name += '_' + chioce_dict.get(c) # misc.imsave(save_path + '/' + str(cc) + str(name) + '.jpg', img) # cc += 1 # print(cc) return all_image,all_output,unknown_img,all_lable
def gamma(self): gamma = round(random.uniform(0.4, 0.9), 2) gain = round(random.uniform(0.4, 0.9), 2) return exposure.adjust_gamma(self, gamma, gain)
def _changeLight(self, img): # random.seed(int(time.time())) flag = random.uniform(0.5, 1.5) #flag>1为调暗,小于1为调亮 return exposure.adjust_gamma(img, flag)
ax_hist.set_xlim(0, 1) ax_hist.set_yticks([]) # Display cumulative distribution 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() # Gamma gamma_corrected = exposure.adjust_gamma(img, 2) # Logarithmic logarithmic_corrected = exposure.adjust_log(img, 1) # Display results fig, axes = plt.subplots(nrows=2, ncols=3, 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(gamma_corrected, axes[:, 1])
def test_adjust_gamma_one(): """Same image should be returned for gamma equal to one""" image = np.random.uniform(0, 255, (8, 8)) result = exposure.adjust_gamma(image, 1) assert_array_equal(result, image)
def gamma_(img, gamma=1.2): #gamma < 1 ise parlaklık artar. #gamma > 1 ise parlaklık azalır. return exposure.adjust_gamma(img, gamma)
def combine_data(resize=False, newHeight=0, newWidth=0, training_ratio=0.8, distortionRate=0.0, carOriginPos=[376.0, 480.0], addFlipped=True): class DataSets(object): pass data_sets = DataSets() # Read images and labels images = read_images(DATA_FOLDERS, resize, newHeight, newWidth) labels = read_labels(DATA_FOLDERS, newHeight, newWidth) # Delete images and labels for which the labels are infinite mask = labels[:,0] > -1000 labels = labels[mask] images = images[mask] # Convert from [0, 255] -> [0.0, 1.0]. images = images.astype(np.float32) images = np.multiply(images, 1.0 / 256.0) #images = np.multiply(images, 1.0 / np.float(np.max(images))) # Batch randomisation nr_of_splits = 40 nrsplits = images.shape[0] / nr_of_splits nrrows = int(training_ratio * nrsplits) trainidx = np.sort(np.random.choice(nrsplits, nrrows, replace=False)) testidx = [x for x in range(nrsplits) if x not in trainidx] #validx = np.sort(np.random.choice(testidx, np.ceil(((1.0-training_ratio)/2.0)*nrsplits).astype(int), replace=False)) #testidx = list(set(testidx) - set(validx)) # Training Set images_split = np.array_split(images, nrsplits) labels_split = np.array_split(labels, nrsplits) train_images = images_split[trainidx[0]] train_labels = labels_split[trainidx[0]] for idx in trainidx[1:]: train_images = np.append(train_images, images_split[idx], axis=0) train_labels = np.append(train_labels, labels_split[idx], axis=0) # Flipped Images if addFlipped: print('\nFlips Images..') flipped_images = [np.fliplr(i) for i in images] flipped_labels = np.copy(labels) for lidx in range(flipped_labels.shape[0]): if flipped_labels[lidx][0] >= carOriginPos[0]: flipped_labels[lidx][0] = carOriginPos[0] - (flipped_labels[lidx][0] - carOriginPos[0]) else: flipped_labels[lidx][0] = carOriginPos[0] + (carOriginPos[0] - flipped_labels[lidx][0]) flipped_images_split = np.array_split(flipped_images, nrsplits) flipped_labels_split = np.array_split(flipped_labels, nrsplits) ''' IMAGE = 600 print(labels[IMAGE]) print(flipped_labels[IMAGE]) plt.figure() plt.imshow(imresize(images[IMAGE], [480, 752], 'bilinear'), cmap='gray') plt.plot(labels[IMAGE][0], labels[IMAGE][1], "ro") plt.figure() plt.imshow(imresize(flipped_images[IMAGE], [480, 752], 'bilinear'), cmap='gray') plt.plot(flipped_labels[IMAGE][0], flipped_labels[IMAGE][1], "ro") plt.figure() plt.imshow(exposure.adjust_gamma(images[IMAGE], random.uniform(1.0, 3.0)), cmap='gray') plt.figure() plt.imshow(filters.gaussian(images[IMAGE], random.uniform(0.5, 2.0)), cmap='gray') plt.figure() plt.imshow(exposure.equalize_hist(images[IMAGE]), cmap='gray') plt.show() ''' del flipped_images del flipped_labels for idx in trainidx: train_images = np.append(train_images, flipped_images_split[idx], axis=0) train_labels = np.append(train_labels, flipped_labels_split[idx], axis=0) del flipped_images_split del flipped_labels_split # Distorted Images print('\nDistorting Images..') distorted_images = np.copy(images) del images del labels for i in range(distorted_images.shape[0]): distortionType = random.randrange(0,3) if (distortionType == 0): # Gamma Correction distorted_images[i] = exposure.adjust_gamma(distorted_images[i], random.uniform(1.0, 3.0)) elif (distortionType == 1): # Gaussian Blur distorted_images[i] = filters.gaussian(distorted_images[i], random.uniform(0.5, 2.0)) else: # Histogram Equalization distorted_images[i] = exposure.equalize_hist(distorted_images[i]) distorted_images_split = np.array_split(distorted_images, nrsplits) for idx in trainidx: if (random.uniform(0.0, 1.0) < distortionRate): train_images = np.append(train_images, distorted_images_split[idx], axis=0) train_labels = np.append(train_labels, labels_split[idx], axis=0) del distorted_images del distorted_images_split # Test set test_images = images_split[testidx[0]] test_labels = labels_split[testidx[0]] for idx in testidx[1:]: test_images = np.append(test_images, images_split[idx], axis=0) test_labels = np.append(test_labels, labels_split[idx], axis=0) # Put the training, validation, and test set into a dataset data_sets.train = DataSet(np.expand_dims(train_images, axis=3), train_labels) data_sets.test = DataSet(np.expand_dims(test_images, axis=3), test_labels) print('') print('Training Set Shape: ' + str(data_sets.train.images.shape)) print('Test Set Shape: ' + str(data_sets.test.images.shape)) return (data_sets)
def __getitem__(self, idx): image_path = self.image_dir[idx] aseg_path = self.label_dir[idx] image = sio.loadmat(image_path)['img'] aseg_img = sio.loadmat(aseg_path)['img'] if image.shape[2] > 276: image = image[:, :, 20:276] aseg_img = aseg_img[:, :, 20:276] else: image = image[:, :, :256] aseg_img = aseg_img[:, :, :256] flip = random.random() > 0.5 angle = random.uniform(-10, 10) dx = np.round(random.uniform(-15, 15)) dy = np.round(random.uniform(-15, 15)) im = Image.fromarray(image[0]) target = Image.fromarray(aseg_img[0]) if self.train_data: if self.flipping and flip: im = im.transpose(0) target = target.transpose(0) if self.rotation: im = im.rotate(angle) target = target.rotate(angle) if self.translation: im = im.transform((256, 256), 0, (1, 0, dx, 0, 1, dy)) target = target.transform((256, 256), 0, (1, 0, dx, 0, 1, dy)) guassian_flag = random.random() > 0.5 im = np.array(im, np.float64, copy=False) min_im = np.min(im) max_im = np.max(im) im = (im - min_im) / (max_im - min_im + 1e-4) if self.train_data and guassian_flag: sigma_rand = random.uniform(0.65, 1.0) im_sigma = gaussian(im, sigma=sigma_rand) gamma_rand = random.uniform(1.6, 2.4) im_sigma_gamma = exposure.adjust_gamma(im_sigma, gamma_rand) im = (im_sigma_gamma - np.min(im_sigma_gamma)) / ( np.max(im_sigma_gamma) - np.min(im_sigma_gamma) + 1e-4) if self.coord: im = np.array([im, x_coordinate, y_coordinate], np.float64, copy=False) im = torch.from_numpy(im).type(torch.FloatTensor) else: im = torch.from_numpy(im).type(torch.FloatTensor).unsqueeze(0) target = np.array(target, np.float64, copy=False) target_label = np.zeros((len(self.rest_available) + 1, 256, 256)) for i, a in enumerate(self.available_segments): temp = (target == a).astype(int) if a in self.rest_available: target_label[self.rest_available.index(a), :, :] = temp else: target_label[len(self.rest_available), :, :] = target_label[ len(self.rest_available), :, :] + temp target_label[len(self.rest_available), :, :] = ( target_label[len(self.rest_available), :, :] >= 1).astype(int) target_label[self.rest_available.index(43), :, :] = np.logical_and( target >= 100, target % 2 != 0) target_label[self.rest_available.index(42), :, :] = np.logical_and( target >= 100, target % 2 == 0) target_label = torch.from_numpy(target_label).type(torch.FloatTensor) sample = {'x': im, 'y': target_label} return sample