def crop_image(x, target_height=227, target_width=227):

    if isinstance(x, str):
        image = skimage.img_as_float(skimage.io.imread(x)).astype(np.float32)
    else:
        image = skimage.img_as_float(x).astype(np.float32)

    if len(image.shape) == 2:
        image = np.tile(image[:,:,None], 3)
    elif len(image.shape) == 4:
        image = image[:,:,:,0]

    height, width, rgb = image.shape
    if width == height:
        resized_image = cv2.resize(image, (target_height,target_width))

    elif height < width:
        resized_image = cv2.resize(image, (int(width * float(target_height)/height), target_width))
        cropping_length = int((resized_image.shape[1] - target_height) / 2)
        resized_image = resized_image[:,cropping_length:resized_image.shape[1] - cropping_length]

    else:
        resized_image = cv2.resize(image, (target_height, int(height * float(target_width) / width)))
        cropping_length = int((resized_image.shape[0] - target_width) / 2)
        resized_image = resized_image[cropping_length:resized_image.shape[0] - cropping_length,:]

    return cv2.resize(resized_image, (target_height, target_width))
def transform_image(image, params=[], tags=[]):
    image = apply_tags(image, tags)
    image = extract_roi(image, params)
    for k,v in params:
        if k=='saturation':
            saturation = float(v)
            image = scale_saturation(image, saturation)
        elif k=='gamma':
            gamma = float(v)
            image = np.power(img_as_float(image), gamma)
        elif k=='brightness':
            b = float(v)
            image = (img_as_float(image) * b).clip(0.,1.)
        elif k=='width':
            scale = float(v) / image.shape[1]
            image = rescale(image, scale)
        elif k=='maxwidth':
            w = float(v)
            if image.shape[1] > w:
                scale = w / image.shape[1]
                image = rescale(image, scale)
        elif k=='height':
            scale = float(v) / image.shape[0]
            image = rescale(image, scale)
        elif k=='maxheight':
            h = float(v)
            if image.shape[0] > h:
                scale = h / image.shape[0]
                image = rescale(image, scale)
    return image
def readTrainingFragment(datapath, fragList, imgSize=(1,224,224), meanImage=[], classNum=10):
    ch, ih, iw = imgSize
    fragLen = len(fragList)
    if ch == 1:
        X = np.zeros((fragLen, 1, ih, iw))
        Y = np.zeros((fragLen), dtype=int)
        idx = -1
        print('reading data')
        for f in fragList:
            idx += 1
            # print(f)
            label = np.int(f[0])
            img = skimage.img_as_float(skio.imread(datapath+f) )
#            img -= meanImage
            X[idx, 0, ...] = img
            Y[idx] = label
    elif ch == 3:
        X = np.zeros((fragLen, 3, ih, iw))
        Y = np.zeros((fragLen), dtype=int)
        idx = -1
        print('reading data')
        for f in fragList:
            idx += 1
            label = np.int(f[0])
            img = skimage.img_as_float(skio.imread(datapath+f) )
            img = img.swapaxes(1, 2)
            img = img.swapaxes(0, 1)
#            img -= meanImage
            X[idx, ...] = img
            Y[idx] = label
    X -= np.tile(meanImage, [fragLen, 1, 1, 1])
    Y = np_utils.to_categorical(Y, classNum)
    return X, Y
def main():
    img = img_as_float(imread("HJoceanSmall.png"))
    img_seam_v = img_as_float(imread("HJoceanSmall.png"))
    img_transformed_v = img_as_float(imread("HJoceanSmall.png"))
    iterations = 20
    img_seam_v, img_transformed_v = seam_carve(iterations, img_seam_v, img_transformed_v)

    figure()

    subplot(221)
    imshow(img)
    title("1. Original")

    subplot(222)
    imshow(img_seam_v)
    title("2. Seam carved vertical")

    # Transposed Image

    img_seam_hv = img_transformed_v.transpose(1, 0, 2)
    img_transformed_hv = img_transformed_v.transpose(1, 0, 2)
    iterations = 20

    img_seam_hv, img_transformed_hv = seam_carve(iterations, img_seam_hv, img_transformed_hv)

    subplot(223)
    imshow(img_seam_hv.transpose(1, 0, 2))
    title("3. Seam carved horizontal")

    subplot(224)
    imshow(img_transformed_hv.transpose(1, 0, 2))
    title("4. Transformed Image")

    show()
Example #5
0
def image_compare(df, IMAGES_DIR='/home/ryan/asi_images/'):
    '''
    takes a list of n image ids and returns sum(n..n-1) n comparisons of r2 difference, r2(fft) difference, and average number of thresholded pixels
    '''
    img_buffer = {}
    return_list = []
    artdf = df[['_id', 'images']].copy()
    artdf.images = artdf.images.apply(getpath) 
    paths = artdf[['_id','images']].dropna()
    paths.index = paths._id
    paths = paths.images
    if paths.shape[0] < 2:
        return DataFrame([])
    for id_pair in combinations(paths.index, 2):
        if id_pair[0] in img_buffer:
            img1 = img_buffer[id_pair[0]]
        else:
            img_buffer[id_pair[0]] = img_as_float(rgb2gray(resize(imread(IMAGES_DIR + paths[id_pair[0]]), (300,300))))
            img1 = img_buffer[id_pair[0]]
        
        if id_pair[1] in img_buffer:
            img2 = img_buffer[id_pair[1]]
        else:
            img_buffer[id_pair[1]] = img_as_float(rgb2gray(resize(imread(IMAGES_DIR + paths[id_pair[1]]), (300,300))))
            img2 = img_buffer[id_pair[1]]
        return_list.append(
                [id_pair[0], id_pair[1], \
                    norm(img1 - img2), \
                    norm(fft2(img1) - fft2(img2)), \
                    #mean([sum(img1 > threshold_otsu(img1)), sum(img2 > threshold_otsu(img2))])]
                    #mean([sum(img1 > 0.9), sum(img2 > 0.9)])] 
                    std(img1)+std(img2)/2.]
       )
    return DataFrame(return_list, columns=['id1','id2','r2diff', 'fftdiff', 'stdavg'])
def readTestingFragment(datapath, fragList, imgSize=(1,224,224), meanImage=[]):
    ch, ih, iw = imgSize
    fragLen = len(fragList)
    if ch == 1:
        X = np.zeros((fragLen, 1, ih, iw))
        idx = -1
        print('reading data')
        for f in fragList:
            idx += 1
            # print(f)
            img = skimage.img_as_float(skio.imread(datapath+f) )
#            img -= meanImage
            X[idx, 0, ...] = img
    elif ch == 3:
        X = np.zeros((fragLen, 3, ih, iw))
        idx = -1
        print('reading data')
        for f in fragList:
            idx += 1
            img = skimage.img_as_float(skio.imread(datapath+f) )
            img = img.swapaxes(1, 2)
            img = img.swapaxes(0, 1)
#            img -= meanImage
            X[idx, ...] = img
    X -= np.tile(meanImage, [fragLen, 1, 1, 1])
    return X
def main():
    img = imread('givenImg.png')
    img = img_as_float(img)
    subplot(1, 3, 1)
    imshow(imread('givenImg.png'))
    title('Given')
    figure()
    gray()
    """
    >>>img = imread('givenImg.png')
    >>>img = img_as_float(img)
    >>>energy=dual_gradient_energy(img)
    >>>minval,minIndex,sOfIJ=find_seam(img,energy)
    >>>print minval
    0.488050766739
    """
    for i in range(50): #Plot 50 Seams
        energy = dual_gradient_energy(img)
        minval, minIndex, sOfIJ = find_seam(img, energy)
        img = plot_seam(img, minIndex, sOfIJ)
    subplot(1, 3, 2)
    imshow(img)
    title('Seam Plot')
    img = imread('givenImg.png')
    img = img_as_float(img)
    for i in range(50): #Delete 50 Seams
        energy = dual_gradient_energy(img)
        minval, minIndex, sOfIJ = find_seam(img, energy)
        print minval
        img = remove_seam(img, minIndex, sOfIJ)
    subplot(1, 3, 3)
    imshow(img)
    title('Resized Image')
    show()
    pass
def compute_mean_image(training_data_path, testing_data_path, save_flag=True, save_file=''):
    print('computing mean images')
    folder = os.listdir(training_data_path)
    trainNum = len(folder)
    init_flag = True
    for f in folder:
        img = skimage.img_as_float( skio.imread(training_data_path+f) )
        if init_flag:
            mean_image = img
            init_flag = False
        else:
            mean_image += img
    
    folder = os.listdir(testing_data_path)
    testNum = len(folder)
    for f in folder:
        img = skimage.img_as_float( skio.imread(testing_data_path+f) )
        mean_image += img
    
    mean_image /= (trainNum + testNum)
    
    
    if len(mean_image.shape) == 2:
        '''if gray, (h, w) to (1, h, w)'''
        tmp = np.zeros((1, mean_image.shape[0], mean_image.shape[1]))
        tmp[0, ...] = mean_image
        mean_image = tmp
    else:
        '''if color, swap (h, w, ch) to (ch, h, w)'''
        mean_image = mean_image.swapaxes(1,2)
        mean_image = mean_image.swapaxes(0,1)
    if save_flag:
        with open(save_file, 'wb') as f:
            np.save(f, mean_image)
    return mean_image
def computeMeanImage(trainingPath, testingPath, savePath, imgSize):
    ch, ih, iw = imgSize
    meanImage = np.zeros((ch, ih, iw))
    print('computing mean image')
    folder = os.listdir(trainingPath)
    trainNum = 0
    for f in folder:
        if not f[-4:] == '.jpg':
            continue
        img = skimage.img_as_float( skio.imread(trainingPath+f) )
        trainNum += 1
        if ch == 3:
            img = img.swapaxes(1, 2)
            img = img.swapaxes(0, 1)
        meanImage += img
    
    folder = os.listdir(testingPath)
    testNum = 0
    for f in folder:
        if not f[-4:] == '.jpg':
            continue
        img = skimage.img_as_float( skio.imread(testingPath+f) )
        testNum += 1
        if ch == 3:
            img = img.swapaxes(1, 2)
            img = img.swapaxes(0, 1)
        meanImage += img
    meanImage /= (trainNum + testNum)
    with open(savePath, 'wb') as f:
        np.save(f, meanImage)
