def data_generator_landmarks(dataCSV,
                             selection,
                             batchSize,
                             pimgSize=256,
                             augment=True):
    aug = Augmenter()
    numSamples = len(dataCSV)
    pathImgs = np.array(dataCSV['img'])
    pathKps = np.array(dataCSV['keypoints'])
    idxRange = np.array(range(numSamples))
    while True:
        rndIdx = np.random.permutation(idxRange)[:batchSize]
        dataX = None
        dataY = None
        for ii, iidx in enumerate(rndIdx):
            pimg = pathImgs[iidx]
            pkp = pathKps[iidx]
            timg = read_img(pimg, pimgSize).astype(np.uint8)
            tkp = read_keypoints_synth(pkp, selection) * 256
            timg, tkp = utils.fixbb(timg, tkp)
            timg = utils.background_blend(timg)
            tkp = tkp / 256
            if np.random.rand() < 0.5:  # we always augment flips
                timg, tkp = utils.fliplr(timg, tkp)
            tkp = (tkp * 256).astype(np.int32)
            timg = utils.to_uint8(timg)
            if dataX is None:
                dataX = np.zeros([batchSize] + list(timg.shape),
                                 dtype=np.uint8)
                dataY = np.zeros([batchSize] + list(tkp.shape), dtype=np.int32)
            dataX[ii] = timg
            dataY[ii] = tkp
        # augment and flatten dataY
        if augment:
            keypoints = [
                ia.KeypointsOnImage.from_coords_array(ptsi, (256, 256))
                for ptsi in dataY
            ]
            seq_geom = aug.seq_geom.to_deterministic()
            seq_color = aug.seq_color.to_deterministic()
            dataX, keypoints = list(
                seq_geom.augment_batches(batches=[dataX, keypoints],
                                         background=False))
            dataX, = list(
                seq_color.augment_batches(batches=[dataX], background=False))
            dataX = dataX.astype(np.float32) / 127.5 - 1.0
            dataY = np.array([kp.get_coords_array() for kp in keypoints])
        else:
            dataX = dataX.astype(np.float32) / 127.5 - 1.0
        yield dataX, dataY.reshape((batchSize, -1)).astype(np.float32) / 256
def data_generator_segmentation(dataCSV,
                                numCls,
                                batchSize=4,
                                pimgSize=256,
                                augment=False):
    aug = Augmenter()
    numSamples = len(dataCSV)
    pathImgs = np.array(dataCSV['img'])
    pathMsks = np.array(dataCSV['mask'])
    idxRange = np.array(range(numSamples))
    while True:
        rndIdx = np.random.permutation(idxRange)[:batchSize]
        dataX = None
        dataY = None
        for ii, iidx in enumerate(rndIdx):
            pimg = pathImgs[iidx]
            pmsk = pathMsks[iidx]
            timg = read_img(pimg, pimgSize)
            timg = utils.background_blend(timg)
            timg = utils.to_uint8(timg)
            tmsk = read_msk(pmsk, pimgSize)[..., :3]
            if dataX is None:
                dataX = np.zeros([batchSize] + list(timg.shape),
                                 dtype=np.uint8)
                dataY = np.zeros([batchSize] + list(tmsk.shape),
                                 dtype=np.uint8)
            dataX[ii] = timg
            dataY[ii] = tmsk
        # augment and flatten dataY
        if augment:
            seq_geom = aug.seq_geom.to_deterministic()
            seq_color = aug.seq_color.to_deterministic()
            #seq_geom.augment_batches()
            dataX, dataY = list(
                seq_geom.augment_batches(batches=[dataX, dataY],
                                         background=False))
            dataX, = list(
                seq_color.augment_batches(batches=[dataX], background=False))
            dataY = labels_from_colored(dataY)
            dataY = np_utils.to_categorical(dataY, numCls).reshape(
                batchSize, pimgSize * pimgSize, numCls)
            dataX = dataX.astype(np.float32) / 127.5 - 1.0
        else:
            dataY = labels_from_colored(dataY)
            dataY = np_utils.to_categorical(dataY, numCls).reshape(
                batchSize, pimgSize * pimgSize, numCls)
            dataX = dataX.astype(np.float32) / 127.5 - 1.0
        yield dataX, dataY
