def main(): # CMD args parser parser = argparse.ArgumentParser(description='Generate smaller patches from images') parser.add_argument("--input", action = "store", help = "Input images dir") parser.add_argument("--output", action = "store", help = "Output images dir") parser.add_argument("--width", action = "store", help = "Patch width", type=int, default = 32) parser.add_argument("--height", action = "store", help = "Patch height", type=int, default = 32) parser.add_argument("--patches", action = "store", help = "Number of patches", type=int, default = 10) # Parse CMD args args = parser.parse_args() if (args.input == None or args.output == None): parser.print_help() sys.exit(1) count = 0 images = os.listdir(args.input) while (count < args.patches): random.shuffle(images) for i in xrange(len(images)): img = io.imread(args.input+'/'+images[i]) patches = image.extract_patches_2d(img, patch_size=(args.width, args.height), max_patches=100, random_state=np.random.RandomState(0)) random.shuffle(patches) for p in patches: # Save low contrast patches only if (exposure.is_low_contrast(p) == False): io.imsave(args.output+'/patch_%.4d.ppm'%(count), p) count += 1 break if (count == args.patches): break
def gird_crop(im, savedir, stride=64, size=128, show=True): if not os.path.exists(savedir[0]): os.mkdir(savedir[0]) if not os.path.exists(savedir[1]): os.mkdir(savedir[1]) im0, im1 = im[0], im[1] nc = len(im0.shape) h, w = im0.shape[:2] # print(h,w) counter = 0 mean_sub = [] for i in range(0, h - size, stride): for j in range(0, w - size, stride): if nc == 2: sub0 = im0[i:i + size, j:j + size, :] sub1 = im1[i:i + size, j:j + size] else: sub0 = im0[i:i + size, j:j + size, :] sub1 = im1[i:i + size, j:j + size] # image contrast,ignore depth contrast result = exposure.is_low_contrast(sub0) print(result) if not result: io.imsave(savedir[0] + str(counter) + '.png', sub0) io.imsave(savedir[1] + str(counter) + '.png', sub1) counter += 1 mean = np.mean(sub1) mean_sub.append(mean) if show: print('id=%d mean=%f' % (counter, mean)) return mean_sub
def transform_img(img, name, patch_size, center): from skimage.exposure import is_low_contrast rows, cols, _ = img.shape if rows < patch_size or cols < patch_size: print("Image too small: %s-%s.png" % (name)) return [] row_blocks = int(rows / patch_size) col_blocks = int(cols / patch_size) v_margin = (rows - (row_blocks * patch_size)) / 2 if center else 0 l_margin = (cols - (col_blocks * patch_size)) / 2 if center else 0 patches = [] for i in range(row_blocks): row_patches = [] for j in range(col_blocks): patch = img[v_margin + patch_size * i:v_margin + patch_size * (i + 1), l_margin + patch_size * j:l_margin + patch_size * (j + 1), :] if not is_low_contrast(patch): row_patches.append(patch) patches.append(row_patches) return patches
def cut_char(pk): page = get_object_or_404(Page, pk=pk) page_img_path = page.get_image_path() char_lst = Character.objects.filter(page_id=pk) image = io.imread(page_img_path, 0) binary = binarisation(image) binary_image = (binary * 255).astype('ubyte') char_dir = settings.CHARACTER_IMAGE_ROOT+ pk+'/' if not os.path.exists(char_dir): os.makedirs(char_dir) for char in char_lst: char_image = binary_image[char.top:char.bottom,char.left:char.right] char_filename = char.id+'.png' char_path = char_dir+char_filename try: io.imsave(char_path, char_image) status = 0 if is_low_contrast(char_image): status = -5 except: char_filename = '' status = -6 char.is_correct = status char.image = char_filename char.save() append_char_stastics.delay(pk) return 'cutchar:'+pk
def get_center(img): if is_low_contrast(img, fraction_threshold=1e-4, upper_percentile=99): print('Low contrast image!') return sx / 2, sy / 2, sz / 2 else: # Blur the image and find the center of mass img_y_max_proj = np.max(img, axis=1) #+np.max(img,axis=2) img_y_max_proj = rescale_intensity(img_y_max_proj, in_range='image', out_range=(0, 1.)) img_y_max_proj = gaussian(img_y_max_proj, sigma=4.) img_y_max_proj = np.max(img_y_max_proj, axis=1) #+np.max(img,axis=2) cmz = np.argmax(img_y_max_proj) img_z_max_proj = np.max(img, axis=0) #np.copy(img[cmz,:,:]) img_z_max_proj = rescale_intensity(img_z_max_proj, in_range='image', out_range=(0, 1.)) mxz = np.max(img_z_max_proj) img_z_max_proj = gaussian(img_z_max_proj, sigma=50.) #print 'Maximum: ', np.unravel_index(img_z_max_proj.argmax(), img_z_max_proj.shape) cmy, cmx = np.unravel_index(img_z_max_proj.argmax(), img_z_max_proj.shape) return int(cmx), int(cmy), int(cmz)
def transform_img(img, name, patch_size): from skimage.transform import rotate from skimage.exposure import is_low_contrast rows, cols, _ = img.shape max_dim = max(rows, cols) # If more rows than cols, vertical vertical = True if rows == max_dim else False if rows < patch_size or cols < patch_size: print("Image too small: %s-%s.png" % (name)) return [] # Make all images vertical for now (more rows than cols) if not vertical: img = img.transpose((1, 0, 2)) rows, cols, _ = img.shape row_blocks = rows / patch_size col_blocks = cols / patch_size patches = [] for i in range(row_blocks): for j in range(col_blocks): patch = img[patch_size * i:patch_size * (i + 1), patch_size * j:patch_size * (j + 1), :] if not is_low_contrast(patch): patches.append(patch) return patches
def cut_char(pk): page = get_object_or_404(Page, pk=pk) page_img_path = page.get_image_path() char_lst = Character.objects.filter(page_id=pk) image = io.imread(page_img_path, 0) binary = binarisation(image) binary_image = (binary * 255).astype('ubyte') char_dir = settings.CHARACTER_IMAGE_ROOT + pk + '/' if not os.path.exists(char_dir): os.makedirs(char_dir) for char in char_lst: char_image = binary_image[char.top:char.bottom, char.left:char.right] char_filename = char.id + '.png' char_path = char_dir + char_filename try: io.imsave(char_path, char_image) status = 0 if is_low_contrast(char_image): status = -5 except: char_filename = '' status = -6 char.is_correct = status char.image = char_filename char.save() append_char_stastics.delay(pk) return 'cutchar:' + pk
def feature_augment(image): #####judge contrast is low or not######## result = exposure.is_low_contrast(image) print(result) #####enhance contrast################## imagenhancer_Contrast = ImageEnhance.Contrast(image) gam2 = imagenhancer_Contrast.enhance(2) # gam3 = exposure.adjust_log(image) # gam1.show() # gam2.show() fig, ax = plt.subplots() ax.imshow(image) ax.set_title('orig') # fig, ax = plt.subplots() # ax.imshow(gam1) # ax.set_title('gam1') fig, ax = plt.subplots() ax.imshow(gam2) ax.set_title('gam2') # # fig, ax = plt.subplots() # ax.imshow(gam3) # ax.set_title('gam3') # plt.show() return gam2
def preprocessing_filters(image, blur_params=None, temperature_params=None, low_contrast_params=None, center=True): """ Meta function for preprocessing images. Parameters ---------- image : ndarray input rgb image blur_band : int band of rgb to check for blur blur_params : dict or `None` parameters for `pyroots.detect_blur` temperature_params : dict or `None` parameters for `pyroots.calc_temperature_distance` low_contrast_params : dict or `None` parameters for `skimage.exposure.is_low_contrast` center : bool Take middle 25% of an image for blur detection? Returns ------- bool - should the image be pre-processed? Must pass all criteria given. """ try: if center is True: blur = detect_motion_blur(_center_image(image), **blur_params) else: blur = detect_motion_blur(image, **blur_params) except: blur = True if blur_params is not None: warn("Skipping motion blur check", UserWarning) pass try: bands = calc_temperature_distance(image, **temperature_params) except: bands = True if missing_band_params is not None: warn("Skipping temperature check", UserWarning) pass try: contrast = ~exposure.is_low_contrast( filters.gaussian(image, sigma=10, multichannel=True), ** low_contrast_params) except: contrast = True if low_contrast_params is not None: warn("Skipping low contrast check", UserWarning) pass return (blur * bands * contrast)
def preprocessing_filters(image, blur_params=None, temperature_params=None, low_contrast_params=None, center=True): """ Meta function for preprocessing images. Parameters ---------- image : ndarray input rgb image blur_band : int band of rgb to check for blur blur_params : dict or `None` parameters for `pyroots.detect_blur` temperature_params : dict or `None` parameters for `pyroots.calc_temperature_distance` low_contrast_params : dict or `None` parameters for `skimage.exposure.is_low_contrast` center : bool Take middle 25% of an image for blur detection? Returns ------- bool - should the image be pre-processed? Must pass all criteria given. """ try: if center is True: blur = detect_motion_blur(_center_image(image), **blur_params) else: blur = detect_motion_blur(image, **blur_params) except: blur = True if blur_params is not None: warn("Skipping motion blur check", UserWarning) pass try: bands = calc_temperature_distance(image, **temperature_params) except: bands = True if missing_band_params is not None: warn("Skipping temperature check", UserWarning) pass try: contrast = ~exposure.is_low_contrast(filters.gaussian(image, sigma=10, multichannel=True), **low_contrast_params) except: contrast = True if low_contrast_params is not None: warn("Skipping low contrast check", UserWarning) pass return(blur * bands * contrast)
def is_low_contrast(self, threshold): """ By default the threshold value is set to 0.05. We override that amount by setting our own threshold. TODO: Find threshold we will use. """ return exposure.is_low_contrast(self.scikitImage, fraction_threshold=threshold)
def write_image(img, path, percentiles=None): rgb = np.dstack(img[:3, :, :]).astype(np.uint8) if exposure.is_low_contrast(rgb): return False os.makedirs(os.path.dirname(path), exist_ok=True) if not os.path.exists(path): imsave(path, rgb) return True
def _extract_images_from_windows(self, windows, raster, percentiles, output_dir): with rasterio.open(raster) as src: for window in windows: fname = self._prepare_img_filename(raster, window) rgb = self._extract_img(src, window, percentiles=percentiles) if not exposure.is_low_contrast(rgb): self._save_jpg(output_dir, fname, rgb)
def contrast_img(img: Image, mask: Image, mode): img = np.array(img) if exposure.is_low_contrast(img): img = exposure.adjust_gamma(img, 0.4) print(True) else: print(False) return Img.fromarray(img), mask
def _start(self, component, channel, prop, image): if not self.has_glcm and not self.has_hrlk: return assert image.ndim == 2, 'Expecting 2D object image, got shape {}'.format( image.shape) # Mask out parts of object image that don't overlap with object binary image (i.e. set to 0) image = image * prop.image.astype(image.dtype) if exposure.is_low_contrast(image, fraction_threshold=.01): image = np.zeros(image.shape, dtype=np.uint8) else: # Both greycomatrix and haralick require 8-bit images image = exposure.rescale_intensity(image, in_range='dtype', out_range='uint8').astype( np.uint8) assert image.dtype == np.uint8 # NOTE: Both skimage and mahotas GLCM-based features are very similar though there is a critical difference # in that the mahotas implementation allows zeros to be ignored, which leads to substantially different # results that are otherwise nearly identical. Also, the mahotas version is used in CellProfiler # with ignore_zeros=True so it should be preferred. # TODO: make distance parameterized distance = 16 # Initialize GLCM matrix if necessary if self.has_glcm: from skimage.feature import greycomatrix, greycoprops # With these 4 angles, results are nearly identical to mhf.haralick with ignore_zeros=False glcm = greycomatrix(image, [distance], [0, np.pi / 2, np.pi, 3 * np.pi / 2], symmetric=True, normed=True) # Average across array of shape [d, a] where d = num distances (1 in this case) # and a = num angles (4 in this case) self.glcm_fn = lambda feature: (greycoprops(glcm, feature).mean(), prop.label) # Gather Haralick features if necessary if self.has_hrlk: from mahotas import features as mhf try: hrlk = mhf.haralick(image, distance=distance, ignore_zeros=True, return_mean=True) except ValueError as e: # If an error is thrown about empty features (which can happen even with non-empty images), # then get the default values using a small empty array if 'mahotas.haralick_features: the input is empty. Cannot compute features!' not in str( e): raise hrlk = mhf.haralick(np.zeros((2, 2), dtype=np.uint8), distance=1, ignore_zeros=False, return_mean=True) hrlk = dict(zip(TEXTURE_HRLK_FEATURES, hrlk)) self.hrlk_fn = lambda feature: (hrlk[feature], prop.label)
def is_too_bright(self, threshold): """ Here we simply use a higher threshold to inverse the use of the exposure.is_low_contrast and detect high contrast. TODO: Find threshold we will use. """ return exposure.is_low_contrast(self.scikitImage, fraction_threshold=threshold)
def gamma_adjust(): path = '/Users/chendanxia/sophie/inference/WechatIMG46.jpeg' img = imageio.imread(path) img1 = exposure.adjust_gamma(img, 0.5) img2 = exposure.adjust_gamma(img, 1.5) img3 = exposure.adjust_gamma(img, 2) print(exposure.is_low_contrast(img)) print(exposure.is_low_contrast(img1)) print(exposure.is_low_contrast(img2)) print(exposure.is_low_contrast(img3)) plt.subplot(221) plt.imshow(img) plt.subplot(222) plt.imshow(img1) plt.subplot(223) plt.imshow(img2) plt.subplot(224) plt.imshow(img3) plt.show()
def improve_scenebkp(sub_templateg, alg=1): ref = np.asarray(PIL.Image.fromarray(np.asarray(pd.read_pickle('./AuxFiles/refhist.pkl')))) #if alg==1: #sub_templateg = cv2.fastNlMeansDenoising(sub_templateg,None,10,7,21) if is_low_contrast(sub_templateg, 0.35): temp_img = np.copy(sub_templateg).astype(float) temp_img[temp_img <= 30] = np.nan temp_img_std = np.nanstd(temp_img) del temp_img if temp_img_std <= 30: print('Scene is low contrasted, improving...') sub_templateg = hist.adjust_gamma(sub_templateg, gamma=1.7) sub_templateg = adjust_sigmoid(sub_templateg) sub_templateg = hist.adjust_gamma(sub_templateg, gamma=1.2) hist1 = cv2.calcHist([sub_templateg], [0], None, [256], [0, 256]) hist2 = cv2.calcHist([ref], [0], None, [256], [0, 256]) sim = cv2.compareHist(hist1, hist2, 0) if sim < 0.8: if alg == 1: print('Computing large objects.') sub_templateg = cv2.medianBlur(sub_templateg, 5) sub_templateg = cv2.bilateralFilter(sub_templateg, 3, 3, 3) sub_templateg = hist.adjust_gamma(sub_templateg, gamma=3.0) sub_templateg = adjust_sigmoid(sub_templateg) sub_templateg = hist.adjust_gamma(sub_templateg, gamma=1.0) if alg == 2: print('Computing small objects') sub_templateg = hist.adjust_gamma(sub_templateg, gamma=1.5) else: if alg ==1: print('Computing large objects.') sub_templateg = hist.adjust_gamma(sub_templateg, gamma=1.5) sub_templateg = adjust_sigmoid(sub_templateg) sub_templateg = hist.adjust_gamma(sub_templateg, gamma=1.0) else: print('Computing small objects.') sub_templateg = match_histograms(sub_templateg, ref) sub_templateg = convert(sub_templateg, 0, 255, np.uint8) return sub_templateg
def assert_image(image, name=None, convert=False): """ Checks if an image is present (not black, white, or low contrast :param image: Image to check :param name: str, optional file name. image is saved is specified :param convert: bool specifies if image is converted to RGB for saving """ from skimage.exposure import is_low_contrast if name: image.save(results + name, autoconvert=convert) if is_low_contrast(image.array): logging.warning('image %s has low contrast' % name)
def assert_image(image,name=None,convert=False): """ Checks if an image is present (not black, white, or low contrast :param image: Image to check :param name: str, optional file name. image is saved is specified :param convert: bool specifies if image is converted to RGB for saving """ from skimage.exposure import is_low_contrast if name: image.save(results+name,autoconvert=convert) if is_low_contrast(image.array): logging.warning('image %s has low contrast'%name)
def is_rightcontrast(image): img = img_as_float(io.imread(image, as_gray=True)) # hist = exposure.histogram(img, nbins=3) # if hist[1][0] > 0: # brightness = (hist[1][2]) / hist[1][0] # else: # brightness = 0 lowcont = exposure.is_low_contrast(img, fraction_threshold=0.02, lower_percentile=20, upper_percentile=80) return lowcont
def mask_test(img_filepath): (img, img_smooth, img_otsu, mask, cleared_mask) = mask_gen(img_filepath) print(str(img_filepath.split("/")[-1])) print("img low contrast: " + str(exposure.is_low_contrast(img))) print("img_histeq low contrast: " + str(exposure.is_low_contrast(img_otsu))) print("img_otsu low contrast: " + str(exposure.is_low_contrast(img_otsu))) # Generate plot fig, (ax1, ax2, ax3, ax4, ax5) = mpl.pyplot.subplots(1, 5, figsize=(9, 3), sharex=True, sharey=True) # Display input image ax1.imshow(img, cmap=mpl.pyplot.cm.gray) ax1.axis("off") ax1.set_title("orig", fontsize=12) # Display input image ax2.imshow(img_smooth, cmap=mpl.pyplot.cm.gray) ax2.axis("off") ax2.set_title("histeq", fontsize=12) # Display histeq image ax3.imshow(img_otsu, cmap=mpl.pyplot.cm.gray) ax3.axis("off") ax3.set_title("otsu", fontsize=12) # Display otsu image ax4.imshow(mask, cmap=mpl.pyplot.cm.gray) ax4.axis("off") ax4.set_title("mask", fontsize=12) # Display mask ax5.imshow(cleared_mask, cmap=mpl.pyplot.cm.gray) ax5.axis("off") ax5.set_title("cleared", fontsize=12) filename = str(str(img_filepath.split("/")[-1]).split(".")[-2]) fig.savefig("Results/" + filename + "plot.png") mpl.pyplot.close('all')
def rasterize(w, h, pad, impad, tickness, most_common_classes, out, nbpoints, shuffle): mapping = get_symbol_mapping() fd = open('train-data.csv') lines = fd.readlines()[1:] fd = open('test-data.csv') lines += fd.readlines()[1:] lines = map(parse_line, lines) lines = list(filter(lambda l:l['user_id']==16925, lines)) if shuffle: np.random.seed(42) np.random.shuffle(lines) if nbpoints > 0: lines = lines[0:nbpoints] y_str = [mapping[l['symbol_id']] for l in lines] y_str = np.array(y_str) y = [l['symbol_id'] for l in lines] y = np.array(y) points = list(map(points_from_line, lines)) X = list(map(partial(to_image, w=w, h=h, tickness=tickness, pad=pad), points)) for i in range(len(X)): if impad>0: X[i] = np.pad(X[i], impad, 'constant') X[i] = resize(X[i], (h, w), preserve_range=True) mask = np.ones(len(y), dtype=np.bool) if most_common_classes: counter = Counter(y) all_classes = set(y) common_classes = set(y_id for y_id, _ in counter.most_common(most_common_classes)) filtered_classes = set(all_classes) - set(common_classes) for y_id in filtered_classes: mask[y==y_id] = False low_constrast_mask = list(map(lambda i:is_low_contrast(X[i]), np.arange(len(X)))) low_constrast_mask = np.array(low_constrast_mask, dtype=np.bool) mask = mask & (~low_constrast_mask) X = np.array(X) X = X[mask] y = y[mask] y_str = y_str[mask] y = LabelEncoder().fit_transform(y) i = 0 print(X.shape) X = X[:, None, :, :] np.savez(out, X=X, y=y, y_str=y_str)
def save(data, target): low_contrast = 0 for symbol, images in data.items(): path = os.path.join(target, symbol) if not os.path.exists(path): os.mkdir(path) index = 0 for image in images: filename = os.path.join(path, str(index) + 'a.png') low_contrast += 1 if exposure.is_low_contrast(image) else 0 index += 1 cv2.imwrite(filename=filename, img=image) return low_contrast
def check_img(img): img = np.mean(img, axis=0) max_value = np.max(img) ratios = np.count_nonzero(img < 20) / img.size if max_value < 20 or ratios > 0.1: return False if exposure.is_low_contrast(img): return False cloud_img = img cloud_ratio = np.count_nonzero(cloud_img > 230) / cloud_img.size if cloud_ratio > 0.5: return False return True
def _preprocess(self): img_out = self.img.copy() # img_out = rescale(img_out) if is_low_contrast(self.img) or self.adjust_gamma: img_out = self._gamma_adjustment(self.img, self.gamma) if self.dilate: img_out = self._background_correction(img_out) if self.enhance: img_out = self._ahe(img_out, self.limit, self.grid_size) if self.cutoff_perc: img_out = self._cutoff_percentile(img_out) img_out = rescale(img_out) return np.expand_dims(img_out, axis=0)
def is_highquality(image, night=True): lowcont = is_rightcontrast(image) if night: img = img_as_float(io.imread(image, as_gray=True)) if exposure.is_low_contrast(img, fraction_threshold=0.35, lower_percentile=20, upper_percentile=99): print('low contrast original + night...') return False else: print('higher contrast original + night...') return True else: return True
def train(self): # self.img = self.img / 256 self.img = img_as_float(self.img) self.img = exposure.rescale_intensity(self.img) self.img = self.img * 256 while exposure.is_low_contrast(self.img): self.img = exposure.adjust_gamma(self.img, 0.8) self.img = self.img.astype(np.uint8) self.img = img_as_ubyte(self.img) '''cv2 median filter-->>Reduce Noise''' '''Optional:cv2 gaussian filter-->>cv2.GaussianBlur''' self.img = cv2.medianBlur(self.img, 3) '''cv2 threshold-->>gain contrast''' '''Optional:cv2.adaptiveThreshold-->>have no good result''' ret, self.img = cv2.threshold(self.img, 50, 255, cv2.THRESH_BINARY)
def write_tif(img, path, *, window, meta, transform, bands): if exposure.is_low_contrast(img): return False os.makedirs(os.path.dirname(path), exist_ok=True) meta.update({ "driver": "GTiff", "height": window.height, "width": window.width, "transform": rasterio.windows.transform(window, transform), "count": len(bands), }) img = np.array([img[b - 1, :, :] for b in bands]) with rasterio.open(path, "w", **meta) as dst: dst.write(img) return True
def _color_jitter(self, mode=1, gamma=0.5): ''' Color jitter according to mode - mode : - -1 : rgb -> gray - 0 : rgb -> hsv - 1 : adjust brightness - 2 : rescale intensity ''' N, H, W, C = self.data['X_train'].shape color_data = None if mode == -1: color_data = np.zeros(N, H, W) for index in range(N): color_data[index] = cv2.cvtColor(self.data['X_train'][index], cv2.COLOR_BGR2GRAY) # TODO else: color_data = np.zeros_like(self.data['X_train']) if mode == 0: for index in range(N): color_data[index] = cv2.cvtColor( self.data['X_train'][index], cv2.COLOR_BGR2HSV) elif mode == 1: for index in range(N): color_data[index] = exposure.adjust_gamma( self.data['X_train'][index], gamma) # gamma # color_data[index] = exposure.adjust_log(self.X_data[index], gamma) # log elif mode == 2: for index in range(N): flag = exposure.is_low_contrast( self.data['X_train'][index]) if flag: color_data[index] = exposure.rescale_intensity( self.data['X_train'][index]) else: color_data[index] = self.data['X_train'][index] else: raise ValueError('Unrecognized color jitter mode') return color_data
def test_is_low_contrast(): image = np.linspace(0, 0.04, 100) assert exposure.is_low_contrast(image) image[-1] = 1 assert exposure.is_low_contrast(image) assert not exposure.is_low_contrast(image, upper_percentile=100) image = (image * 255).astype(np.uint8) assert exposure.is_low_contrast(image) assert not exposure.is_low_contrast(image, upper_percentile=100) image = (image.astype(np.uint16)) * 2**8 assert exposure.is_low_contrast(image) assert not exposure.is_low_contrast(image, upper_percentile=100)
def GetFrame(capture): _, frame = capture.read() # Flip a 2D array. "1" means flipping around y-axis frame = cv2.flip(frame, 1) frame = cv2.resize(frame, dsize=(900, 700)) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) check = True # Here we try to check either surrounding to user have not low intensity of light # if light present surrounding to user is less than 30% we not take picture of it if is_low_contrast(gray, fraction_threshold=0.30): check = False return check, frame
def randomized_patches(slide, level, patchsize, randomizer, n_max): counter = 0 shape = slide.level_dimensions[level] for i, j in randomized_regular_seed(shape, patchsize, randomizer): # check number of images already yielded if counter >= n_max: break # get the image x = j * (2**level) y = i * (2**level) image = numpy.array( slide.read_region((x, y), level, (patchsize, patchsize)))[:, :, 0:3] # check whether image is yieldable if (not is_low_contrast(image)) and (get_tissue(image).sum() > 0.5 * patchsize * patchsize): counter += 1 yield x, y, level, image
def extract_images(image, paths): ''' Extract lines from IMAGE with known segmentation PATHS. return the extracted images as a list ''' extracted_lines = [] previous_path = paths[0] paths = paths[1:] for path in paths: upper_bound = min(previous_path) lower_bound = max(path) height = lower_bound - upper_bound if height <= 0: continue line = np.zeros((height, image.shape[1])) status = True try: for j in range(len(path) - 1): upper = previous_path[j] lower = path[j] distance = lower - upper if distance < 0: raise ValueError('Distance between path is negative.', 'lower:', lower, 'upper', upper) delta = upper - upper_bound try: line[delta:delta + distance, j] = image[upper:lower, j] except ValueError as e: debugger.display('Error:', e) debugger.display('delta:', delta, 'dlu:', delta + distance, 'upper:', upper, 'lower:', lower) debugger.display('line shape:', line[delta:delta + distance].shape, 'image shape:', image[upper:lower].shape) debugger.display('Ori image shape', image.shape) except ValueError as e: debugger.display(e) continue if status: if not exposure.is_low_contrast(line): extracted_lines.append(line) previous_path = path return extracted_lines
def gen_neg_samples(base_path, groups): out_path = os.path.join(base_path, 'subgestures', 'gb1113', 'neg') for group in groups: """ % 2. load 'GROUPNAME_annotation.mat'. you get -> % GROUPNAME_annotation, by which you can access information for each % subgestures and the directory, formatted as: % (1) group name | (2) sign ID | (3) dir name | (4) scene name | % (5) start frame Nbr | (6) ending frame Nbr | (7)duration | (8)lexicon % NOTE: it starts from 1, so you have to -1 for indexing in python """ mat_path = os.path.join(base_path, 'annotations', group + '_annotation.mat') print 'loading ' + mat_path mat = scipy.io.loadmat(mat_path) mat_data = mat.get(group + '_annotation') sign_num, col_size = mat_data.shape print 'start preprocess %d subgestures in group %s' % (sign_num, group) for i in xrange(sign_num): """ for every subgesture, load its annotation file of GROUPNAME_signID_lexicon.txt, with data formatted as: (1) frame Nbr | (2) sign type | (3-6) (left) x, y, width, height | (7-10) (right) x, y, width, height """ anno_file_path = os.path.join(base_path, 'annotations', 'subgestures_hand', group + '_' + str(mat_data[i, 1][0][0]) + '_' + foramt_mat_string_cell(mat_data[i, 7]) + '.txt') anno = np.loadtxt(anno_file_path, dtype=np.dtype('uint32')) for offset, f_idx in enumerate(range(mat_data[i, 4], mat_data[i, 5] + 1)): print 'sampling negative data on gesture %d, frame %s' % (i, gen_frame_num(f_idx)) pic_path = os.path.join(base_path, 'snaps', group, foramt_mat_string_cell(mat_data[i, 2]), foramt_mat_string_cell(mat_data[i, 3]), gen_frame_num(f_idx) + '.jpg') img = Image.open(pic_path) W, H = img.size arr_img = PIL2array(img) mask_hands = np.zeros_like(arr_img) # need to check whether x indicates row here: if anno[offset, 1] == 0: # no/bad annotation pass elif anno[offset, 1] == 1: # only left hand (rarely used) mask_hands[anno[offset, 2]:(anno[offset, 2] + anno[offset, 4] - 1), anno[offset, 3]:(anno[offset, 3] + anno[offset, 5] - 1)] = 1 area_hands = anno[offset, 5] * anno[offset, 4] elif anno[offset, 1] == 2: # only right hand mask_hands[anno[offset, 7]:(anno[offset, 7] + anno[offset, 9] - 1), anno[offset, 6]:(anno[offset, 6] + anno[offset, 8] - 1)] = 1 area_hands = anno[offset, 8] * anno[offset, 9] elif anno[offset, 1] == 3: # both hands mask_hands[anno[offset, 2]:(anno[offset, 2] + anno[offset, 4] - 1), anno[offset, 3]:(anno[offset, 3] + anno[offset, 5] - 1)] = 1 mask_hands[anno[offset, 7]:(anno[offset, 7] + anno[offset, 9] - 1), anno[offset, 6]:(anno[offset, 6] + anno[offset, 8] - 1)] = 1 area_hands = min(anno[offset, 5] * anno[offset, 4], anno[offset, 8] * anno[offset, 9]) else: raise ValueError('Type enumerate not recognized!') # for each snap, we randomly generate 5 negative samples for cnt in xrange(5): # extract a fixed [80, 80] region for tb, lb datasets, [65, 65] for gb dataset, # then resize it to [58, 58] left, top = random.randint(1, W - 57), random.randint(1, H - 57) if group == 'gb1113': right, bottom = min(W, left + 65 - 1), min(H, top + 65 - 1) else: right, bottom = min(W, left + 80 - 1), min(H, top + 80 - 1) mask_cropped = np.zeros_like(arr_img) mask_cropped[left:right, top:bottom] = 1 mask = np.bitwise_and(mask_hands, mask_cropped) if group == 'gb1113': area_cropped = 65 * 65 else: area_cropped = 80 * 80 cropped = img.crop((left, top, right, bottom)).resize((58, 58)) # tmp = exposure.histogram(img_as_float(cropped), nbins=8) # if exposure.is_low_contrast(cropped, fraction_threshold=0.02) == True: # # without the line below, the figure won't show # pylab.show(); plt.imshow(PIL2array(cropped)) # else: # pylab.show(); plt.imshow(PIL2array(cropped)) """ Remove those samples which we think may not aid performance in training """ if group == 'gb1113': contrast_th = 0.1 else: contrast_th = 0.1 # for cropped image: if the ratio of overlapped region against hand regions has exceeded 0.1, or it has lower # contrast, then skip it: if 1.0 * np.count_nonzero(mask[:]) / min(area_cropped, area_hands) > 0.1 \ or exposure.is_low_contrast(cropped, fraction_threshold=contrast_th) == True: # pylab.show() # f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2) # ax1.imshow(arr_img); ax1.set_title('arr_img') # ax2.imshow(mask_hands * 255); ax2.set_title('mask_hands') # ax3.imshow(mask_cropped * 255); ax3.set_title('mask_cropped') # ax4.imshow(mask * 255); ax4.set_title('mask') continue out = os.path.join(out_path, group + '_' + foramt_mat_string_cell(mat_data[i, 2]) + '_' + foramt_mat_string_cell(mat_data[i, 3]) + '_' + gen_frame_num(f_idx) + '_' + 'rand_' + str(cnt+1) + '.jpg') cropped.save(out)
print for val in value: if num_samples > 10000: break img = coco.loadImgs(val)[0] # Load up an image I = io.imread('%s/%s/%s'%(dataDir,dataType,img['file_name'])) #io.imsave('coco_super_category_crops/super_%s.jpg'%(key), I) #print I.shape try: if (len(I.shape) == 3): I_crop = resize(I, (256, 256), mode='edge') else: I_crop = resize(I, (256, 256), mode='edge') scene_name = 'scene_%s_%d.jpg'%(key, num_samples) if is_low_contrast(I_crop): continue label_map[scene_name] = class_id io.imsave('coco_indoor_outdoor/%s'%(scene_name), I_crop) num_samples = num_samples + 1 except: continue class_id = class_id + 1 # Write the image names and labels in a shuffled order img_names = list(label_map.keys()) random.shuffle(img_names) label_file = open('scene_labels.txt', 'w') for name in img_names: