Example #1
0
    def Get_Parameters(self):
        copy_img = self.img
        if len(self.img.shape) == 3:
            copy_img = rgb2gray(self.img)

        self.Variance = variance(laplace(copy_img, ksize=3))
        self.MaxVariance = np.amax(laplace(copy_img, ksize=3))
        self.Noise = estimate_sigma(copy_img)
        self.Scharr = variance(scharr(copy_img))

        print(self.Variance, self.MaxVariance, self.Scharr, self.Noise)
Example #2
0
def highpass_filter(image, what):
    newImage = np.array(image)
    if newImage.ndim == 3:
        Filters = {
            'roberts': [
                filters.roberts(image[:, :, 0]),
                filters.roberts(image[:, :, 1]),
                filters.roberts(image[:, :, 2])
            ],
            'sobel': [
                filters.sobel(image[:, :, 0]),
                filters.sobel(image[:, :, 1]),
                filters.sobel(image[:, :, 2])
            ],
            'scharr': [
                filters.scharr(image[:, :, 0]),
                filters.scharr(image[:, :, 1]),
                filters.scharr(image[:, :, 2])
            ],
            'prewitt': [
                filters.prewitt(image[:, :, 0]),
                filters.prewitt(image[:, :, 1]),
                filters.prewitt(image[:, :, 2])
            ],
            'laplace': [
                filters.laplace(image[:, :, 0]),
                filters.laplace(image[:, :, 1]),
                filters.laplace(image[:, :, 2])
            ],
            'canny': [
                feature.canny(image[:, :, 0]),
                feature.canny(image[:, :, 1]),
                feature.canny(image[:, :, 2])
            ]
        }
        newImageR = Filters[what][0]
        newImageG = Filters[what][1]
        newImageB = Filters[what][2]

        newImage = np.array(newImageR + newImageG + newImageB)
    else:
        Filters = {
            'roberts': filters.roberts(image),
            'sobel': filters.sobel(image),
            'scharr': filters.scharr(image),
            'prewitt': filters.prewitt(image),
            'laplace': filters.laplace(image),
            'canny': feature.canny(image)
        }
        newImage = Filters[what]

    return np.clip(newImage, 0, 255)
Example #3
0
def getVarianceAndMaxi(img_type):
    obtained_laplaces = []
    if (img_type == 'blur'):
        path = '/media/blur'
    elif (img_type == 'sharp'):
        path = '/media/sharp'

    for image_path in os.listdir(path):
        print(f"img_path: {image_path}")
        img = io.imread(path + '/' + image_path)

        # Resizing the image.
        img = resize(img, (400, 600))

        # Convert the image to greyscale
        img = rgb2gray(img)

        # Detecting the Edge of the image
        edge_laplace = laplace(img, ksize=3)

        print(f"Variance: {variance(edge_laplace)}")
        print(f"Maximum : {np.amax(edge_laplace)}")

        # Adding the variance and maximum to a list of sets
        obtained_laplaces.insert(i + 1, ((variance(edge_laplace),
                                          (np.amax(edge_laplace)))))
        if (img_type == 'blur'):
            print(f"blur_obtained_laplaces : {obtained_laplaces}")
        elif (img_type == 'sharp'):
            print(f"sharp_laplaces : {obtained_laplaces}")
    return obtained_laplaces
Example #4
0
    def compute(self, image, n_region, mask=None):
        """
        Parameters
        ----------
        image: numpy ndarray
            image array to be represented as regions
        n_region : int
            the number of regions to be generated
        mask: mask image to give region of interest
        Returns
        -------
        regions : a list of regions
        """

        gray_image = image.copy()
        if mask is not None:
            gray_image[mask > 0] = 0

        # Convert the original image to the 0~255 gray image
        gray_image = (gray_image - gray_image.min()) / (gray_image.max() - gray_image.min())
        labels = segmentation.slic(gray_image.astype(np.float),
                                   n_segments=n_region,
                                   slic_zero=True, sigma=2,
                                   multichannel=False,
                                   enforce_connectivity=True)
        # edge_map = filters.sobel(gray_image)  # just for 2-D image
        edge_map = filters.laplace(gray_image)

        # Given an image's initial segmentation and its edge map this method constructs the
        # corresponding Region Adjacency Graph (RAG). Each node in the RAG represents a set of
        # pixels within the image with the same label in labels. The weight between two adjacent
        # regions is the average value in edge_map along their boundary.
        rag = graph.rag_boundary(labels, edge_map)

        regions = []
        n_labels = labels.max() + 1
        for r in np.arange(n_labels):
            # get vox_pos
            position = np.transpose(np.nonzero(labels == r))

            # get vox_feat
            vox_feat = np.zeros((position.shape[0], 1))
            n_D = position.shape[1]
            for i in range(position.shape[0]):
                if n_D == 2:
                    vox_feat[i][0] = image[position[i][0], position[i][1]]
                elif n_D == 3:
                    vox_feat[i][0] = image[position[i][0], position[i][1], position[i][2]]
                else:
                    raise RuntimeError("We just consider 2_D and 3_D images at present!")

            regions.append(Region(position, vox_feat=vox_feat, r_id=r))

        for r in range(n_labels):
            for r_key in rag.edge[r].keys():
                regions[r].add_neighbor(regions[r_key])

        self.regions = regions
        self.image_shape = image.shape
        return regions
Example #5
0
    def _calc_crispness(self, grey_array):
        """Calculate three measures of the crispness of an channel.

        PARAMETERS
        ----------
        grey_array : 2D numpy array
            Raw data for the grey channel.

        PRODUCES
        --------
        crispnesses : list
            Three measures of the crispness in the grey channel of types:

            - ``sobel``, ``canny``, and ``laplace``
        """
        grey_array = grey_array/255
        sobel_var = filters.sobel(grey_array).var()
        canny_array = feature.canny(grey_array, sigma=1).var()
        canny_ratio = np.sum(canny_array == True)/float(
                                                    len(canny_array.flatten()))
        laplace_var = filters.laplace(grey_array, ksize=3).var()
        self.feature_data.extend([sobel_var, canny_ratio, laplace_var])
        if self.columns_out:
            self.column_names.extend(['crisp_sobel', 'crisp_canny',
                                      'crisp_laplace'])
Example #6
0
    def initialShapeFilter(self):
        # openRedMIP = self.openImage_runnable(self.cellData.stack_channel_images[self.cellData.channels[1]][0])
        #runs an opening to amplify separation between cells

        gaussianredmip = trackpy.bandpass(
            self.cellData.stack_channel_images[self.cellData.channels[1]][0],
            1, 27)
        gaussianredmip = gaussian(
            self.cellData.stack_channel_images[self.cellData.channels[1]][0],
            sigma=7)
        gaussianredmip = laplace(gaussianredmip)
        #gaussian smoothing for binary mask

        binary_gaussian_red = shapeFilter.getBinary_runnable(
            gaussianredmip, use_percentile=True, percentile=0.5)

        self.cellData.saveImages(binary_gaussian_red.astype(numpy.uint16),
                                 self.cellData.basedir, 'Labeled_Binary_Red',
                                 'binary_mip')
        #creates binary mask using otsu

        binary_gaussian_red = shapeFilter.labelBinaryImage_runnable(
            binary_gaussian_red)

        self.cellData.saveImages(binary_gaussian_red.astype(numpy.uint16),
                                 self.cellData.basedir, 'Labeled_Binary_Red',
                                 'initial_labeling')

        binary_gaussian_red = shapeFilter.areaFilter_runnable(
            binary_gaussian_red)

        self.cellData.saveImages(binary_gaussian_red.astype(numpy.uint16),
                                 self.cellData.basedir, 'Labeled_Binary_Red',
                                 'binary_opened_mip')
        #removes small objects from binary mask

        Image_properties, binary_gaussian_red = shapeFilter.getImageCoordinates_runnable(
            binary_gaussian_red)
        # gets properties of labeled binary objects

        for i in range(len(Image_properties)):
            props = Image_properties[i]
            if int(props.equivalent_diameter) > 40:
                pass
            else:
                self.cellData.labeled_properties[i] = {
                    'bbox': list(props.bbox),
                    'area': int(props.filled_area),
                    'y': int(props.centroid[1]),
                    'x': int(props.centroid[0]),
                    'diameter': int(props.equivalent_diameter),
                    'label': int(props.label)
                }

        self.cellData.processed_stack_images[self.cellData.channels[1]][
            'Labeled Binary Red'] = binary_gaussian_red
        self.cellData.saveImages(binary_gaussian_red.astype(numpy.uint16),
                                 self.cellData.basedir, 'Labeled_Binary_Red',
                                 'binary_mip_labeled')
        self.saveMetaData(foldername='Labeled_Binary_Red')
Example #7
0
def edges_diblasi(img, gauss=5, details=1, plot=[]):

    # RGB to gray ("Luminance channel" in Di Blasi)
    img_gray = sk.color.rgb2gray(img)

    # equalize histogram
    img_eq = sk.exposure.equalize_hist(img_gray)

    # soften image
    img_gauss = filters.gaussian(img_eq, sigma=16, truncate=gauss / 16)

    # segment bright areas to blobs
    variance = img_gauss.std()**2  #  evtl. direkt die std verwenden
    img_seg = np.ones((img.shape[0], img.shape[1]))
    threshold = variance / 4 * 2 * details
    img_seg[abs(img_gauss - img_gauss.mean()) > threshold] = 0

    ### 5. Kanten finden
    img_edge = filters.laplace(img_seg, ksize=3)
    img_edge[img_edge != 0] = 1

    if 'edges' in plot:
        plotting.plot_image(img_edge, inverted=True, title='Di Blasi')

    return img_edge
Example #8
0
def laplacian_of_gaussian_img(img, sigma=5, labels_img=None, thresh=None):
    """Generate a Laplacian of Gaussian (LoG) image.
    
    Args:
        img: Image as Numpy array from which the LoG will be generated.
        sigma: Sigma for Gaussian filters; defaults to 5.
        labels_img: Labels image as Numpy array in same shape as ``img``, 
            to use to assist with thresholding out background. Defaults 
            to None to skip background removal.
        thresh: Threshold of atlas image to find background solely from 
            atlas, without using labels; ``labels_img`` will be 
            ignored if ``thresh`` is given. Defaults to None.
             
    """
    # apply Laplacian of Gaussian filter (sequentially)
    img_log = filters.gaussian(img, sigma)
    img_log = filters.laplace(img_log)
    vmin, vmax = np.percentile(img_log, (2, 98))
    img_log = np.clip(img_log, vmin, vmax)

    # remove background signal to improve zero-detection at outside borders
    mask = None
    if thresh is not None:
        # simple threshold of atlas to find background
        mask = img > thresh
    elif labels_img is not None:
        # remove signal below threshold while keeping any signal in labels;
        # TODO: consider using atlas rather than LoG image
        mask = segmenter.mask_atlas(img_log, labels_img)
    if mask is not None:
        img_log[~mask] = np.amin(img_log)

    return img_log
    def predict_image(self, test_img):
        """
        predicts classes of input image
        :param test_img: filepath to image to predict on
        :param show: displays segmentation results
        :return: segmented result
        """
        img = np.array( rgb2gray( imread( test_img ).astype( 'float' ) ).reshape( 5, 216, 160 )[-2] ) / 256

        plist = []

        # create patches from an entire slice
        img_1 = adjust_sigmoid( img ).astype( float )
        edges_1 = adjust_sigmoid( img, inv=True ).astype( float )
        edges_2 = img_1
        edges_5_n = normalize( laplace( img_1 ) )
        edges_5_n = img_as_float( img_as_ubyte( edges_5_n ) )

        plist.append( extract_patches_2d( edges_1, (23, 23) ) )
        plist.append( extract_patches_2d( edges_2, (23, 23) ) )
        plist.append( extract_patches_2d( edges_5_n, (23, 23) ) )
        patches = np.array( zip( np.array( plist[0] ), np.array( plist[1] ), np.array( plist[2] ) ) )

        # predict classes of each pixel based on model
        full_pred = self.model.predict_classes( patches )
        fp1 = full_pred.reshape( 194, 138 )
        return fp1
Example #10
0
    def predict_image(self, test_img):
        """
        predicts classes of input image
        :param test_img: filepath to image to predict on
        :param show: displays segmentation results
        :return: segmented result
        """
        img = np.array(
            rgb2gray(imread(test_img).astype('float')).reshape(5, 216,
                                                               160)[-2]) / 256

        plist = []

        # create patches from an entire slice
        img_1 = adjust_sigmoid(img).astype(float)
        edges_1 = adjust_sigmoid(img, inv=True).astype(float)
        edges_2 = img_1
        edges_5_n = normalize(laplace(img_1))
        edges_5_n = img_as_float(img_as_ubyte(edges_5_n))

        plist.append(extract_patches_2d(edges_1, (23, 23)))
        plist.append(extract_patches_2d(edges_2, (23, 23)))
        plist.append(extract_patches_2d(edges_5_n, (23, 23)))
        patches = np.array(
            zip(np.array(plist[0]), np.array(plist[1]), np.array(plist[2])))

        # predict classes of each pixel based on model
        full_pred = self.model.predict_classes(patches)
        fp1 = full_pred.reshape(194, 138)
        return fp1
def laplace_filt(
    folder
):  # iterate through folders, assembling feature, label, and classname data objects
    class_id = 0
    features = []
    labels = np.array([])
    classnames = []
    for root, dirs, filenames in os.walk(folder):
        for d in sorted(dirs):
            #print("Reading data from", d)
            classnames.append(
                d)  # use the folder name as the class name for this label
            files = os.listdir(os.path.join(root, d))
            for f in files:
                imgFile = os.path.join(root, d, f)  # Load the image file
                img = plt.imread(imgFile)
                img = cv2.resize(
                    img,
                    (128,
                     128))  # Resizing all the images to insure proper reading
                #img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
                laplace_filter = laplace(img, ksize=3, mask=None)

                features.append(laplace_filter.ravel())
                labels = np.append(
                    labels, class_id)  # Add it to the numpy array of labels
            class_id += 1

    features = np.array(
        features)  # Convert the list of features into a numpy array
    return features, labels, classnames
Example #12
0
def main():
    colour_path = sys.argv[1]
    render_path = sys.argv[2]
    out_path = sys.argv[3]

    if not os.path.exists(out_path):
        os.makedirs(out_path)
    else:
        print("path already exists")
        return

    cimgs = sorted(glob.glob(os.path.join(colour_path, "colour_*.png")),
                   key=keyFunc)
    rimgs = sorted(glob.glob(os.path.join(render_path, "depth_*.png")),
                   key=keyFunc)

    #for cimg_path, rimg_path in zip(cimgs, rimgs)[:10]:
    for cimg_path, rimg_path in zip(cimgs, rimgs):
        filename = os.path.splitext(os.path.basename(cimg_path))[0]
        file_nr = filter(str.isdigit, filename)

        cimg = io.imread(cimg_path)
        rimg = io.imread(rimg_path)

        mask = (rimg == rimg.max())

        edge_laplace = laplace(mask)

        cimg[edge_laplace > 0] = [255, 0, 0]

        #io.imshow(cimg)
        #io.show()

        io.imsave(os.path.join(out_path, "colour_overlay_" + file_nr + ".png"),
                  cimg)
Example #13
0
def laplace_only(data, *args, **kwargs):
    from skimage.filters import laplace
    h, w = data.shape[-2:]
    arr = []
    for frame in data.reshape((-1, h, w)):
        arr += [laplace(frame, *args, **kwargs)]
    return np.array(arr).reshape(data.shape)
Example #14
0
    def normalize_filtered_image(self, img, fissure_image):
        prefiltered = self.remove_salt_noise(img)
        gauss_img = gaussian(image, sigma=1.5)
        max_val = np.max(gauss_img)
        min_val = np.min(gauss_img)
        
        img_norm = (img - min_val) / (max_val - min_val)
        img_norm[img_norm > 1.0] = 1.0
        img_norm[img_norm < 0.0] = 0.0

        laplace_img[:,:,i] = laplace(gauss_img[:,:,i], 3)
        laplace_img[fissure_image>0] = -1
        laplace_max = np.max(laplace_img)
        
        spots = h_maxima(log, h=h)
        spots[log<=eps] = 0
        spots[gauss_img<=gauss_threshold] = 0
            
        lab = label(spots)
        properties = regionprops(lab)
        coordinates = [properties[i]['centroid'] for i in range(len(properties))]

        props = regionprops(ws, gauss_img[:,:,i])
        intensities['gauss'][channel] = np.array([props[k]['mean_intensity'] for k in range(len(props))])
        perc = np.percentile(gauss_img[:,:,i], perc_for_vis)
        maxvals['gauss'][channel] = perc[1]
        minvals['gauss'][channel] = perc[0]

            
        return
Example #15
0
def laplace_image(input_folder):
    sub_folders = os.listdir(input_folder)
    variances = []
    maximumes = []

    for folder in sub_folders:
        sub_folder = os.path.join(input_folder, folder)
        if not os.path.isdir(sub_folder):
            continue
        list_file = os.listdir(sub_folder)

        for file in list_file:
            if file.endswith(('.png', '.jpg', 'JPEG')):
                input_file = os.path.join(sub_folder, file)

                #preprocessing
                img = io.imread(input_file)
                img = resize(img, (400, 600))
                img = rgb2gray(img)

                #Edge Detection use Laplace
                edge_laplace = laplace(img, ksize=3)

                #print(f"Variance: {variance(edge_laplace)}")
                variances.append(variance(edge_laplace))

                #print(f'Maximum: {np.amax(edge_laplace)}')
                maximumes.append(np.amax(edge_laplace))
    return variances, maximumes
Example #16
0
def preprocess_data(data, process_method='default'):
    """
    Args:
        data(dict): Python dict loaded using io_tools.
        process_method(str): processing methods needs to support
          ['raw', 'default'].
        if process_method is 'raw'
          1. Convert the images to range of [0, 1]
          2. Remove mean.
          3. Flatten images, data['image'] is converted to dimension (N, 28*28)
        if process_method is 'default':
          1. Convert images to range [0,1]
          2. Apply laplacian filter with window size of 11x11. (use skimage)
          3. Remove mean.
          3. Flatten images, data['image'] is converted to dimension (N, 28*28)

    Returns:
        data(dict): Apply the described processing based on the process_method
        str to data['image'], then return data.
    """
    if process_method == 'default':
        #convert the images to range of [0,1]
        file_size = len(data['image'])
        for x in range(file_size):
            data['image'][x] = skimage.img_as_float(data['image'][x])

        data = remove_data_mean(data)
        flatten_images = np.zeros(shape=(len(data['image']), 28 * 28))
        for x in range(file_size):
            flatten_images[x] = data['image'][x].flatten()
        data['image'] = flatten_images

    elif process_method == 'raw':
        file_size = len(data['image'])
        for x in range(file_size):
            data['image'][x] = skimage.img_as_float(data['image'][x])
            filters.laplace(data['image'][x], 11)
        data = remove_data_mean(data)
        flatten_images = np.zeros(shape=(len(data['image']), 28 * 28))
        for x in range(file_size):
            flatten_images[x] = data['image'][x].flatten()
        data['image'] = flatten_images

    elif process_method == 'custom':
        pass

    return data
Example #17
0
 def _find_front(self):
     """ Find the front using laplacian on the mask
     The laplacian will give us the edges of the mask, it will be positive
     at the higher region (white) and negative at the lower region (black).
     We only want the the white region, which is inside the mask, so we
     filter the negative values.
     """
     self.front = (laplace(self.working_mask) > 0).astype('uint8')
Example #18
0
def test_laplace_mask():
    """Laplace on a masked array should be zero."""
    # Create a synthetic 2D image
    image = np.zeros((9, 9))
    image[3:-3, 3:-3] = 1
    # Define the mask
    result = filters.laplace(image, ksize=3, mask=np.zeros((9, 9), bool))
    assert (np.all(result == 0))
Example #19
0
def test_laplace_mask():
    """Laplace on a masked array should be zero."""
    # Create a synthetic 2D image
    image = np.zeros((9, 9))
    image[3:-3, 3:-3] = 1
    # Define the mask
    result = filters.laplace(image, ksize=3, mask=np.zeros((9, 9), dtype=bool))
    assert (np.all(result == 0))
def laplace_filter(image):
	im = io.imread(image, as_gray = True)
	im_new = filters.laplace(im)
	im_new = color.rgb2gray(im_new)
	plt.figure()
	plt.imshow(im_new, cmap='Greys_r')
	plt.savefig('laplacefilter.png')
	plt.show()
Example #21
0
 def get_bg_contrast(img_gray: np.ndarray, params: Dict[str, Any]):
     bg_radius = params.get("bg_radius", 5)
     bg_smooth_thresh = params.get("bg_smooth_thresh", 0.03)
     img_laplace = np.abs(laplace(img_gray))
     mask = gaussian(img_laplace, sigma=bg_radius) <= bg_smooth_thresh
     background = (mask != 0) * img_gray
     background[mask == 0] = 1  # background[mask_background].mean()
     return background, mask
Example #22
0
def get_info_from_image(image):

    if len(image.shape) == 3:  # convert image to gray scale
        img = rgb2gray(image)

    x1 = variance(laplace(img, ksize=3))  # return variance of laplancian
    x2 = estimate_sigma(img)  # returns Noise

    return x1, x2