def data_generator_landmarks(dataCSV, selection, batchSize, pimgSize=256, augment=True):
    aug = Augmenter()
    numSamples = len(dataCSV)
    pathImgs = np.array(dataCSV['img'])
    pathKps = np.array(dataCSV['keypoints'])
    idxRange = np.array(range(numSamples))
    while True:
        rndIdx = np.random.permutation(idxRange)[:batchSize]
        dataX = None
        dataY = None
        for ii, iidx in enumerate(rndIdx):
            pimg = pathImgs[iidx]
            pkp = pathKps[iidx]
            timg = read_img(pimg, pimgSize).astype(np.uint8)
            tkp = read_keypoints_synth(pkp, selection)*256
            timg, tkp = utils.fixbb(timg, tkp)
            timg = utils.background_blend(timg)
            tkp = tkp/256
            if np.random.rand() < 0.5: # we always augment flips
                timg, tkp = utils.fliplr(timg, tkp)
            tkp = (tkp*256).astype(np.int32)
            timg = utils.to_uint8(timg)
            if dataX is None:
                dataX = np.zeros([batchSize] + list(timg.shape), dtype=np.uint8)
                dataY = np.zeros([batchSize] + list(tkp.shape), dtype=np.int32)
            dataX[ii] = timg
            dataY[ii] = tkp
        # augment and flatten dataY
        if augment:
            keypoints = [ia.KeypointsOnImage.from_coords_array(ptsi, (256, 256)) for ptsi in dataY]
            seq_geom = aug.seq_geom.to_deterministic()
            seq_color = aug.seq_color.to_deterministic()
            dataX, keypoints = list(seq_geom.augment_batches(batches=[dataX, keypoints], background=False))
            dataX, = list(seq_color.augment_batches(batches=[dataX], background=False))
            dataX = dataX.astype(np.float32) / 127.5 - 1.0
            dataY = np.array([kp.get_coords_array() for kp in keypoints])
        else:
            dataX = dataX.astype(np.float32) / 127.5 - 1.0
        yield dataX, dataY.reshape((batchSize,-1)).astype(np.float32)/256
def data_generator_bgfg(dataCSV, batchSize=4, pimgSize=256, augment=False):
    numCls=2
    aug = Augmenter()
    numSamples = len(dataCSV)
    pathImgs = np.array(dataCSV['img'])
    pathMsks = np.array(dataCSV['mask'])
    idxRange = np.array(range(numSamples))
    while True:
        rndIdx = np.random.permutation(idxRange)[:batchSize]
        dataX = None
        dataY = None
        for ii, iidx in enumerate(rndIdx):
            pimg = pathImgs[iidx]
            pmsk = pathMsks[iidx]
            timg = read_img(pimg, pimgSize)
            timg = utils.background_blend(timg)
            timg = utils.to_uint8(timg)
            tmsk = read_msk(pmsk, pimgSize)[...,:3]
            if dataX is None:
                dataX = np.zeros([batchSize] + list(timg.shape), dtype=np.uint8)
                dataY = np.zeros([batchSize] + list(tmsk.shape), dtype=np.uint8)
            dataX[ii] = timg
            dataY[ii] = tmsk
        # augment and flatten dataY
        if augment:
            seq_geom = aug.seq_geom.to_deterministic()
            seq_color = aug.seq_color.to_deterministic()
            #seq_geom.augment_batches()
            dataX, dataY = list(seq_geom.augment_batches(batches=[dataX, dataY], background=False))
            dataX, = list(seq_color.augment_batches(batches=[dataX], background=False))
            dataY = labels_bg_fg(dataY)
            dataY = np_utils.to_categorical(dataY, numCls).reshape(batchSize, pimgSize*pimgSize, numCls)
            dataX = dataX.astype(np.float32)/127.5 - 1.0
        else:
            dataY = labels_bg_fg(dataY)
            dataY = np_utils.to_categorical(dataY, numCls).reshape(batchSize, pimgSize * pimgSize, numCls)
            dataX = dataX.astype(np.float32) / 127.5 - 1.0
        yield dataX, dataY
