def saver(stepName, img, dbg=None, mode=mode):        
        path = (processedDir / str(imgName)).with_suffix(".{}.png".format(stepName) if stepName else ".png")
        
        if mode == 'cache' and processedDir and imgName:
            mode = 'save'
            if path.exists():
                print("Loading cached image:", path)
                img = ski.img_as_ubyte(io.imread(str(path)))
                mode = 'done'
            elif isinstance(img,type(None)):
                print("Caching image:", path)
                img = ski.img_as_ubyte(io.imread(str(imgName)))
            
        assert not isinstance(img,type(None))
        
        if mode == 'save' and processedDir and imgName:
            try:
                print("Saving:", img.shape, img.dtype, path.name, flush=True, )
                pil_img = PIL.Image.fromarray(img_as_ubyte(img))
                pil_img.save(str(path))
                if dbg:
                    dbg.saved_path = path
            except Exception as err:
                print("Error Saving:",path, err, flush=True, )

        elif mode == 'plot':
            plt.imshow(img)
            plt.suptitle(stepName+" "+imgName.name)
            plt.show(block=True)
            plt.close()

        return img
Beispiel #2
0
def _apply(func8, func16, image, selem, out, mask, shift_x, shift_y):
    selem = img_as_ubyte(selem > 0)
    image = np.ascontiguousarray(image)

    if mask is None:
        mask = np.ones(image.shape, dtype=np.uint8)
    else:
        mask = np.ascontiguousarray(mask)
        mask = img_as_ubyte(mask)

    if image is out:
        raise NotImplementedError("Cannot perform rank operation in place.")

    is_8bit = image.dtype in (np.uint8, np.int8)

    if func8 is not None and (is_8bit or func16 is None):
        out = _apply8(func8, image, selem, out, mask, shift_x, shift_y)
    else:
        image = img_as_uint(image)
        if out is None:
            out = np.zeros(image.shape, dtype=np.uint16)
        bitdepth = find_bitdepth(image)
        if bitdepth > 11:
            image = image >> 4
            bitdepth = find_bitdepth(image)
        func16(image, selem, shift_x=shift_x, shift_y=shift_y, mask=mask,
               bitdepth=bitdepth + 1, out=out)

    return out
Beispiel #3
0
def color_check(plugin, fmt='png'):
    """Check roundtrip behavior for color images.

    All major input types should be handled as ubytes and read
    back correctly.
    """
    img = img_as_ubyte(data.chelsea())
    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)
    testing.assert_allclose(r3, 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_ubyte(img4))

    img5 = img_as_uint(img)
    r5 = roundtrip(img5, plugin, fmt)
    testing.assert_allclose(r5, img)
Beispiel #4
0
def _handle_input(image, selem, out, mask, out_dtype=None):

    if image.dtype not in (np.uint8, np.uint16):
        image = img_as_ubyte(image)

    selem = np.ascontiguousarray(img_as_ubyte(selem > 0))
    image = np.ascontiguousarray(image)

    if mask is None:
        mask = np.ones(image.shape, dtype=np.uint8)
    else:
        mask = img_as_ubyte(mask)
        mask = np.ascontiguousarray(mask)

    if out is None:
        if out_dtype is None:
            out_dtype = image.dtype
        out = np.empty_like(image, dtype=out_dtype)

    if image is out:
        raise NotImplementedError("Cannot perform rank operation in place.")

    is_8bit = image.dtype in (np.uint8, np.int8)

    if is_8bit:
        max_bin = 255
    else:
        max_bin = max(4, image.max())

    bitdepth = int(np.log2(max_bin))
    if bitdepth > 10:
        warnings.warn("Bitdepth of %d may result in bad rank filter "
                      "performance due to large number of bins." % bitdepth)

    return image, selem, out, mask, max_bin
def worker(input_file_path, queue):
    int_values = []
    path_sections = input_file_path.split("/")

    for string in path_sections:
        if re.search(r'\d+', string) is not None:
            int_values.append(int(re.search(r'\d+', string).group()))

    file_count = int_values[-1]

    image = cv2.imread(input_file_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    edges = img_as_ubyte(canny(image, sigma=canny_sigma))
    img_bw = cv2.threshold(edges, 250, 255, cv2.THRESH_BINARY)[1]

    point = _find_bottom_edge(img_bw)

    try:
        distance = len(img_bw) - point[1]
    except TypeError:
        try:
            edges = img_as_ubyte(canny(image, sigma=canny_sigma_closeup))
            img_bw = cv2.threshold(edges, 250, 255, cv2.THRESH_BINARY)[1]

            distance = len(img_bw) - point[1]
        except TypeError:
            distance = 0

    output = str(file_count) + ":" + str(distance) + "\n"
    queue.put(output)

    return output
def define_matrix(image, number_of_iterations = 5000, termination_eps = 1e-10, warp = 'Affine'):

    warp_mode_dct = {
    'Translation' : cv2.MOTION_TRANSLATION,
    'Affine' : cv2.MOTION_AFFINE,
    'Euclidean' : cv2.MOTION_EUCLIDEAN,
    'Homography' : cv2.MOTION_HOMOGRAPHY
    }

    img = oib.image_reorder(image)

    color1 = img_as_ubyte(img[0,0,:,:,0])
    color2 = img_as_ubyte(img[0,0,:,:,1])

    warp_mode = warp_mode_dct.pop('%s' % warp)

    if warp_mode == cv2.MOTION_HOMOGRAPHY :
        warp_matrix = np.eye(3, 3, dtype=np.float32)
    else :
        warp_matrix = np.eye(2, 3, dtype=np.float32)

    number_of_iterations = number_of_iterations
    termination_eps = termination_eps
    criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, number_of_iterations,  termination_eps)

    (cc, warp_matrix) = cv2.findTransformECC (color1,color2,warp_matrix, warp_mode, criteria)

    return warp_matrix
Beispiel #7
0
def dilation(image, selem=None, out=None, shift_x=False, shift_y=False):
    """Return greyscale morphological dilation of an image.

    Morphological dilation sets a pixel at (i,j) to the maximum over all pixels
    in the neighborhood centered at (i,j). Dilation enlarges bright regions
    and shrinks dark regions.

    Parameters
    ----------

    image : ndarray
        Image array.
    selem : ndarray, optional
        The neighborhood expressed as a 2-D array of 1's and 0's.
        If None, use cross-shaped structuring element (connectivity=1).
    out : ndarray, optional
        The array to store the result of the morphology. If None, is
        passed, a new array will be allocated.
    shift_x, shift_y : bool, optional
        shift structuring element about center point. This only affects
        eccentric structuring elements (i.e. selem with even numbered sides).

    Returns
    -------
    dilated : uint8 array
        The result of the morphological dilation.

    Notes
    -----
    For `uint8` (and `uint16` up to a certain bit-depth) data, the lower
    algorithm complexity makes the `skimage.filter.rank.maximum` function more
    efficient for larger images and structuring elements.

    Examples
    --------
    >>> # Dilation enlarges bright regions
    >>> import numpy as np
    >>> from skimage.morphology import square
    >>> bright_pixel = np.array([[0, 0, 0, 0, 0],
    ...                          [0, 0, 0, 0, 0],
    ...                          [0, 0, 1, 0, 0],
    ...                          [0, 0, 0, 0, 0],
    ...                          [0, 0, 0, 0, 0]], dtype=np.uint8)
    >>> dilation(bright_pixel, square(3))
    array([[0, 0, 0, 0, 0],
           [0, 1, 1, 1, 0],
           [0, 1, 1, 1, 0],
           [0, 1, 1, 1, 0],
           [0, 0, 0, 0, 0]], dtype=uint8)

    """

    if image is out:
        raise NotImplementedError("In-place dilation not supported!")

    image = img_as_ubyte(image)
    selem = img_as_ubyte(selem)
    return cmorph._dilate(image, selem, out=out,
                          shift_x=shift_x, shift_y=shift_y)
Beispiel #8
0
def dynamic_masking(image,method='edges',filter_size=7,threshold=0.005):
    """ Dynamically masks out the objects in the PIV images
    
    Parameters
    ----------
    image: image
        a two dimensional array of uint16, uint8 or similar type
        
    method: string
        'edges' or 'intensity':
        'edges' method is used for relatively dark and sharp objects, with visible edges, on 
        dark backgrounds, i.e. low contrast
        'intensity' method is useful for smooth bright objects or dark objects or vice versa, 
        i.e. images with high contrast between the object and the background
    
    filter_size: integer
        a scalar that defines the size of the Gaussian filter
    
    threshold: float
        a value of the threshold to segment the background from the object
        default value: None, replaced by sckimage.filter.threshold_otsu value
            
    Returns
    -------
    image : array of the same datatype as the incoming image with the object masked out
        as a completely black region(s) of zeros (integers or floats).
    
    
    Example
    --------
    frame_a  = openpiv.tools.imread( 'Camera1-001.tif' )
    imshow(frame_a) # original
    
    frame_a = dynamic_masking(frame_a,method='edges',filter_size=7,threshold=0.005)
    imshow(frame_a) # masked 
        
    """
    imcopy = np.copy(image)
    # stretch the histogram
    image = exposure.rescale_intensity(img_as_float(image), in_range=(0, 1))
    # blur the image, low-pass
    blurback = img_as_ubyte(gaussian_filter(image,filter_size))
    if method is 'edges':
        # identify edges
        edges = sobel(blurback)
        blur_edges = gaussian_filter(edges,21)
        # create the boolean mask 
        bw = (blur_edges > threshold)
        bw = img_as_ubyte(binary_fill_holes(bw))
        imcopy -= blurback
        imcopy[bw] = 0.0
    elif method is 'intensity':
        background = gaussian_filter(median_filter(image,filter_size),filter_size)
        imcopy[background > threshold_otsu(background)] = 0

        
    return imcopy #image
Beispiel #9
0
def preprocess(filename):
    image = img_as_ubyte(io.imread(filename, as_grey = True))

    if image.shape[0] != 768:
        print(image.shape)
        print("WARN: Resizing image to old iPad Size. TODO> Move forward to retina images!")
        return img_as_ubyte(transform.resize(image, (768, 1024)))

    return image
def test_threshold_minimum():
    camera = skimage.img_as_ubyte(data.camera())

    threshold = threshold_minimum(camera)
    assert_equal(threshold, 76)

    astronaut = skimage.img_as_ubyte(data.astronaut())
    threshold = threshold_minimum(astronaut)
    assert_equal(threshold, 114)
Beispiel #11
0
 def test_divide_and_reassemble(self):
     pic = img_as_ubyte(io.imread("./kitteh.jpg"))
     pic = resize(pic, (500,500), mode='nearest')
     pic = img_as_ubyte(pic)
     print "size "+str(pic.shape)
     for shape in [(10,10),(10,20),(30,30),(33,48)]:
         print shape
         parts = divide_into_parts(pic,*shape)
         pic2 = assemble_from_parts(parts, False, False)
         self.assertTrue((pic==pic2).all())
Beispiel #12
0
def open_image(address):
    image = imread(address)
    gray_image = rgb2gray(image)
    r = image[:,:,0]
    g = image[:,:,1]
    b = image[:,:,2]
#    
#    m,n,_ = image.shape
#
#    gray_image = img_as_ubyte(gray_image)
    
    return img_as_ubyte(gray_image), img_as_ubyte(r), img_as_ubyte(g), img_as_ubyte(b)
def test_threshold_minimum():
    camera = skimage.img_as_ubyte(data.camera())

    threshold = threshold_minimum(camera)
    assert threshold == 76

    threshold = threshold_minimum(camera, bias='max')
    assert threshold == 77

    astronaut = skimage.img_as_ubyte(data.astronaut())
    threshold = threshold_minimum(astronaut)
    assert threshold == 117
Beispiel #14
0
 def load_disp_image(self, img_name,j,display_only=False):
     print ("Setting image: %s" % str(img_name))
     try:
         temp_img = img_as_ubyte(imread(img_name))
     except:
         print("Error reading file, setting zero image")
         h_img=self.exp1.active_params.m_params.imx
         v_img=self.exp1.active_params.m_params.imy
         temp_img = img_as_ubyte(np.zeros((h_img,v_img)))
     if not display_only:
         ptv.py_set_img(temp_img,j)
     if len(temp_img)>0:
         self.camera_list[j].update_image(temp_img)