Example #10
0
def mpl_image_to_rgba(mpl_image):
    """Return RGB image from the given matplotlib image object.

    Each image in a matplotlib figure has its own colormap and normalization
    function. Return RGBA (RGB + alpha channel) image with float dtype.

    Parameters
    ----------
    mpl_image : matplotlib.image.AxesImage object
        The image being converted.

    Returns
    -------
    img : array of float, shape (M, N, 4)
        An image of float values in [0, 1].
    """
    image = mpl_image.get_array()
    if image.ndim == 2:
        input_range = (mpl_image.norm.vmin, mpl_image.norm.vmax)
        image = rescale_intensity(image, in_range=input_range)
        # cmap complains on bool arrays
        image = mpl_image.cmap(img_as_float(image))
    elif image.ndim == 3 and image.shape[2] == 3:
        # add alpha channel if it's missing
        image = np.dstack((image, np.ones_like(image)))
    return img_as_float(image)
Example #11
0
def find_movement():
    # img = imread('shot1.jpg')
    # img2 = imread('shot2.jpg')
    img = imread("frame0.jpg")
    img2 = imread("frame2.jpg")
    img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
    img1 = img_as_float(img1)
    img2 = img_as_float(img2)
    # print img1
    h1, w1 = img1.shape
    h2, w2 = img2.shape

    img3 = zeros((h1, w1))

    for x in range(0, h1 - 1):
        for y in range(0, w1 - 1):
            if abs(img1[x, y] - img2[x, y]) > 0.01:
                # print img1[x, y], " ", img2[x, y]
                img3[x, y] = 1

    figure()
    # subplot(1, 2, 1), imshow(img)
    # subplot(1, 2, 2), \
    imshow(img3)
    show()
Example #12
0
def repeated_sales(df, artistname, artname, r2thresh=7000, fftr2thresh=10000, IMAGES_DIR='/home/ryan/asi_images/'):
    """
        Takes a dataframe, artistname and artname and tries to decide, via image matching, if there is a repeat sale. Returns a dict of lot_ids, each entry a list of repeat sales
    """
    artdf = df[(df['artistID']==artistname) & (df['artTitle']==artname)]

    artdf.images = artdf.images.apply(getpath)
    paths = artdf[['_id','images']].dropna()
    id_dict = {}
    img_buffer = {}
    already_ordered = []
    for i, path_i in paths.values:
        id_dict[i] = []
        img_buffer[i] = img_as_float(rgb2gray(resize(imread(IMAGES_DIR + path_i), (300,300))))
        for j, path_j in paths[paths._id != i].values:
            if j > i and j not in already_ordered:
                if j not in img_buffer.keys():
                    img_buffer[j] = img_as_float(rgb2gray(resize(imread(IMAGES_DIR + path_j), (300,300))))
                if norm(img_buffer[i] - img_buffer[j]) < r2thresh and\
                        norm(fft2(img_buffer[i]) - fft2(img_buffer[j])) < fftr2thresh:
                    id_dict[i].append(j)
                    already_ordered.append(j)
    for key in id_dict.keys():
        if id_dict[key] == []:
            id_dict.pop(key)
    return id_dict
Example #13
0
    def __init__(self, headers, lightfield_name, darkfield_name):
        """
        Load images from a detector for given Header(s). Subtract
        dark images from each corresponding light image automatically.

        Parameters
        ----------
        headers : Header or list of Headers
        lightfield_name : str
            alias (data key) of lightfield images
        darkfield_name : str
            alias (data key) of darkfield images

        Example
        -------
        >>> header = DataBroker[-1]
        >>> images = SubtractedImages(header, 'my_lightfield', 'my_darkfield')
        >>> for image in images:
                # do something
        """
        self.light = Images(headers, lightfield_name)
        self.dark = Images(headers, darkfield_name)
        if len(self.light) != len(self.dark):
            raise ValueError("The streams from {0} and {1} have unequal "
                             "length and cannot be automatically subtracted.")
        self._len = len(self.light)
        example = img_as_float(self.light[0]) - img_as_float(self.dark[0])
        self._dtype = example.dtype
        self._shape = example.shape
Example #14
0
def test_copy():
    x = np.array([1], dtype=np.float64)
    y = img_as_float(x)
    z = img_as_float(x, force_copy=True)

    assert y is x
    assert z is not x
Example #15
0
def main(image):

    matplotlib.rcParams["font.size"] = 10

    def show_img(img, axes):
        """Plot the image as float"""
        # img = img_as_float(img)
        ax_img = axes
        ax_img.imshow(img, cmap=plt.cm.gray)
        ax_img.set_axis_off()

        return ax_img

    # Open and read in the fits image
    try:
        fits = pyfits.open(image)
        # fits = Image.open(image)
    except IOError:
        print "Can not read the fits image: " + image + " !!"

    # Check the input image
    img = fits[0].data
    # img = np.array(fits)
    if img.ndim != 2:
        raise NameError("Data need to be 2-D image !")

    # Logrithm scaling of the image
    img_log = np.log10(img)
    img_log = img_as_float(img_log)

    # Contrast streching
    p5, p95 = np.percentile(img, (2, 98))
    img_rescale = exposure.rescale_intensity(img, in_range=(p5, p95))

    # Adaptive equalization
    img_new = bytescale(img_rescale)
    img_ahe = exposure.equalize_adapthist(img_new, ntiles_x=16, ntiles_y=16, clip_limit=0.05, nbins=256)
    img_ahe = img_as_float(img_ahe)

    # Display results
    fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(16, 5))

    # Original image
    ax_img = show_img(img_log, axes[0])
    ax_img.set_title("Original")

    # Contrast Enhanced one
    ax_img = show_img(img_rescale, axes[1])
    ax_img.set_title("Rescale")

    # AHE Enhanced one
    ax_img = show_img(img_ahe, axes[2])
    ax_img.set_title("AHE")

    # Prevent overlap of y-axis
    fig.subplots_adjust(bottom=0.1, right=0.9, top=0.9, left=0.1, wspace=0.05)

    # Save a PNG file
    plt.gcf().savefig("ahe_test.png")
Example #16
0
def test_bounding_values():
    image = img_as_float(data.page())
    template = np.zeros((3, 3))
    template[1, 1] = 1
    result = match_template(img_as_float(data.page()), template)
    print(result.max())
    assert result.max() < 1 + 1e-7
    assert result.min() > -1 - 1e-7
Example #17
0
def main():
    """
    >>> img = imread('C:\Users\DELL\PycharmProjects\cc3\image.png')
    >>> img = img_as_float(img)
    >>> energy = dual_gradient_energy(img)
    >>> minVal,minIndex,sOfTJ=find_seam(img,energy)
    >>> img_modified, path = plot_seam(img, minIndex, sOfTJ)
    >>> print minVal
    0.488050766739
    """
    img = imread("C:\Users\DELL\PycharmProjects\cc3\image.png")
    img = img_as_float(img)
    R = img[:, :, 0]
    G = img[:, :, 1]
    B = img[:, :, 2]
    figure(1)
    gray()
    subplot(1, 4, 1)
    imshow(img)
    title("RGB")
    subplot(1, 4, 2)
    imshow(R)
    title("Red")
    subplot(1, 4, 3)
    imshow(G)
    title("Green")
    subplot(1, 4, 4)
    imshow(B)
    title("Blue")
    show()
    energy = dual_gradient_energy(img)
    figure(2)
    subplot(2, 1, 1)
    imshow(energy)
    title("Dual Energy Gradient")
    show()

    for i in range(50):
        energy = dual_gradient_energy(img)
        val, index, seam = find_seam(img, energy)
        img1, path = plot_seam(img, index, seam)
    figure(3)
    subplot(3, 2, 1)
    imshow(img1)
    title("Seams Plotted")
    img = imread('C:\Users\DELL\PycharmProjects\cc3\image.png')
    img = img_as_float(img)
    for i in range(50):
        energy = dual_gradient_energy(img)
        val, index, seam = find_seam(img, energy)
        img1, path = plot_seam(img, index, seam)
        img = remove_seam(img1, path)
    subplot(3, 2, 2)
    imshow(img)
    title("Seams Removed")
    show()
Example #18
0
def mpl_image_to_rgba(mpl_image):
    """Return RGB image from the given matplotlib image object.

    Each image in a matplotlib figure has it's own colormap and normalization
    function. Return RGBA (RGB + alpha channel) image with float dtype.
    """
    input_range = (mpl_image.norm.vmin, mpl_image.norm.vmax)
    image = rescale_intensity(mpl_image.get_array(), in_range=input_range)
    image = mpl_image.cmap(img_as_float(image)) # cmap complains on bool arrays
    return img_as_float(image)
def preprocess(im, blur=True, boxcar=True, noise_size=1, boxcar_width=5):
    """
    Perform bluring and background subtraction of image.

    Parameters
    ----------
    im : ndarray
        Image to preprocess.
    blur : bool
        If True, perform a Gaussian blur on each image.
    boxcar : bool
        If True, perfrom a boxcar (mean) filter to compute background.
        The background is then subtracted.
    noise_size : float
        The characteristic length scale of noise in the images
        in units of pixels.  This is used to set the sigma value
        in the Gaussian blur.  Ignored if blur is False.
    boxcar_width : int
        Width of the boxcar filter.  Should be an odd integer greater
        than the pixel radius, but smaller than interparticle distance.

    Returns
    -------
    output : ndarray, shape as im, dtype float
        Blurred, background subtracted image.
    """

    # Convert the image to float
    im = skimage.img_as_float(im)

    # Return image back if we do nothing
    if not blur and not boxcar:
        return skimage.img_as_float(im)

    # Compute background using boxcar (mean) filter
    if boxcar:
        if boxcar_width % 2 == 0:
            raise ValueError('boxcar_width must be odd.')

        # Perform mean filter with a square structuring element
        im_bg = scipy.ndimage.uniform_filter(im, boxcar_width)
    else:
        im_bg = 0.0

    # Perform Gaussian blur
    if blur:
        im_blur = skimage.filters.gaussian(im, noise_size)
    else:
        im_blur = im

    # Subtract background from blurred image
    bg_subtracted_image = im_blur - im_bg

    # Set negative values to zero and return
    return np.maximum(bg_subtracted_image, 0)
