Esempio n. 1
0
def read_video(name, image_shape):
    if name.lower().endswith('.png') or name.lower().endswith('.jpg'):
        image = io.imread(name)

        if len(image.shape) == 2 or image.shape[2] == 1:
            image = gray2rgb(image)

        if image.shape[2] == 4:
            image = image[..., :3]

        image = img_as_float32(image)

        video_array = np.moveaxis(image, 1, 0)

        video_array = video_array.reshape((-1,) + image_shape)
        video_array = np.moveaxis(video_array, 1, 2)
    elif name.lower().endswith('.gif') or name.lower().endswith('.mp4') or name.lower().endswith('.mov'):
        video = np.array(mimread(name))
        if len(video.shape) == 3:
            video = np.array([gray2rgb(frame) for frame in video])
        if video.shape[-1] == 4:
            video = video[..., :3]
        video_array = img_as_float32(video)
    else:
        raise Exception("Unknown file extensions  %s" % name)

    return video_array
Esempio n. 2
0
def cast_img_float32(tensor):
    """Cast the data in np.float32.

    If the input data is in (unsigned) integer, the values are scaled between
    0 and 1. When converting from a np.float dtype, values are not modified.

    Parameters
    ----------
    tensor : np.ndarray
        Image to cast.

    Returns
    -------
    tensor : np.ndarray, np.float32
        image cast.

    """
    # check tensor dtype
    check_array(tensor,
                ndim=[2, 3, 4, 5],
                dtype=[
                    np.uint8, np.uint16, np.uint32, np.uint64, np.int8,
                    np.int16, np.int32, np.int64, np.float32, np.float64
                ])

    # cast tensor
    if parse_version(skimage.__version__) < parse_version("0.16.0"):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            tensor = img_as_float32(tensor)
    else:
        tensor = img_as_float32(tensor)

    return tensor
Esempio n. 3
0
def memfunc():
    # Note: these files default to notre dame, unless otherwise specified
    # image1_file = "../data/NotreDame/NotreDame1.jpg"
    #   image2_file = "../data/NotreDame/NotreDame2.jpg"
    #eval_file = "../data/NotreDame/NotreDameEval.mat"

    image1_file = "../data/EpiscopalGaudi/EGaudi_1.jpg"
    image2_file = "../data/EpiscopalGaudi/EGaudi_1.jpg"
    eval_file = "../data/EpiscopalGaudi/EGaudiEval.mat"

    #image1_file = "../data/MountRushmore/Mount_Rushmore1.jpg"
    #image2_file = "../data/MountRushmore/Mount_Rushmore2.jpg"
    #eval_file = "../data/MountRushmore/MountRushmoreEval.mat"

    scale_factor = 0.5
    feature_width = 16

    image1 = img_as_float32(
        rescale(rgb2gray(io.imread(image1_file)), scale_factor))
    image2 = img_as_float32(
        rescale(rgb2gray(io.imread(image2_file)), scale_factor))

    (x1, y1) = student.get_interest_points(image1, feature_width, 0.4, 0.06, 0,
                                           0, 0, 400)
    (x2, y2) = student.get_interest_points(image2, feature_width, 5, 0.06, 0,
                                           0, 0, 500)

    image1_features = student.get_features(image1, x1, y1, feature_width)
    image2_features = student.get_features(image2, x2, y2, feature_width)

    matches, confidences = student.match_features(image1_features,
                                                  image2_features)

    evaluate_correspondence(image1, image2, eval_file, scale_factor, x1, y1,
                            x2, y2, matches, confidences, 0)
Esempio n. 4
0
    def __getitem__(self, idx):
        piece = self.pair_list[idx]
        # flip_p = np.random.uniform(0, 1)
        img_temp = skimage.io.imread(piece[0])
        im1 = skimage.img_as_float32(img_temp)
        # im1 = utils_data.flip(img_temp, flip_p)

        img_temp = skimage.io.imread(piece[1])
        im2 = skimage.img_as_float32(img_temp)
        # im2 = utils_data.flip(img_temp, flip_p)

        label_tmp = int(piece[2])
        if label_tmp == 1:
            gt_temp = skimage.io.imread(piece[3])
            if len(gt_temp.shape) > 2:
                gt_temp = gt_temp[..., 0]
            gt1 = skimage.img_as_float32(gt_temp)
            # gt1 = utils_data.flip(gt_temp, flip_p)

            gt_temp = skimage.io.imread(piece[4])
            if len(gt_temp.shape) > 2:
                gt_temp = gt_temp[..., 0]
            gt2 = skimage.img_as_float32(gt_temp)
            # gt2 = utils_data.flip(gt_temp, flip_p)
        else:
            gt1 = np.zeros(im1.shape[:2], dtype=np.float32)
            gt2 = np.zeros(im1.shape[:2], dtype=np.float32)

        imt1, gtt1 = self.transform(im1, gt1)
        imt2, gtt2 = self.transform(im2, gt2)

        return imt1, imt2, gtt1, gtt2, label_tmp