Beispiel #15
0
def dilation(image, selem, out=None, shift_x=False, shift_y=False):
    """Return greyscale morphological dilation of an image.

    Morphological dilation sets a pixel at (i,j) to the maximum over all pixels
    in the neighborhood centered at (i,j). Dilation enlarges bright regions
    and shrinks dark regions.

    Parameters
    ----------

    image : ndarray
        Image array.
    selem : ndarray
        The neighborhood expressed as a 2-D array of 1's and 0's.
    out : ndarray
        The array to store the result of the morphology. If None, is
        passed, a new array will be allocated.
    shift_x, shift_y : bool
        shift structuring element about center point. This only affects
        eccentric structuring elements (i.e. selem with even numbered sides).

    Returns
    -------
    dilated : uint8 array
        The result of the morphological dilation.

    Examples
    --------
    >>> # Dilation enlarges bright regions
    >>> import numpy as np
    >>> from skimage.morphology import square
    >>> bright_pixel = np.array([[0, 0, 0, 0, 0],
    ...                          [0, 0, 0, 0, 0],
    ...                          [0, 0, 1, 0, 0],
    ...                          [0, 0, 0, 0, 0],
    ...                          [0, 0, 0, 0, 0]], dtype=np.uint8)
    >>> dilation(bright_pixel, square(3))
    array([[0, 0, 0, 0, 0],
           [0, 1, 1, 1, 0],
           [0, 1, 1, 1, 0],
           [0, 1, 1, 1, 0],
           [0, 0, 0, 0, 0]], dtype=uint8)

    """

    if image is out:
        raise NotImplementedError("In-place dilation not supported!")
    image = img_as_ubyte(image)
    selem = img_as_ubyte(selem)
    return cmorph._dilate(image, selem, out=out,
                          shift_x=shift_x, shift_y=shift_y)
def noise_reduction(image, background, window_size = 5, mode = 0):
	#  GrayScale image use mode = 0
	#  mode = 0: dealing RGB image with each channel 
	#  mode!= 0: dealing RGB image with HSV value 
	if(mode == 0):
		med_image = median_each(image, disk(window_size))
		med_bckg  = median_each(background, disk(window_size))

	else :
		med_image = img_as_ubyte(median_hsv(image, disk(window_size)))
		med_bckg  = img_as_ubyte(median_hsv(background, disk(window_size)))

	norm_image = np.true_divide(med_image, (med_bckg/255*254+1))/255	
	return norm_image
Beispiel #17
0
def _merge_images(img_top, img_bottom, mask=0):
    """
    Function to combine two images with mask by replacing all pixels of img_bottom which
    equals to mask by pixels from img_top.

    :param img_top: greyscale image which will replace masked pixels
    :param img_bottom: greyscale image which pixels will be replace
    :param mask: pixel value to be used as mask (int)
    :return: combined greyscale image
    """
    img_top = skimage.img_as_ubyte(img_top)
    img_bottom = skimage.img_as_ubyte(img_bottom)
    merge_layer = img_top == mask
    img_top[merge_layer] = img_bottom[merge_layer]
    return img_top
Beispiel #18
0
def get_textural_features(img, isMultidirectional=False, distance=1):
    '''Extract GLCM feature vector from image
    Args:
        img: input image.

        isMultidirectional: Controls whether co-occurence should be calculated
            in other directions (ie 45 degrees, 90 degrees and 135 degrees).

        distance: Distance between pixels for co-occurence.

    Returns:
        features: if isMultidirectional=False, this is a 4 element vector of
        [dissimilarity, correlation,homogeneity, energy]. If not it is a 16
        element vector containing each of the above properties in each direction.
    '''
    if(isMultidirectional):
        img = img_as_ubyte(rgb2gray(img))
        glcm = greycomatrix(img, [distance], [0, 0.79, 1.57, 2.36], 256, symmetric=True, normed=True)
        dissimilarity_1 = greycoprops(glcm, 'dissimilarity')[0][0]
        dissimilarity_2 = greycoprops(glcm, 'dissimilarity')[0][1]
        dissimilarity_3 = greycoprops(glcm, 'dissimilarity')[0][2]
        dissimilarity_4 = greycoprops(glcm, 'dissimilarity')[0][3]
        correlation_1 = greycoprops(glcm, 'correlation')[0][0]
        correlation_2 = greycoprops(glcm, 'correlation')[0][1]
        correlation_3 = greycoprops(glcm, 'correlation')[0][2]
        correlation_4 = greycoprops(glcm, 'correlation')[0][3]
        homogeneity_1 = greycoprops(glcm, 'homogeneity')[0][0]
        homogeneity_2 = greycoprops(glcm, 'homogeneity')[0][1]
        homogeneity_3 = greycoprops(glcm, 'homogeneity')[0][2]
        homogeneity_4 = greycoprops(glcm, 'homogeneity')[0][3]
        energy_1 = greycoprops(glcm, 'energy')[0][0]
        energy_2 = greycoprops(glcm, 'energy')[0][1]
        energy_3 = greycoprops(glcm, 'energy')[0][2]
        energy_4 = greycoprops(glcm, 'energy')[0][3]
        feature = np.array([dissimilarity_1, dissimilarity_2, dissimilarity_3,\
         dissimilarity_4, correlation_1, correlation_2, correlation_3, correlation_4,\
         homogeneity_1, homogeneity_2, homogeneity_3, homogeneity_4, energy_1,\
         energy_2, energy_3, energy_4])
        return feature
    else:
        img = img_as_ubyte(rgb2gray(img))
        glcm = greycomatrix(img, [distance], [0], 256, symmetric=True, normed=True)
        dissimilarity = greycoprops(glcm, 'dissimilarity')[0][0]
        correlation = greycoprops(glcm, 'correlation')[0][0]
        homogeneity = greycoprops(glcm, 'homogeneity')[0][0]
        energy = greycoprops(glcm, 'energy')[0][0]
        feature = np.array([dissimilarity, correlation, homogeneity, energy])
        return feature
Beispiel #19
0
 def didClickSubmitButton(self, event):
     print(self.imageFilePath)
     img = img_as_ubyte(io.imread(CLUSTER_IMAGE_FILENAME))
     roi_img = spectral_roi.extract_roi(img, gui_checkbox_handlers.getSelectedClusters())
     roi_img_filename = "{}.png".format(Helper.generate_random_id())
     io.imsave(roi_img_filename, roi_img)
     Display.show_image(roi_img, roi_img_filename)
Beispiel #20
0
    def __init__(self, path=None, array=None, xy_array=None):
        self._modified = False
        self.scale = 1
        self._path = None
        self._format = None

        n_args = len([a for a in [path, array, xy_array] if a is not None])
        if n_args != 1:
            msg = "Must provide a single keyword arg (path, array, xy_array)."
            ValueError(msg)
        elif path is not None:
            if not is_url(path):
                path = os.path.abspath(path)
            self._path = path
            with file_or_url_context(path) as context:
                self.array = img_as_ubyte(io.imread(context))
                self._format = imghdr.what(context)
        elif array is not None:
            self.array = array
        elif xy_array is not None:
            self.xy_array = xy_array

        # Force RGBA internally (use max alpha)
        if self.array.shape[-1] == 3:
            self.array = np.insert(self.array, 3, values=255, axis=2)
Beispiel #21
0
def test_equalize_ubyte():
    with expected_warnings(['precision loss']):
        img = skimage.img_as_ubyte(test_img)
    img_eq = exposure.equalize_hist(img)

    cdf, bin_edges = exposure.cumulative_distribution(img_eq)
    check_cdf_slope(cdf)
Beispiel #22
0
def height_texture(z, colormap = 'viridis'):
    """Create a texture corresponding to the heights in z and the given colormap."""
    from matplotlib import cm
    from skimage import img_as_ubyte
    import numpy as np

    colormap = cm.get_cmap(colormap)
    im = z.copy()
    # rescale to be in [0,1], scale nan to be the smallest value
    im -= np.nanmin(im)
    im /= np.nanmax(im)
    im = np.nan_to_num(im)

    import warnings
    with warnings.catch_warnings():
        # ignore the precision warning that comes from converting floats to uint8 types
        warnings.filterwarnings('ignore',
                                message='Possible precision loss when converting from',
                                category=UserWarning,
                                module='skimage.util.dtype')
        rgba_im = img_as_ubyte(colormap(im))  # convert the values to rgba image using the colormap

    rgba_list = list(rgba_im.flat)  # make a flat list

    return DataTexture(data=rgba_list, format='RGBAFormat', width=z.shape[1], height=z.shape[0])
def transformation(image, q):
	imagesize = image.shape
	newpixels = np.zeros(shape=imagesize)
	print q

	log255 = m.log(255)
	Rlog = np.zeros(shape=(imagesize[0], imagesize[1]))
	Glog = np.zeros(shape=(imagesize[0], imagesize[1]))
	Blog = np.zeros(shape=(imagesize[0], imagesize[1]))

	Rlog[:,:] = -((255.0*np.log(image[:,:,0]+1)/255)/log255)
	Glog[:,:] = -((255.0*np.log(image[:,:,1]+1)/255)/log255)
	Blog[:,:] = -((255.0*np.log(image[:,:,2]+1)/255)/log255)

	for i in (0,1,2):
		Rscaled = Rlog[:,:]*q[(i-1)*3+1]
		Gscaled = Glog[:,:]*q[(i-1)*3+2]
		Bscaled = Blog[:,:]*q[i*3]

		output = np.exp(-((Rscaled+Gscaled+Bscaled)-255)*np.log(255)/255)
		output [output > 255] = 255

		newpixels[:,:,i] = np.floor(output+0.5)

	print newpixels.shape
	return img_as_ubyte(newpixels)
Beispiel #24
0
def _apply8(func8, image, selem, out, mask, shift_x, shift_y):
    if out is None:
        out = np.zeros(image.shape, dtype=np.uint8)
    image = img_as_ubyte(image)
    func8(image, selem, shift_x=shift_x, shift_y=shift_y,
          mask=mask, out=out)
    return out
Beispiel #25
0
def test_li_astro_image():
    image = skimage.img_as_ubyte(data.astronaut())
    threshold = threshold_li(image)
    ce_actual = _cross_entropy(image, threshold)
    assert 64 < threshold < 65
    assert ce_actual < _cross_entropy(image, threshold + 1)
    assert ce_actual < _cross_entropy(image, threshold - 1)
def test_color_gmm():
    print 'Entering: test_color'
    dataset = data_loader.ColorizeImageNet(
                root, split='val', set='tiny',
                bins='soft', num_hc_bins=16,
                gmm_path=GMM_PATH, mean_l_path=MEAN_L_PATH)

    img, labels = dataset.__getitem__(0)
    gmm = dataset.gmm

    labels = labels.numpy()
    img = img.squeeze().numpy()
    labels = labels.astype(gmm.means_.dtype)
    img = img.astype(gmm.means_.dtype)

    # expectation over GMM centroids
    hc_means = gmm.means_.astype(labels.dtype)
    im_hc = np.tensordot(labels, hc_means, (2,0)) 
    im_l = img + dataset.mean_l.astype(img.dtype)
    im_rgb = dataset.hue_chroma_to_rgb(im_hc, im_l)
    low, high = np.min(im_rgb), np.max(im_rgb)
    im_rgb = (im_rgb - low) / (high - low)
    im_out = img_as_ubyte(im_rgb)
    skimage.io.imsave("tests/output.png", im_out)

    img_file = dataset.files['val'][0]
    im_orig = skimage.io.imread(img_file)
    skimage.io.imsave("tests/orig.png", im_orig)
Beispiel #27
0
def preProcessing(imGrayLevel):
    
    #Escopo de algumas Operações básicas utilizadas no pré-processamento:
    #..............................................
    h = ia.iahistogram(imGrayLevel) #Equalização...
    n = imGrayLevel.size
    T = 255./n * np.cumsum(h)
    T = T.astype(uint8)
    #..............................................
    T1 = np.arange(256)  # função identidade
    T2 = ia.ianormalize(np.log(T1+30)) # logaritmica - realce partes escuras
    
    
    #T5 = ia.ianormalize(T1/50) # reduz o número de níveis de cinza
    #..................................................
    
    ax1.imshow(imRGB)
    ax1.set_title('rgb')
    
    ax2.imshow(imGrayLevel, vmin=0, vmax=255, cmap=plt.cm.gray)
    ax2.set_title('gray level')
    
    imGrayLevel =  denoise_tv_chambolle(imGrayLevel, weight=0.1, multichannel=True)
    imGrayLevel = img_as_ubyte(imGrayLevel)#Conversão de Float para UINT-8
    ax3.imshow(imGrayLevel, vmin=0, vmax=255, cmap=plt.cm.gray) #Filtro de suavização de textura
    ax3.set_title('tv signal filter')
    
    realceNucleos = T2[T[imGrayLevel]] #Realce de partes escuras da imagem equalizada
    ax4.imshow(realceNucleos, vmin=0, vmax=255, cmap=plt.cm.gray) 
    ax4.set_title('logaritimica')
    return realceNucleos