Example #20
0
def compute_mean_image(training_data_path
                       , testing_data_path
                       , save_file):

    if os.path.exists(save_file):
        logging.info("mean file already exists at %s, return it directly" % save_file)

        mean_img = skio.imread(save_file)
        logging.info("mean img is %s" % mean_img)
        return mean_img
    logging.info('computing mean images')
    folder = ["c%d" % x for x in range(10)]
    total_num = 0
    mean_image = None
    # count image first
    for train_path in training_data_path:
        for f in folder:
            folder_path = os.path.join(train_path, f)
            total_num += len(load_image_path_list(folder_path))

    for path in testing_data_path:
        total_num += len(load_image_path_list(path))

    i = 0
    for train_path in training_data_path:
        for f in folder:
            folder_path = os.path.join(train_path, f)
            for img_path in load_image_path_list(folder_path):
                i += 1
                if i % 100 == 0:
                    logging.info("process %d/%d images now" % (i, total_num))
                img = skimage.img_as_float(skio.imread(img_path))
                if mean_image is None:
                    mean_image = np.zeros(img.shape)
                mean_image += img / total_num

    for path in testing_data_path:
        for file_path in load_image_path_list(path):
            i += 1
            if i % 100 == 0:
                logging.info("process %d/%d images now" % (i, total_num))
            img = skimage.img_as_float( skio.imread(file_path))
            mean_image += img / total_num
        
    if save_file != "":
        base_path = os.path.dirname(save_file)
        if not os.path.exists(base_path):
            os.makedirs(base_path)
        with open(save_file, 'wb') as f:
            imsave(f, mean_image)
            logging.debug("saving mean file to %s" % save_file)
    print mean_image
    return mean_image
def match_ims(a, b):
    """ Match the mean of image b to
    image a; return image b.

    """
    a = img_as_float(a)
    b = img_as_float(b)
    # z_im = (b - b.mean()) / b.std()
    # renormed = a.mean() + z_im * a.std()
    renormed = (b - b.mean()) + a.mean()
    renormed[renormed < 0.] = 0.
    renormed[renormed > 1.] = 1.
    return renormed
def main():
    img = pylab.imread('someimage.png')         # getting the image
    transpose_img = img.transpose(1, 0, 2)      # getting the transpose image
    img = img_as_float(img)
    transpose_img = img_as_float(transpose_img)

    seam = find_seam(img)                       # find seam
    transpose_seam = find_seam(transpose_img)   # find transpose seam

    plot_seam(img, seam)                        # plot seam
    plot_seam(transpose_img, transpose_seam)    # plot transpose seam

    pylab.figure()

    pylab.subplot(2, 2, 1)
    pylab.imshow(img)                           # plot original image
    pylab.title("Original Image")

    h, w = img.shape[:2]
    print 'original image dimensions: W = ' + str(w) + ' H = ' + str(h)

    pylab.subplot(2, 2, 2)
    pylab.imshow(transpose_img)                 # plot transpose of original image
    pylab.title("Transpose Image")

    h, w = transpose_img.shape[:2]
    print 'Transpose image dimensions: W = ' + str(w) + ' H = ' + str(h)

    removed_img = remove_seam(img, seam)
    for i in range(0, 49):                      # image after removing 50 seams
        seam = find_seam(removed_img)
        removed_img = remove_seam(removed_img, seam)
    pylab.subplot(2, 2, 3)
    pylab.imshow(removed_img)                   # plot original image after carving 50 times
    pylab.title("Image after Carving")

    h, w = removed_img.shape[:2]
    print 'After carving image dimensions: W = ' + str(w) + ' H = ' + str(h)

    removed_transpose_img = remove_seam(transpose_img, transpose_seam)
    for i in range(0, 49):                      # transpose image after removing 50 seams
        transpose_seam = find_seam(removed_transpose_img)
        removed_transpose_img = remove_seam(removed_transpose_img, transpose_seam)
    pylab.subplot(2, 2, 4)
    pylab.imshow(removed_transpose_img)         # plot original image after carving 50 times
    pylab.title("Transpose Image after Carving")

    h, w = removed_transpose_img.shape[:2]
    print 'After carving transpose image dimensions: W = ' + str(w) + ' H = ' + str(h)

    pylab.show()
Example #23
0
def main():
    args = sys.argv[1:]
    if len(args) < 3:
        print >> sys.stderr, 'check_dist.py a.png b.png out.pfm'
        sys.exit(1)
    
    if use_c:
        pfm_arg = args[2]
        if os.path.splitext(pfm_arg)[1] == '.mat':
            nnf = read_mat(pfm_arg)
            if nnf.shape[2] != 3:
                nnf = numpy.dstack((nnf, numpy.zeros((nnf.shape[0], nnf.shape[1]))))
            pfm_arg = '_temp_dist.pfm'
            pfm.writepfm(nnf, pfm_arg) 
        ans = subprocess.check_output('./patchtable check_dist %s %s %s' % (args[0], args[1], pfm_arg), shell=True)
        sys.stdout.write(ans)
        return
    a = skimage.img_as_float(skimage.io.imread(args[0]))
    b = skimage.img_as_float(skimage.io.imread(args[1]))
    if os.path.splitext(args[2])[1] == '.pfm':
        nnf = pfm.readpfm(args[2])
    else:
        nnf = read_mat(args[2])
        
    beh = b.shape[0]-patch_w+1
    bew = b.shape[1]-patch_w+1
    
    nnf_h = a.shape[0]-patch_w+1
    nnf_w = a.shape[1]-patch_w+1
    dimg = numpy.zeros((nnf_h, nnf_w))
    for ay in range(nnf_h):
        for ax in range(nnf_w):
            bx = nnf[ay,ax,0]
            by = nnf[ay,ax,1]
            assert 0 <= bx < bew, (bx, bew)
            assert 0 <= by < beh, (by, beh)
            apatch = a[ay:ay+patch_w, ax:ax+patch_w, :].flatten()
            bpatch = b[by:by+patch_w, bx:bx+patch_w, :].flatten()
            d = (apatch-bpatch)
            d = numpy.sqrt(numpy.sum(d*d))
            dimg[ay,ax] = d
            
            #d_nnf = numpy.sqrt(nnf[ay,ax,2])
            #delta = abs(d-d_nnf)
            #if delta > 1e-4:
            #    print 'error:', ay, ax, d, dcorrect
    #print '0,0 nnf:', nnf[0,0,:]
    #print '0,0 correct distance:', dimg[0,0]
    #print '%d,0 nnf:'%(nnf_w-1), nnf[0,nnf_w-1,:]
    #print '%d,0 correct distance:'%(nnf_w-1), dimg[0,nnf_w-1]
    print numpy.mean(dimg.flatten())
Example #24
0
    def add_bg(self, nobgimg, bgid=None):
        '''
        Add background to the rendered image
        The sky option in 'Render Layers' needs to be disabled
        '''
        import skimage.io
        from skimage import img_as_float
        import random
        import numpy as np

        if isinstance(nobgimg, str):
            nobgimg = skimage.io.imread(nobgimg)


        im = nobgimg
        alpha = im[:,:,3]
        im = im[:,:,0:3]
        [h, w, c] = im.shape

        bgfiles = self.get_bgfiles()
        if bgid is None:
            bgid = random.randint(0, len(bgfiles)-1)
        bgfile = bgfiles[bgid]
        L.debug('BG id is %d', bgid)

        # bg = plt.imread(bgfile)
        bg = skimage.io.imread(bgfile)
        bg = img_as_float(bg)
        im = img_as_float(im)
        assert im.dtype == bg.dtype, 'im type %s and bg type %s' % (im.dtype, bg.dtype)
        bgCrop = bg[0:h, 0:w, :]

        L.debug('The range of alpha is (%f, %f)', alpha.min(), alpha.max())
        fgmask = np.tile(alpha[:,:,np.newaxis], (1, 1, 3));
        bgmask = 1 - fgmask;
        combine = bgCrop * bgmask + im * fgmask

        if L.isDebug():
            L.debug('Show the images for composite')
            import matplotlib.pyplot as plt
            plt.subplot(2,2,1)
            plt.imshow(im)
            plt.subplot(2,2,2)
            plt.imshow(bgCrop)
            plt.subplot(2,2,3)
            plt.imshow(combine)
            plt.show()


        return combine
 def ssim(self, image1, image2):
     image1 = Image.open(image1).convert('RGB')
     if image1.size[0] > 300:
         new_size = (300, int(image1.size[1] / image1.size[0] * 300))
     else:
         new_size = image1.size
     print(image1.size, new_size)
     image1 = image1.resize(new_size)
     image2 = Image.open(image2).resize(new_size).convert('RGB')
     image1 = array(image1)
     image2 = array(image2)
     img1 = img_as_float(image1)
     img2 = img_as_float(image2)
     return compare_ssim(img1, img2, win_size=None, gradient=False, multichannel=True)
Example #26
0
def ReadInFaces(Faces, NFaces):
    for index in range(100):
#         if (sys.platform == 'linux2'):
#             path1 = "Faces/face" + str(index) + ".png"
#             path2 = "NonFaces/nface" + str(index) + ".png"
#         else:
#             path1 = "Faces\\face" + str(index) + ".png"
#             path2 = "NonFaces\\nface" + str(index) + ".png"
        path1 = "Testset\\test" + str(index) + ".png"
        path2 = "Testset\\test" + str(index + 100) + ".png"
        Faceimage = skimage.img_as_float(skimage.io.imread(path1))
        NFaceimage = skimage.img_as_float(skimage.io.imread(path2))
        Faces[index] = Faceimage
        NFaces[index] = NFaceimage  
    return
 def _detect(self, board):
     input_images = [transformer.preprocess('data', skimage.img_as_float(square).astype(np.float32)) for square in row for row in board]
     net.blobs['data'].data[...] = np.array(input_images)
     out = net.forward()['prob']
     predictions = np.asarray(map(lambda x: categories[x], np.argmax(out, axis=1)))
     prob = np.sum(np.max(out, axis=1))
     return np.reshape(predictions, (8, 8)), prob