Esempio n. 5
0
    def __getitem__(self, idx):
        if self.is_train and self.id_sampling:
            name = self.videos[idx]
            path = np.random.choice(
                glob.glob(os.path.join(self.root_dir, name + '*.mp4')))
        else:
            name = self.videos[idx]
            path = os.path.join(self.root_dir, name)

            video_name = os.path.basename(path)
        print(video_name)
        if self.is_train and os.path.isdir(path):
            frames_source = os.listdir(os.path.join(path, "orig"))
            frames_driving = os.listdir(os.path.join(path, "gened"))

            num_frames = len(frames_driving)
            frame_idx = np.sort(
                np.random.choice(num_frames, replace=True, size=2))
            print(frame_idx)
            video_array = [
                img_as_float32(
                    io.imread(
                        os.path.join(path, "orig",
                                     frames_source[frame_idx[0]]))),
                img_as_float32(
                    io.imread(
                        os.path.join(path, "gened",
                                     frames_driving[frame_idx[1]]))),
                img_as_float32(
                    io.imread(
                        os.path.join(path, "orig",
                                     frames_source[frame_idx[1]])))
            ]
        else:
            video_array = read_video(path, frame_shape=self.frame_shape)
            num_frames = len(video_array)
            frame_idx = np.sort(
                np.random.choice(
                    num_frames, replace=True,
                    size=2)) if self.is_train else range(num_frames)
            video_array = video_array[frame_idx]

        if self.transform is not None:
            video_array = self.transform(video_array)

        out = {}
        if self.is_train:
            source = np.array(video_array[0], dtype='float32')
            driving = np.array(video_array[1], dtype='float32')
            target = np.array(video_array[2], dtype='float32')
            out['driving'] = driving.transpose((2, 0, 1))
            out['source'] = source.transpose((2, 0, 1))
            out['target'] = target.transpose((2, 0, 1))
        else:
            video = np.array(video_array, dtype='float32')
            out['video'] = video.transpose((3, 0, 1, 2))

        out['name'] = video_name

        return out
Esempio n. 6
0
    def get_frames_from_video(self):
        # randomly select one video and get frames (with labels)
        name = np.random.choice(list(self.im_mani_root.iterdir()))

        frame_list = []
        for _file in name.iterdir():
            if _file.suffix == ".png":
                im_file = str(_file)
                mask_file = os.path.join(str(self.mask_root), name.name,
                                         (_file.stem + ".jpg"))
                try:
                    assert os.path.exists(mask_file)
                except AssertionError:
                    continue

            image = io.imread(im_file)
            image = skimage.img_as_float32(image)  # image in [0-1] range

            _mask = io.imread(mask_file)

            if len(_mask.shape) > 2:
                mval = (0, 0, 255)
                ind = (_mask[:, :, 2] > mval[2] / 2)

                mask = np.zeros(_mask.shape[:2], dtype=np.float32)
                mask[ind] = 1
            else:
                mask = skimage.img_as_float32(_mask)

            yield image, mask
def memfunc():

    # Note: these files default to notre dame, unless otherwise specified
    image1_file = "../data/NotreDame/NotreDame1.jpg"
    image2_file = "../data/NotreDame/NotreDame2.jpg"
    eval_file = "../data/NotreDame/NotreDameEval.mat"

    scale_factor = 0.5
    feature_width = 16

    image1 = img_as_float32(
        rescale(rgb2gray(io.imread(image1_file)), scale_factor))
    image2 = img_as_float32(
        rescale(rgb2gray(io.imread(image2_file)), scale_factor))

    (x1, y1) = student.get_interest_points(image1, feature_width)
    (x2, y2) = student.get_interest_points(image2, feature_width)

    image1_features = student.get_features(image1, x1, y1, feature_width)
    image2_features = student.get_features(image2, x2, y2, feature_width)

    matches, confidences = student.match_features(image1_features,
                                                  image2_features)

    evaluate_correspondence(image1, image2, eval_file, scale_factor, x1, y1,
                            x2, y2, matches, confidences, 0)