Beispiel #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 get_tight_mean_var():
    data = []
    for img_path in sorted(os.listdir(args.data_dir), key=lambda x: int(x.split('.')[0])):
        img = skio.imread(os.path.join(args.data_dir, img_path)) 
        if len(img.shape) < 3:
            img = np.dstack((img, img, img))
        img = resize(img, (args.ylen, args.xlen))
        img = skimage.img_as_ubyte(img)
        data.append(img)
    data = np.array(data)
    data = data[args.sequence_time_start:args.sequence_time_end]
    t, ylen, xlen, c = data.shape
    small_mean = np.mean(data, axis=0)
    norm_data = data - small_mean
    small_mean = np.zeros((ylen, xlen, c))
    small_var = np.zeros((ylen, xlen, c, c))
    for x in range(xlen):
        print(x)
        for y in range(ylen):
            H, edges = np.histogramdd(data[:, y, x], bins=(24,) * 3,
                                      range=((0, 255), (0, 255), (0, 255)))
            bin_idx = np.unravel_index(np.argmax(H), H.shape)
            mean = np.array([(edges[i][bin_idx[i]] + edges[i][bin_idx[i] + 1]) \
                              / 2 for i in range(len(edges))])
            small_mean[y, x] = mean

            cov = norm_data[:, y, x].T.dot(norm_data[:, y, x]) / (t - 1)
            if np.linalg.det(cov) < 1e-2:
                cov = np.eye(3)
            small_var[y, x] = cov
    return small_mean.reshape(xlen * ylen, c), small_var.flatten()
Beispiel #30
0
def test_li_camera_image():
    image = skimage.img_as_ubyte(data.camera())
    threshold = threshold_li(image)
    ce_actual = _cross_entropy(image, threshold)
    assert 62 < threshold_li(image) < 63
    assert ce_actual < _cross_entropy(image, threshold + 1)
    assert ce_actual < _cross_entropy(image, threshold - 1)
Beispiel #31
0
    # fig, ax = ski.filters.try_all_threshold(img, figsize=(10,8), verbose = False)
    # plt.show()
    thresh = ski.filters.threshold_otsu(img)
    img = (img > thresh) * 255

# Blur/Sharpen if necessary

if blur > 0:
    img = ski.filters.gaussian(img, sigma=blur)

# Sharpening TBD
#if sharpen > 0:
#    img = img - 0.3*ski.filters.gaussian(img, sigma=sharpen)

# Convert image to 8-bit
img = ski.img_as_ubyte(img)

# Save image to output path
if maskname is None:
    (input_filename, ext) = path.splitext(path.basename(imgpath))
    output_filename = 'mask_' + input_filename + '.tif'
else:
    output_filename = maskname + '.tif'

if maskpath is None:
    outputdir = path.dirname(imgpath)
else:
    outputdir = maskpath

outputpath = path.join(outputdir, output_filename)
ski.io.imsave(outputpath, img)
def showSegmentation(rgbfile,
                     labelfile,
                     pixelClasses=horsePixelClasses,
                     crfmodel=horseCRF,
                     visualizeSegmentation=True):
    start_time = time.time()

    # Read RGB and label image
    image = img_as_float(skimageIO.imread(rgbfile))
    bgrImage = cv2.imread(rgbfile, cv2.IMREAD_COLOR)
    #bgrImage = cv2.fastNlMeansDenoisingColored(bgrImage)
    #bgrImage = exposure.adjust_sigmoid(bgrImage)
    if len(image.shape) == 2:
        image = color.gray2rgb(image)

    # Resize
    #image = resize(image,(120,120), preserve_range=True )
    #bgrImage = resize(bgrImage,(120,120), preserve_range=True)

    # Derive superpixels and get their average RGB component
    segments = slic(image, n_segments=500, sigma=1.0)
    rgb_segments = img_as_ubyte(mark_boundaries(image, segments))
    avg_rgb = color.label2rgb(segments, image, kind='avg')

    if labelfile is not None:
        labelImage = img_as_float(skimageIO.imread(labelfile))
        #labelImage = resize(labelImage,(120,120), preserve_range=True)
        #thresh = threshold_otsu(image)
        #labelImage = labelImage > thresh
        if len(labelImage.shape) == 2:
            labelImageRGB = color.gray2rgb(labelImage)
        else:
            labelImageRGB = labelImage
        label_segments = img_as_ubyte(mark_boundaries(labelImageRGB, segments))
        avg_label = color.label2rgb(segments, labelImageRGB, kind='avg')

    # Create graph of superpixels and compute their centers
    vertices, edges = imagesegmentation.make_graph(segments)
    gridx, gridy = numpy.mgrid[:segments.shape[0], :segments.shape[1]]
    centers = dict()
    for v in vertices:
        centers[v] = [gridy[segments == v].mean(), gridx[segments == v].mean()]

    # compute for features
    xInstance = []
    yInstance = []
    n_features = []
    n_labels = []
    edge_features = []

    for v in vertices:
        # unary feature - average rgb of superpixel
        avg_rgb2 = avg_rgb[int(centers[v][1])][int(centers[v][0])]
        hist, hogFeatures = imagesegmentation.getHistogramFeatures(
            bgrImage,
            int(centers[v][1]),
            int(centers[v][0]),
            forUnaryFeature=True)
        node_feature = numpy.concatenate([avg_rgb2, hist, hogFeatures])
        n_features.append(node_feature)

        # label
        if labelfile is not None:
            minEuclideanDistance = numpy.inf  # simulate infinity
            pixelClass = -1
            for i in range(0, len(pixelClasses)):
                # set the label of the superpixel to the pixelClass with minimum euclidean distance
                dist = numpy.linalg.norm(
                    avg_label[int(centers[v][1])][int(centers[v][0])] -
                    pixelClasses[i])
                if dist < minEuclideanDistance:
                    pixelClass = i
                    minEuclideanDistance = dist
            n_labels.append(pixelClass)

    histogramCache = {}
    for e in edges:
        # pairwise feature - euclidean distance of adjacent superpixels
        dist = numpy.linalg.norm(
            avg_rgb[int(centers[e[0]][1])][int(centers[e[0]][0])] -
            avg_rgb[int(centers[e[1]][1])][int(centers[e[1]][0])])

        if e[0] not in histogramCache:
            hist1, lbphist1 = imagesegmentation.getHistogramFeatures(
                bgrImage, int(centers[e[0]][1]), int(centers[e[0]][0]))
            histogramCache[e[0]] = {'hist': hist1, 'lbphist': lbphist1}
        else:
            hist1 = histogramCache[e[0]]['hist']
            lbphist1 = histogramCache[e[0]]['lbphist']
        if e[1] not in histogramCache:
            hist2, lbphist2 = imagesegmentation.getHistogramFeatures(
                bgrImage, int(centers[e[1]][1]), int(centers[e[1]][0]))
            histogramCache[e[1]] = {'hist': hist2, 'lbphist': lbphist2}
        else:
            hist2 = histogramCache[e[1]]['hist']
            lbphist2 = histogramCache[e[1]]['lbphist']

        histogramDist = cv2.compareHist(hist1, hist2,
                                        3)  # Bhattacharyya distance
        textureSimilarity = imagesegmentation.kullback_leibler_divergence(
            lbphist1, lbphist2)  # KL divergence

        pairwise_feature = numpy.array(
            [dist, histogramDist, textureSimilarity])
        edge_features.append(pairwise_feature)

    xInstance.append((numpy.array(n_features), numpy.array(edges),
                      numpy.array(edge_features)))
    yInstance.append(numpy.array(n_labels))

    # Create superpixeled image
    if labelfile is not None:
        labeledSuperPixeledRGB = numpy.zeros(labelImageRGB.shape)
        labeledSuperPixeled = numpy.zeros(labelImage.shape)
        #print labeledSuperPixeled.shape
        #print labelImageRGB.shape
        for i in range(0, labeledSuperPixeledRGB.shape[0]):
            for j in range(0, labeledSuperPixeledRGB.shape[1]):
                labeledSuperPixeledRGB[i][j] = pixelClasses[n_labels[
                    segments[i][j]]]
                labeledSuperPixeled[i][j] = n_labels[segments[i][j]]

    # Predict with CRF and build image label
    y_pred = crfmodel.predict(numpy.array(xInstance))
    labeledPredictionRGB = numpy.zeros(image.shape)
    labeledPrediction = numpy.zeros((image.shape[0], image.shape[1]))
    #print labeledPrediction.shape
    for i in range(0, labeledPredictionRGB.shape[0]):
        for j in range(0, labeledPredictionRGB.shape[1]):
            labeledPredictionRGB[i][j] = pixelClasses[y_pred[0][segments[i]
                                                                [j]]]
            labeledPrediction[i][j] = y_pred[0][segments[i][j]]

    # Print performance
    if labelfile is not None:
        pixelwise_accuracy = accuracy_score(
            labelImage.flatten().flatten(),
            labeledPrediction.flatten().flatten())
        pixelwise_precision = precision_score(
            labelImage.flatten().flatten(),
            labeledPrediction.flatten().flatten())
        pixelwise_recall = recall_score(labelImage.flatten().flatten(),
                                        labeledPrediction.flatten().flatten())
        pixelwise_f1 = f1_score(labelImage.flatten().flatten(),
                                labeledPrediction.flatten().flatten())
        pixelwise_so = foregroundQualityScore(
            labelImage.flatten().flatten(),
            labeledPrediction.flatten().flatten())

        # comment on f1 score
        if pixelwise_f1 >= 0.9:
            comment = ' <------------ HIGH!'
        elif pixelwise_f1 <= 0.8:
            comment = ' <------------ LOW!'
        else:
            comment = ''

        print ''
        print 'Segmentation completed in ' + str(time.time() -
                                                 start_time) + ' seconds.'
        print 'Total Pixels: ' + str(labelImage.flatten().flatten().shape[0])
        print 'SLIC Pixelwise Accuracy: ' + str(
            accuracy_score(labelImage.flatten().flatten(),
                           labeledSuperPixeled.flatten().flatten()))
        print ''
        print 'Pixelwise Accuracy: ' + str(pixelwise_accuracy)
        print 'Pixelwise Precision: ' + str(pixelwise_precision)
        print 'Pixelwise Recall: ' + str(pixelwise_recall)
        print 'Pixelwise F1: ' + str(pixelwise_f1) + comment
        print 'Pixelwise S0: ' + str(pixelwise_so)
    else:
        'There is no label image hence no performance stats...'

    # Show the Images
    if visualizeSegmentation:
        fig, ax = plt.subplots(2, 3)
        fig.canvas.set_window_title('Image Segmentation')
        ax[0, 0].imshow(image)
        ax[0, 0].set_title("Original Image")

        ax[0, 1].imshow(rgb_segments)
        ax[0, 1].set_title("Super Pixels")

        if labelfile is not None:
            ax[0, 2].imshow(label_segments)
            ax[1, 0].imshow(labelImageRGB)
            ax[1, 1].imshow(labeledSuperPixeledRGB)

        ax[0, 2].set_title("Segmented Ground Truth")
        ax[1, 0].set_title("Ground truth")
        ax[1, 1].set_title("Labeled Super Pixels")

        ax[1, 2].imshow(labeledPredictionRGB)
        ax[1, 2].set_title("Prediction")

        for a in ax.ravel():
            a.set_xticks(())
            a.set_yticks(())
        plt.show()

    # Save result
    #skimageIO.imsave('slic-' + ntpath.basename(rgbfile) , rgb_segments)
    #skimageIO.imsave('slicground-' + ntpath.basename(rgbfile) , label_segments)
    #skimageIO.imsave('result-' + ntpath.basename(rgbfile) , labeledPredictionRGB)

    # Return metrics
    if labelfile is not None:
        return pixelwise_accuracy, pixelwise_precision, pixelwise_recall, pixelwise_f1, pixelwise_so
    else:
        return
