def get_all_faces(imgs, labels, augment=True): '''Get a list of face images of size BOX_SIZE''' faces = [] for img_id, rects in tqdm(labels.items(), desc='Extracting all faces'): for face_rect in rects: face = face_rect.extract_from_img(imgs[img_id]) ratio = face.shape[1] / face.shape[0] if ratio < BOX_SIZE.w / BOX_SIZE.h: scaleW = BOX_SIZE.w / face.shape[1] face = rescale(face, scaleW, multichannel=False) # crop top and bottom diffH = face.shape[0] - BOX_SIZE.h face_cropped = crop(face, ((diffH // 2, diffH - diffH//2), (0, 0))) else: scaleH = BOX_SIZE.h / face.shape[0] face = rescale(face, scaleH, multichannel=False) # crop left and right diffW = face.shape[1] - BOX_SIZE.w face_cropped = crop(face, ((0, 0), (diffW // 2, diffW - diffW // 2))) faces.append(face_cropped.astype(np.float32)) if augment: return augment_data(faces) else: return faces
def crop(img, top, left, width, height): """crop image from position top and left, width and height Arguments: img {numpy.ndarray} -- input image top {int} -- start position row left {int} -- start position column width {int} -- crop width height {int} -- crop height """ if not all([isinstance(x, int) for x in (top, left, width, height)]): raise ValueError("params should be integer!") if (width > img.shape[0] or height > img.shape[1]): raise ValueError( "the output imgage size should be small than input image!!!") if len(img.shape) == 2: img_height, img_width = img.shape else: img_height, img_width, _ = img.shape right = img_width - (left + width) bottom = img_height - (top + height) if len(img.shape) == 2: img_croped = util.crop(img, ((top, bottom), (left, right))) else: img_croped = util.crop(img, ((top, bottom), (left, right), (0, 0))) return img_croped
def crop_and_flip(img, scale="small"): h, w = np.shape(img)[0], np.shape(img)[1] if len(np.shape(img)) > 2: cropped_img = sutil.crop(img, ((CROP_SIZE, CROP_SIZE), (CROP_SIZE, CROP_SIZE), (0, 0))) if scale is "small": # img_scale is small ,r.g height, width<640 lu = cropped_img[:256, :256, :] ru = cropped_img[:256, w - 256:w, :] ld = cropped_img[h - 256:h, :256, :] rd = cropped_img[h - 256:h, w - 256:w, :] else: print("wait to updates!") else: cropped_img = sutil.crop(img, ((10, 10), (10, 10))) if scale is "small": lu = cropped_img[:256, :256] ru = cropped_img[:256, w - 256:w] ld = cropped_img[h - 256:h, :256] rd = cropped_img[h - 256:h, w - 256:w] else: print("wait to updates!") return lu, ru, ld, rd, np.fliplr(lu), np.fliplr(ru), np.fliplr( ld), np.fliplr(rd)
def process_photo(filename, size=(8, 8)): """ Returns a greyscale version of an image of a given size. If the original image has a different aspect ratio to the target size, the image will first be cropped before being resized to prevent it getting distorted. Parameters ---------- filename : string name of image file (including path) size : int tuple size that we want input image to be rescaled to """ # read photo as greyscale photo = io.imread(filename, as_grey=True) # crop photo so it's the correct ratio if photo.shape[0] / photo.shape[1] > size[0] / size[1]: crop_amount = int( 0.5 * photo.shape[0] * (photo.shape[0] / photo.shape[1] - size[0] / size[1])) small_photo = util.crop(photo, ((crop_amount, crop_amount), (0, 0))) else: crop_amount = int( 0.5 * photo.shape[1] * (photo.shape[1] / photo.shape[0] - size[1] / size[0])) small_photo = util.crop(photo, ((0, 0), (crop_amount, crop_amount))) # now resize the photo small_photo = transform.resize(small_photo, size) return small_photo
def fit_crop(self, size): std_H = self.std.shape[0] std_W = self.std.shape[1] test_H = self.test.shape[0] test_W = self.test.shape[1] self.std = crop(self.std, ((int( (std_H - size) / 2), int((std_H - size) / 2)), (int( (std_W - size) / 2), int((std_W - size) / 2)), (0, 0))) self.test = crop(self.test, ((int( (test_H - size) / 2), int((test_H - size) / 2)), (int( (test_W - size) / 2), int((test_W - size) / 2)), (0, 0)))
def circularcrop(img, border=200, threshold=20000, threshold1=100): """ This function trims the circular image by border pixels, nullifies outside borders and crops the total img to the disk size parameters: img: retina image to be processed border: width of the border that will be trimmed from the disk. This allows to get rid of camera edge distortion threshold: threshold for detection image shape threshold1: threshold for detection image shape """ s = np.sum(img, axis=2) cols = np.sum(s, axis=0) > threshold rows = np.sum(s, axis=1) > threshold height = rows.shape[0] width = cols.shape[0] x_min = np.argmax(cols[0:width]) x_max = width / 2 + np.argmin(cols[width / 2:width - 1]) y_min = np.argmax(rows[0:height / 2]) y_max = np.argmin(cols[height / 2:height - 1]) y_max = height / 2 + y_max if y_max > 0 else height radius = (x_max - x_min) / 2 center_x = x_min + radius center_y = y_min + radius # the default case (if y_min != 0) if y_min == 0: # the upper side is cropped if height - y_max > 0: # lower border is not 0 center_y = y_max - radius else: upper_line_width = np.sum( s[0, :] > threshold1) # threshold for single line center_y = math.sqrt(radius**2 - (upper_line_width / 2)**2) radius1 = radius - border mask = np.zeros(img.shape[0:2]) rr, cc = circle(center_y, center_x, radius1, img.shape) mask[rr, cc] = 1 img[:, :, 0] *= mask img[:, :, 1] *= mask img[:, :, 2] *= mask x_borders = (center_x - radius1, img.shape[1] - center_x - radius1) y_borders = (max(center_y - radius1, 0), max(img.shape[0] - center_y - radius1, 0)) imgres = util.crop(img, (y_borders, x_borders, (0, 0))) maskT = util.crop(mask, (y_borders, x_borders)) border_pixels = np.sum(1 - maskT) return imgres, maskT, center_x, center_y, radius
def open_and_resize(im, size): h, w, c = im.shape if h < w: before_n = (w - h) / 2 after_n = w - (h + before_n) im_cropped = crop(im, ((0, 0), (before_n, after_n), (0, 0))) else: before_n = (h - w) / 2 after_n = h - (w + before_n) im_cropped = crop(im, ((before_n, after_n), (0, 0), (0, 0))) im_cropped = resize(im_cropped, (size, size, c)) return im_cropped
def test_copy_crop(): arr = np.arange(45).reshape(9, 5) out0 = crop(arr, 1, copy=True) assert out0.flags.c_contiguous out0[0, 0] = 100 assert not np.any(arr == 100) assert not np.may_share_memory(arr, out0) out1 = crop(arr, 1) out1[0, 0] = 100 assert arr[1, 1] == 100 assert np.may_share_memory(arr, out1)
def _mean_std(image, w): """Return local mean and standard deviation of each pixel using a neighborhood defined by a rectangular window size ``w``. The algorithm uses integral images to speedup computation. This is used by :func:`threshold_niblack` and :func:`threshold_sauvola`. Parameters ---------- image : ndarray Input image. w : int, or iterable of int Window size specified as a single odd integer (3, 5, 7, …), or an iterable of length ``image.ndim`` containing only odd integers (e.g. ``(1, 5, 5)``). Returns ------- m : ndarray of float, same shape as ``image`` Local mean of the image. s : ndarray of float, same shape as ``image`` Local standard deviation of the image. References ---------- .. [1] F. Shafait, D. Keysers, and T. M. Breuel, "Efficient implementation of local adaptive thresholding techniques using integral images." in Document Recognition and Retrieval XV, (San Jose, USA), Jan. 2008. :DOI:`10.1117/12.767755` """ if not isinstance(w, Iterable): w = (w, ) * image.ndim _validate_window_size(w) pad_width = tuple((k // 2 + 1, k // 2) for k in w) padded = np.pad(image.astype('float'), pad_width, mode='reflect') padded_sq = padded * padded integral = integral_image(padded) integral_sq = integral_image(padded_sq) kern = np.zeros(tuple(k + 1 for k in w)) for indices in itertools.product(*([[0, -1]] * image.ndim)): kern[indices] = (-1)**(image.ndim % 2 != np.sum(indices) % 2) total_window_size = np.prod(w) sum_full = ndi.correlate(integral, kern, mode='constant') mean = crop(sum_full, pad_width) / total_window_size sum_sq_full = ndi.correlate(integral_sq, kern, mode='constant') ex2 = crop(sum_sq_full, pad_width) / total_window_size stdev = np.sqrt(ex2 - mean**2) return mean, stdev
def circularcrop(img, border=200, threshold=20000, threshold1=100): """ This function trims the circular image by border pixels, nullifies outside borders and crops the total img to the disk size parameters: img: retina image to be processed border: width of the border that will be trimmed from the disk. This allows to get rid of camera edge distortion threshold: threshold for detection image shape threshold1: threshold for detection image shape """ s = np.sum(img, axis=2) cols = np.sum(s, axis=0) > threshold rows = np.sum(s, axis=1) > threshold height = rows.shape[0] width = cols.shape[0] x_min = np.argmax(cols[0:width]) x_max = width/2 + np.argmin(cols[width/2:width-1]) y_min = np.argmax(rows[0:height/2]) y_max = np.argmin(cols[height/2:height-1]) y_max = height/2 + y_max if y_max > 0 else height radius = (x_max - x_min)/2 center_x = x_min + radius center_y = y_min + radius # the default case (if y_min != 0) if y_min == 0: # the upper side is cropped if height - y_max > 0: # lower border is not 0 center_y = y_max - radius else: upper_line_width = np.sum(s[0,:] > threshold1) # threshold for single line center_y = math.sqrt( radius**2 - (upper_line_width/2)**2) radius1 = radius - border mask = np.zeros(img.shape[0:2]) rr, cc = circle(center_y, center_x, radius1, img.shape) mask[rr, cc] = 1 img[:,:,0] *= mask img[:,:,1] *= mask img[:,:,2] *= mask x_borders = (center_x - radius1, img.shape[1] - center_x - radius1) y_borders = (max(center_y - radius1,0), max(img.shape[0] - center_y - radius1, 0)) imgres = util.crop(img, (y_borders, x_borders, (0,0))) maskT = util.crop(mask, (y_borders, x_borders)) border_pixels = np.sum(1 - maskT) return imgres, maskT, center_x, center_y, radius
def _mean_std(image, w): """Return local mean and standard deviation of each pixel using a neighborhood defined by a rectangular window with size w times w. The algorithm uses integral images to speedup computation. This is used by threshold_niblack and threshold_sauvola. Parameters ---------- image : ndarray Input image. w : int Odd window size (e.g. 3, 5, 7, ..., 21, ...). Returns ------- m : 2-D array of same size of image with local mean values. s : 2-D array of same size of image with local standard deviation values. References ---------- .. [1] F. Shafait, D. Keysers, and T. M. Breuel, "Efficient implementation of local adaptive thresholding techniques using integral images." in Document Recognition and Retrieval XV, (San Jose, USA), Jan. 2008. DOI:10.1117/12.767755 """ if w == 1 or w % 2 == 0: raise ValueError("Window size w = %s must be odd and greater than 1." % w) left_pad = w // 2 + 1 right_pad = w // 2 padded = np.pad(image.astype('float'), (left_pad, right_pad), mode='reflect') padded_sq = padded * padded integral = integral_image(padded) integral_sq = integral_image(padded_sq) kern = np.zeros((w + 1, ) * image.ndim) for indices in itertools.product(*([[0, -1]] * image.ndim)): kern[indices] = (-1)**(image.ndim % 2 != np.sum(indices) % 2) sum_full = ndi.correlate(integral, kern, mode='constant') m = util.crop(sum_full, (left_pad, right_pad)) / (w**image.ndim) sum_sq_full = ndi.correlate(integral_sq, kern, mode='constant') g2 = util.crop(sum_sq_full, (left_pad, right_pad)) / (w**image.ndim) s = np.sqrt(g2 - m * m) return m, s
def test_cropped_camera_image(): image = crop(camera(), ((206, 206), (206, 206))) assert_allclose(frangi(image), np.zeros((100, 100)), atol=1e-03) assert_allclose(frangi(image, black_ridges=True), np.zeros((100, 100)), atol=1e-03) assert_allclose(hessian(image), np.ones((100, 100)), atol=1 - 1e-07)
def contour_to_binary_mask(filename, mask_shape): current_path = os.path.abspath(os.path.dirname(__file__)) image_path = os.path.join(current_path, filename) image = io.imread(image_path) image_gray = color.rgb2gray(image) # Deleting white spaces around certain contour images binary_image = image_gray < 0.996 y_crop = np.nonzero(binary_image)[0][0] x_crop = np.nonzero(binary_image)[1][0] cropped_image = crop(image, ((y_crop, y_crop), (x_crop, x_crop), (0, 0))) cropped_image = resize(cropped_image, mask_shape, preserve_range=True).astype(np.uint8) cropped_gray = color.rgb2gray(cropped_image) edge_mask = np.zeros(cropped_gray.shape) size_y = np.shape(cropped_image)[0] size_x = np.shape(cropped_image)[1] # A condition for thresholding found empirically for y in range(size_y): for x in range(size_x): if abs(int(cropped_image[y, x][0]) - int(cropped_image[y, x][1])) > 10.0: edge_mask[y, x] = 1 binary_contour = nd.morphology.binary_fill_holes(edge_mask).astype(int) return binary_contour
def original2patch(item): inPath = os.path.join(item.inPath, item.ID) files = sorted(glob.glob(os.path.join(inPath, '*.tif'))) for fileName in files: basename = os.path.splitext(fileName)[0] basename = os.path.basename(basename) # print(basename) img = imread(fileName) print(basename, img.shape) width = img.shape[1] height = img.shape[0] i = 0 while i < 300: rx = random.randint(0, width - wndSize) ry = random.randint(0, height - wndSize) xw = (rx, width - (rx + wndSize)) yw = (ry, height - (ry + wndSize)) zw = (0, 0) imgC = crop(img, (yw, xw, zw)) # print(rx, ry, imgC.shape) filename = os.path.join(item.outPath, basename + '_{0:06d}'.format(rx) + '_{0:06d}.png'.format(ry)) imsave(filename, imgC) i += 1
def crop_pad_around(image, size): """ Pads or crops an image so that the image is of a given size. Parameters ---------- image: numpy array, 3 channel numpy array to crop/pad. size: tuple of integers, size to achieve. Returns ------- A padded or croped version of image so that image has a size of size. """ x, y, z = image.shape x_pad_width = size[0] - x if size[0] > x else 0 y_pad_width = size[1] - y if size[1] > y else 0 if x_pad_width > 0 or y_pad_width > 0: pad_width = [(x_pad_width, y_pad_width) for _ in range(2)] pad_width +=[(0, 0)] image = np.pad(image, pad_width, mode='constant') x, y, z = image.shape shapes = [x, y, z] x_crop_width = x - size[0] if x > size[0] else 0 y_crop_width = y - size[1] if y > size[1] else 0 if x_crop_width > 0 or y_crop_width > 0: crops = [] for i, c in enumerate([x_crop_width, y_crop_width]): crop_v = np.random.randint(0, c) if c != 0 else 0 crops.append((crop_v, shapes[i] - size[i] - crop_v)) crops.append((0,0)) image = crop(image, crops, copy=True) return image
def random_translation(image_array, limit): seed = np.random.randint(limit, size=6) choice = np.random.choice(2, 3) rt = util.crop(copy=True, ar=image_array, crop_width=((seed[0], seed[1]), (seed[2], seed[3]), (seed[4], seed[5]))) if (choice[0] == 0): rt = util.pad(rt, ((seed[0] + seed[1], 0), (0, 0), (0, 0)), 'constant') else: rt = util.pad(rt, ((0, seed[0] + seed[1]), (0, 0), (0, 0)), 'constant') if (choice[1] == 0): rt = util.pad(rt, ((0, 0), (seed[2] + seed[3], 0), (0, 0)), 'constant') else: rt = util.pad(rt, ((0, 0), (0, seed[2] + seed[3]), (0, 0)), 'constant') if (choice[2] == 0): rt = util.pad(rt, ((0, 0), (0, 0), (seed[4] + seed[5], 0)), 'constant') else: rt = util.pad(rt, ((0, 0), (0, 0), (0, seed[4] + seed[5])), 'constant') return rt
def extract_state(self, prev_frame, curr_frame): diff_frame = rgb2gray(curr_frame) - rgb2gray(prev_frame) #frame = rgb2gray(frame) frame = crop(diff_frame, ((25, 10), (0,0))) frame = resize(frame, (84, 84)) state = torch.from_numpy(frame).unsqueeze(0).unsqueeze(0).float() return state
def preprocessFiles(in_dir, out_file, num_images=-1): files = os.listdir(in_dir) if num_images > 0: files = files[0:num_images] # This will store data for all images. datas = np.ndarray([len(files), DESIRED_SHAPE[0], DESIRED_SHAPE[1]], dtype=np.float16) for i, fname in enumerate(files): # Load image and convert to greyscale data = io.imread(os.path.join(in_dir, fname), as_grey=True) if data.shape[0] > data.shape[1]: # Make all images have width be the longest dimension. data = transform.rotate(data, 90, resize=True) # Scale images so that the shortest dimension matches the corresponding one in the # DESIRED_SHAPE. We do this so that later we can crop a bit of the longer axis and all # images the same dimensions without distortion. scale_factor = (float(DESIRED_SHAPE[1]) / float(data.shape[1]) if DESIRED_ASPECT_RATIO < float(data.shape[0]) / float(data.shape[1]) else float(DESIRED_SHAPE[0]) / float(data.shape[0])) data = transform.rescale(data, scale_factor) # Crop whatever is left on the long axis data = util.crop(data, [(0, data.shape[0] - DESIRED_SHAPE[0]), (0, data.shape[1] - DESIRED_SHAPE[1])]) datas[i] = data print '%s: %s, %0.5f' % (fname, data.shape, float(data.shape[0]) / float(data.shape[1])) with open(out_file, 'w') as f: f.write(datas.tobytes())
def random_augmentation(image, blur_percent=0.5, pepper_percent=0.5, crop_percent=0.5, rotation_percent=0.5): pepper_sigma = np.random.normal(0.100, 0.010) blur_sigma = np.random.normal(1, 0.1) rotate_angle = np.random.normal(0, 5) image_x = image.shape[0] image_y = image.shape[1] delta_x_percent = tuple( [int(image_x * abs(np.random.normal(0, 0.03))) for _ in range(2)]) delta_y_percent = tuple( [int(image_y * abs(np.random.normal(0, 0.03))) for _ in range(2)]) if random.random() < pepper_percent: image = random_noise(image, var=pepper_sigma**2) if random.random() < blur_percent: image = gaussian(image, sigma=blur_sigma, multichannel=True) if random.random() < rotation_percent: image = rotate(image, angle=rotate_angle, mode='reflect') if random.random() < crop_percent: image = crop(image, (delta_x_percent, delta_y_percent, (0, 0)), copy=False) return image
def data_extend(image, angle, crop_num, prelim_size, final_size): array_out = [] #import as 100x100 numpy array, perform distortions and resize to 32x32 to train image = s.imprep(image, prelim_size) image = np.reshape(image, [prelim_size, prelim_size]) #consider changing the imprep function because its so stupid angles = np.linspace(-angle, angle, 2*angle+1) for a in angles: a = int(a) image2 = rotate(image, a) for i in range(abs(a)): #gets rid of black border from rotation, f(angle) for j in range(prelim_size): image2[i,j]=0.985 image2[j,i]=0.985 image2[prelim_size-1-i,j]=0.985 image2[j,prelim_size-1-i]=0.985 gradient = np.linspace(0.90, 1, 11) for colour in gradient: for i in range(prelim_size): for j in range(prelim_size): if image2[i,j]>0.5: image2[i,j]=colour for c in range(crop_num): image3 = crop(image2, c) image3 = resize(image3, (final_size, final_size)) array_out.append(image3) array_out = np.array(array_out) return array_out
def feature_visualization(feature, inp, steps): """ Images were optimized for 2560 steps in a color-decorrelated fourier-transformed space, using Adam at a learning rate of 0.05. We used each of following transformations in the given order at each step of the optimization: • Padding the input by 16 pixels to avoid edge artifacts • Jittering by up to 16 pixels • Scaling by a factor randomly selected from this list: 1, 0.975, 1.025, 0.95, 1.05 • Rotating by an angle randomly selected from this list; in degrees: -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 • Jittering a second time by up to 8 pixels • Cropping the padding """ shape = [d._value for d in inp.shape] shape[0] = 1 x = np.random.randn(*shape) * .1 sess = K.get_session() grad = K.gradients(feature, inp) for i in range(steps): g = sess.run(grad, {inp: x})[0] g = filters.gaussian(g, sigma=1, multichannel=True, preserve_range=True) o_size = g.shape g = transform.rescale(g, np.random.choice([1, 0.975, 1.025, 0.95, 1.05]), multichannel=True) g = util.crop(g, [g.shape[0] - o_size[0], g.shape[1] - o_size[1], g.shape[2] - o_size[2]]) x += g return x
def randomZoom(image, mask, m, u=0.5): """ Randomly zoom the image and mask by trimming the top, bottom, left and right. Args: image: the iamge mask: the mask m: the maximum to trim from the top, bottom, left and right u: the augmentation probability """ if random() < u: original_shape = image.shape c = ((randint(0,m),randint(0,m)),(randint(0,m),randint(0,m)),(0,0)) image = resize(crop(image, crop_width=c), original_shape) mask = resize(crop(mask, crop_width=c), original_shape) return image, mask
def _translate_one_array(one_array, seed, choice, pad_val=0): rt = util.crop(copy=True, ar=one_array, crop_width=((seed[0], seed[1]), (seed[2], seed[3]), (seed[4], seed[5]))) if (choice[0] == 0): rt = util.pad(rt, ((seed[0] + seed[1], 0), (0, 0), (0, 0)), 'constant', constant_values=pad_val) else: rt = util.pad(rt, ((0, seed[0] + seed[1]), (0, 0), (0, 0)), 'constant', constant_values=pad_val) if (choice[1] == 0): rt = util.pad(rt, ((0, 0), (seed[2] + seed[3], 0), (0, 0)), 'constant', constant_values=pad_val) else: rt = util.pad(rt, ((0, 0), (0, seed[2] + seed[3]), (0, 0)), 'constant', constant_values=pad_val) if (choice[2] == 0): rt = util.pad(rt, ((0, 0), (0, 0), (seed[4] + seed[5], 0)), 'constant', constant_values=pad_val) else: rt = util.pad(rt, ((0, 0), (0, 0), (0, seed[4] + seed[5])), 'constant', constant_values=pad_val) return rt
def preprocess(image): # color 210 x 160 preproc_img = color.rgb2gray(image) # gray 210 x 160 preproc_img = crop(preproc_img, ((150), (0))) preproc_img = resize(preproc_img, output_shape=(IMG_WIDTH, IMG_HEIGHT), anti_aliasing=False) # 110 x 84 return preproc_img
def __call__(self, image): pad_or_crop = np.random.randint(2) if pad_or_crop: # pad margin_top, margin_bottom, margin_left, margin_right = np.random.randint( self.max_margin + 1, size=4) padded_image = pad(image, ((margin_top, margin_bottom), (margin_left, margin_right)), mode='constant', constant_values=1.0) resized_image = resize(padded_image, image.shape, mode='constant', anti_aliasing=True) else: # crop margin_top, margin_bottom, margin_left, margin_right = np.random.randint( self.max_margin + 1, size=4) cropped_image = crop(image, ((margin_top, margin_bottom), (margin_left, margin_right)), copy=True) resized_image = resize(cropped_image, image.shape, mode='constant', anti_aliasing=True) return resized_image
def _center_crop(image): x_start = int((image.shape[0] - IMAGE_DIMS[0]) / 2) y_start = int((image.shape[1] - IMAGE_DIMS[1]) / 2) x_end = image.shape[0] - (IMAGE_DIMS[0] + x_start) y_end = image.shape[1] - (IMAGE_DIMS[1] + y_start) return util.crop(image, ((x_start, x_end), (y_start, y_end)), copy=True)
def deconvolve(self, image, iterations): ## Code for deconvolution psf = ai.airy_psf(self.window, self.psf_radius) image = np.pad(image, int(psf.shape[0] / 2), mode="reflect") deconvolved = restoration.richardson_lucy(image, psf, iterations, False) deconvolved = util.crop(deconvolved, int(psf.shape[0] / 2)) return deconvolved
def crop_center(image): if not is_more_than_720(image) : return [] height, width, _ = image.shape if width < height: # very rare case return [] x_margin = width - height return skutil.crop(image,((0, 0), (x_margin/2, x_margin/2), (0, 0)))
def load_image_data(filelist, perc_tr=0.7, im_func=None): """ -load in the images as numpy arrays and return the following: 1. a numpy array of shape (perc_tr*len(filelist), 1, 48, 48) that contains the training image data 2. a numpy array of shape (perc_tr*len(filelist),) that contains the training emotion code for each training image 3. a numpy array of shape ((1-perc_tr)*len(filelist), 1, 48, 48) that contains the test image data 4. a numpy array of shape ((1-perc_tr)*len(filelist), ) that contains the test emotion codes for each test image :param filelist: :return: """ if perc_tr < 0.5: print "Training subset percent too low, defaulting to 50%..." perc_tr = 0.5 # subset the list of file names into training and testing # note that the file names have been shuffled randomly before coming # in to the function tr_len = int(round(perc_tr * len(filelist))) te_len = len(filelist) - tr_len training = filelist[:tr_len] test = filelist[tr_len:] # initialize empty arrays to hold the training and testing image data # we are cropping the images to 42 X 42 imgs_train = np.empty(shape=(tr_len, 1, 42, 42), dtype=np.float32) emos_train = np.empty(shape=tr_len, dtype=np.int8) imgs_test = np.empty(shape=(te_len, 1, 42, 42), dtype=np.float32) emos_test = np.empty(shape=te_len, dtype=np.int8) # populate the training arrays for i in range(len(training)): img = imread(training[i], as_grey=True) img_crop = crop(img, crop_width=3) img_crop = im_func(img_crop) imgs_train[i, 0] = img_crop emos_train[i] = get_emo_int(training[i]) # populate the test arrays for i in range(len(test)): img = imread(test[i], as_grey=True) img_crop = crop(img, crop_width=3) img_crop = im_func(img_crop) imgs_test[i, 0] = img_crop emos_test[i] = get_emo_int(test[i]) return imgs_train, emos_train, imgs_test, emos_test
def crop_center(image): if not is_more_than_720(image): return [] height, width, _ = image.shape if width < height: # very rare case return [] x_margin = width - height return skutil.crop(image, ((0, 0), (x_margin / 2, x_margin / 2), (0, 0)))
def image_crop(img, img_size_ori=101, img_size_target=img_size_t): if img_size_ori == img_size_target: return img crop_width = ((int(np.floor((img_size_t-img_size_ori)/2)), int(np.ceil((img_size_t-img_size_ori)/2))), (int(np.floor((img_size_t-img_size_ori)/2)), int(np.ceil((img_size_t-img_size_ori)/2)))) out_image = util.crop(img, crop_width) return out_image
def split_letters(image, num_letters=4, debug=False): """ split full captcha image into `num_letters` lettersself. return list of letters binary image (0: white, 255: black) """ # move left left = crop(image, ((0, 0), (0, image.shape[1] - SHIFT_PIXEL)), copy=True) image[:, :-SHIFT_PIXEL] = image[:, SHIFT_PIXEL:] image[:, -SHIFT_PIXEL:] = left # binarization binary = image > BINARY_THRESH # find contours # contours = find_contours(binary, 0.5) # contours = [[ # [int(floor(min(contour[:, 1]))), int(floor(min(contour[:, 0])))], # top-left point # [int(ceil(max(contour[:, 1]))), int(ceil(max(contour[:, 0])))] # down-right point # ] for contour in contours] # # keep letters order # contours = sorted(contours, key=lambda contour: contour[0][0]) # # find letters box # letter_boxs = [] letter_boxs = [[[0, 7], [11, 24]], [[13, 5], [30, 30]], [[30, 5], [45, 29]], [[47, 4], [61, 28]]] # for contour in contours: # if len(letter_boxs) > 0 and contour[0][0] < letter_boxs[-1][1][0] - 5: # # skip inner contour # continue # # extract letter boxs by contour # boxs = get_letter_boxs(binary, contour) # for box in boxs: # letter_boxs.append(box) # check letter outer boxs number if len(letter_boxs) != num_letters: print('ERROR: number of letters is NOT valid', len(letter_boxs)) # debug if debug: print(letter_boxs) plt.imshow(binary, interpolation='nearest', cmap=plt.cm.gray) for [x_min, y_min], [x_max, y_max] in letter_boxs: plt.plot([x_min, x_max, x_max, x_min, x_min], [y_min, y_min, y_max, y_max, y_min], linewidth=2) plt.xticks([]) plt.yticks([]) plt.show() return None # normalize size (40x40) letters = [] for [x_min, y_min], [x_max, y_max] in letter_boxs: letter = resize(image[y_min:y_max, x_min:x_max], LETTER_SIZE) letter = img_as_ubyte(letter < 0.6) letters.append(letter) return letters
def detect_face(data): img = read_image(data) dets, scores, idx = detector.run(img, 1) scores = list(scores) best = dets[scores.index(max(scores))] x,y,c = img.shape c_img = util.crop(img, ((best.top(), y - best.bottom()), (best.left(), x - best.right()), (0,0))) s = write_image(c_img) return s
def slightRotation(img, degrees): height = img.shape[0] width = img.shape[1] channels = img.shape[2] rotation = trans.rotate(img, degrees) margin = np.ceil(np.abs((img.shape[0] * np.pi * degrees) / 360)) / 2 rotation = crop(rotation, ((margin, margin), (margin, margin), (0, 0))) rotation = trans.resize(rotation, (height, width, channels)) return rotation
def crop_manual(img): ''' Set the "threshold" & crop based on when we have reached it ''' threshold = 20000 s = np.sum(img, axis=2) cols = np.sum(s, axis=0) > threshold rows = np.sum(s, axis=1) > threshold left_border = np.argmax(cols[0:len(cols)/2]) right_border = np.argmax(cols[len(cols)-1:len(cols)/2:-1]) upper_border = np.argmax(rows[0:len(rows)/2]) lower_border = np.argmax(rows[len(rows)-1:len(rows)/2:-1]) return crop(img, ((upper_border, lower_border),(left_border, right_border), (0,0)))
def fill_holes_with_contour_filling(gray_mask,inverse=False): ''' Input: Grayscale image Returns: Image with holes filled by contour mapping ''' filled = gray_mask.copy() filled = pad(filled,((5,5),(0,0)),'constant',constant_values=255) if inverse: filled = cv2.bitwise_not(filled) image, contour, _ = cv2.findContours(filled, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) for cnt in contour: cv2.drawContours(filled,[cnt], 0, 255, -1) if inverse: filled = cv2.bitwise_not(filled) filled = crop(filled,((5,5),(0,0))) return filled
def crop_ronc(self, ronc): """ Crop the input Ronchigram by the specified ammount using the specified method. Parameters ---------- ronc : numpy.array Input image to be cropped. Returns ------- cropped_ronc : numpy.array Cropped image """ if self.crop_ammount is None: return ronc crop_ammount = self.crop_ammount crop_method = self.crop_method if crop_method == 'percent': crop_ammount = np.round(np.atleast_2d(crop_ammount) / 100.0 * ronc.shape) crop_ammount = tuple([tuple(row) for row in crop_ammount.astype(np.uint32)]) elif crop_method == 'absolute': if isinstance(crop_ammount, int): crop_ammount = ((crop_ammount,), (crop_ammount,)) elif len(crop_ammount) == 2: crop_ammount = ((crop_ammount[0],), (crop_ammount[1],)) elif len(crop_ammount) == 4: crop_ammount = ((crop_ammount[0], crop_ammount[1]), (crop_ammount[2], crop_ammount[3])) else: raise ValueError('The crop_ammount should be an integer or list of 2 or 4 integers.') else: raise ValueError('Allowed values of crop_method are percent and absolute.') crop_ammount = ((0,), (0,)) + crop_ammount cropped_ronc = crop(ronc, crop_ammount) if any([dim == 0 for dim in cropped_ronc.shape]): warn("Requested crop ammount is greater than the image size. No cropping will be done.") return ronc return cropped_ronc
def mkbox( mask_seg ): ''' Crop the non_zeros pixels of an image to a new image and pad with zeros to make double the new image size to avoid periodic artifacts from the FFT at correlation lengths. ''' pxlst = np.where(mask_seg.ravel())[0] dims = mask_seg.shape imgwidthy = dims[1] #dimension in y, but in plot being x imgwidthx = dims[0] #dimension in x, but in plot being y #x and y are flipped??? #matrix notation!!! pixely = pxlst%imgwidthy pixelx = pxlst//imgwidthy minpixelx = np.min(pixelx) minpixely = np.min(pixely) maxpixelx = np.max(pixelx) maxpixely = np.max(pixely) img_crop = crop( mask_seg, ((minpixelx, imgwidthx - maxpixelx -1 ), (minpixely, imgwidthy - maxpixely -1 )) ) widthx = maxpixelx - minpixelx + 1 widthy = maxpixely - minpixely + 1 oldimg = np.zeros(dims) oldimg.ravel()[pxlst] = 1 #img_crop = np.copy(oldimg[minpixelx:maxpixelx+1,minpixely:maxpixely+1]) #subpxlst = np.where(img_crop.ravel() != 0)[0] #print("min/maxpixelx: {},{};min/maxpixely: {},{}".format(minpixelx,maxpixelx,minpixely,maxpixely)) #padx = 2**np.int( np.ceil( log(widthx +20 )/log(2) ) ) - widthx #pady = 2**np.int( np.ceil( log(widthy +20)/log(2) ) ) -widthy padx = 2**np.int( np.ceil( log(widthx*3)/log(2) ) ) - widthx pady = 2**np.int( np.ceil( log(widthy*3)/log(2) ) ) -widthy img_pad = pad( img_crop, ((0, padx),(0, pady ) ) ,'constant', constant_values=(0,0)) subpxlst = np.where(img_pad.ravel() != 0)[0] return np.array(subpxlst,dtype = np.int32), np.array(img_pad,dtype = np.int32)
def crop_to_fit(image, shape): """ Return a copy of image resized and cropped to precisely fill a shape. To resize a colored 2D image, pass in a shape with two entries. When ``len(shape) < image.ndim``, higher dimensions are ignored. Parameters ---------- image : array shape : tuple e.g., ``(height, width)`` but any length <= ``image.ndim`` is allowed Returns ------- cropped_image : array """ # Resize smallest dimension (width or height) to fit. d = np.argmin(np.array(image.shape)[:2] / np.array(shape)) enlarged_shape = ( tuple(np.ceil(np.array(image.shape[: len(shape)]) * shape[d] / image.shape[d])) + image.shape[len(shape) :] ) resized = resize(image, enlarged_shape) # Now the image is as large or larger than the shape along all dimensions. # Crop any overhang in the other dimension. crop_width = [] for actual, target in zip(resized.shape, shape): overflow = actual - target # Center the image and crop, biasing left if overflow is odd. left_margin = np.floor(overflow / 2) right_margin = np.ceil(overflow / 2) crop_width.append((left_margin, right_margin)) # Do not crop any additional dimensions beyond those given in shape. for _ in range(resized.ndim - len(shape)): crop_width.append((0, 0)) cropped = crop(resized, crop_width) return cropped
def get_our_images(imdir): """ -resize our images to 48X48 which is what the model is expecting :param imdir: :return: """ jacob = imdir + "HJACOB.png" alex = imdir + "HALEX.png" david = imdir + "HDAVID.png" yannet = imdir + "HYANNET.png" james = imdir + "HJAMES.png" paul = imdir + "DPAUL.png" # load us in X_jacob_orig = io.imread(jacob, as_grey=True) X_alex_orig = io.imread(alex, as_grey=True) X_david_orig = io.imread(david, as_grey=True) X_yannet_orig = io.imread(yannet, as_grey=True) X_james_orig = io.imread(james, as_grey=True) X_paul_orig = io.imread(paul, as_grey=True) # resize to 48 X 48 X_jacob_orig = transform.resize(X_jacob_orig, [48, 48]) X_alex_orig = transform.resize(X_alex_orig, [48, 48]) X_david_orig = transform.resize(X_david_orig, [48, 48]) X_yannet_orig = transform.resize(X_yannet_orig, [48, 48]) X_james_orig = transform.resize(X_james_orig, [48, 48]) X_paul_orig = transform.resize(X_paul_orig, [48, 48]) # smooth the image a tiny bit X_jacob_orig = ndimage.median_filter(X_jacob_orig, 1.5) X_alex_orig = ndimage.median_filter(X_alex_orig, 1.5) X_david_orig = ndimage.median_filter(X_david_orig, 1.5) X_yannet_orig = ndimage.median_filter(X_yannet_orig, 1.5) X_james_orig = ndimage.median_filter(X_james_orig, 1.5) X_paul_orig = ndimage.median_filter(X_paul_orig, 1.5) # crop the image down to 42 X 42 X_jacob_orig = crop(X_jacob_orig, crop_width=3) X_alex_orig = crop(X_alex_orig, crop_width=3) X_david_orig = crop(X_david_orig, crop_width=3) X_yannet_orig = crop(X_yannet_orig, crop_width=3) X_james_orig = crop(X_james_orig, crop_width=3) X_paul_orig = crop(X_paul_orig, crop_width=3) # rescale the image to [0, 1] scale X_jacob = rescale_image(X_jacob_orig) X_alex = rescale_image(X_alex_orig) X_david = rescale_image(X_david_orig) X_yannet = rescale_image(X_yannet_orig) X_james = rescale_image(X_james_orig) X_paul = rescale_image(X_paul_orig) originals = np.empty(shape=(6, 1, 42, 42)) our_test = np.empty(shape=(6, 1, 42, 42), dtype=np.float32) our_emos = np.empty(shape=6, dtype=np.int8) # original images resized originals[0, 0] = X_jacob_orig # io.imshow(originals[0,0]) # io.show() originals[1, 0] = X_alex_orig originals[2, 0] = X_david_orig originals[3, 0] = X_yannet_orig originals[4, 0] = X_james_orig originals[5, 0] = X_paul_orig # inputs our_test[0, 0] = X_jacob our_test[1, 0] = X_alex our_test[2, 0] = X_david our_test[3, 0] = X_yannet our_test[4, 0] = X_james our_test[5, 0] = X_paul # input classifications our_emos[0,] = 0 our_emos[1,] = 0 our_emos[2,] = 0 our_emos[3,] = 1 our_emos[4,] = 0 our_emos[5,] = 1 return originals, our_test, our_emos
def _gen_batches(): num_samples = len(self.X) idx = np.random.permutation(num_samples) batches = range(0, num_samples - self.batchsize + 1, self.batchsize) for batch in batches: X_batch = self.X[idx[batch:batch + self.batchsize]] y_batch = self.y[idx[batch:batch + self.batchsize]] # set empty copy to hold augmented images so that we don't overwrite X_batch_aug = np.empty(shape = (X_batch.shape[0], 3, cropPIXELS, cropPIXELS), dtype = 'float32') # random rotations betweein -8 and 8 degrees dorotate = randint(-30,30) # random translations trans_1 = randint(-12,12) trans_2 = randint(-12,12) crop_amt = ((12 - trans_1, 12 + trans_1), (12 - trans_2, 12 + trans_2), (0,0)) # random zooms zoom = uniform(1, 1.3) # shearing shear_deg = uniform(-10, 10) # set the transform parameters for skimage.transform.warp # have to shift to center and then shift back after transformation otherwise # rotations will make image go out of frame center_shift = np.array((PIXELS, PIXELS)) / 2. - 0.5 tform_center = transform.SimilarityTransform(translation=-center_shift) tform_uncenter = transform.SimilarityTransform(translation=center_shift) tform_aug = transform.AffineTransform(rotation = np.deg2rad(dorotate), #translation = (trans_1, trans_2), shear = np.deg2rad(shear_deg), scale = (1/zoom, 1/zoom)) tform = tform_center + tform_aug + tform_uncenter r_intensity = randint(0,1) g_intensity = randint(0,1) b_intensity = randint(0,1) intensity_scaler = uniform(-0.25, 0.25) # images in the batch do the augmentation for j in range(X_batch.shape[0]): img = X_batch[j] img = img.transpose(1, 2, 0) img_aug = np.zeros((PIXELS, PIXELS, 3)) for k in range(0,3): img_aug[:, :, k] = fast_warp(img[:, :, k], tform, output_shape = (PIXELS, PIXELS)) img_aug = crop(img_aug, crop_amt) if r_intensity == 1: img_aug[:, :, 0] = img_aug[:, :, 0] + (np.std(img_aug[:, :, 0]) * intensity_scaler) if g_intensity == 1: img_aug[:, :, 1] = img_aug[:, :, 1] + (np.std(img_aug[:, :, 1]) * intensity_scaler) if b_intensity == 1: img_aug[:, :, 2] = img_aug[:, :, 2] + (np.std(img_aug[:, :, 2]) * intensity_scaler) X_batch_aug[j] = img_aug.transpose(2, 0, 1) # Flip half of the images in this batch at random: bs = X_batch.shape[0] indices = np.random.choice(bs, bs / 2, replace=False) X_batch_aug[indices] = X_batch_aug[indices, :, :, ::-1] yield [X_batch_aug, y_batch]
def test_multi_crop(): arr = np.arange(45).reshape(9, 5) out = crop(arr, ((1, 2), (2, 1))) assert_array_equal(out[0], [7, 8]) assert_array_equal(out[-1], [32, 33]) assert_equal(out.shape, (6, 2))
def test_int_crop(): arr = np.arange(45).reshape(9, 5) out = crop(arr, 1) assert_array_equal(out[0], [6, 7, 8]) assert_array_equal(out[-1], [36, 37, 38]) assert_equal(out.shape, (7, 3))
def test_pair_crop(): arr = np.arange(45).reshape(9, 5) out = crop(arr, (1, 2)) assert_array_equal(out[0], [6, 7]) assert_array_equal(out[-1], [31, 32]) assert_equal(out.shape, (6, 2))
def crop_images(images, shape): left, top, right, bottom = shape crop_width = ((0, 0),(top, bottom),(left, right),(0, 0)) return util.crop(images.reshape((1, images.shape[0], images.shape[1], images.shape[2])), crop_width)
def test_cropped_camera_image(): image = crop(camera(), ((206, 206), (206, 206))) assert_allclose(frangi(image), np.zeros((100, 100)), atol=1e-03) assert_allclose(frangi(image, black_ridges=True), np.zeros((100, 100)), atol=1e-03) assert_allclose(hessian(image), np.ones((100, 100)), atol=1-1e-07)
def test_zero_crop(): arr = np.arange(45).reshape(9, 5) out = crop(arr, 0) assert out.shape == (9, 5)