Beispiel #5
0
    for row, col in seed_points:
        segmented[row, col] = True
        neighbourhood(im, segmented, row, col, T)

    return segmented
    ### END YOUR CODE HERE ###


if __name__ == "__main__":
    # DO NOT CHANGE
    im = utils.read_image("defective-weld.png")

    seed_points = [  # (row, column)
        [254, 138],  # Seed point 1
        [253, 296],  # Seed point 2
        [233, 436],  # Seed point 3
        [232, 417],  # Seed point 4
    ]
    intensity_threshold = 50
    segmented_image = region_growing(im, seed_points, intensity_threshold)

    assert im.shape == segmented_image.shape, \
        "Expected image shape ({}) to be same as thresholded image shape ({})".format(
            im.shape, segmented_image.shape)
    assert segmented_image.dtype == np.bool, \
        "Expected thresholded image dtype to be np.bool. Was: {}".format(
            segmented_image.dtype)

    segmented_image = utils.to_uint8(segmented_image)
    utils.save_im("defective-weld-segmented.png", segmented_image)
Beispiel #6
0
        args:
            im: np.ndarray of shape (H, W) with boolean values (dtype=np.bool)
        return:
            (np.ndarray) of shape (H, W). dtype=np.bool
    """
    ### START YOUR CODE HERE ### (You can change anything inside this block)
    # You can also define other helper functions
    structuring_element = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]],
                                   dtype=bool)
    eroded_im = binary_erosion(im, selem=structuring_element)
    boundary = np.bitwise_xor(im, eroded_im)
    return boundary
    ### END YOUR CODE HERE ###


if __name__ == "__main__":
    im = utils.read_image("lincoln.png")
    binary_image = (im != 0)
    boundary = extract_boundary(binary_image)

    assert im.shape == boundary.shape, \
        "Expected image shape ({}) to be same as resulting image shape ({})".format(
            im.shape, boundary.shape)
    assert boundary.dtype == np.bool, \
        "Expected resulting image dtype to be np.bool. Was: {}".format(
            boundary.dtype)

    boundary = utils.to_uint8(boundary)
    utils.save_im("lincoln-boundary.png", boundary)
Beispiel #7
0
    Returns:
        im: np.array of shape [H, W]
    """
    laplacian = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]])
    ### START YOUR CODE HERE ### (You can change anything inside this block)

    # Convolve img with laplacian kernel
    convolved_im = convolve_im(im, laplacian, verbose=True)
    # Use equation 6.
    resulting_im = np.add(im, (im * convolved_im))

    # Limit values between [0,1]
    im = np.add(resulting_im, resulting_im.min())
    im = np.multiply(im, 1 / im.max())
    ### END YOUR CODE HERE ###
    return im


if __name__ == "__main__":
    # DO NOT CHANGE
    im = skimage.data.moon()
    im = utils.uint8_to_float(im)
    sharpen_im = sharpen(im)

    sharpen_im = utils.to_uint8(sharpen_im)
    im = utils.to_uint8(im)
    # Concatenate the image, such that we get
    # the original on the left side, and the sharpened on the right side
    im = np.concatenate((im, sharpen_im), axis=1)
    utils.save_im("moon_sharpened.png", im)
Beispiel #8
0
        A function that computes the distance to the closest boundary pixel.

        args:
            im: np.ndarray of shape (H, W) with boolean values (dtype=np.bool)
        return:
            (np.ndarray) of shape (H, W). dtype=np.int32
    """
    # START YOUR CODE HERE ### (You can change anything inside this block)
    # You can also define other helper functions
    assert im.dtype == np.bool
    structuring_element = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]],
                                   dtype=bool)
    result = im.astype(np.int32)
    return result
    ### END YOUR CODE HERE ###


if __name__ == "__main__":
    im = utils.read_image("noisy.png")
    binary_image = (im != 0)
    noise_free_image = remove_noise(binary_image)
    distance = distance_transform(noise_free_image)

    assert im.shape == distance.shape, "Expected image shape ({}) to be same as resulting image shape ({})".format(
        im.shape, distance.shape)
    assert distance.dtype == np.int32, "Expected resulting image dtype to be np.int32. Was: {}".format(
        distance.dtype)

    distance = utils.to_uint8(distance)
    utils.save_im("noisy-distance.png", distance)
            (np.ndarray) of shape (H, W). dtype=np.bool
    """
    ### START YOUR CODE HERE ### (You can change anything inside this block)
    # You can also define other helper functions
	
    sh = disk(10)
    binary_closing(im, selem=sh, out = im)
    binary_opening(im, selem=sh, out = im)