Beispiel #33
0
def _compute_epicflow(I1, I2, edgefile=None, matchfile=None, preset='sintel'):
    """ Internal function to compute EPIC flow.

    Moved into a separate function to be able to use caching.
    """

    path_sed = os.path.join(PATH_EPIC, 'sed', 'release')
    path_toolbox = os.path.join(PATH_EPIC, 'sed', 'toolbox')
    path_deepmatching = os.path.join(PATH_EPIC, 'deepmatching/deepmatching_1.2.2_c++')
    path_epic_bin = os.path.join(PATH_EPIC, 'EpicFlow_v1.00')

    tempdir = tempfile.mkdtemp()


    if matchfile is None:
        matchfile = os.path.join(tempdir, 'matches.txt')
    if edgefile is None:
        edgefile = os.path.join(tempdir, 'edges.dat')


    # Save images into temp dir
    if type(I1) == str:
        path_I1 = I1
    else:
        path_I1 = os.path.join(tempdir, 'image1.png')
        sio.imsave(path_I1, img_as_ubyte(I1))

    if type(I2) == str:
        path_I2 = I2
    else:
        path_I2 = os.path.join(tempdir, 'image2.png')
        sio.imsave(path_I2, img_as_ubyte(I2))

    # We need an output file for epic flow.
    path_flow = os.path.join(tempdir, 'flow.flo')

    #
    # Compute edges
    #
    if (not os.path.isfile(edgefile)) or os.path.getsize(edgefile) < 10:
        sys.stdout.write('\nComputing edges...')
        sys.stdout.flush()
        te0 = time.time()
        call = ['matlab',
                '-nodesktop',
                '-nojvm',
                '-r', "addpath('{0}'); addpath(genpath('{1}')); load('{2}/modelFinal.mat'); I = uint8(imread('{3}')); if size(I,3)==1, I = cat(3,I,I,I); end; edges = edgesDetect(I, model); fid=fopen('{4}','wb'); fwrite(fid,transpose(edges),'single'); fclose(fid); exit".format(path_sed, path_toolbox, path_epic_bin, path_I1, edgefile)]

        with open(os.devnull, 'w') as dnull:
            #subprocess.call(call, stdout=dnull,stderr=subprocess.STDOUT)
            subprocess.call(call)
        te1 = time.time()
        sys.stdout.write('done. Took {} sec'.format(te1-te0))
        sys.stdout.flush()
        assert(os.path.isfile(edgefile))
    else:
        print('Using edges from {}'.format(edgefile))

    #
    # Compute deep matches
    #
    if (not os.path.isfile(matchfile)) or os.path.getsize(matchfile) < 10:
        sys.stdout.write('\nComputing matches...')
        sys.stdout.flush()
        tm0 = time.time()

        call = [os.path.join(path_deepmatching, 'deepmatching'),
                path_I1, 
                path_I2]

        with open(matchfile, 'w') as mfile:
            subprocess.call(call, stdout=mfile)

        tm1 = time.time()

        sys.stdout.write('done. Took {} sec'.format(tm1-tm0))
        sys.stdout.flush()

        assert (os.path.isfile(matchfile))

    else:
        print('Using matches from {}'.format(matchfile))

    #
    # Compute EPIC Flow
    #
    sys.stdout.write('\nComputing flow...')
    sys.stdout.flush()
    if preset == 'sintel':
        pflag = '-sintel'
    elif preset == 'kitti':
        pflag = '-kitti'

    tf0 = time.time()
    call = [os.path.join(path_epic_bin, 'epicflow'),
        path_I1,
        path_I2,
        edgefile,
        matchfile,
        path_flow,
        pflag]
    subprocess.call(call)
    tf1 = time.time()
    sys.stdout.write('done. Took {} sec'.format(tf1-tf0))
    sys.stdout.flush()

    assert (os.path.isfile(path_flow))

    # Finally, read the flow back again and return.
    u,v = flow_read(path_flow)

    # Delete temp dir
    #shutil.rmtree(tempdir)

    return u,v

out_path = './patch_extract/' + basename(img_path).replace('.svs', '.png')
GT_path = os.path.join(fold, 'gt_thumbnails/' + basename(img_path).replace('.svs', '.png'))


scan = openslide.OpenSlide(img_path)
last_dim_n = len(scan.level_dimensions) - 1
last_dim = scan.level_dimensions[last_dim_n]
last_dim_inv = (last_dim[1], last_dim[0])
whole_img = scan.read_region((0,0), last_dim_n, last_dim)
whole_img = np.array(whole_img)[:,:,0:3]


small_img = resize(whole_img, (500, 500))
small_img = img_as_ubyte(small_img)
mask = ROI_binary_mask(small_img, ticket=(80,80,80))
mask = mask.astype('uint8')
mask[mask > 0] = 255
mask_tissue = resize(mask, last_dim_inv, order=0)
mask_tissue = img_as_ubyte(mask_tissue)
mask_tissue[mask_tissue > 0] = 255