Esempio n. 8
0
def showCorrespondence(imgA, imgB, matches_kp1, matches_kp2):

    imgA = img_as_float32(imgA)
    imgB = img_as_float32(imgB)

    fig = plt.figure()
    plt.axis('off')

    Height = max(imgA.shape[0], imgB.shape[0])
    Width = imgA.shape[1] + imgB.shape[1]
    numColors = imgA.shape[2]

    newImg = np.zeros([Height, Width, numColors])
    newImg[0:imgA.shape[0], 0:imgA.shape[1], :] = imgA
    newImg[0:imgB.shape[0], -imgB.shape[1]:, :] = imgB
    plt.imshow(newImg)

    shift = imgA.shape[1]
    for i in range(0, matches_kp1.shape[0]):

        r = lambda: random.randint(0, 255)
        cur_color = ('#%02X%02X%02X' % (r(), r(), r()))

        x1 = matches_kp1[i, 1]
        y1 = matches_kp1[i, 0]
        x2 = matches_kp2[i, 1]
        y2 = matches_kp2[i, 0]

        x = np.array([x1, x2])
        y = np.array([y1, y2 + shift])
        plt.plot(y, x, c=cur_color, linewidth=0.5)

    plt.show()
Esempio n. 9
0
def read_EM(path):
    x_train = tifffile.imread(path + 'train-volume.tif')
    x_train = img_as_float32(x_train)
    t_train = tifffile.imread(path + 'train-labels.tif')
    t_train = img_as_float32(t_train)
    x_test = tifffile.imread(path + 'test-volume.tif')
    x_test = img_as_float32(x_test)
    return x_train, t_train, x_test
Esempio n. 10
0
def read_video(name, frame_shape, is_train=True):
    """
    Read video which can be:
      - an image of concatenated frames
      - '.mp4' and'.gif'
      - folder with videos
    """

    if os.path.isdir(name):
        frames = sorted(os.listdir(name))
        num_frames = len(frames)
        video_array = np.array([
            img_as_float32(io.imread(os.path.join(name, frames[idx])))
            for idx in range(num_frames)
        ])
    elif name.lower().endswith('.png') or name.lower().endswith('.jpg'):
        image = io.imread(name)

        if len(image.shape) == 2 or image.shape[2] == 1:
            image = gray2rgb(image)

        if image.shape[2] == 4:
            image = image[..., :3]

        image = img_as_float32(image)

        video_array = np.moveaxis(image, 1, 0)

        video_array = video_array.reshape((-1, ) + frame_shape)
        video_array = np.moveaxis(video_array, 1, 2)
    elif name.lower().endswith('.gif') or name.lower().endswith(
            '.mp4') or name.lower().endswith('.mov'):
        vc = cv2.VideoCapture(name)
        n_frames = int(vc.get(cv2.CAP_PROP_FRAME_COUNT))
        frame_idx = np.sort(np.random.choice(
            n_frames, replace=True, size=2)) if is_train else range(n_frames)

        video = []
        for idx in frame_idx:
            vc.set(cv2.CAP_PROP_POS_FRAMES, idx)
            ret, frame = vc.read()
            if not ret:
                break

            video.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

        video = np.stack(video)
        if len(video.shape) == 3:
            video = np.array([gray2rgb(frame) for frame in video])
        if video.shape[-1] == 4:
            video = video[..., :3]
        video_array = img_as_float32(video)
    else:
        raise Exception("Unknown file extensions  %s" % name)

    return video_array
Esempio n. 11
0
def calc_iou(result, gt):
    img_gt = np.array(skimage.img_as_float32(skio.imread(
        gt, as_gray=True))).astype(np.uint8)
    img_result = np.array(
        skimage.img_as_float32(skio.imread(result,
                                           as_gray=True))).astype(np.uint8)

    intersection = img_result * img_gt
    union = img_result + img_gt
    union[union > 1] = 1

    return np.sum(intersection) / np.sum(union)
 def load_image(rootdir=r"", channel=1):
     im = plt.imread(rootdir)
     try:
         im.shape[2]
     except IndexError:
         im = img_as_float32(im)
     else:
         im = img_as_float32(im[:, :, channel])
     im[im == 0] = "nan"
     im[im == 1] = np.nanmin(im)
     im[np.isnan(im)] = np.nanmin(im)
     return im
