python类resize()的实例源码

preprocessing.py 文件源码 项目:kaggle-yelp-restaurant-photo-classification 作者: u1234x1234 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
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))
predict.py 文件源码 项目:kaggle-yelp-restaurant-photo-classification 作者: u1234x1234 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
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))
mxnet_predict_example.py 文件源码 项目:mxnet_tk1 作者: starimpact 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
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)
preprocessing.py 文件源码 项目:deepjets 作者: deepjets 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
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]
preprocessing.py 文件源码 项目:deepjets 作者: deepjets 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
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]
heat_map.py 文件源码 项目:heatmap 作者: durandtibo 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
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)
fast_neural_style.py 文件源码 项目:neural_style 作者: wangchen1ren 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 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
data_loading_tutorial.py 文件源码 项目:tutorials 作者: pytorch 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
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}
preprocess.py 文件源码 项目:neith 作者: Rabrg 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
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
phocnet_trainer.py 文件源码 项目:phocnet 作者: ssudholt 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
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
fast_neural_style.py 文件源码 项目:TensorFlowBook 作者: DeepVisionTeam 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 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
lipnet_dataset.py 文件源码 项目:lipnet 作者: grishasergei 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
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
autoencoder.py 文件源码 项目:TensorFlow-World 作者: astorfi 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
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
convert_data.py 文件源码 项目:aapm_thoracic_challenge 作者: xf4j 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
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
convert_data.py 文件源码 项目:aapm_thoracic_challenge 作者: xf4j 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
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
utils.py 文件源码 项目:aapm_thoracic_challenge 作者: xf4j 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
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
utils.py 文件源码 项目:aapm_thoracic_challenge 作者: xf4j 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
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
server.py 文件源码 项目:vqa-mcb 作者: akirafukui 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
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'
server.py 文件源码 项目:vqa-mcb 作者: akirafukui 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
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
server.py 文件源码 项目:vqa-mcb 作者: akirafukui 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def downsample_image(img):
    img_h, img_w, img_c = img.shape
    img = resize(img, (448 * img_h / img_w, 448))
    return img


问题


面经


文章

微信
公众号

扫码关注公众号