Example #23
0
def hfn(gt,pred):

    hfn_total = []

    for ii in range(gt.shape[-1]):
        gt_slice = gt[:,:,ii]
        pred_slice = pred[:,:,ii]

        pred_slice[pred_slice<0] = 0 #bring the range to 0 and 1.
        pred_slice[pred_slice>1] = 1

        gt_slice_laplace = laplace(gt_slice)        
        pred_slice_laplace = laplace(pred_slice)

        hfn_slice = np.sum((gt_slice_laplace - pred_slice_laplace) ** 2) / np.sum(gt_slice_laplace **2)
        hfn_total.append(hfn_slice)

    return np.mean(hfn_total)
Example #24
0
def image_to_df(image_name):
    car_id = image_name.split('_')[0]
    image = misc.imread('train_sample/' + image_name + '.jpg',
                        flatten=True).astype(float)
    image_rgb = misc.imread('train_sample/' + image_name + '.jpg')
    image_float = image_rgb.astype(float)
    image_mask_all_angles = misc.imread('train_all_masks/' + car_id + '.jpg',
                                        flatten=True) / 255.
    image_mask = misc.imread('train_masks/' + image_name + '_mask.gif',
                             flatten=True) / 255
    #io.imshow(image_mask)
    image_index = np.where(image >= 0)
    sobel = filters.sobel(image)  # working
    #io.imshow(sobel)
    sobel_blurred = filters.gaussian(sobel, sigma=1)  # Working
    #io.imshow(sobel_blurred)
    canny_filter_image = canny(image / 255.)
    #io.imshow(canny_filter_image)
    #    threshold_niblack_11 = filters.threshold_niblack(sobel_blurred,201)
    #io.imshow(threshold_niblack)
    threshold_li = filters.threshold_li(image)
    mask_li = image > threshold_li
    #io.imshow(mask)
    sobel_h = filters.sobel_h(image)
    sobel_v = filters.sobel_v(image)
    laplace = filters.laplace(image)
    threshold_local_51 = filters.threshold_local(image, 51)
    mask_local_51 = image > threshold_local_51
    #io.imshow(mask)
    df = pd.DataFrame()
    #    df['y'] = (image_index[0] - 639.5)/639.5
    #    df['x'] = (image_index[1] - 958.5)/958.5
    df['l1_dist_y'] = abs((image_index[0] - 639.5) / 639.5)
    df['l1_dist_x'] = abs((image_index[1] - 958.5) / 958.5)
    df['l2_dist'] = np.sqrt((df['l1_dist_x'])**2 +
                            (df['l1_dist_y'])**2) / np.sqrt(2)
    df['grey_values'] = image.reshape((1, 1918 * 1280))[0] / 255.
    df['red_values'] = image_rgb.reshape((3, 1918 * 1280))[0] / 255.
    df['blue_values'] = image_rgb.reshape((3, 1918 * 1280))[1] / 255.
    df['green_values'] = image_rgb.reshape((3, 1918 * 1280))[2] / 255.
    df['red_float'] = image_float.reshape((3, 1918 * 1280))[0] / 255.
    df['blue_float'] = image_float.reshape((3, 1918 * 1280))[1] / 255.
    df['green_float'] = image_float.reshape((3, 1918 * 1280))[2] / 255.
    df['sobel_blurred'] = sobel_blurred.reshape((1, 1918 * 1280))[0] / 255.
    df['canny_filter_image'] = canny_filter_image.reshape(
        (1, 1918 * 1280))[0].astype(int)
    df['sobel_h'] = sobel_h.reshape((1, 1918 * 1280))[0] / 255.
    df['sobel_v'] = sobel_v.reshape((1, 1918 * 1280))[0] / 255.
    df['laplace'] = laplace.reshape((1, 1918 * 1280))[0] / 511.
    df['threshold_local_51'] = mask_local_51.reshape(
        (1, 1918 * 1280))[0].astype(int)
    #    df['threshold_niblack_11'] = threshold_niblack_11.reshape((1,1918*1280))[0]#/255.
    df['threshold_li'] = mask_li.reshape((1, 1918 * 1280))[0].astype(int)
    df['mask'] = image_mask.reshape((1, 1918 * 1280))[0]
    df['mask'] = df['mask'].astype('category')
    return df
Example #25
0
def segment_clusters(imgf, bkg_imgf=None, offset=10):
    """Segment out bright clusters from fluorescence images."""
    img, sig = preprocess_img(imgf, bkg_imgf, offset)
    log = laplace(gaussian(img, sigma=1.5))
    bkg = threshold_local(log,
                          block_size=25,
                          offset=-2 * sig,
                          method='gaussian')
    thr = log > bkg
    return thr, img
    def getBoundary(self):
        # semantic contours
        uniqueId = np.unique(self.semanticId)
        self.semanticContours = {}
        for uid in uniqueId:
            mask = (self.semanticId == uid).astype(np.uint8) * 255
            mask_filter = filters.laplace(mask)
            self.semanticContours[uid] = np.expand_dims(
                np.abs(mask_filter) > 0, 2)

        # instance contours
        globalId = local2global(self.semanticId, self.instanceId)
        uniqueId = np.unique(globalId)
        self.instanceContours = {}
        for uid in uniqueId:
            mask = (globalId == uid).astype(np.uint8) * 255
            mask_filter = filters.laplace(mask)
            self.instanceContours[uid] = np.expand_dims(
                np.abs(mask_filter) > 0, 2)