Esempio n. 13
0
def load_data(file_name):
    """
     1) Load stuff
     There are numerous other image sets in the supplementary data on the
     project web page. You can simply download images off the Internet, as
     well. However, the evaluation function at the bottom of this script will
     only work for three particular image pairs (unless you add ground truth
     annotations for other image pairs). It is suggested that you only work
     with the two Notre Dame images until you are satisfied with your
     implementation and ready to test on additional images. A single scale
     pipeline works fine for these two images (and will give you full credit
     for this project), but you will need local features at multiple scales to
     handle harder cases.

     If you want to add new images to test, create a new elif of the same format as those
     for notre_dame, mt_rushmore, etc. You do not need to set the eval_file variable unless
     you hand create a ground truth annotations. To run with your new images use
     python main.py -p <your file name>.

    :param file_name: string for which image pair to compute correspondence for

        The first three strings can be used as shortcuts to the
        data files we give you

        1. notre_dame
        2. mt_rushmore
        3. e_gaudi

    :return: a tuple of the format (image1, image2, eval file)
    """

    # Note: these files default to notre dame, unless otherwise specified
    image1_file = "../data/NotreDame/NotreDame1.jpg"
    image2_file = "../data/NotreDame/NotreDame2.jpg"

    eval_file = "../data/NotreDame/NotreDameEval.mat"

    if file_name == "notre_dame":
        pass
    elif file_name == "mt_rushmore":
        image1_file = "../data/MountRushmore/Mount_Rushmore1.jpg"
        image2_file = "../data/MountRushmore/Mount_Rushmore2.jpg"
        eval_file = "../data/MountRushmore/MountRushmoreEval.mat"
    elif file_name == "e_gaudi":
        image1_file = "../data/EpiscopalGaudi/EGaudi_1.jpg"
        image2_file = "../data/EpiscopalGaudi/EGaudi_2.jpg"
        eval_file = "../data/EpiscopalGaudi/EGaudiEval.mat"

    image1 = img_as_float32(io.imread(image1_file))
    image2 = img_as_float32(io.imread(image2_file))

    return image1, image2, eval_file
Esempio n. 14
0
 def __getitem__(self, indx):
     with h5.File(self.file5, 'r') as file_5:
         images = file_5[self.patch_num[indx]]
         H, W, Ch = images.shape
         c = Ch // 2
         Y = np.array(images[:, :, :c])
         X = np.array(images[:, :, c:])
     X = img_as_float32(X)
     Y = img_as_float32(Y)
     Y = torch.tensor(Y)
     Y = Y.permute(2, 0, 1)
     X = torch.tensor(X)
     X = X.permute(2, 0, 1)
     return Y, X
Esempio n. 15
0
def coarse_to_fine(I0, I1, solver, downscale=2, nlevel=10, min_size=16):
    """Generic coarse to fine solver.

    Parameters
    ----------
    I0 : ~numpy.ndarray
        The first gray scale image of the sequence.
    I1 : ~numpy.ndarray
        The second gray scale image of the sequence.
    solver : callable
        The solver applyed at each pyramid level.
    downscale : float
        The pyramid downscale factor.
    nlevel : int
        The maximum number of pyramid levels.
    min_size : int
        The minimum size for any dimension of the pyramid levels.

    Returns
    -------
    u, v : tuple[~numpy.ndarray]
        The horizontal and vertical components of the estimated
        optical flow.

    """

    if (I0.ndim != 2) or (I1.ndim != 2):
        raise ValueError("Only grayscale images are supported.")

    if I0.shape != I1.shape:
        raise ValueError("Input images should have the same shape")

    pyramid = list(
        zip(
            get_pyramid(skimage.img_as_float32(I0), downscale, nlevel,
                        min_size),
            get_pyramid(skimage.img_as_float32(I1), downscale, nlevel,
                        min_size)))

    u = np.zeros_like(pyramid[0][0])
    v = np.zeros_like(u)

    u, v = solver(pyramid[0][0], pyramid[0][1], u, v)

    for J0, J1 in pyramid[1:]:
        u, v = resize_flow(u, v, J0.shape)
        u, v = solver(J0, J1, u, v)

    return u, v