#    im = binary_erosion(im, selem=sh) 
#    im = binary_dilation(im, selem=sh)
    return im
    ### END YOUR CODE HERE ### 


if __name__ == "__main__":
    # DO NOT CHANGE
    im = utils.read_image("noisy.png")
    binary_image = (im != 0)
    noise_free_image = remove_noise(binary_image)

    assert im.shape == noise_free_image.shape, \
        "Expected image shape ({}) to be same as resulting image shape ({})".format(
            im.shape, noise_free_image.shape)
    assert noise_free_image.dtype == np.bool, \
        "Expected resulting image dtype to be np.bool. Was: {}".format(
            noise_free_image.dtype)

    noise_free_image = utils.to_uint8(noise_free_image)
    utils.save_im("noisy-filtered.png", noise_free_image)

def imread(filename, as_gray = False):
    image = skio.imread(filename, as_gray = as_gray)
    if image.dtype == np.float64 :
        image = utils.to_uint8(image)
    return image
Beispiel #11
0
            (np.ndarray) of shape (H, W). dtype=np.bool
    """
    # START YOUR CODE HERE ### (You can change anything inside this block)
    # You can also define other helper functions
    structuring_element = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]],
                                   dtype=bool)
    result = im.copy()
    return result
    ### END YOUR CODE HERE ###


if __name__ == "__main__":
    im = utils.read_image("balls-with-reflections.png")
    binary_image = im != 0
    starting_points = [  # (row, column)
        [51, 64], [44, 180], [35, 365], [156, 94], [141, 264], [138, 467],
        [198, 180], [229, 413], [294, 103], [302, 230], [368, 388], [352, 489],
        [454, 57], [457, 236], [469, 400], [489, 506]
    ]
    num_iterations = 30

    result = fill_holes(binary_image, starting_points, num_iterations)

    assert im.shape == result.shape, "Expected image shape ({}) to be same as resulting image shape ({})".format(
        im.shape, result.shape)
    assert result.dtype == np.bool, "Expected resulting image dtype to be np.bool. Was: {}".format(
        result.dtype)

    result = utils.to_uint8(result)
    utils.save_im("balls-with-reflections-filled.png", result)
import utils
import matplotlib.pyplot as plt
import utils

if __name__ == '__main__':
    #filename = '../images/color/rgb_cube.png'
    #filename = '../images/color/fichas.jpg'
    filename = '/home/jsaavedr/Documents/Docencia/2020/CC5508/tareas/tarea3_2020/tarea3/im3.jpg'
    image = pai_io.imread(filename)
    print('shape: {}'.format(image.shape))
    im_red = image[:, :, 0]
    im_green = image[:, :, 1]
    im_blue = image[:, :, 2]
    #yellow
    im_yellow = im_red.astype(np.float64) + im_green.astype(
        np.float64) - im_blue.astype(np.float64)
    im_yellow = utils.to_uint8(im_yellow / (255.0 + 255.0))
    ##showing image
    fig, ax = plt.subplots(2, 3)
    ax[0, 1].imshow(image)
    ax[0, 1].set_title('image')
    ax[1, 0].imshow(im_red, cmap='gray')
    ax[1, 0].set_title('Red')
    ax[1, 1].imshow(im_green, cmap='gray')
    ax[1, 1].set_title('Green')
    ax[1, 2].imshow(im_yellow, cmap='gray')
    ax[1, 2].set_title('Yellow')
    for i in range(2):
        for j in range(3):
            ax[i, j].set_axis_off()
    plt.show()