Example #27
0
def adjustData_L_rgb_3(img, mask):
    if(np.max(img) > 1):
        img = img / 255
        mask = mask / 255
        mask[mask > 0.5] = 1
        mask[mask <= 0.5] = 0
#         gray = np.resize(gray, (256,256,1))
        laplacian = filters.laplace(img, 3)
        laplacian = normalize(laplacian)
        laplacian = np.resize(laplacian, img.shape)
    return (img, laplacian, mask)
Example #28
0
def laplace_folder(input_folder, sub_folder_b=False):
    sub_folders = []
    if sub_folder_b:
        sub_folders = os.listdir(input_folder)
    else:
        sub_folders.append(input_folder)
    variances = []
    maximumes = []

    variance_sobel = []
    maximumes_sobel = []

    for sub_folder in sub_folders:
        if sub_folder_b:
            folder = os.path.join(input_folder, sub_folder)
        else:
            folder = sub_folder
        if not os.path.isdir(folder):
            continue
        list_file = os.listdir(folder)

        for file in list_file:
            if file.endswith(('.png', '.jpg', 'JPEG')):
                input_file = os.path.join(folder, file)

                #preprocessing
                image = io.imread(input_file)
                img = resize(image, (112, 112))
                img = rgb2gray(img)

                #edge detection use laplace
                edge_laplace = laplace(img, ksize=3)

                #edge detection with Sobel filter
                edge_soble = sobel(img)

                variances.append(variance(edge_laplace))
                maximumes.append(np.amax(edge_laplace))

                # test_blurry = '/home/duong/Documents/researching/GAN/common/image_enhance/image_cmt/test_blurry'
                # blurry_folder = os.path.join(test_blurry,file)

                # test_good = '/home/duong/Documents/researching/GAN/common/image_enhance/image_cmt/test_good'
                # good_folder = os.path.join(test_good,file)

                # if variance(edge_laplace) < 0.0015 and np.amax(edge_laplace) < 0.3:
                #     io.imsave(blurry_folder,image)
                # else:
                #     io.imsave(good_folder,image)

                #variance_sobel.append(variance(edge_soble))
                #maximumes_sobel.append(np.amax(edge_soble))
    #return variances, maximumes, variance_sobel, maximumes_sobel
    return variances, maximumes
Example #29
0
def computeModels(img):
    '''Calcule les resultats des differentes methodes de detection d'objets'''
    #results = {'Image original': ing}
    results = {}
    results['Canny'] = feature.canny(img, sigma=1)
    results['Roberts'] = roberts(img)
    results['Sobel'] = sobel(img)
    results['Scharr'] = scharr(img)
    results['Prewitt'] = prewitt(img)
    results['Laplace'] = laplace(img)
    return results
Example #30
0
 def calculate_laplacian(self):
     for image_name, image_path in utils.folder_reader(self.frames_raw):
         image_read = io.imread(os.path.join(image_path, image_name))
         gray = rgb2gray(image_read)
         gauss = gaussian(gray)
         fm = laplace(gauss, ksize=3)
         # Output
         self.fm_list.append(fm)
         self.variance_list.append(variance(fm) * 1000)
         self.max_list.append(np.amax(fm))
         os.makedirs(self.frames_blur, exist_ok=True)
Example #31
0
def laplace_rgb(image):
    """ Aplica o filtro Laplaciano, utilizando o decorador
    @adapt_rgb para que o filtro seja aplicado em todos
    os canais RGB da imagem.

    @param image numpy.ndarray.

    @return result numpy.ndarray.
    """

    return filters.laplace(image)
Example #32
0
def smoothe_and_sharp():
    image=data.astronaut()
    g3=filters.gaussian(image,3)
    io.imsave('astronautgaussian3.png',g3)
    g9=filters.gaussian(image,9)
    io.imsave('astronautgaussian9.png',g9)
    g15=filters.gaussian(image,15)
    io.imsave('astronautgaussian15.png',g15)
    
    image=io.imread('astronautgaussian3.png')
    sharprgb=image.copy()
    for i in range(3):
        l=np.abs(filters.laplace(image[:,:,i]))
        sharprgb[:,:,i]=np.uint8(np.minimum(image[:,:,i]+l/l.max()*55,255))
    io.imsave('astronautsharprgb.png',sharprgb)
    
    sharphsv=color.rgb2hsv(image)
    l=np.abs(filters.laplace(sharphsv[:,:,2]))
    sharphsv[:,:,2]=np.minimum(l/l.max()*0.5+sharphsv[:,:,2],1)
    io.imsave('astronautsharphsv.png',color.hsv2rgb(sharphsv))
Example #33
0
def sieberth_blur_metric(img_path, **kwargs):
    """
    Compute the Sieberth et al.'s Saturation Image Edge
    Difference Standard Deviation (SIEDS) metric.

    T. Sieberth, R. Wackrow, and J. H. Chandler. Automatic
    Detection of Blurred Images in UAV Image Sets. ISPRS
    Journal of Photogrammetry and Remote Sensing, 122:1–16,
    Dec. 2016.

    :param img_path: String. Path to the image file.

    :return: List of floats. Containing the SIEDS metric value.
    """

    if img_path is None:
        return [-1]

    # read the image
    img = imread(img_path)

    # downsample the image to 1/3
    img = rescale(img, 1 / 3., preserve_range=True, mode='constant')

    # get the saturation info
    s = rgb2hsv(img)

    # blur the original image
    b = blur(s, (3, 3))

    # apply high-pass filter on both versions
    se = laplace(s, 3)
    be = laplace(b, 3)

    # compute the discrepancy map
    d = np.abs(se - be)

    # compute the SIEDS (Saturation Image Edge Difference Standard Deviation)
    sieds = d.std()

    return [sieds]