Esempio n. 16
0
def read_video(name, frame_shape):
    """
    Read video which can be:
      - an image of concatenated frames
      - '.mp4' and'.gif'
      - folder with videos
    """

    if os.path.isdir(name):
        frames = sorted(os.listdir(name))
        num_frames = len(frames)

        video_array = []
        for idx in range(num_frames):
            try:
                tmpim = img_as_float32(
                    io.imread(os.path.join(name, frames[idx])))
                video_array.append(tmpim)
            except:
                print('error while loading image.')

        video_array = np.array(video_array)
    elif name.lower().endswith('.png') or name.lower().endswith('.jpg'):
        image = io.imread(name)

        if len(image.shape) == 2 or image.shape[2] == 1:
            image = gray2rgb(image)

        if image.shape[2] == 4:
            image = image[..., :3]

        image = img_as_float32(image)

        video_array = np.moveaxis(image, 1, 0)

        video_array = video_array.reshape((-1, ) + frame_shape)
        video_array = np.moveaxis(video_array, 1, 2)
    elif name.lower().endswith('.gif') or name.lower().endswith(
            '.mp4') or name.lower().endswith('.mov'):
        video = np.array(mimread(name))
        if len(video.shape) == 3:
            video = np.array([gray2rgb(frame) for frame in video])
        if video.shape[-1] == 4:
            video = video[..., :3]
        video_array = img_as_float32(video)
    else:
        raise Exception("Unknown file extensions  %s" % name)

    return video_array
Esempio n. 17
0
def read_video(name, frame_shape):
    """
    Read video which can be:
      - an image of concatenated frames
      - '.mp4' and'.gif'
      - folder with videos
    """

    if os.path.isdir(name):
        frames = sorted(os.listdir(name))
        num_frames = len(frames)
        video_array = [img_as_float32(io.imread(os.path.join(name, frames[idx]))) for idx in range(num_frames)]
        if frame_shape is not None:
            video_array = np.array([resize(frame, frame_shape) for frame in video_array])
    elif name.lower().endswith('.png') or name.lower().endswith('.jpg'):
        image = io.imread(name)

        if frame_shape is None:
            raise ValueError('Frame shape can not be None for stacked png format.')

        frame_shape = tuple(frame_shape)

        if len(image.shape) == 2 or image.shape[2] == 1:
            image = gray2rgb(image)

        if image.shape[2] == 4:
            image = image[..., :3]

        image = img_as_float32(image)

        video_array = np.moveaxis(image, 1, 0)

        video_array = video_array.reshape((-1,) + frame_shape + (3, ))
        video_array = np.moveaxis(video_array, 1, 2)
    elif name.lower().endswith('.gif') or name.lower().endswith('.mp4') or name.lower().endswith('.mov'):
        video = mimread(name)
        if len(video[0].shape) == 2:
            video = [gray2rgb(frame) for frame in video]
        if frame_shape is not None:
            video = np.array([resize(frame, frame_shape) for frame in video])
        video = np.array(video)
        if video.shape[-1] == 4:
            video = video[..., :3]
        video_array = img_as_float32(video)
    else:
        raise Exception("Unknown file extensions  %s" % name)

    return video_array
Esempio n. 18
0
    def __getitem__(self, idx):
        if self.is_train and self.id_sampling:
            name = self.videos[idx]
            path = np.random.choice(glob.glob(os.path.join(self.root_dir, name + '*.mp4')))
        else:
            name = self.videos[idx]
            path = os.path.join(self.root_dir, name)

        video_name = os.path.basename(path)

        if self.is_train and os.path.isdir(path):
            if self.tha_dataset:
                files = os.listdir(path)
                png_files = [x for x in files if x[-9:] == "_rest.png"]
                count = len(png_files)
                idx = random.randrange(count)
                rest_file = os.path.join(path, ("%08d_rest.png" % idx))
                posed_file = os.path.join(path, ("%08d_posed.png" % idx))
                video_array = [img_as_float32(io.imread(rest_file))[:,:,:3], img_as_float32(io.imread(posed_file))[:,:,:3]]
            else:
                frames = os.listdir(path)
                num_frames = len(frames)
                frame_idx = np.sort(np.random.choice(num_frames, replace=True, size=2))
                video_array = [img_as_float32(io.imread(os.path.join(path, frames[idx]))) for idx in frame_idx]
        else:
            video_array = read_video(path, frame_shape=self.frame_shape)
            num_frames = len(video_array)
            frame_idx = np.sort(np.random.choice(num_frames, replace=True, size=2)) if self.is_train else range(
                num_frames)
            video_array = video_array[frame_idx]

        if self.transform is not None:
            video_array = self.transform(video_array)

        out = {}
        if self.is_train:
            source = np.array(video_array[0], dtype='float32')
            driving = np.array(video_array[1], dtype='float32')

            out['driving'] = driving.transpose((2, 0, 1))
            out['source'] = source.transpose((2, 0, 1))
        else:
            video = np.array(video_array, dtype='float32')
            out['video'] = video.transpose((3, 0, 1, 2))

        out['name'] = video_name

        return out
