我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用skimage.transform.resize()。
def restore_labels(labels, roi, read_info): if roi == -1: # Pad first, then resize to original shape labels = np.pad(labels, ((0, 0), (CROP, CROP), (CROP, CROP)), 'constant') restored_labels = np.zeros(read_info['shape'], dtype=np.float32) for z in range(N_CLASSES): roi = resize((labels == z + 1).astype(np.float32), read_info['shape'], mode='constant') roi[roi >= 0.5] = 1 roi[roi < 0.5] = 0 roi = clean_contour(roi, is_prob=False) restored_labels[roi == 1] = z + 1 else: labels = clean_contour(labels, is_prob=True) # Resize to extracted shape, then pad to original shape labels = resize(labels, read_info['extract_shape'], mode='constant') restored_labels = np.zeros(read_info['shape'], dtype=np.float32) extract = read_info['extract'] restored_labels[extract[0][0] : extract[0][1], extract[1][0] : extract[1][1], extract[2][0] : extract[2][1]] = labels return restored_labels
def test_image_transformation(): s = XSeries([generate_image(False) for _ in range(100)]) try: image_transformer = ImageTransformer().fit() assert False except: assert True image_transformer = ImageTransformer(skimage_transform.hough_circle, radius=5).fit() s_transformed = image_transformer.transform(s) assert s_transformed.data_type == np.ndarray image_transformer = ImageTransformer(skimage_transform.resize, output_shape=(10, 10)).fit() s_transformed = image_transformer.transform(s) assert s_transformed.data_type == np.ndarray
def load_box(inp, bboxes, img_input=False, norm=False): n = bboxes.shape[0] if img_input: im = inp else: im = load_image(inp, norm) res = np.zeros([n, 117, 117, 3]) for i in range(n): img_crop = im_crop(im, bboxes[i]) img_resize = transform.resize(img_crop, [117, 117]) res[i] = img_resize return res ########################################################################### # load_patch # ###########################################################################
def transform_mnist_rts(in_data): img, label = in_data img = img[0] # Remove channel axis for skimage manipulation # Rotate img = transform.rotate(img, angle=np.random.uniform(-45, 45), resize=True, mode='constant') # Scale img = transform.rescale(img, scale=np.random.uniform(0.7, 1.2), mode='constant') # Translate h, w = img.shape if h >= img_size[0] or w >= img_size[1]: img = transform.resize(img, output_shape=img_size, mode='constant') img = img.astype(np.float32) else: img_canvas = np.zeros(img_size, dtype=np.float32) ymin = np.random.randint(0, img_size[0] - h) xmin = np.random.randint(0, img_size[1] - w) img_canvas[ymin:ymin+h, xmin:xmin+w] = img img = img_canvas img = img[np.newaxis, :] # Add the bach channel back return img, label
def transfer(rgbImage, new_dims): im = np.dot(rgbImage[..., :3], [0.229, 0.587, 0.144]) im_min, im_max = im.min(), im.max() if im_max > im_min: # skimage is fast but only understands {1,3} channel images # in [0, 1]. im_std = (im - im_min) / (im_max - im_min) resized_std = resize(im_std, new_dims, order=1) resized_im = resized_std * (im_max - im_min) + im_min else: # the image is a constant -- avoid divide by 0 ret = np.empty((new_dims[0], new_dims[1], im.shape[-1]), dtype=np.float32) ret.fill(im_min) return ret return resized_im.astype(np.float32)
def _extraction_iterator_corners(image, use_local_thresholding=False, apply_gaussian=False, n=5): # If the image is too small, then double its scale until big enough. img = image while max(img.shape) < 500: img = resize(img, np.array(img.shape) * 2) if apply_gaussian: img = gaussian_filter(image, (3.0, 3.0)) for corner_points in iter_blob_extremes(img, n=n): try: warped_image = geometry.warp_image_by_corner_points_projection(corner_points, img) sudoku, bin_image = geometry.split_image_into_sudoku_pieces_adaptive_global( warped_image, otsu_local=use_local_thresholding, apply_gaussian=apply_gaussian) except SudokuExtractError: # Try next blob. pass except Exception as e: raise else: yield sudoku, bin_image
def phash64(img): """Compute a perceptual hash of an image. :param img: a rgb image to be hashed :type img: numpy.ndarray :return: a perceptrual hash of img coded on 64 bits :rtype: int """ resized = rgb2grey(resize(img, (8, 8))) mean = resized.mean() boolean_matrix = resized > mean hash_lst = boolean_matrix.reshape((1, 64))[0] hash_lst = list(map(int, hash_lst)) im_hash = 0 for v in hash_lst: im_hash = (im_hash << 1) | v return im_hash
def dhash(img): """Compute a perceptual has of an image. Algo explained here : https://blog.bearstech.com/2014/07/numpy-par-lexemple-une-implementation-de-dhash.html :param img: an image :type img: numpy.ndarray :return: a perceptual hash of img coded on 64 bits :rtype: int """ TWOS = np.array([2 ** n for n in range(7, -1, -1)]) BIGS = np.array([256 ** n for n in range(7, -1, -1)], dtype=np.uint64) img = rgb2grey(resize(img, (9, 8))) h = np.array([0] * 8, dtype=np.uint8) for i in range(8): h[i] = TWOS[img[i] > img[i + 1]].sum() return (BIGS * h).sum()
def resize_mnist_data(images, new_size_a, new_size_b=None): """ Resizes a set of images :param images: :param new_size: :return: """ from skimage.transform import resize if new_size_b is None: new_size_b = new_size_a resized_data = np.zeros((images.shape[0], 1, new_size_a, new_size_b)) for i in range(len(images)): resized_data[i, 0, :, :] = resize(images[i, 0, :, :], (new_size_a, new_size_b)) return np.float32(resized_data)
def pyramid_expand_3d(volume, upscale=2, sigma=None, order=1, mode='reflect', cval=0): _check_factor(upscale) #_check_float32(volume) #volume=img_as_float(volume) volume=volume.astype('float64') # /(12641.6) # x = volume.shape[0] y = volume.shape[1] z = volume.shape[2] out_x = math.ceil(upscale * x) out_y = math.ceil(upscale * y) out_z = math.ceil(upscale * z) if sigma is None: # automatically determine sigma which covers > 99% of distribution sigma = 2 * upscale / 6.0 start_time = time.time() resized = resize(volume, (out_x, out_y, out_z), order=order, mode=mode, cval=cval) start_time = time.time() out = _smooth_3d(resized, sigma, mode, cval) # I want it to be float32 start_time = time.time() out=out.astype('float32') return out
def showImage( batch_id, dictionary, imSize, attr, outfile): images = dictionary.get('data') labels = dictionary.get('labels') for i in xrange(10000): singleImage = images[i] recon = np.zeros( (imSize, imSize, 3), dtype = np.uint8 ) singleImage = singleImage.reshape( (imSize*3, imSize)) red = singleImage[0:imSize,:] blue = singleImage[imSize:2*imSize,:] green = singleImage[2*imSize:3*imSize,:] recon[:,:,0] = red recon[:,:,1] = blue recon[:,:,2] = green outpath = os.path.abspath(".") + "/" + attr + "/" + str(batch_id) + "_" + str(i) + ".jpg" #recon = resize(recon, (256, 256)) io.imsave(outpath, recon) outfile.write(outpath + " " + str(labels[i]) + "\n")
def sliding_window(num_boxes, image, windowSize, workers=3): image_batch = None #'''Guarantee that middle of picture is always covered''' #data = get_middle_box(image, windowSize) #img = image[data[1]:data[3],data[0]:data[2]] #image_batch = img[np.newaxis] for i in range((num_boxes / workers)): y = random.randrange(0, image.shape[0] - windowSize[0]) x = random.randrange(0, image.shape[1] - windowSize[1]) img = image[y:y + windowSize[1], x:x + windowSize[0]] img = resize(img, (299, 299)) if image_batch is None: image_batch = img[np.newaxis] else: image_batch = np.append(image_batch, img[np.newaxis], axis=0) return image_batch
def slide_window(img, F, window_size, stride): window_list = [] w = img.shape[1] h = img.shape[0] w_re = int(float(w)*window_size/F) h_re = int(float(h)*window_size/F) if w_re<=window_size+stride or h_re<=window_size+stride: return None img = resize(img, (h_re, w_re, 3)) img = image_preprocess(img) if len(img.shape)!=3: return None for i in range(int((w_re-window_size)/stride)): for j in range(int((h_re-window_size)/stride)): box = [j*stride, i*stride, j*stride+window_size, i*stride+window_size] window_list.append(box) return img, np.asarray(window_list)
def resize(image, w, h, **kwargs): """ Resize image. Image can be up- or down-sized (using interpolation). For details see: http://scikit-image.org/docs/dev/api/skimage.transform.html#skimage.transform.resize >>> image = np.ones((10,5), dtype='uint8') >>> resize(image, 4, 3) array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], dtype=uint8) :param numpy array image: Numpy array with range [0,255] and dtype 'uint8'. :param int w: Width in pixels. :param int h: Height in pixels. :param kwargs kwargs: Keyword arguments for the underlying scikit-image resize function, e.g. order=1 for linear interpolation. :return: Resized image :rtype: numpy array with range [0,255] and dtype 'uint8' """ set_default_order(kwargs) return skt.resize(image, (h, w), mode='constant', preserve_range=True, **kwargs).astype('uint8')
def flatFieldFromFit(self): ''' calculate flatField from 2d-polynomal fit filling all high gradient areas within averaged fit-image returns flatField, average background level, fitted image, valid indices mask ''' fitimg, mask = self._prepare() out = fitimg.copy() lastm = 0 for _ in range(10): out = polyfit2dGrid(out, mask, 2) mask = highGrad(out) m = mask.sum() if m == lastm: break lastm = m out = np.clip(out, 0.1, 1) out = resize(out, self._orig_shape, mode='reflect') return out, self.bglevel / self._n, fitimg, mask
def get_rgbd_file(self, dirname, offset): associations = self.seq_dir_map[dirname]['associations'] if associations[offset, 1].startswith('depth'): rgb_filename = os.path.join(dirname, associations[offset, 3]) depth_filename = os.path.join(dirname, associations[offset, 1]) else: rgb_filename = os.path.join(dirname, associations[offset, 1]) depth_filename = os.path.join(dirname, associations[offset, 3]) rgb_img = ndimage.imread(rgb_filename) depth_img = ndimage.imread(depth_filename) width = height = 224 # Reshape depth_img = np.reshape(depth_img, list(depth_img.shape) + [1]) depth_img = 255 * depth_img / np.max(depth_img) rgbd_img = np.concatenate((rgb_img, depth_img), 2) # Resize rgbd_img = transform.resize(rgbd_img, [width, height], preserve_range=True) return rgb_filename, depth_filename, rgbd_img.astype(np.float32)
def get_blend_map(img, att_map, blur=True, overlap=True): # att_map -= att_map.min() # if att_map.max() > 0: # att_map /= att_map.max() att_map = 1.0 - att_map att_map = transform.resize(att_map, (img.shape[:2]), order = 3, mode='edge') # print att_map.shape if blur: att_map = filters.gaussian(att_map, 0.02*max(img.shape[:2])) att_map -= att_map.min() att_map /= att_map.max() cmap = plt.get_cmap('jet') att_map_v = cmap(att_map) att_map_v = np.delete(att_map_v, 3, 2) if overlap: att_map = 1*(1-att_map**0.7).reshape(att_map.shape + (1,))*img + (att_map**0.7).reshape(att_map.shape+(1,)) * att_map_v return att_map
def update(self, obs): obs = resize(obs, self.factors.shape, preserve_range=True) obs = np.floor((obs*self.num_bins)).astype(np.int32) context = [0, 0, 0, 0] log_prob = 0.0 log_recoding_prob = 0.0 for i in range(self.factors.shape[0]): for j in range(self.factors.shape[1]): context[3] = obs[i, j-1] if j > 0 else 0 context[2] = obs[i-1, j] if i > 0 else 0 context[1] = obs[i-1, j-1] if i > 0 and j > 0 else 0 context[0] = obs[i-1, j+1] if i > 0 and j < self.factors.shape[1]-1 else 0 log_prob += self.factors[i, j].update(context, obs[i, j]) log_recoding_prob += self.factors[i, j].log_prob(context, obs[i, j]) return self.exploration_bonus(log_prob, log_recoding_prob)
def load_image(path, height, width, mode='RGB'): """ Load an image from disk Returns an np.ndarray (channels x width x height) Arguments: path -- path to an image on disk width -- resize dimension height -- resize dimension Keyword arguments: mode -- the PIL mode that the image should be converted to (RGB for color or L for grayscale) """ image = PIL.Image.open(path) image = image.convert(mode) image = np.array(image) # squash image = scipy.misc.imresize(image, (height, width), 'bilinear') return image # Forward pass of input through the network
def fits2jpg(fname): hdu_list = fits.open(fname) image = hdu_list[0].data image = np.squeeze(image) img = np.copy(image) idx = np.isnan(img) img[idx] = 0 img_clip = np.flipud(img) sigma = 3.0 # Estimate stats mean, median, std = sigma_clipped_stats(img_clip, sigma=sigma, iters=10) # Clip off n sigma points img_clip = clip(img_clip,std*sigma) if img_clip.shape[0] !=150 or img_clip.shape[1] !=150: img_clip = resize(img_clip, (150,150)) #img_clip = rgb2gray(img_clip) outfile = fname[0:-5] +'.png' imsave(outfile, img_clip) return img_clip,outfile # Do the fusion classification
def PostprocessImage(img): """ Postprocess target style image 1. add the images dataset mean to optimized image 2. swap axis (b,g,r) to (r,g,b) and save it Parameters -------- img: ndarray (1x3xMxN), optimized image Returns out : ndarray (3xMxN), Postprocessed image """ img = np.resize(img, (3, img.shape[2], img.shape[3])) img[0, :] += 123.68 img[1, :] += 116.779 img[2, :] += 103.939 img = np.swapaxes(img, 0, 2) img = np.swapaxes(img, 0, 1) img = np.clip(img, 0, 255) return img.astype('uint8')
def resize_img(img, opt): """ CNN predictions are made at the 36x36 pixel lvl and the test set needs to be at the 608x608 lvl. The function resizes. Args: numpy array 36x36 for test or 50x50 for train Returns: numpy array 608x608 for test or 400x400 for train """ #print(img.shape) if opt == 'test': img = resize(img, (conf.test_image_size, conf.test_image_size)) return img elif opt == 'train': size = conf.train_image_size blocks = 8 #conf.gt_res # resolution of the gt is 8x8 pixels for one class steps = 50 #conf.train_image_size // blocks # 50 dd = np.zeros((size, size)) for i in range(steps): for j in range(steps): dd[j*blocks:(j+1)*blocks,i*blocks:(i+1)*blocks] = img[j,i] return dd else: raise ValueError('test or train plz')
def ll_of_count(counts, means, stds): cm = np.copy(counts) cm = (cm*255./cm.max()).astype(np.uint8) cm = cm[np.where(cm.sum(axis=1))] if cm.shape[0] == 0: cm = np.zeros((10, 30), dtype = np.uint8) im = Image.fromarray(cm).resize((30,10), Image.ANTIALIAS) counts_resized_arr = np.array(im.getdata(), dtype=np.float32).reshape(10,30)/255. max_ll = -10000000 for roll_by in xrange(30): resized_counts = np.roll(counts_resized_arr, roll_by, axis=1).flatten() ll = 0. for i in xrange(resized_counts.shape[0]): ll += np.log(scipy.stats.norm.pdf(resized_counts[i], loc=means[i], scale=stds[i])) if ll > max_ll: max_ll = ll return max_ll
def postprocess(imgs, size, grayscale=False): print("Postprocessing images and resize (at %d)" % size) keyname = ('gray_%d' if grayscale else 'color_%d') % size for img in imgs: # Continue if already calculated if img.isSetByName(keyname): continue floatimg = img_as_float(img.image) floatimg = resize(floatimg, (size, size)) if grayscale: floatimg = rgb2gray(floatimg) img.setByName(keyname, floatimg) # expect to return floats # Augment images
def pre_proc(X): '''????? ???. Args: X(np.array): ??? ???? ??? ???? ? 84X84? ???? ??? ????? ??????(??? ?? ??? ??) 255? ?? Returns: np.array: ??? ??? ''' # ?? ? frame? ???? max? ????? flickering? ?? # x = np.maximum(X, X1) # ??? ????? ????? ?? ??? ?? ?? x = np.uint8(resize(rgb2gray(X), (HEIGHT, WIDTH), mode='reflect') * 255) return x
def evaluate_sliding_window(img_filename, crops): img = io.imread(img_filename).astype(np.float32)/255 if img.ndim == 2: # Handle B/W images img = np.expand_dims(img, axis=-1) img = np.repeat(img, 3, 2) img_crops = np.zeros((batch_size, 227, 227, 3)) for i in xrange(len(crops)): crop = crops[i] img_crop = transform.resize(img[crop[1]:crop[1]+crop[3],crop[0]:crop[0]+crop[2]], (227, 227))-0.5 img_crop = np.expand_dims(img_crop, axis=0) img_crops[i,:,:,:] = img_crop # compute ranking scores scores = sess.run([score_func], feed_dict={image_placeholder: img_crops}) # find the optimal crop idx = np.argmax(scores[:len(crops)]) best_window = crops[idx] # return the best crop return (best_window[0], best_window[1], best_window[2], best_window[3])
def PreprocessContentImage(path, long_edge): img = io.imread(path) logging.info("load the content image, size = %s", img.shape[:2]) factor = float(long_edge) / max(img.shape[:2]) new_size = (int(img.shape[0] * factor), int(img.shape[1] * factor)) resized_img = transform.resize(img, new_size) sample = np.asarray(resized_img) * 256 # swap axes to make image from (224, 224, 3) to (3, 224, 224) sample = np.swapaxes(sample, 0, 2) sample = np.swapaxes(sample, 1, 2) # sub mean sample[0, :] -= 123.68 sample[1, :] -= 116.779 sample[2, :] -= 103.939 logging.info("resize the content image to %s", new_size) return np.resize(sample, (1, 3, sample.shape[1], sample.shape[2]))
def PreprocessImage(path, show_img=True): # load image img = io.imread(path) print("Original Image Shape: ", img.shape) # we crop image from center short_egde = min(img.shape[:2]) yy = int((img.shape[0] - short_egde) / 2) xx = int((img.shape[1] - short_egde) / 2) crop_img = img[yy : yy + short_egde, xx : xx + short_egde] # resize to 299, 299 resized_img = transform.resize(crop_img, (299, 299)) if show_img: io.imshow(resized_img) # convert to numpy.ndarray sample = np.asarray(resized_img) * 256 # swap axes to make image from (299, 299, 3) to (3, 299, 299) sample = np.swapaxes(sample, 0, 2) sample = np.swapaxes(sample, 1, 2) # sub mean normed_img = sample - 128. normed_img /= 128. return np.reshape(normed_img, (1, 3, 299, 299))
def PreprocessImage(path, show_img=True): # load image img = io.imread(path) # print("Original Image Shape: ", img.shape) # we crop image from center short_egde = min(img.shape[:2]) yy = int((img.shape[0] - short_egde) / 2) xx = int((img.shape[1] - short_egde) / 2) crop_img = img[yy : yy + short_egde, xx : xx + short_egde] # resize to 299, 299 resized_img = transform.resize(crop_img, (299, 299)) if show_img: io.imshow(resized_img) # convert to numpy.ndarray sample = np.asarray(resized_img) * 256 # swap axes to make image from (299, 299, 3) to (3, 299, 299) sample = np.swapaxes(sample, 0, 2) sample = np.swapaxes(sample, 1, 2) # sub mean normed_img = sample - 128. normed_img /= 128. return np.reshape(normed_img, (1, 3, 299, 299))
def PreprocessImage(path, show_img=False): # load image img = io.imread(path) print("Original Image Shape: ", img.shape) # we crop image from center short_egde = min(img.shape[:2]) yy = int((img.shape[0] - short_egde) / 2) xx = int((img.shape[1] - short_egde) / 2) crop_img = img[yy : yy + short_egde, xx : xx + short_egde] # resize to 224, 224 resized_img = transform.resize(crop_img, (224, 224)) if show_img: io.imshow(resized_img) # convert to numpy.ndarray sample = np.asarray(resized_img) * 255 # swap axes to make image from (224, 224, 3) to (3, 224, 224) sample = np.swapaxes(sample, 0, 2) sample = np.swapaxes(sample, 1, 2) # sub mean normed_img = sample - mean_img normed_img.resize(1, 3, 224, 224) return normed_img # Get preprocessed batch (single image batch)
def zoom_image_fixed_size(image, zoom): """Return rescaled and cropped image array. """ if zoom < 1: raise ValueError("Zoom scale factor must be at least 1.") elif zoom == 1: # copy return np.array(image) width, height = image.shape t_width = int(np.ceil(zoom*width)) t_height = int(np.ceil(zoom*height)) if t_width//2 != width//2: t_width -= 1 if t_height//2 != height//2: t_height -= 1 t_image = transform.resize(image, (t_width, t_height), order=3) return t_image[(t_width-width)/2:(t_width+width)/2, (t_height-height)/2:(t_height+height)/2]
def zoom_image(image, zoom, out_width=25): """Return rescaled and cropped image array with width out_width. """ if zoom < 1: raise ValueError("Zoom scale factor must be at least 1.") width, height = image.shape #if width < out_width: # raise ValueError( # "image width before zooming ({0}) is less " # "than requested output width ({1})".format(width, out_width)) out_height = int(np.rint(float(out_width * height) / width)) t_width = int(np.rint(out_width * zoom)) t_height = int(np.rint(out_height * zoom)) if t_width // 2 != out_width // 2: t_width += 1 if t_height // 2 != out_height // 2: t_height += 1 # zoom with cubic interpolation t_image = transform.resize(image, (t_width, t_height), order=3) # crop return t_image[(t_width - out_width) / 2:(t_width + out_width) / 2, (t_height - out_height) / 2:(t_height + out_height) / 2]
def add(image, heat_map, alpha=0.6, display=False, save=None, cmap='viridis', axis='on', verbose=False): height = image.shape[0] width = image.shape[1] # resize heat map heat_map_resized = transform.resize(heat_map, (height, width)) # normalize heat map max_value = np.max(heat_map_resized) min_value = np.min(heat_map_resized) normalized_heat_map = (heat_map_resized - min_value) / (max_value - min_value) # display plt.imshow(image) plt.imshow(255 * normalized_heat_map, alpha=alpha, cmap=cmap) plt.axis(axis) if display: plt.show() if save is not None: if verbose: print('save image: ' + save) plt.savefig(save, bbox_inches='tight', pad_inches=0)
def crop_image(image, shape): factor = float(min(shape[:2])) / min(image.shape[:2]) new_size = [int(image.shape[0] * factor), int(image.shape[1] * factor)] if new_size[0] < shape[0]: new_size[0] = shape[0] if new_size[1] < shape[0]: new_size[1] = shape[0] resized_image = transform.resize(image, new_size) sample = np.asarray(resized_image) * 256 if shape[0] < sample.shape[0] or shape[1] < sample.shape[1]: xx = int((sample.shape[0] - shape[0])) yy = int((sample.shape[1] - shape[1])) x_start = xx / 2 y_start = yy / 2 x_end = x_start + shape[0] y_end = y_start + shape[1] sample = sample[x_start:x_end, y_start:y_end, :] return sample
def __call__(self, sample): image, landmarks = sample['image'], sample['landmarks'] h, w = image.shape[:2] if isinstance(self.output_size, int): if h > w: new_h, new_w = self.output_size * h / w, self.output_size else: new_h, new_w = self.output_size, self.output_size * w / h else: new_h, new_w = self.output_size new_h, new_w = int(new_h), int(new_w) img = transform.resize(image, (new_h, new_w)) # h and w are swapped for landmarks because for images, # x and y axes are axis 1 and 0 respectively landmarks = landmarks * [new_w / w, new_h / h] return {'image': img, 'landmarks': landmarks}
def extract_chars(pixels): # use sci-kit image to find the contours of the image contours = measure.find_contours(pixels, CONTOUR_LEVEL) # calls an algorithm on the contours to remove unwanted overlapping contours like the holes in 6's, 8's, and 9's contours = __remove_overlap_contours(contours) # populate a dictionary with key of the left most x coordinate of the contour and value of the resized contour resized_char_dict = dict() for n, contour in enumerate(contours): min, max = __get_min_max(contour) resized_contour = transform.resize(pixels[int(min[0]):int(max[0]), int(min[1]):int(max[1])], (32, 32)) resized_char_dict[min[1]] = resized_contour # sort the map by key (left most x coordinate of the contour) sorted_dict = sorted(resized_char_dict.items(), key=operator.itemgetter(0)) # extract the contours from the sorted dictionary into a list extracted_chars = np.asarray([i[1] for i in sorted_dict]) # normalize the contours by subtracting 0.5 to each pixel value np.subtract(extracted_chars, 0.5, out=extracted_chars) return extracted_chars
def __check_size(self, img): ''' checks if the image accords to the minimum size requirements Returns: tuple (img, bool): img: the original image if the image size was ok, a resized image otherwise bool: flag indicating whether the image was resized ''' if np.amin(img.shape[:2]) < self.min_image_width_height: if np.amin(img.shape[:2]) == 0: return None, False scale = float(self.min_image_width_height+1)/float(np.amin(img.shape[:2])) new_shape = (int(scale*img.shape[0]), int(scale*img.shape[1])) new_img = resize(image=img, output_shape=new_shape) return new_img, True else: return img, False
def _read_image(self, image_name): """ Read image from self._path_to_img and perform any necessary preparation :param image_name: string, image name, which is added to self._path_to_img :return: numpy 2d array """ filename = image_name try: img = io.imread(filename) except IOError: return None img = img_as_float(img) if len(img.shape) > 2: img = img[:, :, 0] img = resize(img, (self._image_width, self._image_height)) img = img.reshape((self._image_width, self._image_height, 1)) return img
def resize_batch(imgs): # A function to resize a batch of MNIST images to (32, 32) # Args: # imgs: a numpy array of size [batch_size, 28 X 28]. # Returns: # a numpy array of size [batch_size, 32, 32]. imgs = imgs.reshape((-1, 28, 28, 1)) resized_imgs = np.zeros((imgs.shape[0], 32, 32, 1)) for i in range(imgs.shape[0]): resized_imgs[i, ..., 0] = transform.resize(imgs[i, ..., 0], (32, 32)) return resized_imgs
def read_images(path): for subdir, dirs, files in os.walk(path): dcms = glob.glob(os.path.join(subdir, '*.dcm')) if len(dcms) > 1: slices = [dicom.read_file(dcm) for dcm in dcms] slices.sort(key = lambda x: float(x.ImagePositionPatient[2])) images = np.stack([s.pixel_array for s in slices], axis=0).astype(np.float32) images = images + slices[0].RescaleIntercept images = normalize(images) inplane_scale = slices[0].PixelSpacing[0] / PIXEL_SPACING inplane_size = int(np.rint(inplane_scale * slices[0].Rows / 2) * 2) z_scale = slices[0].SliceThickness / SLICE_THICKNESS z_size = int(np.rint(z_scale * images.shape[0])) if inplane_size != INPLANE_SIZE or z_scale != 1: images = resize(images, (z_size, inplane_size, inplane_size), mode='constant') if inplane_size != INPLANE_SIZE: if inplane_size > INPLANE_SIZE: crop = int((inplane_size - INPLANE_SIZE) / 2) images = images[:, crop : crop + INPLANE_SIZE, crop : crop + INPLANE_SIZE] else: pad = int((INPLANE_SIZE - new_size) / 2) images = np.pad(images, ((0, 0), (pad, pad), (pad, pad))) return images
def read_images_labels(path): # Read the images and labels from a folder containing both dicom files for subdir, dirs, files in os.walk(path): dcms = glob.glob(os.path.join(subdir, '*.dcm')) if len(dcms) == 1: structure = dicom.read_file(dcms[0]) contours = read_structure(structure) elif len(dcms) > 1: slices = [dicom.read_file(dcm) for dcm in dcms] slices.sort(key = lambda x: float(x.ImagePositionPatient[2])) images = np.stack([s.pixel_array for s in slices], axis=0).astype(np.float32) images = images + slices[0].RescaleIntercept images = normalize(images) labels = get_labels(contours, images.shape, slices) inplane_scale = slices[0].PixelSpacing[0] / PIXEL_SPACING inplane_size = int(np.rint(inplane_scale * slices[0].Rows / 2) * 2) z_scale = slices[0].SliceThickness / SLICE_THICKNESS z_size = int(np.rint(z_scale * images.shape[0])) if inplane_size != INPLANE_SIZE or z_scale != 1: images = resize(images, (z_size, inplane_size, inplane_size), mode='constant') new_labels = np.zeros_like(images, dtype=np.float32) for z in range(N_CLASSES): roi = resize((labels == z + 1).astype(np.float32), (z_size, inplane_size, inplane_size), mode='constant') new_labels[roi >= 0.5] = z + 1 labels = new_labels if inplane_size != INPLANE_SIZE: if inplane_size > INPLANE_SIZE: crop = int((inplane_size - INPLANE_SIZE) / 2) images = images[:, crop : crop + INPLANE_SIZE, crop : crop + INPLANE_SIZE] labels = labels[:, crop : crop + INPLANE_SIZE, crop : crop + INPLANE_SIZE] else: pad = int((INPLANE_SIZE - new_size) / 2) images = np.pad(images, ((0, 0), (pad, pad), (pad, pad)), 'constant') labels = np.pad(labels, ((0, 0), (pad, pad), (pad, pad)), 'constant') return images, labels
def resize_images_labels(images, labels): resized_images = resize_images(images) # labels size = (ALL_IM_SIZE[0], ALL_IM_SIZE[1] + CROP * 2, ALL_IM_SIZE[2] + CROP * 2) resized_labels = np.zeros(size, dtype=np.float32) for z in range(N_CLASSES): roi = resize((labels == z + 1).astype(np.float32), size, mode='constant') resized_labels[roi >= 0.5] = z + 1 resized_labels = resized_labels[:, CROP:-CROP, CROP:-CROP] return resized_images, resized_labels
def resize_images(images): size = (ALL_IM_SIZE[0], ALL_IM_SIZE[1] + CROP * 2, ALL_IM_SIZE[2] + CROP * 2) resized_images = resize(images, size, mode='constant') resized_images = resized_images[:, CROP:-CROP, CROP:-CROP] return resized_images
def setup(): global resnet_mean global resnet_net global vqa_net # data provider vqa_data_provider_layer.CURRENT_DATA_SHAPE = EXTRACT_LAYER_SIZE # mean substraction blob = caffe.proto.caffe_pb2.BlobProto() data = open( RESNET_MEAN_PATH , 'rb').read() blob.ParseFromString(data) resnet_mean = np.array( caffe.io.blobproto_to_array(blob)).astype(np.float32).reshape(3,224,224) resnet_mean = np.transpose(cv2.resize(np.transpose(resnet_mean,(1,2,0)), (448,448)),(2,0,1)) # resnet caffe.set_device(GPU_ID) caffe.set_mode_gpu() resnet_net = caffe.Net(RESNET_LARGE_PROTOTXT_PATH, RESNET_CAFFEMODEL_PATH, caffe.TEST) # our net vqa_net = caffe.Net(VQA_PROTOTXT_PATH, VQA_CAFFEMODEL_PATH, caffe.TEST) # uploads if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) if not os.path.exists(VIZ_FOLDER): os.makedirs(VIZ_FOLDER) print 'Finished setup'
def trim_image(img): y,x,c = img.shape if c != 3: raise Exception('Expected 3 channels in the image') resized_img = cv2.resize( img, (TARGET_IMG_SIZE, TARGET_IMG_SIZE)) transposed_img = np.transpose(resized_img,(2,0,1)).astype(np.float32) ivec = transposed_img - resnet_mean return ivec
def downsample_image(img): img_h, img_w, img_c = img.shape img = resize(img, (448 * img_h / img_w, 448)) return img