Example #34
0
def gradient_vector_flow(f_x, f_y, mu=0.2, max_iter=10, keep_calm=True):
    # calculate parameters of flow
    dt = 0.5 / (4.0 * mu)
    r = mu * dt
    # make copy of data
    ux = np.copy(f_x)
    uy = np.copy(f_y)
    
    B = np.power(f_x, 2.0) + np.power(f_y, 2.0)
    C = B * f_x
    D = B * f_y
    
    # iterate flow    
    for it in xrange(max_iter):
        ux = (1.0 - B * dt) * ux - r * laplace(ux) + C * dt
        uy = (1.0 - B * dt) * uy - r * laplace(uy) + D * dt
        if it % 100 == 0 and not keep_calm:
            print "  [GVF] Iteration:", it
    if not keep_calm:
        print "  [GVF] Finished: {0} iterations".format(max_iter)
    return ux, uy
def laplacian_filter(image):
    """Computes the Laplace operator over the original image.

    Keyword arguments:
        image -- an ndarray representing the image to which the filter
        will be applied

    Returns:
        ndarray: an image that is the same size as the original image
        representing the Laplacian of the original image.
    """
    return filters.laplace(image, ksize=3)
Example #36
0
def test_laplace_zeros():
    """Laplace on a square image."""
    # Create a synthetic 2D image
    image = np.zeros((9, 9))
    image[3:-3, 3:-3] = 1
    result = filters.laplace(image)
    res_chk = np.array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
                        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
                        [ 0.,  0.,  0., -1., -1., -1.,  0.,  0.,  0.],
                        [ 0.,  0., -1.,  2.,  1.,  2., -1.,  0.,  0.],
                        [ 0.,  0., -1.,  1.,  0.,  1., -1.,  0.,  0.],
                        [ 0.,  0., -1.,  2.,  1.,  2., -1.,  0.,  0.],
                        [ 0.,  0.,  0., -1., -1., -1.,  0.,  0.,  0.],
                        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
                        [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]])
    assert_allclose(result, res_chk)
Example #37
0
plt.figure()            
io.imshow(filt_imag)    
io.show()   

##
from skimage.filters import gaussian, gaussian_filter, laplace,prewitt
from skimage.filters import prewitt_v,prewitt_h,scharr, wiener

gauss=gaussian(gray0, sigma=5, multichannel=True)
plt.imshow(gauss)

gauss2=gaussian_filter(gray0, sigma=5, multichannel=True)
plt.imshow(gauss2)

lap=laplace(gray0,ksize=100)
plt.imshow(lap)

pre=prewitt(gray0, mask=None)
plt.imshow(pre)

pre_v=prewitt_v(gray0, mask=None)
plt.imshow(pre_v)

from skimage import filters
edges2 = filters.roberts(gray0)
plt.imshow(edges2)