Esempio n. 19
0
def load_image(path):
    """
        Loads and transforms an image as float32.
        Resize image to dimensions INPUT_WIDTH x INPUT_HEIGHT.
        Rotate image by 0, 90, 180 and 270 degrees.

    Args:
        path: path to the image

    Returns:
        a list of transforms of the given image
    """
    image = skimage.io.imread(path)
    image = skimage.img_as_float32(image)
    resized_image = skimage.transform.resize(image, (INPUT_WIDTH, INPUT_HEIGHT),
                                             anti_aliasing=True, mode='constant')
    # only keep 3 channels
    if resized_image.shape[2] != 3:
        resized_image = resized_image[:, :, 0:3]

    # extend data set by transforming data
    rotate_angles = [0, 90, 180, 270]
    images = [skimage.transform.rotate(resized_image, angle) for angle in rotate_angles]

    return images
def _compute_features_gray(img,
                           intensity=True,
                           edges=True,
                           texture=True,
                           sigma_min=0.5,
                           sigma_max=16):
    """Features for a single channel image. ``img`` can be 2d or 3d.
    """
    # computations are faster as float32
    img = img_as_float32(img)
    sigmas = np.logspace(
        np.log2(sigma_min),
        np.log2(sigma_max),
        num=int(np.log2(sigma_max) - np.log2(sigma_min) + 1),
        base=2,
        endpoint=True,
    )
    n_sigmas = len(sigmas)
    all_results = [
        _features_sigma(img,
                        sigma,
                        intensity=intensity,
                        edges=edges,
                        texture=texture) for sigma in sigmas
    ]
    return list(itertools.chain.from_iterable(all_results))
Esempio n. 21
0
def geneTrainNpy(image_path, mask_path):
    image_name_training = glob.glob(os.path.join(image_path, "*.mat"))
    mask_name_training = glob.glob(os.path.join(mask_path, "*.mat"))
    #    image_name_validation= glob.glob(os.path.join(image_ptraining,"*.mat"))
    #    mask_name_validation  = glob.glob(os.path.join(mask_training,"*.mat"))

    image_arr = []
    mask_arr = []
    for index, item in enumerate(image_name_training):
        #img = cv2.imread(item, -1)
        img = scio.loadmat(item)
        image = (img['data'])

        image = (image - np.min(image)) / (np.max(image) - np.min(image))
        #image = img_as_float32(image)

        #img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE), interpolation=cv2.INTER_CUBIC)
        mask = scio.loadmat(mask_name_training[index])
        masks = (mask['data'])
        masks = img_as_float32(masks)
        #mask = cv2.resize(mask, (IMAGE_SIZE, IMAGE_SIZE), interpolation=cv2.INTER_CUBIC)
        masks = masks[:, :, np.newaxis]

        image_arr.append(image)
        mask_arr.append(masks)
    image_arr = np.array(image_arr)
    mask_arr = np.array(mask_arr)
    return image_arr, mask_arr
def main(args):
    img_channel = 3
    img_names = glob.glob('{}/*.jpg'.format(args.input))
    img_num = len(img_names)
    print('image num: {}'.format(img_num))

    height_sum = 0
    width_sum = 0
    pixel_sum = 0
    channel_sum = np.zeros(img_channel)
    channel_sum_squared = np.zeros(img_channel)

    for img_name in img_names:
        img = io.imread(img_name)
        img = skimage.img_as_float32(img)
        height_sum += img.shape[0]
        width_sum += img.shape[1]
        pixel_sum += img.size / img_channel
        channel_sum += np.sum(img, axis=(0, 1))
        channel_sum_squared += np.sum(np.square(img), axis=(0, 1))

    mean = channel_sum / pixel_sum
    std = np.sqrt(channel_sum_squared / pixel_sum - np.square(mean))

    height_avg = height_sum / img_num
    width_avg = width_sum / img_num

    print('height: {}, width: {}'.format(height_avg, width_avg))
    print('mean: {}'.format(mean))
    print('std: {}'.format(std))