Example #28
0
def mono_check(plugin, fmt='png'):
    """Check the roundtrip behavior for images that support most types.

    All major input types should be handled.
    """

    img = img_as_ubyte(data.moon())
    r1 = roundtrip(img, plugin, fmt)
    testing.assert_allclose(img, r1)

    img2 = img > 128
    r2 = roundtrip(img2, plugin, fmt)
    testing.assert_allclose(img2.astype(np.uint8), r2)

    img3 = img_as_float(img)
    r3 = roundtrip(img3, plugin, fmt)
    if r3.dtype.kind == 'f':
        testing.assert_allclose(img3, r3)
    else:
        testing.assert_allclose(r3, img_as_uint(img))

    img4 = img_as_int(img)
    if fmt.lower() in (('tif', 'tiff')):
        img4 -= 100
        r4 = roundtrip(img4, plugin, fmt)
        testing.assert_allclose(r4, img4)
    else:
        r4 = roundtrip(img4, plugin, fmt)
        testing.assert_allclose(r4, img_as_uint(img4))

    img5 = img_as_uint(img)
    r5 = roundtrip(img5, plugin, fmt)
    testing.assert_allclose(r5, img5)
    def save_segmented_image(self, filepath_image, modality='t1c', show=False):
        '''
        Creates an image of original brain with segmentation overlay and save it in ./predictions
        INPUT   (1) str 'filepath_image': filepath to test image for segmentation, including file extension
                (2) str 'modality': imaging modality to use as background. defaults to t1c. options: (flair, t1, t1c, t2)
                (3) bool 'show': If true, shows output image. defaults to False.
        OUTPUT  (1) if show is True, shows image of segmentation results
                (2) if show is false, returns segmented image.
        '''
        modes = {'flair': 0, 't1': 1, 't1c': 2, 't2': 3}

        segmentation = self.predict_image(filepath_image, show=False)
        print 'segmentation = ' + str(segmentation)
        img_mask = np.pad(segmentation, (16, 16), mode='edge')
        ones = np.argwhere(img_mask == 1)
        twos = np.argwhere(img_mask == 2)
        threes = np.argwhere(img_mask == 3)
        fours = np.argwhere(img_mask == 4)

        test_im = io.imread(filepath_image)
        test_back = test_im.reshape(5, 216, 160)[modes[modality]]
        # overlay = mark_boundaries(test_back, img_mask)
        gray_img = img_as_float(test_back)

        # adjust gamma of image
        image = adjust_gamma(color.gray2rgb(gray_img), 0.65)
        sliced_image = image.copy()
        red_multiplier = [1, 0.2, 0.2]
        yellow_multiplier = [1, 1, 0.25]
        green_multiplier = [0.35, 0.75, 0.25]
        blue_multiplier = [0, 0.25, 0.9]

        print str(len(ones))
        print str(len(twos))
        print str(len(threes))
        print str(len(fours))

        # change colors of segmented classes
        for i in xrange(len(ones)):
            sliced_image[ones[i][0]][ones[i][1]] = red_multiplier
        for i in xrange(len(twos)):
            sliced_image[twos[i][0]][twos[i][1]] = green_multiplier
        for i in xrange(len(threes)):
            sliced_image[threes[i][0]][threes[i][1]] = blue_multiplier
        for i in xrange(len(fours)):
            sliced_image[fours[i][0]][fours[i][1]] = yellow_multiplier
        #if show=True show the prediction
        if show:
            print 'Showing...'
            io.imshow(sliced_image)
            plt.show()
        #save the prediction
        print 'Saving...'
        try:
            mkdir_p('./predictions/')
            io.imsave('./predictions/' + os.path.basename(filepath_image) + '.png', sliced_image)
            print 'prediction saved.'
        except:
            io.imsave('./predictions/' + os.path.basename(filepath_image) + '.png', sliced_image)
            print 'prediction saved.'
Example #30
0
def test_histogram_of_oriented_gradients():
    img = img_as_float(data.astronaut()[:256, :].mean(axis=2))

    fd = feature.hog(img, orientations=9, pixels_per_cell=(8, 8),
                     cells_per_block=(1, 1))

    assert len(fd) == 9 * (256 // 8) * (512 // 8)
def stabilize_video(f_name,
                    use_first_as_reference=False,
                    print_full_results=False,
                    use_gaussian_weights=True,
                    selected_points=None):
    """
    Stabilization of video sample.

    :param f_name:                  path to the video sample file
    :param use_first_as_reference:  use first frame as a reference image, default is False
    :param print_full_results:      if you want print all results from stabilization
    :param use_gaussian_weights:    use gaussian weights in SSIM calculation
    :param selected_points:         selected 5 points
    """
    if selected_points is None:
        selected_points = {}
    capture = cv2.VideoCapture(f_name)
    frames = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
    frame_size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
                  int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) - 65)
    fps = capture.get(cv2.CAP_PROP_FPS)
    stabilized_video_name = f_name + "_stabilized.avi"
    vid_writer = get_video_writer(stabilized_video_name, frame_size, fps)

    print("Start stabilization of video:", f_name)
    print("Using first frame as reference:", use_first_as_reference)

    image_process = ImageProcess()
    r_frames = 0
    print_results = []
    first = True
    first_i, image1 = None, None

    euclid_distances = []

    start_time = time.time()
    while True:
        ret, image2 = capture.read()

        if ret:
            # read the camera image:
            rows, cols, _ = image2.shape
            image2 = image2[65:rows, 0:cols]
            if first:
                first = False
                image1 = np.copy(image2)
                first_i = image1
                if not selected_points:
                    selected_points = image_process.select_reference_points(
                        image2)
            else:
                # perform stabilization
                if use_first_as_reference:
                    result, print_result = image_process.stabilize_picture(
                        first_i, image2)
                    ref_image = img_as_float(image_process.to_gray(first_i))
                else:
                    result, print_result = image_process.stabilize_picture(
                        image1, image2)
                    ref_image = img_as_float(image_process.to_gray(image1))

                if r_frames % 10 == 0:
                    tracking_points = image_process.tracking_points(
                        selected_points, result)
                    euclid_distances.append(
                        image_process.euclid_distance(selected_points,
                                                      tracking_points))

                # write stabilized frame
                vid_writer.write(result)
                image1 = np.copy(result)

                # compute SSIM index
                res_image = img_as_float(image_process.to_gray(result))
                print_result['score'] = ssim(
                    ref_image,
                    res_image,
                    data_range=res_image.max() - res_image.min(),
                    multichannel=True,
                    gaussian_weights=use_gaussian_weights)
                print_result['rmse'] = image_process.rmse(ref_image, res_image)
                print_results.append(print_result)
                print("'\rRemaining frames: {0}. Actual frame: {1}".format(
                    frames - r_frames, r_frames),
                      end='')
        else:
            break

        r_frames += 1

    if capture.isOpened():
        capture.release()

    if vid_writer.isOpened():
        vid_writer.release()

    print("'\rRemaining frames: DONE", end='\n')
    print("Result stabilized video:", stabilized_video_name)
    print("-STATS--------------")
    print("DURATION:", round(time.time() - start_time, 3), "s")

    print("EUCLID DISTANCES: ", euclid_distances)
    euclid_average = mean_euclid_distances(euclid_distances)
    print("EUCLID DISTANCES AVERAGE: ", euclid_average)

    if print_full_results:
        ImageProcess.print_ordered("ALL RESULTS::", print_results)

    av = sum(item.get('score', 0)
             for item in print_results) / len(print_results)
    print("MSSIM AVERAGE: ", round(av * 100, 2))
    rmse = sum(item.get('rmse', 0)
               for item in print_results) / len(print_results)
    print("RMSE AVERAGE: ", round(rmse * 100, 2))
    print()