plt.imshow(scharr(gray0))
plt.imshow(threshold_mean(gray0))
plt.imshow(wiener(gray0))
    def _find_patches(self, class_number, per_class):
        """
        this function in dependence of the class number search for patches with the edge pattern
        :param per_class: number of patches to find
        :param class_number the class number
        :return:
        a numpy array of patches and a numpy array of labels
        """
        print()
        patches = []
        labels = np.ones( per_class * self.augmentation_multiplier ) * class_number

        ten_percent_black = 0
        ten_percent_black_value = int( float( per_class ) * 0.0001 )

        start_value_extraction = 0
        full = False

        if isdir( 'patches/' ) and isdir( 'patches/sigma_{}/'.format( self.sigma
                                                                      ) ) and isdir(
            'patches/sigma_{}/class_{}/'.format( self.sigma,
                                                 class_number ) ):

            # load all patches
            # check if quantity is enough to work
            path_to_patches = sorted( glob( './patches/sigma_{}/class_{}/**.png'.format( self.sigma,
                                                                                         class_number ) ),
                                      key=get_right_order )

            for path_index in xrange( len( path_to_patches ) ):
                if path_index < per_class:
                    patch_to_load = np.array( rgb2gray( imread( path_to_patches[path_index],
                                                                dtype=float ) ).reshape( 3,
                                                                                         self.patch_size[0],
                                                                                         self.patch_size[1] ) ).astype(
                        float )
                    patches.append( patch_to_load )
                    for el in xrange( len( patch_to_load ) ):
                        if np.max( patch_to_load[el] ) > 1:
                            patch_to_load[el] /= np.max( patch_to_load[el] )
                    print( '*---> patch {}/{} loaded and added '.format( path_index, per_class ) )
                else:
                    full = True
                    break

            if len( path_to_patches ) < per_class:
                # change start_value_extraction
                start_value_extraction = len( path_to_patches )
            else:
                full = True
        else:
            mkdir_p( 'patches/sigma_{}/class_{}'.format( self.sigma,
                                                         class_number ) )

        patch_to_extract = 25000

        if not full:
            for i in range( start_value_extraction, per_class ):
                extracted = False
                random_image = self.images[randint( 0, len( self.images ) - 1 )]
                while np.array_equal( random_image, np.zeros( random_image.shape ) ):
                    random_image = self.images[randint( 0, len( self.images ) - 1 )]
                patches_from_random = np.array( extract_patches_2d( random_image, self.patch_size, patch_to_extract ) )
                counter = 0

                while not extracted:
                    if counter > per_class / 2:
                        random_image = self.images[randint( 0, len( self.images ) - 1 )]
                        patches_from_random = np.array(
                            extract_patches_2d( random_image, self.patch_size, patch_to_extract ) )
                        counter = 0
                    patch = np.array( patches_from_random[randint( 0, patch_to_extract - 1 )].astype( float ) )
                    if patch.max() > 1:
                        patch = normalize( patch )

                    patch_1 = adjust_sigmoid( patch )
                    edges_1 = adjust_sigmoid( patch, inv=True )
                    edges_2 = patch_1
                    edges_5_n = normalize( laplace( patch ) )
                    edges_5_n = img_as_float( img_as_ubyte( edges_5_n ) )

                    choosing_cond = is_boarder( patch=patch_1, sigma=self.sigma )

                    if class_number == 1 and choosing_cond:
                        final_patch = np.array( [edges_1, edges_2, edges_5_n] )
                        patches.append( final_patch )
                        try:
                            imsave( './patches/sigma_{}/class_{}/{}.png'.format( self.sigma,
                                                                                 class_number,
                                                                                 i ),
                                    final_patch.reshape( (3 * self.patch_size[0], self.patch_size[1]) ),
                                    dtype=float )
                        except:
                            print( 'problem occurred in save for class {}'.format( class_number ) )
                            exit( 0 )

                        print( '*---> patch {}/{} added and saved '.format( i, per_class ) )
                        extracted = True

                    elif class_number == 0 and not choosing_cond:
                        if np.array_equal( patch, np.zeros( patch.shape ) ):
                            if ten_percent_black < ten_percent_black_value:
                                final_patch = np.array( [patch, edges_2, edges_5_n] )
                                patches.append( final_patch )
                                try:

                                    imsave( './patches/sigma_{}/class_{}/{}.png'.format( self.sigma,
                                                                                         class_number,
                                                                                         i ),
                                            final_patch.reshape( (3 * self.patch_size[0], self.patch_size[1]) ),
                                            dtype=float )
                                except:
                                    print( 'problem occurred in save for class {}'.format( class_number ) )
                                    exit( 0 )

                                print( '*---> patch {}/{} added and saved '.format( i, per_class ) )
                                ten_percent_black += 1
                                extracted = True
                            else:
                                pass
                        else:
                            final_patch = np.array( [edges_1, edges_2, edges_5_n] )
                            patches.append( final_patch )
                            try:

                                imsave( './patches/sigma_{}/class_{}/{}.png'.format( self.sigma,
                                                                                     class_number,
                                                                                     i ),
                                        final_patch.reshape( (3 * self.patch_size[0], self.patch_size[1]) ),
                                        dtype=float )
                            except:
                                print( 'problem occurred in save for class {}'.format( class_number ) )
                                exit( 0 )

                            print( '*---> patch {}/{} added and saved '.format( i, per_class ) )
                            extracted = True
                    counter += 1

        if self.augmentation_angle != 0:
            print( "\n *_*_*_*_* proceeding  with data augmentation for class {}  *_*_*_*_* \n".format( class_number ) )

            if isdir( './patches/sigma_{}/class_{}/rotations'.format( self.sigma,
                                                                      class_number ) ):
                print( "rotations folder present " )
            else:
                mkdir_p( './patches/sigma_{}/class_{}/rotations'.format( self.sigma,
                                                                         class_number ) )
                print( "rotations folder created" )
            for el_index in xrange( len( patches ) ):
                for j in range( 1, self.augmentation_multiplier ):
                    try:
                        patch_rotated = np.array( rgb2gray( imread(
                            ('./patches/sigma_{}/class_{}/'
                             'rotations/{}_{}.png'.format( self.sigma,
                                                           class_number,
                                                           el_index,
                                                           self.augmentation_angle * j )) ) ).reshape( 3,
                                                                                                       self.patch_size[
                                                                                                           0],
                                                                                                       self.patch_size[
                                                                                                           1]
                                                                                                       ) ).astype(
                            float ) / (
                                            256 * 256)
                        patches.append( patch_rotated )
                        print( '*---> patch {}/{} loaded and added '
                               'with rotation of {} degrees'.format( el_index, per_class,
                                                                     self.augmentation_angle * j ) )
                    except:
                        final_rotated_patch = rotate_patches( patches[el_index][0],
                                                              patches[el_index][1],
                                                              patches[el_index][2],
                                                              self.augmentation_angle * j )
                        patches.append( final_rotated_patch )
                        imsave( './patches/sigma_{}/class_{}/'
                                'rotations/{}_{}.png'.format( self.sigma,
                                                              class_number,
                                                              el_index,
                                                              self.augmentation_angle * j ),
                                final_rotated_patch.reshape( 3 * self.patch_size[0], self.patch_size[1] ),
                                dtype=float )
                        print( ('*---> patch {}/{} saved and added '
                                'with rotation of {} degrees '.format( el_index, per_class,
                                                                       self.augmentation_angle * j )) )
            print()
            print( 'augmentation done \n' )
        print( 'extraction for class {} complete\n'.format( class_number ) )
        return np.array( patches ), labels