Esempio n. 23
0
 def __getitem__(self, index):
     image = skimage.img_as_float32(imread(self.paths[index]))
     image = crop_largest_component(image,
                                    image[:, :, 3] > 0,
                                    shape=config.SHAPE_REND_SHAPE)
     image = torch.from_numpy(image.transpose((2, 0, 1)))
     return image
def _mutiscale_basic_features_singlechannel(
    img, intensity=True, edges=True, texture=True, sigma_min=0.5, sigma_max=16
):
    """Features for a single channel nd image.

    Parameters
    ----------
    """
    # computations are faster as float32
    img = np.ascontiguousarray(img_as_float32(img))
    sigmas = np.logspace(
        np.log2(sigma_min),
        np.log2(sigma_max),
        num=int(np.log2(sigma_max) - np.log2(sigma_min) + 1),
        base=2,
        endpoint=True,
    )
    all_filtered = Parallel(n_jobs=-1, prefer="threads")(
        delayed(filters.gaussian)(img, sigma) for sigma in sigmas
    )
    features = []
    if intensity:
        features += all_filtered
    if edges:
        all_edges = Parallel(n_jobs=-1, prefer="threads")(
            delayed(filters.sobel)(filtered_img) for filtered_img in all_filtered
        )
        features += all_edges
    if texture:
        all_texture = Parallel(n_jobs=-1, prefer="threads")(
            delayed(_texture_filter)(filtered_img) for filtered_img in all_filtered
        )
        features += itertools.chain.from_iterable(all_texture)
    return features
Esempio n. 25
0
def preprocess_image2d(args, path, arr, i):
    img = io.imread(path)

    # Background removal
    if args.t is not None:
        # verbose_print(args, f"Performing background removal with threshold {args.t}")
        img = remove_background(img, args.t)

    # Histogram equalization
    if args.k is not None:
        if args.k == 0:
            # verbose_print(args, f"Performing histogram equalization with default kernel size")
            kernel_size = None
        else:
            # verbose_print(args, f"Performing histogram equalization with kernel size {args.k}")
            kernel_size = args.k
        img = equalize_adapthist(img, kernel_size=kernel_size)

    # Convert to float (can't normalize based on single slice)
    if args.float:
        img = img_as_float32(img)
        # verbose_print(args, f"Converted to normalized float32: min {img.min():.3f}, max {img.max():.3f}")

    # Denoising
    if args.s is not None:
        # verbose_print(args, f"Performing noise removal with sigma {args.s} and wavelet {args.w}")
        img = denoise2d(img, args.s, args.w)

    arr[i] = img
Esempio n. 26
0
def whiten(img, sigma):
    img = skimage.img_as_float32(img)
    if sigma == 0:
        output = scipy.ndimage.convolve(img, _laplace_kernel)
    else:
        output = scipy.ndimage.gaussian_laplace(img, sigma)
    return output