Example #32
0
def active_contour(image,
                   snake,
                   alpha=0.01,
                   beta=0.1,
                   w_line=0,
                   w_edge=1,
                   gamma=0.01,
                   bc='periodic',
                   max_px_move=1.0,
                   max_iterations=2500,
                   convergence=0.1):
    """Active contour model.

    Active contours by fitting snakes to features of images. Supports single
    and multichannel 2D images. Snakes can be periodic (for segmentation) or
    have fixed and/or free ends.
    The output snake has the same length as the input boundary.
    As the number of points is constant, make sure that the initial snake
    has enough points to capture the details of the final contour.

    Parameters
    ----------
    image : (N, M) or (N, M, 3) ndarray
        Input image.
    snake : (N, 2) ndarray
        Initialisation coordinates of snake. For periodic snakes, it should
        not include duplicate endpoints.
    alpha : float, optional
        Snake length shape parameter. Higher values makes snake contract
        faster.
    beta : float, optional
        Snake smoothness shape parameter. Higher values makes snake smoother.
    w_line : float, optional
        Controls attraction to brightness. Use negative values to attract to
        dark regions.
    w_edge : float, optional
        Controls attraction to edges. Use negative values to repel snake from
        edges.
    gamma : float, optional
        Explicit time stepping parameter.
    bc : {'periodic', 'free', 'fixed'}, optional
        Boundary conditions for worm. 'periodic' attaches the two ends of the
        snake, 'fixed' holds the end-points in place, and'free' allows free
        movement of the ends. 'fixed' and 'free' can be combined by parsing
        'fixed-free', 'free-fixed'. Parsing 'fixed-fixed' or 'free-free'
        yields same behaviour as 'fixed' and 'free', respectively.
    max_px_move : float, optional
        Maximum pixel distance to move per iteration.
    max_iterations : int, optional
        Maximum iterations to optimize snake shape.
    convergence: float, optional
        Convergence criteria.

    Returns
    -------
    snake : (N, 2) ndarray
        Optimised snake, same shape as input parameter.

    References
    ----------
    .. [1]  Kass, M.; Witkin, A.; Terzopoulos, D. "Snakes: Active contour
            models". International Journal of Computer Vision 1 (4): 321
            (1988).

    Examples
    --------
    >>> from skimage.draw import circle_perimeter
    >>> from skimage.filters import gaussian

    Create and smooth image:

    >>> img = np.zeros((100, 100))
    >>> rr, cc = circle_perimeter(35, 45, 25)
    >>> img[rr, cc] = 1
    >>> img = gaussian(img, 2)

    Initiliaze spline:

    >>> s = np.linspace(0, 2*np.pi,100)
    >>> init = 50*np.array([np.cos(s), np.sin(s)]).T+50

    Fit spline to image:

    >>> snake = active_contour(img, init, w_edge=0, w_line=1) #doctest: +SKIP
    >>> dist = np.sqrt((45-snake[:, 0])**2 +(35-snake[:, 1])**2) #doctest: +SKIP
    >>> int(np.mean(dist)) #doctest: +SKIP
    25

    """
    split_version = scipy.__version__.split('.')
    if not (split_version[-1].isdigit()):
        split_version.pop()
    scipy_version = list(map(int, split_version))
    new_scipy = scipy_version[0] > 0 or \
                (scipy_version[0] == 0 and scipy_version[1] >= 14)
    if not new_scipy:
        raise NotImplementedError(
            'You are using an old version of scipy. '
            'Active contours is implemented for scipy versions '
            '0.14.0 and above.')

    max_iterations = int(max_iterations)
    if max_iterations <= 0:
        raise ValueError("max_iterations should be >0.")
    convergence_order = 10
    valid_bcs = [
        'periodic', 'free', 'fixed', 'free-fixed', 'fixed-free', 'fixed-fixed',
        'free-free'
    ]
    if bc not in valid_bcs:
        raise ValueError("Invalid boundary condition.\n" +
                         "Should be one of: " + ", ".join(valid_bcs) + '.')
    img = img_as_float(image)
    RGB = img.ndim == 3

    # Find edges using sobel:
    if w_edge != 0:
        if RGB:
            edge = [
                sobel(img[:, :, 0]),
                sobel(img[:, :, 1]),
                sobel(img[:, :, 2])
            ]
        else:
            edge = [sobel(img)]
        for i in range(3 if RGB else 1):
            edge[i][0, :] = edge[i][1, :]
            edge[i][-1, :] = edge[i][-2, :]
            edge[i][:, 0] = edge[i][:, 1]
            edge[i][:, -1] = edge[i][:, -2]
    else:
        edge = [0]

    # Superimpose intensity and edge images:
    if RGB:
        img = w_line*np.sum(img, axis=2) \
            + w_edge*sum(edge)
    else:
        img = w_line * img + w_edge * edge[0]

    # Interpolate for smoothness:
    if new_scipy:
        intp = RectBivariateSpline(np.arange(img.shape[1]),
                                   np.arange(img.shape[0]),
                                   img.T,
                                   kx=2,
                                   ky=2,
                                   s=0)
    else:
        intp = np.vectorize(
            interp2d(np.arange(img.shape[1]),
                     np.arange(img.shape[0]),
                     img,
                     kind='cubic',
                     copy=False,
                     bounds_error=False,
                     fill_value=0))

    x, y = snake[:, 0].astype(np.float), snake[:, 1].astype(np.float)
    xsave = np.empty((convergence_order, len(x)))
    ysave = np.empty((convergence_order, len(x)))

    # Build snake shape matrix for Euler equation
    n = len(x)
    a = np.roll(np.eye(n), -1, axis=0) + \
        np.roll(np.eye(n), -1, axis=1) - \
        2*np.eye(n)  # second order derivative, central difference
    b = np.roll(np.eye(n), -2, axis=0) + \
        np.roll(np.eye(n), -2, axis=1) - \
        4*np.roll(np.eye(n), -1, axis=0) - \
        4*np.roll(np.eye(n), -1, axis=1) + \
        6*np.eye(n)  # fourth order derivative, central difference
    A = -alpha * a + beta * b

    # Impose boundary conditions different from periodic:
    sfixed = False
    if bc.startswith('fixed'):
        A[0, :] = 0
        A[1, :] = 0
        A[1, :3] = [1, -2, 1]
        sfixed = True
    efixed = False
    if bc.endswith('fixed'):
        A[-1, :] = 0
        A[-2, :] = 0
        A[-2, -3:] = [1, -2, 1]
        efixed = True
    sfree = False
    if bc.startswith('free'):
        A[0, :] = 0
        A[0, :3] = [1, -2, 1]
        A[1, :] = 0
        A[1, :4] = [-1, 3, -3, 1]
        sfree = True
    efree = False
    if bc.endswith('free'):
        A[-1, :] = 0
        A[-1, -3:] = [1, -2, 1]
        A[-2, :] = 0
        A[-2, -4:] = [-1, 3, -3, 1]
        efree = True

    # Only one inversion is needed for implicit spline energy minimization:
    inv = scipy.linalg.inv(A + gamma * np.eye(n))

    # Explicit time stepping for image energy minimization:
    for i in range(max_iterations):
        if new_scipy:
            fx = intp(x, y, dx=1, grid=False)
            fy = intp(x, y, dy=1, grid=False)
        else:
            fx = intp(x, y, dx=1)
            fy = intp(x, y, dy=1)
        if sfixed:
            fx[0] = 0
            fy[0] = 0
        if efixed:
            fx[-1] = 0
            fy[-1] = 0
        if sfree:
            fx[0] *= 2
            fy[0] *= 2
        if efree:
            fx[-1] *= 2
            fy[-1] *= 2
        xn = np.dot(inv, gamma * x + fx)
        yn = np.dot(inv, gamma * y + fy)

        # Movements are capped to max_px_move per iteration:
        dx = max_px_move * np.tanh(xn - x)
        dy = max_px_move * np.tanh(yn - y)
        if sfixed:
            dx[0] = 0
            dy[0] = 0
        if efixed:
            dx[-1] = 0
            dy[-1] = 0
        x += dx
        y += dy

        # Convergence criteria needs to compare to a number of previous
        # configurations since oscillations can occur.
        j = i % (convergence_order + 1)
        if j < convergence_order:
            xsave[j, :] = x
            ysave[j, :] = y
        else:
            dist = np.min(
                np.max(
                    np.abs(xsave - x[None, :]) + np.abs(ysave - y[None, :]),
                    1))
            if dist < convergence:
                break

    return np.array([x, y]).T
def sr_saliency(rgb_image, sigma=3, display_result=False):
    """This Function computes Spectral Residue Based Saliency map


Parameters
----------
Inputs
rgbimage: M x N x 3 array (rgb color image)
rgb input image for computation of saliency
sigma: scalar ,optional
standard deviation of the smoothing filter for
smoothed log magnitude of input rgbimage
display_result: bool value, optional
set true if you want the result to be displayed
Returns
-------
Outputs: 2D array
function return the saliency map of the rgbimage.
Number of rows and cols of output is same as input

References
----------
Xiaodi Hou,Liquin Zhang,
Saliency Detection: A spectral residue approach
IEEE Computer Visoin and Pattern Recognition(2007)
Examples:
>>filename = 'image.jpg'(path of image)
>>rgb_image = imread(filename);
>>saliency_map = sr_saliency(rgb_image,display_result = True)
"""
    #handle cases where the input isn't an rgbimage
    try:
        gray_image = rgb2gray(rgb_image)
    except ValueError:
        gray_image = rgb_image
    gray_image = img_as_float(gray_image)
    # Spectral Residue Computation

    #compute fourier transform on image
    fft_gray_image = fftpack.fft2(gray_image)
    log_magnitude = np.log(np.abs(fft_gray_image))
    phase = np.angle(fft_gray_image)

    #smooth the log magnitude response
    avg_log_magnitude = filters.gaussian_filter(log_magnitude,
                                                sigma,
                                                mode="nearest")

    #compute spectral residue
    spectral_residual = log_magnitude - avg_log_magnitude

    #find inverse fourier transform of spectral residue to obtain the saliency map
    saliency_map = np.abs(fftpack.ifft2(np.exp(spectral_residual +
                                               1j * phase)))**2
    saliency_map = ndimage.gaussian_filter(saliency_map, sigma=3)

    #display option
    if (display_result):
        plt.imshow(saliency_map)
        plt.show()
    return saliency_map
Example #34
0
import cv2
from matplotlib import pyplot as plt
from skimage import io, img_as_float
from skimage.restoration import denoise_tv_chambolle

img = img_as_float(io.imread('images/noisy/noisy_gaussian.jpg'))

# plt.hist(img.flat, bins=100, range=(0, 1))

# plt.show()

denoise_img = denoise_tv_chambolle(img,
                                   weight=0.1,
                                   eps=0.0002,
                                   n_iter_max=200,
                                   multichannel=True)
io.imsave('images/chambolle_denois.jpg', denoise_img)

plt.hist(denoise_img.flat, bins=100,
         range=(0, 1))  # .flat returns the flattened numpy array (1D)