GT_file = imread(GT_path)
GT_file[GT_file > 0] = 255
Red_area = GT_file[:,:,0]
Red_area = img_as_ubyte(resize(Red_area, last_dim_inv, order=0))
Green_area = GT_file[:,:,1]
Green_area = img_as_ubyte(resize(Green_area, last_dim_inv, order=0))
Blue_area = GT_file[:,:,2]
Blue_area = img_as_ubyte(resize(Blue_area, last_dim_inv, order=0))
Beispiel #35
0
        from csv-file read RGB image
        '''
        R_array = np.array(df.query('band == "73"'))
        R_array = R_array.reshape(-1, 1)
        G_array = np.array(df.query('band == "46"'))
        G_array = G_array.reshape(-1, 1)
        B_array = np.array(df.query('band == "15"'))
        B_array = B_array.reshape(-1, 1)
        color_img = np.hstack([R_array, G_array, B_array])
        color_img = color_img.reshape(1101, 960, 3)
        # print(color_img)
        # color_img = img_as_float(color_img)
        # print(color_img)
        color_img = color_img / 256
        color_img = color_img.astype(np.uint8)
        color_img = img_as_ubyte(color_img)

        '''
        from csv-file read gray image
        '''
        gray_img = df.query('band == "169"')
        gray_img = img_as_float(gray_img)
        gray_img = exposure.rescale_intensity(gray_img)
        gray_img = gray_img * 256
        while exposure.is_low_contrast(gray_img):
            gray_img = exposure.adjust_gamma(gray_img, 0.8)
        gray_img = gray_img.astype(np.uint8)
        gray_img = img_as_ubyte(gray_img)
        '''cv2 median filter-->>Reduce Noise'''
        '''Optional:cv2 gaussian filter-->>cv2.GaussianBlur'''
        gray_img = cv2.medianBlur(gray_img, 3)
Beispiel #36
0
def save_to_png(file_name, array):
    """Save the given numpy array as a PNG file."""
    # from skimage._shared._warnings import expected_warnings
    # with expected_warnings(['precision']):
    imsave(file_name, img_as_ubyte(array))
images_to_generate = 36  #you can change this value according to your requirement
i = 1  # variable to iterate till images_to_generate

while i <= images_to_generate:
    image = random.choice(images)
    original_image = io.imread(image)
    transformed_image = None
    #     print(i)
    n = 0  #variable to iterate till number of transformation to apply
    transformation_count = random.randint(
        1, len(transformations)
    )  #choose random number of transformation to apply on the image

    while n <= transformation_count:
        key = random.choice(
            list(transformations))  #randomly choosing method to call
        transformed_image = transformations[key](original_image)
        n = n + 1

    new_image_path = "%s/augmented_image_%s.png" % (augmented_path, i)
    transformed_image = img_as_ubyte(
        transformed_image
    )  #Convert an image to unsigned byte format, with values in [0, 255].
    transformed_image = cv2.cvtColor(
        transformed_image,
        cv2.COLOR_BGR2RGB)  #convert image to RGB before saving it
    cv2.imwrite(new_image_path,
                transformed_image)  # save transformed image to path
    i = i + 1
        m.bias.data.zero_()


# network
size = 128
G = generator(size)
G.cuda()

if os.path.exists("MNIST_DCGAN_results/iter/best_state_" + str(size) + ".pkl"):
    checkpoint = torch.load("MNIST_DCGAN_results/iter/best_state_" +
                            str(size) + ".pkl")
    G.load_state_dict(checkpoint['G'])

z_ = torch.randn((1000, 100)).view(-1, 100, 1, 1)
z_ = Variable(z_.cuda(), volatile=True)
G.eval()
test_images = G(z_)

images_numpy = test_images.cpu().data.numpy()

path_G = 'MNIST_DCGAN_results/RGB/G_' + str(size)
if not os.path.isdir(path_G):
    os.mkdir(path_G)

for i in range(0, 1000):
    src = skimage.transform.resize(images_numpy[i][0], (64, 64))
    imageio.imwrite("temp.jpg", skimage.img_as_ubyte(src))
    src = cv2.imread("temp.jpg", 0)
    src_RGB = cv2.cvtColor(src, cv2.COLOR_GRAY2RGB)
    cv2.imwrite(path_G + '/' + str(i) + ".jpg", src_RGB)
Beispiel #39
0
def main(input_dir='database',
         output_dir='database_aligned',
         image_size=182,
         margin=44,
         gpu_memory_fraction=1,
         random_order=False):
    sleep(random.random())
    output_dir = os.path.expanduser(output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    # Store some git revision info in a text file in the log directory
    # src_path, _ = os.path.split(os.path.realpath(__file__))
    cwd = os.getcwd()
    facenet.store_revision_info(cwd, output_dir, ' '.join(sys.argv))
    dataset = facenet.get_dataset(input_dir)

    print('Creating networks and loading parameters')

    with tf.Graph().as_default():
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=gpu_memory_fraction)  # noqa: E501
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, None)

    minsize = 20  # minimum size of face
    threshold = [0.6, 0.7, 0.7]  # three steps's threshold
    factor = 0.709  # scale factor

    # Add a random key to the filename to allow alignment using multiple
    # processes
    random_key = np.random.randint(0, high=99999)
    bounding_boxes_filename = os.path.join(
        output_dir, 'bounding_boxes_%05d.txt' % random_key)  # noqa: E501

    with open(bounding_boxes_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        if random_order:
            random.shuffle(dataset)
        for cls in dataset:
            output_class_dir = os.path.join(output_dir, cls.name)
            if not os.path.exists(output_class_dir):
                os.makedirs(output_class_dir)
                if random_order:
                    random.shuffle(cls.image_paths)
            for image_path in cls.image_paths:
                nrof_images_total += 1
                filename = os.path.splitext(os.path.split(image_path)[1])[0]
                output_filename = os.path.join(output_class_dir,
                                               filename + '.png')
                print(image_path)
                if not os.path.exists(output_filename):
                    try:
                        img = imageio.imread(image_path)
                    except (IOError, ValueError, IndexError) as e:
                        errorMessage = '{}: {}'.format(image_path, e)
                        print(errorMessage)
                    else:
                        if img.ndim < 2:
                            print('Unable to align "%s"' % image_path)
                            text_file.write('%s\n' % (output_filename))
                            continue
                        if img.ndim == 2:
                            img = facenet.to_rgb(img)
                        img = img[:, :, 0:3]

                        bounding_boxes, _ = detect_face.detect_face(
                            img, minsize, pnet, rnet, onet, threshold,
                            factor)  # noqa: E501

                        nrof_faces = bounding_boxes.shape[0]
                        if nrof_faces > 0:
                            det = bounding_boxes[:, 0:4]
                            img_size = np.asarray(img.shape)[0:2]
                            if nrof_faces > 1:
                                bounding_box_size = (det[:, 2] - det[:, 0]) * (
                                    det[:, 3] - det[:, 1])  # noqa: E501
                                img_center = img_size / 2
                                offsets = np.vstack([
                                    (det[:, 0] + det[:, 2]) / 2 -
                                    img_center[1],
                                    (det[:, 1] + det[:, 3]) / 2 - img_center[0]
                                ])  # noqa: E501
                                offset_dist_squared = np.sum(
                                    np.power(offsets, 2.0), 0)  # noqa: E501
                                index = np.argmax(
                                    bounding_box_size -
                                    offset_dist_squared * 2.0
                                )  # noqa: E501  # some extra weight on the centering
                                det = det[index, :]
                            det = np.squeeze(det)
                            bb = np.zeros(4, dtype=np.int32)
                            bb[0] = np.maximum(det[0] - margin / 2, 0)
                            bb[1] = np.maximum(det[1] - margin / 2, 0)
                            bb[2] = np.minimum(det[2] + margin / 2,
                                               img_size[1])
                            bb[3] = np.minimum(det[3] + margin / 2,
                                               img_size[0])
                            cropped = img[bb[1]:bb[3], bb[0]:bb[2], :]
                            scaled = resize(cropped, (image_size, image_size),
                                            mode='constant')
                            nrof_successfully_aligned += 1
                            # convert image to uint8 before saving
                            imageio.imwrite(output_filename,
                                            img_as_ubyte(scaled))
                            text_file.write('%s %d %d %d %d\n' %
                                            (output_filename, bb[0], bb[1],
                                             bb[2], bb[3]))  # noqa: E501
                        else:
                            print('Unable to align "%s"' % image_path)
                            text_file.write('%s\n' % (output_filename))

    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' %
          nrof_successfully_aligned)  # noqa: E501
Beispiel #40
0
newsize = int(size * 2.5)
fps = 30

squirm_video = make_squirming_video(newsize,
                                    T=1,
                                    fps=fps,
                                    w=8,
                                    bg=(255, ) * image.shape[-1])
print('Video Generated: squirm_video')

rotating_video = rotate_video(np.concatenate((squirm_video, ) * 3))
print('Video Generated: rotating_video')

result = []
for i in enumerate(rotating_video):
    frame = i[1]
    angle = 360 * i[0] / len(rotating_video)
    result.append(
        overlap_image(rotate(raw_image, -angle),
                      frame, (newsize // 2, newsize // 2),
                      center=True))
    if i[0] % 3 == 0:
        print(end='.', flush=True)
print()

path = 'generated.mp4'
imageio.mimsave(path, [img_as_ubyte(frame) for frame in result], fps=fps)
print(f'Video Saved at {path}')

input('Press ENTER to exit...')
def getColumns(paper):
    thresh = img_as_ubyte(threshold_adaptive(paper, 15, offset=5))
    (_, cnts, hierarchy) = cv2.findContours(thresh.copy(), cv2.RETR_TREE,
                                            cv2.CHAIN_APPROX_SIMPLE)
    # cv2.imshow("debug", cv2.drawContours(cv2.cvtColor(thresh.copy(), cv2.COLOR_GRAY2BGR), cnts, -1, (255,0,0), 1))
    cnts = contours.sort_contours(cnts, method="top-to-bottom")[0]

    (_, cnts, hierarchy) = cv2.findContours(thresh.copy(), cv2.RETR_TREE,
                                            cv2.CHAIN_APPROX_SIMPLE)
    validHierarchy, validCnts = [[]], []
    markerCnts = []
    i = 0
    removedIDs = [-1]
    for c in cnts:
        (x, y, w, h) = cv2.boundingRect(c)
        ar = w / float(h)
        if ar >= 0.75 and ar <= 1.25 and len(
                cv2.approxPolyDP(c, 0.1 * cv2.arcLength(c, True), True)) == 4:
            validCnts.append(c)
            validHierarchy[0].append(hierarchy[0][i])
        else:
            removedIDs.append(i)
        i += 1
    i = 0

    for c in validCnts:
        if validHierarchy[0][i][3] not in removedIDs:
            markerCnts.append(c)
        i += 1
    markerCnts = sorted(markerCnts, key=cv2.contourArea, reverse=True)[:6]

    display = cv2.cvtColor(thresh.copy(), cv2.COLOR_GRAY2BGR)
    cv2.drawContours(display, markerCnts, -1, (0, 255, 0), -1)
    cv2.line(display, (len(display[0]) // 2, 0),
             ((len(display[0]) // 2), len(display)), (0, 0, 255), 3)
    # cv2.imshow("current markers", display)
    if len(markerCnts):
        markerCnts = contours.sort_contours(markerCnts, method="left-to-right")
        leftCnts = markerCnts[0][:4]
        rightCnts = markerCnts[0][2:]
        if len(leftCnts) and len(rightCnts):
            # print(len(markerCnts[0]))

            #  1   2
            #
            #
            #  3   4

            cL1 = contours.sort_contours(leftCnts[:2],
                                         method="top-to-bottom")[0][0]
            cL2 = contours.sort_contours(leftCnts[2:],
                                         method="top-to-bottom")[0][0]
            cL3 = contours.sort_contours(leftCnts[2:],
                                         method="top-to-bottom")[0][1]
            cL4 = contours.sort_contours(leftCnts[:2],
                                         method="top-to-bottom")[0][1]

            pL1 = list(max(cL1, key=(lambda c: c[0][0] + c[0][1]))[0])
            pL2 = list(max(cL2, key=(lambda c: c[0][1] - c[0][0]))[0])
            pL3 = list(min(cL3, key=(lambda c: c[0][0] + c[0][1]))[0])
            pL4 = list(max(cL4, key=(lambda c: c[0][0] - c[0][1]))[0])

            cR1 = contours.sort_contours(rightCnts[:2],
                                         method="top-to-bottom")[0][0]
            cR2 = contours.sort_contours(rightCnts[2:],
                                         method="top-to-bottom")[0][0]
            cR3 = contours.sort_contours(rightCnts[2:],
                                         method="top-to-bottom")[0][1]
            cR4 = contours.sort_contours(rightCnts[:2],
                                         method="top-to-bottom")[0][1]

            pR1 = list(max(cR1, key=(lambda c: c[0][0] + c[0][1]))[0])
            pR2 = list(max(cR2, key=(lambda c: c[0][1] - c[0][0]))[0])
            pR3 = list(min(cR3, key=(lambda c: c[0][0] + c[0][1]))[0])
            pR4 = list(max(cR4, key=(lambda c: c[0][0] - c[0][1]))[0])

            bubbleThresh = img_as_ubyte(
                threshold_adaptive(paper, 257, offset=20))

            leftBox = np.array([pL1, pL2, pL3, pL4])
            rightBox = np.array([pR1, pR2, pR3, pR4])

            print(leftBox)

            left = four_point_transform(bubbleThresh, leftBox)
            right = four_point_transform(bubbleThresh, rightBox)

            # notesThresh = img_as_ubyte(threshold_adaptive(paper, 13, offset = 2.5))
            # notes = paper[pN[1]:]
            # clahe = cv2.createCLAHE(clipLimit=4.0, tileGridSize=(4,4))
            # notes = clahe.apply(notes)
            # cv2.imshow("notes", notes)

            display = cv2.cvtColor(paper.copy(), cv2.COLOR_GRAY2BGR)

            cv2.line(display, tuple(pL1), tuple(pL2), (0, 0, 255), 3)
            cv2.line(display, tuple(pL2), tuple(pL3), (0, 0, 255), 3)
            cv2.line(display, tuple(pL3), tuple(pL4), (0, 0, 255), 3)
            cv2.line(display, tuple(pL4), tuple(pL1), (0, 0, 255), 3)

            cv2.line(display, tuple(pR1), tuple(pR2), (255, 0, 0), 3)
            cv2.line(display, tuple(pR2), tuple(pR3), (255, 0, 0), 3)
            cv2.line(display, tuple(pR3), tuple(pR4), (255, 0, 0), 3)
            cv2.line(display, tuple(pR4), tuple(pR1), (255, 0, 0), 3)

            cv2.imshow("OUTPUT", display)

            return (left, right)

        return None
Beispiel #42
0
 def _deprocess_image(self, img):
     return img_as_ubyte((img + 1) / 2)
Beispiel #43
0
idealLowPass = D <= D0
idealHighPass = 1 - idealLowPass

# Filter our small grayscale image with the ideal lowpass filter
# 1. DFT of image
print(grayImg.dtype)
FTgraySmall = np.fft.fft2(grayImg.astype(float))
# 2. Butterworth filter is already defined in Fourier space
# 3. Elementwise product in Fourier space (notice fftshift of the filter)
FTgraySmallFiltered = FTgraySmall * np.fft.fftshift(idealLowPass)
# FTgraySmallFiltered = FTgraySmall * np.fft.fftshift(idealHighPass)
# 4. Inverse DFT to take filtered image back to the spatial domain
graySmallFiltered = np.abs(np.fft.ifft2(FTgraySmallFiltered))

# idealHighPass = ski.img_as_ubyte(idealHighPass / idealHighPass.max())
idealLowPass = ski.img_as_ubyte(idealLowPass / idealLowPass.max())
graySmallFiltered = ski.img_as_ubyte(graySmallFiltered /
                                     graySmallFiltered.max())

cv2.imwrite("C:/Users/ciezcm15/Documents/Project1/idealLowPass.jpg",
            idealLowPass)
# cv2.imwrite("C:/Users/ciezcm15/Documents/Project1/idealHighPass.jpg", idealHighPass)
cv2.imwrite(
    "C:/Users/ciezcm15/Documents/Project1/grayImageIdealLowpassFiltered.jpg",
    graySmallFiltered)

# Plot the ideal filter and then create and plot Butterworth filters of order
# n = 1, 2, 3, 4
plt.plot(xval,
         idealLowPass[int(idealLowPass.shape[0] / 2), :],
         'c--',
def pro(img,
        mask,
        draw_rect1=True,
        draw_rect2=True,
        draw_lines=True,
        draw_mask=True):
    copy = img.copy()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    faces = detector(gray, 0)

    for face in faces:
        x1 = face.left()
        y1 = face.top()
        x2 = face.right()
        y2 = face.bottom()
        landmarks = predector(gray, face)

        size = copy.shape
        #2D image points. If you change the image, you need to change vector
        image_points = np.array(
            [
                (landmarks.part(33).x, landmarks.part(33).y),  # Nose tip
                (landmarks.part(8).x, landmarks.part(8).y),  # Chin
                (landmarks.part(36).x,
                 landmarks.part(36).y),  # Left eye left corner
                (landmarks.part(45).x,
                 landmarks.part(45).y),  # Right eye right corne
                (landmarks.part(48).x,
                 landmarks.part(48).y),  # Left Mouth corner
                (landmarks.part(54).x, landmarks.part(54).y
                 )  # Right mouth corner
            ],
            dtype="double")

        # 3D model points.
        model_points = np.array([
            (0.0, 0.0, 0.0),  # Nose tip
            (0.0, -330.0, -65.0),  # Chin
            (-225.0, 170.0, -135.0),  # Left eye left corner
            (225.0, 170.0, -135.0),  # Right eye right corne
            (-150.0, -150.0, -125.0),  # Left Mouth corner
            (150.0, -150.0, -125.0)  # Right mouth corner
        ])
        # Camera internals
        focal_length = size[1]
        center = (size[1] / 2, size[0] / 2)
        camera_matrix = np.array([[focal_length, 0, center[0]],
                                  [0, focal_length, center[1]], [0, 0, 1]],
                                 dtype="double")

        dist_coeffs = np.zeros((4, 1))  # Assuming no lens distortion
        (success, rotation_vector,
         translation_vector) = cv2.solvePnP(model_points, image_points,
                                            camera_matrix, dist_coeffs)

        (b1, jacobian) = cv2.projectPoints(np.array([(350.0, 270.0, 0.0)]),
                                           rotation_vector, translation_vector,
                                           camera_matrix, dist_coeffs)
        (b2, jacobian) = cv2.projectPoints(np.array([(-350.0, -270.0, 0.0)]),
                                           rotation_vector, translation_vector,
                                           camera_matrix, dist_coeffs)
        (b3, jacobian) = cv2.projectPoints(np.array([(-350.0, 270, 0.0)]),
                                           rotation_vector, translation_vector,
                                           camera_matrix, dist_coeffs)
        (b4, jacobian) = cv2.projectPoints(np.array([(350.0, -270.0, 0.0)]),
                                           rotation_vector, translation_vector,
                                           camera_matrix, dist_coeffs)

        (b11, jacobian) = cv2.projectPoints(np.array([
            (450.0, 350.0, 400.0)
        ]), rotation_vector, translation_vector, camera_matrix, dist_coeffs)
        (b12,
         jacobian) = cv2.projectPoints(np.array([(-450.0, -350.0, 400.0)]),
                                       rotation_vector, translation_vector,
                                       camera_matrix, dist_coeffs)
        (b13, jacobian) = cv2.projectPoints(np.array([
            (-450.0, 350, 400.0)
        ]), rotation_vector, translation_vector, camera_matrix, dist_coeffs)
        (b14, jacobian) = cv2.projectPoints(np.array([
            (450.0, -350.0, 400.0)
        ]), rotation_vector, translation_vector, camera_matrix, dist_coeffs)

        b1 = (int(b1[0][0][0]), int(b1[0][0][1]))
        b2 = (int(b2[0][0][0]), int(b2[0][0][1]))
        b3 = (int(b3[0][0][0]), int(b3[0][0][1]))
        b4 = (int(b4[0][0][0]), int(b4[0][0][1]))

        b11 = (int(b11[0][0][0]), int(b11[0][0][1]))
        b12 = (int(b12[0][0][0]), int(b12[0][0][1]))
        b13 = (int(b13[0][0][0]), int(b13[0][0][1]))
        b14 = (int(b14[0][0][0]), int(b14[0][0][1]))

        if draw_rect1 == True:
            cv2.line(copy, b1, b3, (255, 255, 0), 10)
            cv2.line(copy, b3, b2, (255, 255, 0), 10)
            cv2.line(copy, b2, b4, (255, 255, 0), 10)
            cv2.line(copy, b4, b1, (255, 255, 0), 10)

        if draw_rect2 == True:
            cv2.line(copy, b11, b13, (255, 255, 0), 10)
            cv2.line(copy, b13, b12, (255, 255, 0), 10)
            cv2.line(copy, b12, b14, (255, 255, 0), 10)
            cv2.line(copy, b14, b11, (255, 255, 0), 10)

        if draw_lines == True:
            cv2.line(copy, b11, b1, (0, 255, 0), 10)
            cv2.line(copy, b13, b3, (0, 255, 0), 10)
            cv2.line(copy, b12, b2, (0, 255, 0), 10)
            cv2.line(copy, b14, b4, (0, 255, 0), 10)

        if draw_mask == True:
            pt = np.float32([b11, b13, b14, b12])

            ty = add_photo(copy, pt, mask)
            tb = img_as_ubyte(ty)

            for i in range(0, ty.shape[0]):
                for j in range(0, ty.shape[1]):
                    k = ty[i, j]
                    if k[0] != -1 and k[1] != -1 and k[2] != -1:
                        copy[i, j] = tb[i, j]

    return copy
Beispiel #45
0
def dataExtractorForTraining(patchesPerImage,
                             nbImages,
                             nbBins=4,
                             filename_to_save='FeaturesForTraining'):
    """Create a file "features.csv" containing glcm features and colors feature extracted from all patches of the
       "patchesDataSet" folder. This file is saved in the "patchesDataSet folder.

    # Arguments :
        patchesPerImage: The amount of patches wanted for each image.
        nbImages:        Int. The amout of images from the PH2Dataset used to extract features.
        nbBins:          Int. The number of bins wanted for color histogram. For example, 4 bins means the color
                         histogram will be divided into 4 parts.
        filename_to_save: String or None.

    # Outputs :
        Only save the "features.csv" file.
    """

    dissimilarityLista = []
    correlationLista = []
    energyLista = []
    contrastLista = []
    homogeneityLista = []

    dissimilarityListb = []
    correlationListb = []
    energyListb = []
    contrastListb = []
    homogeneityListb = []

    lists = list_creator(nbBins)

    resultList = []

    for patchCount in range(0, nbImages * patchesPerImage):

        crit = patchCount // int(patchesPerImage / 2)

        if crit % 2 == 0:

            patch_a, patch_b = load_patches(patchCount, subdir='Similar')

            reda = patch_a[:, :, 0]
            greena = patch_a[:, :, 1]
            bluea = patch_a[:, :, 2]

            patch_a = rgb2gray(patch_a)
            patch_a = img_as_ubyte(patch_a)

            redb = patch_b[:, :, 0]
            greenb = patch_b[:, :, 1]
            blueb = patch_b[:, :, 2]

            patch_b = rgb2gray(patch_b)
            patch_b = img_as_ubyte(patch_b)

            # Compute glcm features extraction
            glcma = greycomatrix(patch_a, [2], [0])
            glcmb = greycomatrix(patch_b, [2], [0])

            dissimilaritya = greycoprops(glcma, 'dissimilarity')[0, 0]
            correlationa = greycoprops(glcma, 'correlation')[0, 0]
            energya = greycoprops(glcma, 'energy')[0, 0]
            contrasta = greycoprops(glcma, 'contrast')[0, 0]
            homogeneitya = greycoprops(glcma, 'homogeneity')[0, 0]
            dissimilarityb = greycoprops(glcmb, 'dissimilarity')[0, 0]
            correlationb = greycoprops(glcmb, 'correlation')[0, 0]
            energyb = greycoprops(glcmb, 'energy')[0, 0]
            contrastb = greycoprops(glcmb, 'contrast')[0, 0]
            homogeneityb = greycoprops(glcmb, 'homogeneity')[0, 0]

            dissimilarityLista.append(dissimilaritya)
            correlationLista.append(correlationa)
            energyLista.append(energya)
            contrastLista.append(contrasta)
            homogeneityLista.append(homogeneitya)
            dissimilarityListb.append(dissimilarityb)
            correlationListb.append(correlationb)
            energyListb.append(energyb)
            contrastListb.append(contrastb)
            homogeneityListb.append(homogeneityb)

            # Compute colors feature extraction (color histograms)
            histo_ra = histogram(reda)
            histo_ga = histogram(greena)
            histo_ba = histogram(bluea)
            histo_rb = histogram(redb)
            histo_gb = histogram(greenb)
            histo_bb = histogram(blueb)

            numPixelsReda = [0] * 256
            cReda = 0
            for index in histo_ra[1]:
                numPixelsReda[index] = histo_ra[0][cReda]
                cReda += 1
            for k in range(0, nbBins):
                lists[k].append(
                    sum(numPixelsReda[k * int(256 / nbBins):(k + 1) *
                                      int(256 / nbBins) - 1]))

            numPixelsGreena = [0] * 256
            cGreena = 0
            for index in histo_ga[1]:
                numPixelsGreena[index] = histo_ga[0][cGreena]
                cGreena += 1
            for k in range(nbBins, 2 * nbBins):
                lists[k].append(
                    sum(numPixelsGreena[(k - nbBins) *
                                        int(256 / nbBins):(k + 1 - nbBins) *
                                        int(256 / nbBins) - 1]))

            numPixelsBluea = [0] * 256
            cBluea = 0
            for index in histo_ba[1]:
                numPixelsBluea[index] = histo_ba[0][cBluea]
                cBluea += 1
            for k in range(2 * nbBins, 3 * nbBins):
                lists[k].append(
                    sum(numPixelsBluea[(k - 2 * nbBins) *
                                       int(256 / nbBins):(k + 1 - 2 * nbBins) *
                                       int(256 / nbBins) - 1]))

            numPixelsRedb = [0] * 256
            cRedb = 0
            for index in histo_rb[1]:
                numPixelsRedb[index] = histo_rb[0][cRedb]
                cRedb += 1
            for k in range(3 * nbBins, 4 * nbBins):
                lists[k].append(
                    sum(numPixelsRedb[(k - 3 * nbBins) *
                                      int(256 / nbBins):(k + 1 - 3 * nbBins) *
                                      int(256 / nbBins) - 1]))

            numPixelsGreenb = [0] * 256
            cGreenb = 0
            for index in histo_gb[1]:
                numPixelsGreenb[index] = histo_gb[0][cGreenb]
                cGreenb += 1
            for k in range(4 * nbBins, 5 * nbBins):
                lists[k].append(
                    sum(numPixelsGreenb[(k - 4 * nbBins) *
                                        int(256 /
                                            nbBins):(k + 1 - 4 * nbBins) *
                                        int(256 / nbBins) - 1]))

            numPixelsBlueb = [0] * 256
            cBlueb = 0
            for index in histo_bb[1]:
                numPixelsBlueb[index] = histo_bb[0][cBlueb]
                cBlueb += 1
            for k in range(5 * nbBins, 6 * nbBins):
                lists[k].append(
                    sum(numPixelsBlueb[(k - 5 * nbBins) *
                                       int(256 / nbBins):(k + 1 - 5 * nbBins) *
                                       int(256 / nbBins) - 1]))

            resultList.append(1)

        else:

            patch_a, patch_b = load_patches(patchCount, subdir='nonSimilar')

            reda = patch_a[:, :, 0]
            greena = patch_a[:, :, 1]
            bluea = patch_a[:, :, 2]

            patch_a = rgb2gray(patch_a)
            patch_a = img_as_ubyte(patch_a)

            redb = patch_b[:, :, 0]
            greenb = patch_b[:, :, 1]
            blueb = patch_b[:, :, 2]

            patch_b = rgb2gray(patch_b)
            patch_b = img_as_ubyte(patch_b)

            # Compute glcm features extraction
            glcma = greycomatrix(patch_a, [2], [0])
            glcmb = greycomatrix(patch_b, [2], [0])

            dissimilaritya = greycoprops(glcma, 'dissimilarity')[0, 0]
            correlationa = greycoprops(glcma, 'correlation')[0, 0]
            energya = greycoprops(glcma, 'energy')[0, 0]
            contrasta = greycoprops(glcma, 'contrast')[0, 0]
            homogeneitya = greycoprops(glcma, 'homogeneity')[0, 0]

            dissimilarityb = greycoprops(glcmb, 'dissimilarity')[0, 0]
            correlationb = greycoprops(glcmb, 'correlation')[0, 0]
            energyb = greycoprops(glcmb, 'energy')[0, 0]
            contrastb = greycoprops(glcmb, 'contrast')[0, 0]
            homogeneityb = greycoprops(glcmb, 'homogeneity')[0, 0]

            dissimilarityLista.append(dissimilaritya)
            correlationLista.append(correlationa)
            energyLista.append(energya)
            contrastLista.append(contrasta)
            homogeneityLista.append(homogeneitya)
            dissimilarityListb.append(dissimilarityb)
            correlationListb.append(correlationb)
            energyListb.append(energyb)
            contrastListb.append(contrastb)
            homogeneityListb.append(homogeneityb)

            # Compute color feature extraction
            histo_ra = histogram(reda)
            histo_ga = histogram(greena)
            histo_ba = histogram(bluea)
            histo_rb = histogram(redb)
            histo_gb = histogram(greenb)
            histo_bb = histogram(blueb)

            numPixelsReda = [0] * 256
            cReda = 0
            for index in histo_ra[1]:
                numPixelsReda[index] = histo_ra[0][cReda]
                cReda += 1
            for k in range(0, nbBins):
                lists[k].append(
                    sum(numPixelsReda[k * int(256 / nbBins):(k + 1) *
                                      int(256 / nbBins) - 1]))

            numPixelsGreena = [0] * 256
            cGreena = 0
            for index in histo_ga[1]:
                numPixelsGreena[index] = histo_ga[0][cGreena]
                cGreena += 1
            for k in range(nbBins, 2 * nbBins):
                lists[k].append(
                    sum(numPixelsGreena[(k - nbBins) *
                                        int(256 / nbBins):(k + 1 - nbBins) *
                                        int(256 / nbBins) - 1]))

            numPixelsBluea = [0] * 256
            cBluea = 0
            for index in histo_ba[1]:
                numPixelsBluea[index] = histo_ba[0][cBluea]
                cBluea += 1
            for k in range(2 * nbBins, 3 * nbBins):
                lists[k].append(
                    sum(numPixelsBluea[(k - 2 * nbBins) *
                                       int(256 / nbBins):(k + 1 - 2 * nbBins) *
                                       int(256 / nbBins) - 1]))

            numPixelsRedb = [0] * 256
            cRedb = 0
            for index in histo_rb[1]:
                numPixelsRedb[index] = histo_rb[0][cRedb]
                cRedb += 1
            for k in range(3 * nbBins, 4 * nbBins):
                lists[k].append(
                    sum(numPixelsRedb[(k - 3 * nbBins) *
                                      int(256 / nbBins):(k + 1 - 3 * nbBins) *
                                      int(256 / nbBins) - 1]))

            numPixelsGreenb = [0] * 256
            cGreenb = 0
            for index in histo_gb[1]:
                numPixelsGreenb[index] = histo_gb[0][cGreenb]
                cGreenb += 1
            for k in range(4 * nbBins, 5 * nbBins):
                lists[k].append(
                    sum(numPixelsGreenb[(k - 4 * nbBins) *
                                        int(256 /
                                            nbBins):(k + 1 - 4 * nbBins) *
                                        int(256 / nbBins) - 1]))

            numPixelsBlueb = [0] * 256
            cBlueb = 0
            for index in histo_bb[1]:
                numPixelsBlueb[index] = histo_bb[0][cBlueb]
                cBlueb += 1
            for k in range(5 * nbBins, 6 * nbBins):
                lists[k].append(
                    sum(numPixelsBlueb[(k - 5 * nbBins) *
                                       int(256 / nbBins):(k + 1 - 5 * nbBins) *
                                       int(256 / nbBins) - 1]))

            resultList.append(0)

    df = pd.DataFrame({
        "Dissimilarity a": dissimilarityLista,
        "Correlation a": correlationLista,
        "Energy a": energyLista,
        "Contrast a": contrastLista,
        "Homogeneity a": homogeneityLista,
        "Dissimilarity b": dissimilarityListb,
        "Correlation b": correlationListb,
        "Energy b": energyListb,
        "Contrast b": contrastListb,
        "Homogeneity b": homogeneityListb
    })

    for k in range(0, len(lists)):
        if k < nbBins:
            df["red " + str(k + 1) + "/" + str(nbBins) + " a"] = lists[k]
        elif k >= nbBins and k < 2 * nbBins:
            df["green " + str(k + 1 - nbBins) + "/" + str(nbBins) +
               " a"] = lists[k]
        elif k >= 2 * nbBins and k < 3 * nbBins:
            df["blue " + str(k + 1 - 2 * nbBins) + "/" + str(nbBins) +
               " a"] = lists[k]
        elif k >= 3 * nbBins and k < 4 * nbBins:
            df["red " + str(k + 1 - 3 * nbBins) + "/" + str(nbBins) +
               " b"] = lists[k]
        elif k >= 4 * nbBins and k < 5 * nbBins:
            df["green " + str(k + 1 - 4 * nbBins) + "/" + str(nbBins) +
               " b"] = lists[k]
        else:
            df["blue " + str(k + 1 - 5 * nbBins) + "/" + str(nbBins) +
               " b"] = lists[k]

    df["Result"] = resultList

    if filename_to_save:
        df.to_csv(
            f"{package_path()}/data/patchesDataSet/{filename_to_save}.csv")

    return df
def process(self):
    """Perform the model warping and output an image grid"""
    if self.getPropertyByIdentifier("off").value:
        print("Image warping is currently turned off")
        return 1

    start_time = time.time()

    if self.getPropertyByIdentifier("display_input").value:
        im_data = []
        for name in INPORT_LIST:
            im_data.append(self.getInport(name).getData())
        out_image = Image(OUT_SIZE, DTYPE)
        out = resize(im_data[0].colorLayers[0].data.transpose(1, 0, 2),
                     OUT_SIZE_LIST)

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            inter_out = img_as_ubyte(out)
        out_image.colorLayers[0].data = inter_out
        self.getOutport("outport").setData(out_image)
        return 1

    if model is None:
        print("No model for synthesis")
        return -1

    cam = inviwopy.app.network.EntryExitPoints.camera
    im_data = []
    for name in INPORT_LIST:
        im_data.append(self.getInport(name).getData())
    for im in im_data:
        if not (im_data[0].dimensions == im.dimensions):
            print("Operation is incompatible with images of different size")
            print("Size 1: ", im_data[0].dimensions)
            print("Size 2: ", im.dimensions)
            return -1

    out_image = Image(OUT_SIZE, DTYPE)
    sample_image = Image(SAMPLE_SIZE, DTYPE)
    im_colour = []
    for idx, name in enumerate(INPORT_LIST):
        im_colour.append(im_data[idx].colorLayers[0].data[:, :, :3].transpose(
            1, 0, 2))

    im_depth = []
    near = cam.nearPlane
    far = cam.farPlane
    baseline = 0.5
    focal_length = cam.projectionMatrix[0][0]
    fov = cam.fov.value

    for idx, name in enumerate(INPORT_LIST):
        im_depth.append(
            conversions.depth_to_pixel_disp(
                im_data[idx].depth.data.transpose(1, 0),
                near=near,
                far=far,
                baseline=baseline,
                focal_length=focal_length,
                fov=fov,
                image_pixel_size=float(im_data[0].dimensions[0])))

    sample = {
        'depth': torch.tensor(im_depth[0], dtype=torch.float32).unsqueeze_(0),
        'colour': torch.tensor(im_colour[0],
                               dtype=torch.float32).unsqueeze_(0),
        'grid_size': GRID_SIZE
    }

    warped = data_transform.transform_inviwo_to_warped(sample)
    warped = data_transform.stack(warped, 65)
    im_input = warped['inputs'].unsqueeze_(0)

    if cuda:
        im_input = im_input.cuda()

    model.eval()
    output = model(im_input)
    im_input = im_input[:, :-3]
    output += im_input
    output = torch.clamp(output, 0.0, 1.0)

    end_time = time.time() - start_time
    print("Grid light field rendered in {:4f}".format(end_time))
    out_unstack = data_transform.torch_unstack(output[0])
    out_colour = cnn_utils.transform_lf_to_torch(out_unstack)

    output_grid = vutils.make_grid(out_colour,
                                   nrow=8,
                                   range=(0, 1),
                                   normalize=False,
                                   padding=2,
                                   pad_value=1.0)

    output_grid = resize(output_grid.cpu().detach().numpy().transpose(1, 2, 0),
                         OUT_SIZE_LIST)

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        inter_out = img_as_ubyte(output_grid)

    #inter_out = denormalise_lf(output_grid)
    #inter_out = inter_out.cpu().detach().numpy().astype(np.uint8).transpose(1, 2, 0)
    # Add an alpha channel here
    shape = tuple(OUT_SIZE_LIST) + (4, )
    final_out = np.full(shape, 255, np.uint8)
    final_out[:, :, :3] = inter_out

    shape = tuple(SAMPLE_SIZE_LIST) + (4, )
    sample_out = np.full(shape, 255, np.uint8)
    sample_out[:, :, :3] = np.around(
        data_transform.denormalise_lf(
            out_unstack).cpu().detach().numpy()).astype(
                np.uint8)[self.getPropertyByIdentifier("sample_num").value]

    # Inviwo expects a uint8 here
    out_image.colorLayers[0].data = final_out
    sample_image.colorLayers[0].data = sample_out
    self.getOutport("outport").setData(out_image)
    self.getOutport("sample").setData(sample_image)

    end_time = time.time() - start_time
    print("Overall render time was {:4f}".format(end_time))
Beispiel #47
0
def int_img(img):
    return img_as_ubyte(img)
Beispiel #48
0
    result = result[borderh:height_ - borderh, borderw:width_ - borderw]
    return result


im = cv2.imread("B638_EP_C2_Mar13.png")
img = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
h, w = img.shape[:2]

bkg = np.array([
    np.mean(im[:h / 7, :, 0]),
    np.mean(im[:h / 7, :, 1]),
    np.mean(im[:h / 7, :, 2])
])
img_contrast = np.sum(np.abs(im - bkg), axis=2)
img_contrast /= np.max(img_contrast)
img_contrast = img_as_ubyte(img_contrast)
img_contrast_rot = rotateImage(img_as_ubyte(img_contrast), 1.60)

plt.imshow(img_contrast_rot)
plt.show()
plt.clf()

tl = np.array([365, 440])
br = tl + np.array([340, 360])
plt.imsave('ScubaEP_template.png',
           img_contrast_rot[tl[1]:br[1], tl[0]:br[0]],
           cmap=plt.cm.gray)

cv2.rectangle(img_contrast_rot, (tl[0], tl[1]), (br[0], br[1]), 255, 5)
cv2.circle(img_contrast_rot, (tl[0] + 170, tl[1] + 240), 10, (255), 10)
Beispiel #49
0
            continue
        # # Load image and ground truth data
        # image, image_meta, gt_class_id, gt_bbox, gt_mask =\
        #     modellib.load_image_gt(dataset, inference_config,
        #                            image_id, use_mini_mask=False)
        # molded_images = np.expand_dims(modellib.mold_image(image, inference_config), 0)
        # # Run object detection
        # results = model.detect([image], verbose=0)
        # r = results[0]
        # # Compute AP
        # AP, precisions, recalls, overlaps =\
        #     utils.compute_ap(gt_bbox, gt_class_id, gt_mask,
        #                      r["rois"], r["class_ids"], r["scores"], r['masks'])
        # APs.append(AP)

        image = skimage.img_as_ubyte(
            plt.imread(os.path.join(dataset_dir, file)))

        results = model.detect([image], verbose=1)
        fig, ax = get_ax()
        r = results[0]
        encountered_ids = []
        filtered_inds = []
        target_ids = [1, 2]
        for i, box in enumerate(r['rois']):
            # ROIs are sorted after confidence, if one was registered discard lower confidence detections to avoid double counting
            class_id = r['class_ids'][i]
            print(class_id)
            if class_id not in target_ids or class_id in encountered_ids:
                continue
            encountered_ids.append(class_id)
            # all_visual_features_unordered.append(r['roi_features'][i])
Beispiel #50
0
def save_image(path, name, img):
    imageio.imsave(path + '/' + name + '.png', img_as_ubyte(img))
Beispiel #51
0
In this example, we will see how to filter a gray-level image using some of the
linear and non-linear filters available in skimage. We use the ``camera`` image
from ``skimage.data`` for all comparisons.

.. [1] Pierre Soille, On morphological operators based on rank filters, Pattern
       Recognition 35 (2002) 527-535.

"""

import numpy as np
import matplotlib.pyplot as plt

from skimage import img_as_ubyte
from skimage import data

noisy_image = img_as_ubyte(data.camera())
hist = np.histogram(noisy_image, bins=np.arange(0, 256))

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 3))
ax1.imshow(noisy_image, interpolation='nearest', cmap=plt.cm.gray)
ax1.axis('off')
ax2.plot(hist[1][:-1], hist[0], lw=2)
ax2.set_title('Histogram of grey values')

######################################################################
#
# Noise removal
# =============
#
# Some noise is added to the image, 1% of pixels are randomly set to 255, 1%
# are randomly set to 0. The **median** filter is applied to remove the
Beispiel #52
0
def filter_image(in_image, out_dir, pallet, generate_id=False):
    # print(in_image)
    image_m = {}
    parts = in_image.split('/')

    if generate_id:
        img_id = str(uuid.uuid4()).replace('-', '')[:8]
    else:
        img_id = parts[-1:][0].split('-')[0]

    image_m['id'] = img_id

    p_colors = get_colors(pallet)
    c_name = p_colors[0].attrib['name']

    img_rgb = img_as_float64(io.imread(in_image))
    grayscale_image = color.rgb2gray(img_rgb)

    # # Create a mask selecting regions with interesting texture.
    # noisy = rank.entropy(img_rgb, np.ones((9, 9)))
    # textured_regions = noisy > 4.25
    # # Note that using `colorize` here is a bit more difficult, since `rgb2hsv`
    # # expects an RGB image (height x width x channel), but fancy-indexing returns
    # # a set of RGB pixels (# pixels x channel).

    color_multiplier_0 = randomize_color(get_color_by_index(p_colors, 0))
    color_multiplier_1 = randomize_color(get_color_by_index(p_colors, 1))
    color_multiplier_2 = randomize_color(get_color_by_index(p_colors, 2))
    color_multiplier_3 = randomize_color(get_color_by_index(p_colors, 3))
    color_multiplier_4 = randomize_color(get_color_by_index(p_colors, 4))
    image_m['colors'] = [
        color_multiplier_0, color_multiplier_1, color_multiplier_2,
        color_multiplier_3, color_multiplier_4
    ]

    thresh = skimage.filters.threshold_otsu(grayscale_image)
    binary = grayscale_image <= thresh

    noisy = rank.threshold(grayscale_image, square(9))

    n_max = noisy.mean()

    textured_regions_0 = noisy < (n_max * .1)
    textured_regions_1 = np.all([noisy >= (n_max * .1), noisy <= (n_max * .3)])
    textured_regions_2 = np.all([noisy >= (n_max * .3), noisy <= (n_max * .5)])
    textured_regions_3 = np.all([noisy >= (n_max * .6), noisy <= (n_max * .8)])
    textured_regions_4 = noisy >= (n_max * .8)

    masked_image = img_rgb.copy()

    masked_image[textured_regions_0, :] *= color_multiplier_0
    masked_image[textured_regions_1, :] *= color_multiplier_1
    masked_image[textured_regions_2, :] *= color_multiplier_2
    masked_image[textured_regions_3, :] *= color_multiplier_3
    masked_image[textured_regions_4, :] *= color_multiplier_4
    masked_image[binary, :] *= color_multiplier_0

    out_img = '{}/{}.png'.format(out_dir, image_m['id'])
    io.imsave(out_img, img_as_ubyte(masked_image))
    print('out:{} pallete:{}'.format(out_img, c_name))
    image_m['out_img'] = out_img

    return image_m
Beispiel #53
0
output.append(skimage.io.imread(class2))
output.append(skimage.io.imread(class3))
conc=0
paperino=0
l_verme=100
b2, F, G, H=0, 0, 0, 0
def adapt_input(im, size):
    h, w = im.shape[0:2]
    sz = min(h, w)
    im=im[(h//2-sz//2):(h//2+sz//2),(w//2-sz//2):(w//2+sz//2),:] 
    im = skimage.transform.resize(im, (size, size, 3), mode='reflect')
    return im

for i in range(len(special_input)):
    special_input2.append(adapt_input(skimage.io.imread(special_input[i]), 64))
    special_input3.append(skimage.img_as_ubyte(adapt_input(skimage.io.imread(special_input[i])[:,:,::-1], 80)))
    special_input4.append(skimage.img_as_ubyte(adapt_input(skimage.io.imread(special_input[i])[:,:,::-1], 120)))
inputtino, inputtinob, inputtinod=special_input2[0], special_input3[0], special_input4[0]
neuron, neuronb=[],[]
for i in range(128):
    neuron.append(adapt_input(skimage.io.imread("IMAGES/NEURONS/neuron%d.jpg" %(i+1)), 64))
    neuronb.append(skimage.img_as_ubyte(adapt_input(skimage.io.imread("IMAGES/NEURONS/neuron%d.jpg"%(i+1))[:,:,::-1], 80)))
for i in range(3):
    neuron.append(adapt_input(skimage.io.imread("IMAGES/NEURONS/output%d.jpg" %(i+1)), 64))
    neuronb.append(skimage.img_as_ubyte(adapt_input(skimage.io.imread("IMAGES/NEURONS/output%d.jpg"%(i+1))[:,:,::-1], 80)))
    
get = K.function([model.layers[0].input], [model.layers[13].output])
get2=[]*11
for i in range(11):
    get2.append(K.function([model.layers[0].input], [model.layers[i].output]))
Beispiel #54
0
def lsr_feature(image_name,
                output,
                block,
                scale,
                mag_threshold=20,
                lsr_threshold=20,
                distance_threshold=8,
                orientation_threshold=22.5):
    assert (type(block) != None and type(scale) != None)

    ds = gdal.Open(image_name)
    image = ds.ReadAsArray()
    geotran = ds.GetGeoTransform()
    ulx = geotran[0]
    uly = geotran[3]
    in_cell_width = geotran[1]
    in_cell_height = geotran[5]
    ds = None

    # block and scale parameters are in meters
    # convert meters to image space (number of pixels)
    # the conversion is very crude at the moment, should really
    # be using projected data
    if "wv2" in image_name:
        cell_width = 0.46
    if "wv3" in image_name:
        cell_width = 0.31
    # in number of pixels relative to the input data GSD
    block = int(block / cell_width)
    scale = int(scale / cell_width)

    out_srs = osr.SpatialReference()
    out_srs.ImportFromEPSG(4326)
    out_srs_wkt = out_srs.ExportToWkt()
    out_cell_width = block * in_cell_width
    out_cell_height = block * in_cell_height

    image = np.moveaxis(image, 0,
                        -1)  # expects an image in rows, columns, channels
    with expected_warnings(['precision']):
        image = skimage.img_as_ubyte(rgb2gray(image))

    lsr_im = line_support_regions(image)

    for i in range(0, lsr_im.shape[0], block):
        outrow = []
        for j in range(0, lsr_im.shape[1], block):
            center_i = int(i + block / 2.)
            center_j = int(j + block / 2.)
            if center_i - int(scale / 2.) < 0:
                top = 0
            else:
                top = center_i - int(scale / 2.)
            if center_i + int(scale / 2.) > lsr_im.shape[0]:
                bot = lsr_im.shape[0]
            else:
                bot = center_i + int(scale / 2.)
            if center_j - int(scale / 2.) < 0:
                left = 0
            else:
                left = center_j - int(scale / 2.)
            if center_j + int(scale / 2.) > lsr_im.shape[1]:
                right = lsr_im.shape[1]
            else:
                right = center_j + int(scale / 2.)

            scale_arr = lsr_im[top:bot + 1, left:right + 1]
            feat_vec = __lsr_hist_feature(scale_arr)
            outrow.append(feat_vec)
        out_image.append(outrow)
    out_image = np.moveaxis(out_image, -1, 0)
    if output:
        out_geotran = (ulx, out_cell_width, 0, uly, 0, out_cell_height)
        write_geotiff(output, out_image, out_geotran, out_srs_wkt)
    return np.array(out_image)
Beispiel #55
0
from skimage import img_as_ubyte
from skimage.io import imread, imsave
import numpy as np

img = imread('img.png')
img = img_as_ubyte(img)
h = np.zeros(256, np.uint32)

for v in img.flatten():
    h[v] += 1

cdf = np.zeros(256, np.uint32)

for v in range(256):
    cdf[v] = cdf[v - 1] + h[v]

min_sum = 99999

for val in cdf:
    if val != 0 and val < min_sum:
        min_sum = val

p = len(img.flatten())
res = img.copy()

for y in range(img.shape[0]):
    for x in range(img.shape[1]):
        pixel = img[y][x]
        res[y][x] = round((cdf[pixel] - min_sum) / (p - 1) * 255)

imsave('out_img.png', res)
Beispiel #56
0
def random_crop(sample, lambda_scale_frac, lambda_shift_frac, min_scale,
                max_scale):
    image, bb = sample['image'], sample['bb']
    image = img_as_ubyte(image)
    if (len(image.shape) == 2):
        image = np.repeat(image[..., None], 3, axis=2)
    im = Image.fromarray(image)
    cols = image.shape[1]
    rows = image.shape[0]
    width = bb[2] - bb[0]
    height = bb[3] - bb[1]
    center_x = bb[0] + width / 2
    center_y = bb[1] + height / 2

    # motion smoothness model
    # adapted from Held's implementation - https://github.com/davheld/GOTURN/
    kMaxNumTries = 10
    kContextFactor = 2.
    new_width = -1
    num_tries_width = 0
    # get new width
    while ((new_width < 0 or new_width > cols - 1)
           and num_tries_width < kMaxNumTries):
        width_scale_factor = max(
            min_scale, min(max_scale, sample_exp_two_sided(lambda_scale_frac)))
        new_width = width * (1 + width_scale_factor)
        new_width = max(1.0, min(cols - 1, new_width))
        num_tries_width = num_tries_width + 1

    new_height = -1
    num_tries_height = 0
    # get new height
    while ((new_height < 0 or new_height > rows - 1)
           and num_tries_height < kMaxNumTries):
        height_scale_factor = max(
            min_scale, min(max_scale, sample_exp_two_sided(lambda_scale_frac)))
        new_height = height * (1 + height_scale_factor)
        new_height = max(1.0, min(rows - 1, new_height))
        num_tries_height = num_tries_height + 1

    first_time_x = True
    new_center_x = -1
    num_tries_x = 0
    # get new center X
    while ((first_time_x
            or new_center_x < center_x - width * kContextFactor / 2
            or new_center_x > center_x + width * kContextFactor / 2
            or new_center_x - new_width / 2 < 0
            or new_center_x + new_width / 2 > cols)
           and num_tries_x < kMaxNumTries):
        new_x_temp = center_x + width * sample_exp_two_sided(lambda_shift_frac)
        new_center_x = min(cols - new_width / 2, max(new_width / 2,
                                                     new_x_temp))
        first_time_x = False
        num_tries_x = num_tries_x + 1

    first_time_y = True
    new_center_y = -1
    num_tries_y = 0
    # get new center Y
    while ((first_time_y
            or new_center_y < center_y - height * kContextFactor / 2
            or new_center_y > center_y + height * kContextFactor / 2
            or new_center_y - new_height / 2 < 0
            or new_center_y + new_height / 2 > rows)
           and num_tries_y < kMaxNumTries):
        new_y_temp = center_y + height * sample_exp_two_sided(
            lambda_shift_frac)
        new_center_y = min(rows - new_height / 2,
                           max(new_height / 2, new_y_temp))
        first_time_y = False
        num_tries_y = num_tries_y + 1

    box = [
        new_center_x - new_width / 2, new_center_y - new_height / 2,
        new_center_x + new_width / 2, new_center_y + new_height / 2
    ]
    box = [int(math.floor(x)) for x in box]
    return box
 def cleanRemoveImg(imgdata):
     result = img_as_ubyte(rgb2gray(deepcopy(imgdata)))
     del (imgdata)
     return result
Beispiel #58
0
def calc_line_support_regions(image_name,
                              mag_threshold=20,
                              lsr_threshold=20,
                              distance_threshold=8,
                              orientation_threshold=22.5,
                              output_lsr_shapefile=False):
    """
    Parameters:
    ------------
    image_name: str
        the image filename
    mag_threshold: int or float
        pixels with magnitude above mag_threshold are considered for line support regions
    lsr_threshold: int or float
        threshold for the smallest line support region
    distance_threshold: int or float
        the size of the kernel used for counting the number of pixels contributing to
        a line support region
    orientation_threshold: int or float
        the number of degrees (+ or -) that is allowed when determining if pixels are in the same
        line support region
    
    Returns:
    --------
    lsf_raster: ndarray (3 bands, n rows, m cols)
        lsf_raster[0] is line length values (in pixels)
        lsf_raster[1] is line orientation values (in degrees)
        lsf_raster[2] is line contrast values
    geotran: gdal geotransform
    """
    ds = gdal.Open(image_name)
    image = ds.ReadAsArray()
    geotran = ds.GetGeoTransform()
    ulx = geotran[0]
    uly = geotran[3]
    in_cell_width = geotran[1]
    in_cell_height = geotran[5]
    ds = None

    out_srs = osr.SpatialReference()
    out_srs.ImportFromEPSG(4326)
    out_srs_wkt = out_srs.ExportToWkt()
    lsf_raster = np.ones(
        shape=(image.shape)) * -1  # output lsf_raster is 3 band.

    image = np.moveaxis(image, 0,
                        -1)  # expects an image in rows, columns, channels
    with expected_warnings(['precision']):
        image = skimage.img_as_ubyte(rgb2gray(image))

    # calculate gradient orientation and magnitude
    mag, ang, dx, dy = __calc_mag_ang(image)
    mag *= 0.001

    # tmp(edmim<magThreshold)=-1;
    temp = np.where(
        mag < mag_threshold, -1,
        ang)  # set pixels to -1 if magnitude is below the mag_threshold

    data_ob = __label_regions(temp, distance_threshold, orientation_threshold)
    lsr_m = data_ob.labeled_regions
    line_idx = np.unique(lsr_m)

    # lsfarr = zeros(max(lsrM(:)),5);
    lsf_arr = np.zeros(shape=(np.max(lsr_m), 5))

    count = 0

    l = 1
    for l in range(1, np.max(line_idx)):
        # idx=find(lsrM==l);
        idx = np.argwhere(lsr_m.ravel() == l)  # returns an array of indices

        # eim = zeros(size(im));
        eim = np.zeros(shape=temp.shape)

        # eim(idx) = 1;
        eim = np.where(lsr_m == l, 1, eim)

        # if (sum(eim(:)) <= lsrThreshold)
        if np.sum(eim) <= lsr_threshold:  # ignore small line support region
            continue

        # ix_wi = dx(idx)
        # iy_wi = dy(idx)
        Ix_wi = dx.ravel()[
            idx]  # extract elements in dx at index locations where lsr_m == l
        Iy_wi = dy.ravel()[idx]
        grd_wi = mag.ravel()[idx]
        # find major orientation
        ST = [[np.sum(Ix_wi**2), np.sum(Ix_wi * Iy_wi)],
              [np.sum(Ix_wi * Iy_wi), np.sum(Iy_wi**2)]]
        # V, D = eig(ST)
        # matlab returns returns diagonal matrix D of eigenvalues and matrix V whose columns are the corresponding right eigenvectors, so that A*V = V*D.
        D, V = np.linalg.eig(ST)  # python's return of D is a 1D array,
        D = np.diag(
            D)  # make D on the diagonal to conform to matlab's procedure
        # if D(1,1)<D(2,2)
        #     lorn=atan(V(2,1)/V(1,1));
        # else
        #     lorn=atan(V(2,2)/V(1,2));
        # end
        if D[0][0] < D[1][1]:
            # lorn=atan(V(2,1)/V(1,1));
            lorn = np.arctan(V[1][0] / V[0][0])
        else:
            # lorn=atan(V(2,2)/V(1,2));
            lorn = np.arctan(V[1][1] / V[0][1])

        # vote for r
        # [Ytmp,Xtmp]=ind2sub(size(im),idx);
        Ytmp, Xtmp = np.unravel_index(idx, temp.shape)
        Ytmp += 1  # indices need += 1 for some indexing weirdness...
        Xtmp += 1
        # Raccm=round(Xtmp.*cos(lorn-pi/2)+Ytmp.*sin(lorn-pi/2));
        Raccm = np.round(Xtmp * math.cos(lorn - (math.pi / 2)) +
                         Ytmp * math.sin(lorn - (math.pi / 2)))
        rng = np.arange(Raccm.min(), Raccm.max() + 1)
        accm = np.zeros(shape=(len(rng)))
        for k in range(len(idx)):
            rc = np.round(Xtmp[k] * math.cos(lorn - math.pi / 2) +
                          Ytmp[k] * math.sin(lorn - math.pi / 2))
            accm[np.where(rng == rc)] = accm[np.where(rng == rc)] + grd_wi[k]

        mxid = np.argmax(accm)
        Xmx = max(Xtmp[np.where(Raccm == rng[mxid])])
        Xmn = min(Xtmp[np.where(Raccm == rng[mxid])])
        Ymx = max(Ytmp[np.where(Raccm == rng[mxid])])
        Ymn = min(Ytmp[np.where(Raccm == rng[mxid])])

        lmx = ((Xmx + Xmn) / 2) - 1
        lmy = ((Ymx + Ymn) / 2) - 1
        llen = math.sqrt((Xmx - Xmn)**2 + (Ymx - Ymn)**2)
        lsf_arr[count][0] = llen
        lsf_arr[count][1] = lmx
        lsf_arr[count][2] = lmy
        lsf_arr[count][3] = lorn
        lcon = np.mean(grd_wi[(np.where(Raccm == rng[mxid]))])
        lsf_arr[count][4] = lcon
        lsf_raster[0][int(lmy)][int(lmx)] = llen
        lsf_raster[1][int(lmy)][int(lmx)] = np.rad2deg(lorn) + 180
        lsf_raster[2][int(lmy)][int(lmx)] = lcon
        count += 1
    lsf_arr = lsf_arr[0:count, :]
    #lsf_arr[:,1] = ulx + lsf_arr[:,1] * in_cell_width
    #lsf_arr[:,2] = uly + lsf_arr[:,2] * in_cell_height
    if output_lsr_shapefile:
        write_lsr_shapefile(
            lsf_arr,
            os.path.join(os.path.dirname(image_name),
                         os.path.basename(image_name)[:-4]) + "_lsr_MT" +
            str(mag_threshold) + "_LT" + str(lsr_threshold) + "_DT" +
            str(distance_threshold) + "_OT" + str(orientation_threshold) +
            ".shp", geotran)
    return lsf_raster
import numpy as np
import matplotlib.pyplot as plt
import skimage
from matplotlib import image
# from scipy.io import loadmat
from scipy import misc
from skimage import io
from Kmeans import Kmeans

img = image.imread('image.png')
print(img.shape)

image1 = img.reshape(img.shape[0] * img.shape[1], 3)
a = Kmeans(image1, 256)
Centroids, Output = a.fit(20)
image2 = []
for i in range(img.shape[0] * img.shape[1]):
    image2.append(Centroids[Output[i]])
image_compressed = np.asarray(image2)
print(image_compressed)
image_compressed2 = image_compressed.reshape(img.shape[0], img.shape[1], 3)
print(image_compressed2)
i10 = skimage.img_as_ubyte(image_compressed2, force_copy=False)
plt.title(' image_compressed 256 colors')
plt.imshow(i10)
plt.show()
io.imsave('image_compressed_K256.png', i10)
# import scipy.misc
# misc.imsave('tiger_small.jpg', image_compressed)
Beispiel #60
0
def to_skeleton(img_binary):
    m = morphology.skeletonize(img_binary > 0)
    cv_image = img_as_ubyte(m)
    return cv_image