Esempio n. 27
0
    def __getitem__(self, _):
        image, annot, _ = load_train_image_and_annot(self.dataset_dir,
                                                     self.train_annot_dir)
        tile_pad = (self.in_w - self.out_w) // 2

        # ensures each pixel is sampled with equal chance
        im_pad_w = self.out_w + tile_pad
        padded_w = image.shape[1] + (im_pad_w * 2)
        padded_h = image.shape[0] + (im_pad_w * 2)
        padded_im = im_utils.pad(image, im_pad_w)

        # This speeds up the padding.
        annot = annot[:, :, :2]
        padded_annot = im_utils.pad(annot, im_pad_w)
        right_lim = padded_w - self.in_w
        bottom_lim = padded_h - self.in_w

        # TODO:
        # Images with less annoations will still give the same number of
        # tiles in the training procedure as images with more annotation.
        # Further empirical investigation into effects of
        # instance selection required are required.
        while True:
            x_in = math.floor(random.random() * right_lim)
            y_in = math.floor(random.random() * bottom_lim)
            annot_tile = padded_annot[y_in:y_in + self.in_w,
                                      x_in:x_in + self.in_w]
            if np.sum(annot_tile) > 0:
                break

        im_tile = padded_im[y_in:y_in + self.in_w, x_in:x_in + self.in_w]

        assert annot_tile.shape == (self.in_w, self.in_w,
                                    2), (f" shape is {annot_tile.shape}")

        assert im_tile.shape == (self.in_w, self.in_w,
                                 3), (f" shape is {im_tile.shape}")

        im_tile = img_as_float32(im_tile)
        im_tile = im_utils.normalize_tile(im_tile)
        im_tile, annot_tile = self.augmentor.transform(im_tile, annot_tile)
        im_tile = im_utils.normalize_tile(im_tile)

        foreground = np.array(annot_tile)[:, :, 0]
        background = np.array(annot_tile)[:, :, 1]

        # Annotion is cropped post augmentation to ensure
        # elastic grid doesn't remove the edges.
        foreground = foreground[tile_pad:-tile_pad, tile_pad:-tile_pad]
        background = background[tile_pad:-tile_pad, tile_pad:-tile_pad]
        # mask specified pixels of annotation which are defined
        mask = foreground + background
        mask = mask.astype(np.float32)
        mask = torch.from_numpy(mask)
        foreground = foreground.astype(np.int64)
        foreground = torch.from_numpy(foreground)
        im_tile = im_tile.astype(np.float32)
        im_tile = np.moveaxis(im_tile, -1, 0)
        im_tile = torch.from_numpy(im_tile)
        return im_tile, foreground, mask
    def __getitem__(self, index):
        hr_name = self.hr_image_names[index]
        name_root = hr_name.split(self.suffix_hr)[0]
        lr_name = osp.join(self.path, name_root + self.suffix_lr)
        hr_name = osp.join(self.path, name_root + self.suffix_hr)

        hr_image = skimage.img_as_float32(cv2.imread(hr_name))
        lr_image = skimage.img_as_float32(cv2.imread(lr_name))
        bic_image = cv2.resize(lr_image, hr_image.shape[:2][::-1],
                               cv2.INTER_CUBIC)

        item = [torch.from_numpy(lr_image.transpose((2, 0, 1))).float(),
                torch.from_numpy(bic_image.transpose((2, 0, 1))).float()],\
               [torch.from_numpy(hr_image.transpose((2, 0, 1))).float()]

        return item
Esempio n. 29
0
def geneTrainNpy2(test_image, test_mask):
    image_name_training = glob.glob(os.path.join(test_image, "*.mat"))
    mask_name_training = glob.glob(os.path.join(test_mask, "*.png"))
    #    image_name_validation= glob.glob(os.path.join(image_ptraining,"*.mat"))
    #    mask_name_validation  = glob.glob(os.path.join(mask_training,"*.mat"))

    image_arr = []
    mask_arr = []
    for index, item in enumerate(image_name_training):
        #img = cv2.imread(item, -1)
        img = scio.loadmat(item)
        image = (img['data']) / 255

        #        mask = scio.loadmat(mask_name_training[index])
        #        masks=(mask['data'])
        #        masks = img_as_float32(masks)
        #        masks = masks[:, :,np.newaxis]

        masks = cv2.imdecode(
            np.fromfile(mask_name_training[index], dtype=np.uint8),
            cv2.IMREAD_LOAD_GDAL)
        masks = img_as_float32(masks)
        masks = masks[:, :, np.newaxis]

        image_arr.append(image)
        mask_arr.append(masks)
    image_arr = np.array(image_arr)
    mask_arr = np.array(mask_arr)
    return image_arr, mask_arr
Esempio n. 30
0
def preprocess_image3d(args, img):
    # Background removal
    if args.t is not None:
        verbose_print(args, f"Performing background removal with threshold {args.t}")
        img = remove_background(img, args.t)

    # Histogram equalization
    if args.k is not None:
        if args.k == 0:
            verbose_print(args, f"Performing histogram equalization with default kernel size")
            kernel_size = None
        else:
            verbose_print(args, f"Performing histogram equalization with kernel size {args.k}")
            kernel_size = args.k
        img = clahe(img, kernel_size=kernel_size)

    # Normalize and convert to float
    if args.float:
        img = rescale_intensity(img_as_float32(img))
        verbose_print(args, f"Converted to normalized float32: min {img.min():.3f}, max {img.max():.3f}")

    # Denoising
    if args.s is not None:
        verbose_print(args, f"Performing noise removal with sigma {args.s} and wavelet {args.w}")
        img = denoise(img, args.s, args.w)

    # Convert to Zarr
    verbose_print(args, f"Saving result to {args.zarr}")
    arr = io.new_zarr(args.zarr, shape=img.shape, dtype=img.dtype, chunks=tuple(args.c))
    arr[:] = img

    return img
Esempio n. 31
0
def test_signed_scaling_float32():
    x = np.array([-128,  127], dtype=np.int8)
    y = img_as_float32(x)
    assert_equal(y.max(), 1)