cv2.imshow("Original", img)
cv2.imshow("TV Filtered", denoise_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Example #35
0
	def process(self, image):
		processor = LocalMax(self.squareSize)
		localMax = skimage.img_as_float(processor.process(image))
		log.saveNumpyImage(localMax, processor.name())
		return image * ((image + self.range) >= localMax)
Example #36
0
 def setup(self):
     self.image = img_as_float(data.moon())
     self.image = rescale(self.image, 2.0, anti_aliasing=False)
Example #37
0
### Example script showing how to perform a weighted 2D Total-Variation filtering with proxTV
import prox_tv as ptv
import numpy as np
from pylab import *
import matplotlib.pyplot as plt
import time
import skimage as ski
from skimage import data, io, filters, color, util

# Load image
X = io.imread('colors.png')
X = ski.img_as_float(X)
X = color.rgb2gray(X)

# Introduce noise
noiseLevel = 0.01
N = util.random_noise(X, mode='speckle', var=noiseLevel)

# Gradient in columns
W1 = 0.01 * np.cumsum(np.ones((X.shape[0] - 1, X.shape[1])), 1)
W2 = 0.01 * np.ones((X.shape[0], X.shape[1] - 1))
print('Solving 2D weighted TV...')
start = time.time()
FW = ptv.tv1w_2d(N, W1, W2)
end = time.time()
print('Elapsed time ' + str(end - start))

plt.subplot(3, 4, 1)
io.imshow(W1)
plt.title('Weights along columns')
plt.subplot(3, 4, 5)
Example #38
0
import pandas as pd
from sklearn.cluster import KMeans
from skimage.io import imread
from skimage import img_as_float
import pylab
import numpy as np

from ml_intro.custutils import to_path

# read data as numpy array with shape n * m * 3, where n an dm is image sizes
image = imread(to_path('parrots.jpg'))
# show image
pylab.imshow(image)
# skimage float format is value in range [0; 1]
img_float = img_as_float(image)
# Reshape array to construct features-objects: every pixel is object, every
# object has 3 features - R, G, B
# reshape with -1: total array size divided by product of all other listed
# dimensions (in this case last dim is 3)
X = img_float.reshape(-1, img_float.shape[-1])
# data frame will be used to groupby by clusters
clust_data = pd.DataFrame(X, columns=['R', 'G', 'B'])
# define psnr - Peak signal-to-noise ratio


def psnr(y_true, y_pred):
    """both params should be vectors, otherwise reshape(-1) performed
    for details see https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
    in this implementation assumed that image encoded in float format which
    means max signal value is 1.0"""
    if len(y_true.shape) > 1:
Example #39
0
lamda = 0.05
rho = 100
K = 200
err = 0.0001
para_tv = pa.Parameter(lamda, rho, K, err, 0)

lamda = 0.1
rho = 50
K = 200
err = 0.0001
alpha_ratio = 0.5
para_mctv = pa.Parameter(lamda, rho, K, err, alpha_ratio)

# denoising
X1 = tv2d.denoising_2D_TV(noi_img, para_tv)
X2 = img_as_float(mpimg.imread('NLTV.jpg'))
X3 = mctv2d.denoising_2D_MCTV(noi_img, para_mctv)

# plot demo result figure
plt.figure()
plt.subplots_adjust(left=0.05,
                    bottom=0.05,
                    right=0.95,
                    top=0.95,
                    wspace=0.05,
                    hspace=0.05)

sub_plot_org()
sub_plot_res(4, noi_img, 'Noisy Image')
sub_plot_res(6, X1, 'TV')
sub_plot_res(8, X2, 'NLTV')
Example #40
0
        factor = cholesky(A)
        x = factor(b)
        x = x.reshape(img.shape, order='F').toarray()
    else:
        x = sparse.linalg.spsolve(A, b)
        x = x.reshape(img.shape, order='F')

    time_taken = time.time() - start_time
    print(solver, " time:", time_taken)
    return x


if __name__ == "__main__":
    #TODO: Replace with your own image
    im = io.imread(path.join('images', 'task4.jpg'), as_gray=True)
    im = skimage.img_as_float(im)

    G = img2grad_field(im)
    Gm = np.sqrt(np.sum(G * G, axis=2))
    w = 1 / (Gm + 0.0001)  # To avoid pinching artefacts

    # imr = reconstruct_grad_field(G,w,im[0,0], im, "spsolve").clip(0,1)
    imr = reconstruct_grad_field(G, w, im[0, 0], im, "cholesky").clip(0, 1)

    plt.figure(figsize=(9, 3))

    plt.subplot(131)
    plt.title('Original')
    plt.axis('off')
    plt.imshow(im, cmap='gray')
Example #41
0
            u,v = get_window(img, x, y, b)
            
            out[y,x] = np.amax(u+v)
                   
    return out

def opening(img,b):
    return dialate(erode(img,b),b)

def closing(img,b):
    return erode(dialate(img,b),b)

def disc(r):
    r2 = r//2
    t = np.fromfunction(lambda x,y: (x-r2)**2+(y-r2)**2 <=r2**2,(r,r))
    w = np.zeros((r,r))
    w[t]=1
    return w

img = io.imread("input/Fig0935(a)(ckt_board_section).tif")
img = skimage.img_as_float(img)

d3 = disc(3*2+1)*0.2
d5 = disc(5*2+1)*0.2
f,axs = plt.subplots(1,3,figsize=(12,9))   

axs[0].imshow(img,cmap="gray")
axs[1].imshow(opening(img,d3),cmap="gray")
axs[2].imshow(closing(img,d5),cmap="gray")   
plt.show()
Example #42
0
def test_each_channel():
    filtered = edges_each(COLOR_IMAGE)
    for i, channel in enumerate(np.rollaxis(filtered, axis=-1)):
        expected = img_as_float(filter.sobel(COLOR_IMAGE[:, :, i]))
        assert_allclose(channel, expected)
Example #43
0
import matplotlib.pyplot as plt
from skimage import img_as_float
import skimage.segmentation
from img import mydata as my

image = img_as_float(my.hollywood())
image = image.sum(-1)
# print(image)
# Feel free to play around with the parameters to see how they impact the result
cv = skimage.segmentation.chan_vese(image,
                                    mu=0.25,
                                    lambda1=1,
                                    lambda2=1,
                                    tol=1e-3,
                                    max_iter=200,
                                    dt=0.5,
                                    init_level_set="checkerboard",
                                    extended_output=True)

fig, axes = plt.subplots(2, 2, figsize=(8, 8))
ax = axes.flatten()

ax[0].imshow(image, cmap="gray")
ax[0].set_axis_off()
ax[0].set_title("Original Image", fontsize=12)

ax[1].imshow(cv[0], cmap="gray")
ax[1].set_axis_off()
title = "Chan-Vese segmentation - {} iterations".format(len(cv[2]))
ax[1].set_title(title, fontsize=12)
Example #44
0
cfg = {
    'disp_path': '/media/tom/seagate-8tb-ext4/data/kitti_derived/monodepth_disparities_kitti/disparities_pp.npy',
    'output_path': '/home/tom/Dropbox/PhD/Experiments/ICCV2019_depth/matlab/roll_fit.csv',
    'output_img': '/home/tom/Dropbox/PhD/Experiments/ICCV2019_depth/matlab/roll_fit.png',
    'index': 0,
    'test_disp': (0.03, 0.031),
    'roll_range': np.linspace(-30.0, 30.0, 100),
    'image_shape': (375, 1242),
}

disp = np.load(cfg['disp_path'], mmap_mode='r')
disp = disp[cfg['index']]
disp = resize(disp, cfg['image_shape'])

slice = (cfg['test_disp'][0] < disp) & (disp < cfg['test_disp'][1])
h, theta, d = hough_line(slice, theta=np.linspace(np.deg2rad(90 - 30), np.deg2rad(90 + 30), 250))
_, angles, dists = hough_line_peaks(h, theta, d, num_peaks=1)
roll = 90 - np.rad2deg(angles[0])

y0 = dists[0] / np.sin(angles[0])
y1 = (dists[0] - slice.shape[1] * np.cos(angles[0])) / np.sin(angles[0])

plt.imshow(img_as_float(slice))
plt.plot([0, slice.shape[1]], [y0, y1])

with open(cfg['output_path'], 'wt') as f:
    w = csv.writer(f)
    w.writerow([y0, y1])

imsave(cfg['output_img'], img_as_float(slice))
Example #45
0
    def standardize(self, x):
        """Apply the normalization configuration to a batch of inputs.

        # Arguments
            x: batch of inputs to be normalized.

        # Returns
            The inputs, normalized.
        """
        if self.preprocessing_function:
            x = self.preprocessing_function(x)
        if self.rescale:
            x *= self.rescale
        # x is a single image, so it doesn't have image number at index 0
        img_channel_axis = self.channel_axis - 1
        if self.samplewise_center:
            x -= np.mean(x, axis=img_channel_axis, keepdims=True)
        if self.samplewise_std_normalization:
            x /= (np.std(x, axis=img_channel_axis, keepdims=True) + 1e-7)

        if self.featurewise_center:
            if self.mean is not None:
                x -= self.mean
            else:
                warnings.warn('This ImageDataGenerator specifies '
                              '`featurewise_center`, but it hasn\'t'
                              'been fit on any training data. Fit it '
                              'first by calling `.fit(numpy_data)`.')
        if self.featurewise_std_normalization:
            if self.std is not None:
                x /= (self.std + 1e-7)
            else:
                warnings.warn('This ImageDataGenerator specifies '
                              '`featurewise_std_normalization`, but it hasn\'t'
                              'been fit on any training data. Fit it '
                              'first by calling `.fit(numpy_data)`.')
        if self.zca_whitening:
            if self.principal_components is not None:
                flatx = np.reshape(x, (x.size))
                whitex = np.dot(flatx, self.principal_components)
                x = np.reshape(whitex, (x.shape[0], x.shape[1], x.shape[2]))
            else:
                warnings.warn('This ImageDataGenerator specifies '
                              '`zca_whitening`, but it hasn\'t'
                              'been fit on any training data. Fit it '
                              'first by calling `.fit(numpy_data)`.')

        # Define Custom CLAHE Algorithm For Image Preprocessing
        if self.adaptive_equalization:

            if np.shape(x)[2] == 1:
                x = exposure.equalize_adapthist(x, clip_limit=0.03, nbins=48)

            if np.shape(x)[2] == 3:

                x = img_as_float(x)
                img_hsv = color.rgb2hsv(x)
                brightness = img_hsv[:, :, 2]

                with warnings.catch_warnings():
                    warnings.simplefilter("ignore")
                    b_adapteq = exposure.equalize_adapthist(brightness,
                                                            clip_limit=0.03,
                                                            nbins=48)

                img_hsv[:, :, 2] = b_adapteq
                x = color.hsv2rgb(img_hsv)

            else:
                warnings.warn('Improper Image Size - Expected Format - ?,?,3')

        return x
Example #46
0
def inpaint_biharmonic(image, mask, multichannel=False):
    """Inpaint masked points in image with biharmonic equations.

    Parameters
    ----------
    image : (M[, N[, ..., P]][, C]) ndarray
        Input image.
    mask : (M[, N[, ..., P]]) ndarray
        Array of pixels to be inpainted. Have to be the same shape as one
        of the 'image' channels. Unknown pixels have to be represented with 1,
        known pixels - with 0.
    multichannel : boolean, optional
        If True, the last `image` dimension is considered as a color channel,
        otherwise as spatial.

    Returns
    -------
    out : (M[, N[, ..., P]][, C]) ndarray
        Input image with masked pixels inpainted.

    References
    ----------
    .. [1]  N.S.Hoang, S.B.Damelin, "On surface completion and image inpainting
            by biharmonic functions: numerical aspects",
            :arXiv:`1707.06567`
    .. [2]  C. K. Chui and H. N. Mhaskar, MRA Contextual-Recovery Extension of
            Smooth Functions on Manifolds, Appl. and Comp. Harmonic Anal.,
            28 (2010), 104-113,
            :DOI:`10.1016/j.acha.2009.04.004`

    Examples
    --------
    >>> img = np.tile(np.square(np.linspace(0, 1, 5)), (5, 1))
    >>> mask = np.zeros_like(img)
    >>> mask[2, 2:] = 1
    >>> mask[1, 3:] = 1
    >>> mask[0, 4:] = 1
    >>> out = inpaint_biharmonic(img, mask)
    """

    if image.ndim < 1:
        raise ValueError('Input array has to be at least 1D')

    img_baseshape = image.shape[:-1] if multichannel else image.shape
    if img_baseshape != mask.shape:
        raise ValueError('Input arrays have to be the same shape')

    if np.ma.isMaskedArray(image):
        raise TypeError('Masked arrays are not supported')

    image = skimage.img_as_float(image)
    mask = mask.astype(np.bool)

    # Split inpainting mask into independent regions
    kernel = ndi.morphology.generate_binary_structure(mask.ndim, 1)
    mask_dilated = ndi.morphology.binary_dilation(mask, structure=kernel)
    mask_labeled, num_labels = label(mask_dilated, return_num=True)
    mask_labeled *= mask

    if not multichannel:
        image = image[..., np.newaxis]

    out = np.copy(image)

    for idx_channel in range(image.shape[-1]):
        known_points = image[..., idx_channel][~mask]
        limits = (np.min(known_points), np.max(known_points))

        for idx_region in range(1, num_labels+1):
            mask_region = mask_labeled == idx_region
            _inpaint_biharmonic_single_channel(mask_region,
                out[..., idx_channel], limits)

    if not multichannel:
        out = out[..., 0]

    return out
def run_to_task():
    import general_utils
    from   general_utils import RuntimeDeterminedEnviromentVars

    tf.logging.set_verbosity(tf.logging.ERROR)
   
    args = parser.parse_args()

    imgs = args.im_name.split(',')

    if args.task == 'ego_motion' and len(imgs) != 3:
        raise ValueError('Wrong number of images, expecting 3 but got {}'.format(len(imgs)))
    if args.task != 'ego_motion' and len(imgs) != 2:
        raise ValueError('Wrong number of images, expecting 2 but got {}'.format(len(imgs)))

    
    task = args.task
    if task not in list_of_tasks:
        raise ValueError('Task not supported')

    cfg = generate_cfg(task)

    input_img = np.empty((len(imgs),256,256,3), dtype=np.float32)
    for i,imname in enumerate(imgs):
        img = load_raw_image_center_crop( imname )
        img = skimage.img_as_float(img)
        scipy.misc.toimage(np.squeeze(img), cmin=0.0, cmax=1.0).save(imname)
        img = cfg[ 'input_preprocessing_fn' ]( img, **cfg['input_preprocessing_fn_kwargs'] )
        input_img[i,:,:,:] = img
    input_img = input_img[np.newaxis, :]
    

    print("Doing {task}".format(task=task))
    general_utils = importlib.reload(general_utils)
    tf.reset_default_graph()
    training_runners = { 'sess': tf.InteractiveSession(), 'coord': tf.train.Coordinator() }

    ############## Set Up Inputs ##############
    # tf.logging.set_verbosity( tf.logging.INFO )
    setup_input_fn = utils.setup_input
    inputs = setup_input_fn( cfg, is_training=False, use_filename_queue=False )
    RuntimeDeterminedEnviromentVars.load_dynamic_variables( inputs, cfg )
    RuntimeDeterminedEnviromentVars.populate_registered_variables()
    start_time = time.time()

    ############## Set Up Model ##############
    model = utils.setup_model( inputs, cfg, is_training=False )
    m = model[ 'model' ]
    model[ 'saver_op' ].restore( training_runners[ 'sess' ], cfg[ 'model_path' ] )

    predicted, representation = training_runners['sess'].run( 
            [ m.decoder_output,  m.encoder_output ], feed_dict={m.input_images: input_img} )

    if args.store_rep:
        s_name, file_extension = os.path.splitext(args.store_name)
        with open('{}.npy'.format(s_name), 'wb') as fp:
            np.save(fp, np.squeeze(representation))

    if args.store_pred:
        s_name, file_extension = os.path.splitext(args.store_name)
        with open('{}_pred.npy'.format(s_name), 'wb') as fp:
            np.save(fp, np.squeeze(predicted))

    if task == 'ego_motion':
        ego_motion(predicted, args.store_name)
        return
    if task == 'fix_pose':
        cam_pose(predicted, args.store_name, is_fixated=True)
        return   
    if task == 'non_fixated_pose':
        cam_pose(predicted, args.store_name, is_fixated=False)
        return
    if task == 'point_match':
        prediction = np.argmax(predicted, axis=1)
        print('the prediction (1 stands for match, 0 for unmatch)is: ', prediction)
        return       
    ############## Clean Up ##############
    training_runners[ 'coord' ].request_stop()
    training_runners[ 'coord' ].join()
    print("Done: {}".format(config_name))

    ############## Reset graph and paths ##############            
    tf.reset_default_graph()
    training_runners['sess'].close()
    return
Example #48
0
def save_video_sidebyside(videofile,
                          percept,
                          savefile,
                          fps=30,
                          ffmpeg_path=None,
                          libav_path=None):
    """Saves both an input video and the percept to file, side-by-side.

    This function creates a new video from an input video file and a
    :py:class`~pulse2percept.stimuli.TimeSeries` object, assuming they
    correspond to model input and model output, and plots them side-by-side.
    Both input video and percept are resampled according to ``fps``.
    The percept is resized to match the height of the input video.

    Parameters
    ----------
    videofile : str
        File name of input video.
    percept : :py:class:`~pulse2percept.stimuli.TimeSeries`
        A TimeSeries object with dimension (M, N, C, T) or (M, N, T), where
        T is the number of frames, M is the height, N is the width, and C is
        the number of channels.
    savefile : str
        File name of output video.
    fps : int, optional, default: 30
        Desired frame rate of output video.
    ffmpeg_path : str, optional, default: system's default path
        Path to ffmpeg library.
    libav_path : str, optional, default: system's default path
        Path to libav library.

    """
    if not has_skvideo:
        raise ImportError("You do not have scikit-video installed. "
                          "You can install it via $ pip install sk-video.")
    if not has_skimage:
        raise ImportError("You do not have scikit-image installed. "
                          "You can install it via $ pip install scikit-image.")

    if not isinstance(percept, TimeSeries):
        raise TypeError("`percept` must be of type TimeSeries.")

    # Set the path if necessary
    _set_skvideo_path(ffmpeg_path, libav_path)

    # Load video from file
    video = load_video(videofile,
                       as_timeseries=True,
                       ffmpeg_path=ffmpeg_path,
                       libav_path=libav_path)

    # Re-sample time series to new frame rate
    new_tsample = 1.0 / float(fps)
    video = video.resample(new_tsample)
    percept = percept.resample(new_tsample)

    # After re-sampling, both video and percept should have about the same
    # length (up to some rounding error)
    combined_len = np.minimum(video.shape[-1], percept.shape[-1])
    if np.abs(video.shape[-1] - percept.shape[-1]) > 5:
        raise ValueError('Could not resample percept, len(video)='
                         '%d != len(percept)=%d.' %
                         (video.shape[-1], percept.shape[-1]))

    # Up-scale percept to fit video dimensions
    pheight = video.shape[0]
    pwidth = int(percept.shape[1] / float(percept.shape[0]) * video.shape[0])

    # Show the two side-by-side
    combined = np.zeros((combined_len, pheight, video.shape[1] + pwidth, 3))
    combined = combined.astype(np.float32)
    for i in range(combined_len):
        vframe = skimage.img_as_float(video.data[..., i])
        pframe = percept.data[..., i] / percept.data.max()
        pframe = skimage.img_as_float(pframe)
        pframe = sic.gray2rgb(
            sit.resize(pframe, (pheight, pwidth), mode='reflect'))
        combined[i, ...] = np.concatenate((vframe, pframe), axis=1)

    save_video(combined,
               savefile,
               fps=fps,
               ffmpeg_path=ffmpeg_path,
               libav_path=libav_path)
Example #49
0
            _gaussian_blur_locals = util.TypeSignatureSet(
                ['input_img', 'output_img'])
        _ignore_names = [
            '_log_argtype_value', '_argtype_values', '_ignore_names'
        ]
        _locals_typeconfig = dict([
            (_k, util.CythonType.from_value(_v, None, error_variable=_k))
            for (_k, _v) in locals().items() if _k not in _ignore_names
        ])

        _gaussian_blur_locals.add(_locals_typeconfig)


input_img = 'small_temple_rgb.png'
input_img_rgb = skimage.io.imread(input_img)
input_img_rgb = skimage.img_as_float(input_img_rgb)


def test(input_img_rgb=input_img_rgb):
    _exc = None
    try:

        def inner_func():

            ans = gaussian_blur(input_img_rgb, np.zeros(input_img_rgb.shape))

            return ans

        ans = inner_func()
    except Exception as _e:
        _exc = _e
Example #50
0
def save_video(data,
               filename,
               width=None,
               height=None,
               fps=30,
               ffmpeg_path=None,
               libav_path=None):
    """Saves a video to file.

    This function stores a NumPy ndarray to file using Scikit-Video.

    Parameters
    ----------
    data : ndarray | TimeSeries
        Video data as a NumPy ndarray must have dimension (T, M, N, C),
        (T, M, N), (M, N, C), or (M, N), where T is the number of frames,
        M is the height, N is the width, and C is the number of channels (must
        be either 1 for grayscale or 3 for RGB).

        Video data as a TimeSeries object must have dimension (M, N, C, T) or
        (M, N, T). The sampling step will be used as the video's frame rate.
    filename : str
        Video file name.
    width : int, optional
        Desired width of the movie.
        Default: Automatically determined based on ``height`` (without changing
        the aspect ratio). If ``height`` is not given, the percept's original
        width is used.
    height : int, optional
        Desired height of the movie.
        Default: Automatically determined based on `width` (without changing
        the aspect ratio). If ``width`` is not given, the percept's original
        height is used.
    fps : int, optional, default: 30
        Desired frame rate of the video (frames per second).
    ffmpeg_path : str, optional, default: system's default path
        Path to ffmpeg library.
    libav_path : str, optional, default: system's default path
        Path to libav library.

    Notes
    -----
    To distinguish between 3-D inputs of shape (T, M, N) and (M, N, C), the
    last dimension is checked: If it is small (<= 4), it is most likely a
    color channel - hence the input is interpreted as (M, N, C).
    Else it is interpreted as (T, M, N).

    """
    if not has_skvideo:
        raise ImportError("You do not have scikit-video installed. "
                          "You can install it via $ pip install sk-video.")
    if not has_skimage:
        raise ImportError("You do not have scikit-image installed. "
                          "You can install it via $ pip install scikit-image.")

    is_ndarray = is_timeseries = False
    if isinstance(data, np.ndarray):
        is_ndarray = True
    elif isinstance(data, TimeSeries):
        is_timeseries = True
    else:
        raise TypeError('Data to be saved must be either a NumPy ndarray '
                        'or a ``TimeSeries`` object')

    # Set the path if necessary, then choose backend
    _set_skvideo_path(ffmpeg_path, libav_path)
    if skvideo._HAS_FFMPEG:
        backend = 'ffmpeg'
    else:
        backend = 'libav'

    if is_ndarray:
        # Use Scikit-Video utility to interpret dimensions and convert to
        # (T, M, N, C)
        data = skvideo.utils.vshape(data)
        oldheight = data.shape[1]
        oldwidth = data.shape[2]
        length = data.shape[0]
    elif is_timeseries:
        # Resample the percept to the given frame rate
        new_tsample = 1.0 / float(fps)
        d_s = 'old: tsample=%f' % data.tsample
        d_s += ', shape=' + str(data.shape)
        data = data.resample(new_tsample)
        d_s = 'new: tsample=%f' % new_tsample
        d_s += ', shape=' + str(data.shape)
        oldheight = data.shape[0]
        oldwidth = data.shape[1]
        length = data.shape[-1]

    # Calculate the desired height and width
    if not height and not width:
        height = oldheight
        width = oldwidth
    elif height and not width:
        width = int(height * 1.0 / oldheight * oldwidth)
    elif width and not height:
        height = int(width * 1.0 / oldwidth * oldheight)
    d_s = "Video scaled to (M, N, T) = (%d, %d, %d)." % (height, width, length)

    # Reshape and scale the data
    savedata = np.zeros((length, height, width, 3), dtype=np.float32)
    for i in range(length):
        if is_ndarray:
            frame = skimage.img_as_float(data[i, ...])
        elif is_timeseries:
            frame = data.data[..., i] / float(data.data.max())
            frame = skimage.img_as_float(frame)

        # resize wants the data to be between 0 and 1
        frame = sic.gray2rgb(sit.resize(frame, (height, width),
                                        mode='reflect'))
        savedata[i, ...] = frame * 255.0

    # Set the input frame rate and frame size
    inputdict = {}
    inputdict['-r'] = str(int(fps))
    inputdict['-s'] = '%dx%d' % (width, height)

    # Set the output frame rate
    outputdict = {}
    outputdict['-r'] = str(int(fps))

    # Save data to file
    svio.vwrite(filename,
                savedata,
                inputdict=inputdict,
                outputdict=outputdict,
                backend=backend)
    logging.getLogger(__name__).info("Saved video to file '%s'." % filename)
Example #51
0
def cv2_image_to_caffe(image):
    return skimage.img_as_float(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).astype(
        np.float32)
Example #52
0
    array = np.array([
        -math.pi, math.pi, -math.pi / 2, -math.pi * 3 / 4, -math.pi / 4, 0,
        math.pi / 4, math.pi / 2, math.pi * 3 / 4
    ])
    idx = (np.abs(array - value)).argmin()
    if (idx == 0 or idx == 1):
        return 0.0
    return array[idx]


filename = input("The file you want to test: ")
T_low = float(input("The low threshold: "))
T_high = float(input("The high threshold: "))

image = io.imread(filename)
image = skimage.img_as_float(image)
image_grey = rgb2grey(image)
T_high = 0.01
T_low = 0.005
gaussian_y = filter_x(3, 3)
gaussian_x = filter_y(3, 3)

image_x = convolve2d(image_grey, gaussian_x)
image_y = convolve2d(image_grey, gaussian_y)
image_F = np.zeros(image_grey.shape)
image_D = np.zeros(image_grey.shape)

for i in range(0, image.shape[0]):
    for j in range(0, image.shape[1]):
        image_F[i][j] = math.sqrt(image_x[i][j] * image_x[i][j] +
                                  image_y[i][j] * image_y[i][j])
 def reshape_image_and_convert_to_float(self, image_path, resize_int):
     img = Image.open(image_path)
     img = img.resize((resize_int, resize_int), PIL.Image.ANTIALIAS)
     return skimage.img_as_float(img)
Example #54
0
def test_float_float_all_ranges():
    arr_in = np.array([[-10., 10., 1e20]], dtype=np.float32)
    np.testing.assert_array_equal(img_as_float(arr_in), arr_in)
    def predict_using_trained_model(self,
                                    images_dir,
                                    plot=False
                                    ):  #, typepassedin1, typepassedin2):
        all_images_directory = [
            images_dir + "{}".format(i) for i in os.listdir(images_dir)
        ]

        _, vals_R, vals_G, vals_B = self.create_lists(all_images_directory,
                                                      self.resize_int)

        images_flattened_R = []
        for image in vals_R:
            images_flattened_R.append(np.array(image).flatten())

        images_flattened_G = []
        for image in vals_G:
            images_flattened_G.append(np.array(image).flatten())

        images_flattened_B = []
        for image in vals_B:
            images_flattened_B.append(np.array(image).flatten())

        images_principal_components_R = self.pca_R.transform(
            images_flattened_R)
        images_principal_components_G = self.pca_G.transform(
            images_flattened_G)
        images_principal_components_B = self.pca_B.transform(
            images_flattened_B)

        # print(images_principal_components_R)
        # images_principal_components_R_tans = self.pca_R.fit_transform(images_flattened_R)
        # print(images_principal_components_R_tans)
        X = []
        for i in range(len(images_principal_components_R)):
            X.append(
                np.concatenate((images_principal_components_R[i],
                                images_principal_components_G[i],
                                images_principal_components_B[i])))

        predictions = self.model.predict_classes(np.array(X))

        predictions_to_return = []
        for p in predictions:
            if p == 0:
                predictions_to_return.append(self.classnames[1])
            else:
                predictions_to_return.append(self.classnames[0])

        if plot:
            for i in range(len(predictions_to_return)):
                print(predictions_to_return[i])
                imgP = Image.open(all_images_directory[i])
                imgP = imgP.resize((50, 50), PIL.Image.ANTIALIAS)
                prediction_as_floats = skimage.img_as_float(imgP)
                #  display.display(Image.open(all_images_directory[i]))
                plt.figure()
                plt.imshow(prediction_as_floats)
                plt.grid(False)
                plt.show()

        return predictions_to_return
Example #56
0
def test_float32_passthrough():
    x = np.array([-1, 1], dtype=np.float32)
    y = img_as_float(x)
    assert_equal(y.dtype, x.dtype)
Example #57
0
import matplotlib.pyplot as plt

from skimage.restoration import (denoise_tv_chambolle, denoise_bilateral, denoise_wavelet, denoise_nl_means, denoise_tv_bregman, estimate_sigma)
from skimage import img_as_float
from skimage.util import random_noise

img = plt.imread("NoiseReduction.png")
img = img_as_float(img[100:250, 50:300])

fig, field = plt.subplots(nrows=4, ncols=3, figsize=(10, 7), sharex=True, sharey=True)
plt.gray()

sigma = 0.155
noisy = random_noise(img, var=sigma**2)

denoised_wavelet = denoise_wavelet(noisy, rescale_sigma=True, multichannel=True)
denoised_bilateral = denoise_bilateral(noisy, sigma_color=0.1, sigma_spatial=15, multichannel=True)
denoised_tv_chambolle = denoise_tv_chambolle(noisy, weight=0.5, multichannel=True)
denoised_nl_means = denoise_nl_means(noisy, multichannel=True)
denoised_tv_bregman = denoise_tv_bregman(noisy, weight=0.5)

field[0,0].axis('off')
field[0,1].axis('off')
field[0,1].set_title('Original')
field[0,1].imshow(img)
field[0,2].axis('off')
field[1,0].axis('off')
field[1,1].axis('off')
field[1,1].set_title('Noisy')
field[1,1].imshow(noisy)
field[1,2].axis('off')
Example #58
0
In 2D, color images are often represented in RGB---3 layers of 2D arrays, where
the 3 layers represent (R)ed, (G)reen and (B)lue channels of the image. The
simplest way of getting a tinted image is to set each RGB channel to the
grayscale image scaled by a different multiplier for each channel. For example,
multiplying the green and blue channels by 0 leaves only the red channel and
produces a bright red image. Similarly, zeroing-out the blue channel leaves
only the red and green channels, which combine to form yellow.
"""

import matplotlib.pyplot as plt
from skimage import data
from skimage import color
from skimage import img_as_float

grayscale_image = img_as_float(data.camera()[::2, ::2])
image = color.gray2rgb(grayscale_image)

red_multiplier = [1, 0, 0]
yellow_multiplier = [1, 1, 0]

fig, (ax1, ax2) = plt.subplots(ncols=2,
                               figsize=(8, 4),
                               sharex=True,
                               sharey=True)
ax1.imshow(red_multiplier * image)
ax2.imshow(yellow_multiplier * image)

######################################################################
# In many cases, dealing with RGB values may not be ideal. Because of that,
# there are many other `color spaces`_ in which you can represent a color
Example #59
0
    if len(line) > 0:
        line = sorted(line, key=lambda x: x[1])
        lines.append(line)
    return lines


true_K = [8, 5, 3, 3]  # ground truth lines of texts
characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
test_y = [
    "TODOLIST1MAKEATODOLIST2CHECKOFFTHEFIRSTTHINGONTODOLIST3REALIZEYOUHAVEALREADYCOMPLETED2THINGS4REWARDYOURSELFWITHANAP",
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890",
    "HAIKUSAREEASYBUTSOMETIMESTHEYDONTMAKESENSEREFRIGERATOR",
    "DEEPLEARNINGDEEPERLEARNINGDEEPESTLEARNING"
]
for i, img in enumerate(os.listdir('../images')):
    im1 = skimage.img_as_float(
        skimage.io.imread(os.path.join('../images', img)))
    bboxes, bw = findLetters(im1)

    plt.imshow(bw, cmap='gray')
    for bbox in bboxes:
        minr, minc, maxr, maxc = bbox
        rect = matplotlib.patches.Rectangle((minc, minr),
                                            maxc - minc,
                                            maxr - minr,
                                            fill=False,
                                            edgecolor='red',
                                            linewidth=2)
        plt.gca().add_patch(rect)
    plt.show()
    # find the rows using..RANSAC, counting, clustering, etc.
    lines = cluster(np.array(bboxes), true_K[i])
Example #60
0
    if sigma == 0: return kernels
    gauss = op.gauss2d(sigma=(sigma, sigma),
                       order=(0, 0),
                       angle=0,
                       nstd=0.67,
                       normalize=True)
    return [
        correlate2d(np.array(ker), np.array(gauss), mode='full')
        for ker in kernels
    ]


#############################################################################

# damaged image and mask
example_setup = img_as_float(imread('./data/image.png')[:, :, 0:3])
mask = np.zeros_like(example_setup[..., 0]).astype(np.bool)
for x, y in coo:
    mask[y - 15:y + 16, x - 15:x + 16] = True
example_setup[mask, :] = 1.0
imsave(output_dir + "example_setup.png", example_setup)
# imsave("./data/mask.png", mask.astype(np.float) )
# exit()

# initialization
image = img_as_float(imread('./data/image.png')[:, :, 0:3])
init_image = image.copy()
init_image[mask, :] = resize(
    pyramid_expand(pyramid_reduce(image, 10, multichannel=True),
                   10,
                   multichannel=True), image.shape)[mask, :]