def get_processed_data_and_metadata(self, data_and_metadata, parameters):
        api = self.__api

        # only works with 2d, scalar data
        assert data_and_metadata.is_data_2d
        assert data_and_metadata.is_data_scalar_type

        # make a copy of the data so that other threads can use data while we're processing
        # otherwise numpy puts a lock on the data.
        data = data_and_metadata.data
        data_copy = data.copy()

        # grab our parameters. ideally this could just access the member variables directly,
        # but it doesn't work that way (yet).
        radius = parameters.get("radius")*100
        #sigma2 = parameters.get("sigma2")
        #weight2 = parameters.get("weight2")
        
        # Apply mean filter to the data
        data_copy = uniform_filter(data_copy, size=radius)
        
        # Calculate squared differences from original
        result = np.square(data_copy - data)
        
        # Apply mean filter to the result
        #result = gaussian_filter(result, radius)/data_copy
        result = uniform_filter(result, size=radius)/data_copy

        intensity_calibration = data_and_metadata.intensity_calibration
        dimensional_calibrations = data_and_metadata.dimensional_calibrations
        metadata = data_and_metadata.metadata
        return api.create_data_and_metadata_from_data(result, intensity_calibration, dimensional_calibrations, metadata)
Exemple #2
0
def test_multiple_modes():
    # Test that the filters with multiple mode cababilities for different
    # dimensions give the same result as applying a single mode.
    arr = np.array([[1., 0., 0.],
                    [1., 1., 0.],
                    [0., 0., 0.]])

    mode1 = 'reflect'
    mode2 = ['reflect', 'reflect']

    assert_equal(sndi.gaussian_filter(arr, 1, mode=mode1),
                 sndi.gaussian_filter(arr, 1, mode=mode2))
    assert_equal(sndi.prewitt(arr, mode=mode1),
                 sndi.prewitt(arr, mode=mode2))
    assert_equal(sndi.sobel(arr, mode=mode1),
                 sndi.sobel(arr, mode=mode2))
    assert_equal(sndi.laplace(arr, mode=mode1),
                 sndi.laplace(arr, mode=mode2))
    assert_equal(sndi.gaussian_laplace(arr, 1, mode=mode1),
                 sndi.gaussian_laplace(arr, 1, mode=mode2))
    assert_equal(sndi.maximum_filter(arr, size=5, mode=mode1),
                 sndi.maximum_filter(arr, size=5, mode=mode2))
    assert_equal(sndi.minimum_filter(arr, size=5, mode=mode1),
                 sndi.minimum_filter(arr, size=5, mode=mode2))
    assert_equal(sndi.gaussian_gradient_magnitude(arr, 1, mode=mode1),
                 sndi.gaussian_gradient_magnitude(arr, 1, mode=mode2))
    assert_equal(sndi.uniform_filter(arr, 5, mode=mode1),
                 sndi.uniform_filter(arr, 5, mode=mode2))
Exemple #3
0
def blurImage(img):
  '''Returns a blurred version of the original image. Blurring from uniform 
     filter with width= 3% of the largest image dimension.'''
  newimg = np.empty(img.shape, dtype=np.uint8)
  blurval = int( 0.03*max(img.shape) )
  if img.ndim == 3:
    for i in range(img.ndim):
      newimg[:,:,i] = ndimage.uniform_filter(img[:,:,i], blurval)
  else:
    newimg = ndimage.uniform_filter(img, blurval)
  return newimg
def box2(x, sigma, output, mode="wrap"):
    """Approximation of Gaussian filter by 2 box blurs.

    sigma(n) = 0.436589 * n + 0.441425
    n(sigma) = 2.290485 * sigma - 1.011077

    See box3 documentation for further details.
    """
    n = int(round(2.290485 * sigma - 1.011077))
    a, b = 1 + 2 * (n / 2), 1 + 2 * ((n + 1) / 2)
    nd.uniform_filter(x, a, mode=mode, output=output)
    nd.uniform_filter(output, b, mode=mode, output=output)
def get_t11t12_texture_data_from_object(dataObj, nwp_obj, ch11, ch12, text_name):
    #https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
    t11 = get_channel_data_from_objectfull_resolution(dataObj, ch11, nodata=-9)
    t12 = get_channel_data_from_objectfull_resolution(dataObj, ch12, nodata=-9)    
    t11t12 = 1.0*np.array(t11-t12)
    K=np.median(t11t12) #K is trick to get better accurracy maybe not needed as differences are often small
    t11t12 = (t11t12-K)
    from scipy.ndimage import uniform_filter
    mean = uniform_filter(t11t12, size=(5,5), mode='mirror')
    mean_of_squared = uniform_filter(t11t12**2, size=(5,5), mode='mirror')    
    t11t12_texture = mean_of_squared - mean**2 
    setattr(nwp_obj, text_name, t11t12_texture)
    return nwp_obj
	def uniformClicked(self):
		global _img, red, green, blue
		
		red = ndimage.uniform_filter(red, 15)
		green = ndimage.uniform_filter(green, 15)
		blue = ndimage.uniform_filter(blue, 15)
		
		for a in range(0, _img.shape[0]):
			for b in range(0, _img.shape[1]):
				_img[a, b, 0] = red[a, b]
				_img[a, b, 1] = green[a, b]
				_img[a, b, 2] = blue[a, b]
		updateImage()
Exemple #7
0
def running_mean(fm,size):
    fm = fm.copy()
    fm.fill_value = 0
    w = (~fm.mask).astype(float) # weights either 0 or 1
    f = fm.filled()
    assert (np.isnan(f)==False).all() ,'mask out nans'
    
    # total flux in bin
    f_sum = nd.uniform_filter(f,size=size) * size 
    # number of unmasked points in bin
    f_count = nd.uniform_filter(w,size=size) * size
    f_mean = ma.masked_array( f_sum / f_count, f_count < 0.5*size) 
    return f_mean
Exemple #8
0
def _rms_filter(image_data, filt_size=7):
    """
    Runs an 'RMS filter' on image data. Really this is the square root of the
    (appropriately scaled) difference of the squared uniform-filtered image and
    the uniform-filtered squared image. i.e. f*sqrt(boxcar(i**2) - boxcar(i)**2)

    :param image_data: Image data to be filtered
    :param filt_size: Size of uniform filter kernel to use
    :return: RMS-filtered version of input image
    """
    mean_sqr = ndimg.uniform_filter(image_data**2, size=filt_size)
    sqr_mean = ndimg.uniform_filter(image_data, size=filt_size)**2
    sqr_diff_scale = filt_size**2 / (filt_size**2 - 1)
    return np.sqrt(np.abs(sqr_diff_scale * (sqr_mean - mean_sqr)))
    def ref_holo_filter(self, rh, holoref_filter_size):
        #rh=binkoala.read_mat_cplx_bin(path) #open recorded holoref        
        if holoref_filter_size != 1:
            amp = numpy.absolute(rh) #keep original amplitude (not filtered)
            rh = numpy.exp(numpy.angle(rh)*complex(0., 1.)) #amp(rh)=1 to filter
            #%% Apply a polynomial fit of 2nd order to suppress most of the
            #phase jumps before being able to perform the filter on real and img
            #part of rh
            s = numpy.shape(rh)
            height = s[0]
            width = s[1]
            #As only the fit would to be done, don't care about pxsize and lambda
            #The propagation is done with d=0! height and width are the rh size              
            #k=pykoala.core.swl.swl_koala(height,width,pxsize_m,lambda_m,0)
            k = pykoala.core.phaseprocessing
            ph_corr = k.phase_correction(width, height, 0, 0, width, height)
                     
            ##define 2 horizontal and 2 vertical profiles on rh border
            # let 40 pixel from the border of the wavefront
            horiz_seg = k.hv_segment(40, 40, width-80, False) 
            vert_seg = k.hv_segment(40, 40, height-80, True)
            ph_corr.new_hv_seg(horiz_seg)
            ph_corr.new_hv_seg(vert_seg)               
            horiz_seg = k.hv_segment(40, height-80, width-80, False)
            vert_seg = k.hv_segment(width-80, 40, height-80, True)              
            ph_corr.new_hv_seg(horiz_seg)
            ph_corr.new_hv_seg(vert_seg)   
            #perform Fit
            ph_corr.perform_fit(numpy.angle(rh), 2)
            #Extract the computed mask
            mask = ph_corr.phase_mask_img
            #Apply mask to rh
            rh = rh*numpy.exp(mask*complex(0., 1.))
            #%%
            #uniform filter apply separatedly on real and img part or rh
            tmp_real = numpy.real(rh) 
            tmp_img = numpy.imag(rh)
            tmp_real = ndimage.uniform_filter(tmp_real, size=holoref_filter_size)
            tmp_img = ndimage.uniform_filter(tmp_img, size=holoref_filter_size)
            #Compute the filtered complex rh from real and img part
            rh = (tmp_real+tmp_img*complex(0., 1.))
            #Introduce again the phase jumps
            rh = rh*numpy.exp(-mask*complex(0., 1.))
            #again the phase jumps using -mask
            #Create final rh by using the original amplitude and the filtered phase
            rh = amp*numpy.exp(numpy.angle(rh)*complex(0., 1.))

        return rh
Exemple #10
0
def find_lines(image):
    """
    Compute the layout from the image
    """

    draw_image = numpy.array(image)
    tmp_im = numpy.array(image)
    ret, thres_im = cv2.threshold(tmp_im, 4, 255, cv2.THRESH_BINARY)

    thres_im = ndimage.uniform_filter(thres_im, (1, 50))

    ret, thres_im = cv2.threshold(thres_im, 50, 255, cv2.THRESH_BINARY)


    lines = cv2.HoughLinesP(thres_im, 1, math.pi / 4.0, 100, None, 100, 10)
    tmp_mask = np.zeros(thres_im.shape, np.uint8)
    for l in lines[0]:
        a = angle((l[0], l[1]), (l[2], l[3]))
        if abs(a) < 1.0: 
            cv2.line(tmp_mask, (l[0], l[1]), (l[2], l[3]), 255, 1)
        
    contours, hier = cv2.findContours(tmp_mask, cv2.RETR_EXTERNAL, 
                                      cv2.CHAIN_APPROX_TC89_L1)  

    boxes = []
    mask = np.zeros(image.shape, np.uint8)
    draw_image = cv2.cvtColor(draw_image, cv2.COLOR_GRAY2BGR)
    for cnt in contours:
        box  = cv2.boundingRect(cnt)
        x, y, width, height = box
        if width > 20 and height > 2 and height < 40:
            boxes.append((x, y, width, height))
    return boxes
Exemple #11
0
def test_multiple_modes_sequentially():
    # Test that the filters with multiple mode cababilities for different
    # dimensions give the same result as applying the filters with
    # different modes sequentially
    arr = np.array([[1., 0., 0.],
                    [1., 1., 0.],
                    [0., 0., 0.]])

    modes = ['reflect', 'wrap']

    expected = sndi.gaussian_filter1d(arr, 1, axis=0, mode=modes[0])
    expected = sndi.gaussian_filter1d(expected, 1, axis=1, mode=modes[1])
    assert_equal(expected,
                 sndi.gaussian_filter(arr, 1, mode=modes))

    expected = sndi.uniform_filter1d(arr, 5, axis=0, mode=modes[0])
    expected = sndi.uniform_filter1d(expected, 5, axis=1, mode=modes[1])
    assert_equal(expected,
                 sndi.uniform_filter(arr, 5, mode=modes))

    expected = sndi.maximum_filter1d(arr, size=5, axis=0, mode=modes[0])
    expected = sndi.maximum_filter1d(expected, size=5, axis=1, mode=modes[1])
    assert_equal(expected,
                 sndi.maximum_filter(arr, size=5, mode=modes))

    expected = sndi.minimum_filter1d(arr, size=5, axis=0, mode=modes[0])
    expected = sndi.minimum_filter1d(expected, size=5, axis=1, mode=modes[1])
    assert_equal(expected,
                 sndi.minimum_filter(arr, size=5, mode=modes))
Exemple #12
0
def uniform_filter(tup, sigma=(2, 2)):
    """
    Apply an uniform filter to data.
    """
    wl, t, d = tup.wl, tup.t, tup.data
    f = nd.uniform_filter(d, size=sigma, mode="nearest")
    return dv.tup(wl, t, f)
Exemple #13
0
 def get_align_indices(self, wave):
     #wave = helpers.blur_image(wave.astype('float'),1)
     wave = sn.uniform_filter(wave.astype('float'), (3,3))
     indices = []
     w_base = wave.mean(axis=0)
     w_base_n = (w_base-w_base.min())/(w_base.max()-w_base.min())
     pad_left = n.ones(wave.shape[1]/2.)*w_base_n[0:10].mean()
     pad_right = n.ones(wave.shape[1]/2.)*w_base_n[-10:].mean()
     ww0=n.hstack((pad_left,w_base_n,pad_right))
     flatten = 3
     for i in range(wave.shape[0]):
         if 0:
             indices.append(0)
         else:
             ww = wave[max(0,i-flatten):min(wave.shape[0], i+flatten)]
             w_i = ww.mean(axis=0)
             w_i2 = helpers.smooth(wave[i])
             w_i = helpers.smooth(w_i)
             w_i_n = (w_i-w_i.min())/(w_i.max()-w_i.min())
             w_i_n2 = (w_i2-w_i2.min())/(w_i2.max()-w_i2.min())
             cc = ss.correlate(ww0, w_i_n, mode='valid')
             indices.append(cc.argmax()-wave.shape[1]/2.)
     #make a nice polynomial fit for the indices
     indices = n.array(indices).astype('int')
     return indices
Exemple #14
0
def smooth(indata, type, kern_px, kern_py, kern_pz):
    """
    Smooth image array

    type = g: gaussian
    type = b: boxcar (average)
    #type = m: boxcar (median), nb kernal must be 3D as currently written
    
    kern_px,y,z: sigma of gaussian kernal/box size
    """
    found_nan=numpy.isnan(indata).sum()
    outdata=indata*1
    if found_nan: outdata=numpy.nan_to_num(outdata)
    if type == "g":
        outdata=ndimage.gaussian_filter(outdata, (kern_pz, kern_px, kern_py))
    elif type == "b":
        outdata=ndimage.uniform_filter(outdata, (kern_pz, kern_px, kern_py))
    #elif type == "m":
    #    outdata=ndimage.filters.median_filter(input=outdata, size=(kern_pz, kern_px, kern_py))
    else:
        sys.stderr.write("ERROR: Smoothing type not recognized.\n")
        sys.exit()
    if found_nan: outdata[numpy.isnan(indata)]=numpy.nan
    
    return outdata
Exemple #15
0
 def do_mask(self):
     self.mask = numpy.ones((self.height, self.width))
     for f in self.filters:
         self.mask = f.cutfilter(self.mask)
     
     self.mask = ndimage.uniform_filter(self.mask, 5)
     self.shifted_mask = fftshift(self.mask)
Exemple #16
0
def sampleImg2(inputImg,nn):
    w,h=inputImg.shape

    h_size=nn
    w_size=nn
    h_loc=random.randint(0, h-h_size)
    w_loc=random.randint(0, w-w_size)
    inputImage=inputImg[w_loc:w_loc+w_size,h_loc:h_loc+h_size]
    if configPara.if_aug == True:
        aug_type=random.randint(0, 12)
        if aug_type==1:
            inputImage=np.fliplr(inputImage);
        if aug_type==2:
            inputImage=np.flipud(inputImage);
        if aug_type==3:
            inputImage=np.rot90(inputImage,1);
        if aug_type==4:
            inputImage=np.rot90(inputImage,2);
        if aug_type==5:
            inputImage=np.rot90(inputImage,3);
        if aug_type==6:
            inputImage=np.flipud(np.rot90(inputImage,1));
        if aug_type==7:
            inputImage=np.flipud(np.rot90(inputImage,2));
        if aug_type==8:
            inputImage=np.flipud(np.rot90(inputImage,3));
        if aug_type==9:
            inputImage=np.fliplr(np.rot90(inputImage,1));
        if aug_type==10:
            inputImage=np.fliplr(np.rot90(inputImage,2));
        if aug_type==11:
            inputImage=np.fliplr(np.rot90(inputImage,3));

        #sigma = random.randint(1, 10)

        intensity_aug=random.randint(1, 5)
        if intensity_aug==2:
            inputImage = inputImage * np.sqrt(np.sqrt(np.abs(inputImage)+1e-12))
            inputImage = inputImage / np.max(inputImage)
        if intensity_aug==3:
            inputImage = inputImage / np.sqrt(np.sqrt(np.abs(inputImage)+1e-12))
            inputImage = inputImage / np.max(inputImage)


    targetImage = np.zeros(inputImage.shape)
    #if image_type==1:
    mean_value = np.mean(np.abs(inputImage))
    noiseImage = inputImage + np.random.normal(0, random.randint(1, 25) * 1e-1 * mean_value, inputImage.shape)
    temp = inputImage - ndimage.uniform_filter(inputImage, size=3)
    targetImage[np.where(np.abs(temp)>0.02)]=1
    #stre=ndimage.generate_binary_structure(1,1)
    #targetImage = ndimage.binary_dilation(targetImage)
    #else:
    #    noiseImage = np.random.normal(0, random.randint(1, 10) * 1e-2, inputImage.shape)
    #noiseImage = np.abs(noiseImage)
    noiseImage=np.expand_dims(np.expand_dims(noiseImage,axis=0),axis=3)
    targetImage=np.expand_dims(np.expand_dims(targetImage,axis=0),axis=3)

    return noiseImage,targetImage
Exemple #17
0
def extract_hog(img,ROI):
	#print("first")
	#cellRows = cellCols = 5
	#binCount = 4
#	BlockRowCells = 3
#	BlockColCells = 3
	orientations=8
	pixels_per_cell=(5, 5)#5,5 - 0.9844
	cells_per_block=(3, 3)#3,3
	img = resize(img,(50,50))
	image = rgb2gray(img)

	image = np.atleast_2d(image)
	#hist = hog(img,binCount,(cellCols,cellRows),(BlockRowCells,BlockColCells))
	#hist = np.divide(hog,np.linalg.norm(hog))
	gx = roll(image, 1, axis = 1) - roll(image, -1, axis = 1)
	gx[:,0],gx[:,-1] = 0,0;
	
	gy = roll(image, 1, axis = 0) - roll(image, -1, axis = 0)
	gy[-1,:],gy[0,:] = 0,0;
	matr = np.square(gx) + np.square(gy)
	matr = np.sqrt(matr)
	orientation = arctan2(gy, (gx + 1e-15)) * (180 / pi) + 90

	imx, imy = image.shape
	cx, cy = pixels_per_cell
	bx, by = cells_per_block

	n_cellsx = int(np.floor(imx // cx))  # number of cells in i
	n_cellsy = int(np.floor(imy // cy))  # number of cells in j

    
	or_hist = np.zeros((n_cellsx, n_cellsy, orientations))
	for i in range(orientations):
		
	
		condition = orientation < 180 / orientations * (i + 1)

		tmp = np.where(condition,orientation, 0)
		condition = orientation >= 180 / orientations * i
		tmp = np.where(condition,tmp, 0)
	
		cond2 = tmp > 0
		temp_mag = np.where(cond2, matr, 0)

		or_hist[:,:,i] = uniform_filter(temp_mag, size=(cx, cy))[cx/2::cx, cy/2::cy].T
	numbx = (n_cellsx - bx) + 1
	numby = (n_cellsy - by) + 1
	normb = np.zeros((numbx, numby, bx, by, orientations))

	for i in range(numbx):
		for j in range(numby):
			block = or_hist[i:i + bx, j:j + by, :]
			eps = 1e-5
			normb[i, j, :] = block / sqrt(block.sum() ** 2 + eps)

    
   
	return normb.ravel()
Exemple #18
0
def test_multiple_modes_uniform():
    # Test uniform filter for multiple extrapolation modes
    arr = np.array([[1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 0.0, 0.0]])

    expected = np.array([[0.32, 0.40, 0.48], [0.20, 0.28, 0.32], [0.28, 0.32, 0.40]])

    modes = ["reflect", "wrap"]

    assert_almost_equal(expected, sndi.uniform_filter(arr, 5, mode=modes))
Exemple #19
0
def apply(array, **kwargs):
    """
    Apply a set of standard filter to array data: 
    
    Call: apply(array-data, <list of key=value arguments>)

    The list of key-value define the filtering to be done and should be given in
    the order to be process. Possible key-value are:
    
      * smooth:  gaussian filtering, value is the sigma parameter (scalar or tuple)
      * uniform: uniform  filtering (2)
      * max:     maximum  filtering (1)
      * min:     minimum  filtering (1)
      * median:  median   filtering (1)
      
      * dilate: grey dilatation (1)
      * erode:  grey erosion    (1)
      * close:  grey closing    (1)
      * open:   grey opening    (1)
      
      * linear_map: call linear_map(), value is the tuple (min,max)   (3)
      * normalize:  call normalize(),  value is the method            (3)
      * adaptive:   call adaptive(),   value is the sigma             (3)
      * adaptive_:  call adaptive(),   with uniform kernel            (3)
          
    The filtering is done using standard scipy.ndimage functions.
    
    (1) The value given (to the key) is the width of the the filter: 
        the distance from the center pixel (the size of the filter is thus 2*value+1)
        The neighborhood is an (approximated) boolean circle (up to discretization)
    (2) Same as (*) but the neighborhood is a complete square
    (3) See doc of respective function
    """
    for key in kwargs:
        value = kwargs[key]
        if key not in ('smooth','uniform'):
            fp = _kernel.distance(array.ndim*(2*value+1,))<=value  # circular filter
            
        if   key=='smooth' : array = _nd.gaussian_filter(array, sigma=value)
        elif key=='uniform': array = _nd.uniform_filter( array, size=2*value+1)
        elif key=='max'    : array = _nd.maximum_filter( array, footprint=fp)
        elif key=='min'    : array = _nd.minimum_filter( array, footprint=fp)
        elif key=='median' : array = _nd.median_filter(  array, footprint=fp)

        elif key=='dilate' : array = _nd.grey_dilation(  array, footprint=fp)
        elif key=='erode'  : array = _nd.grey_erosion(   array, footprint=fp)
        elif key=='open'   : array = _nd.grey_opening(   array, footprint=fp)
        elif key=='close'  : array = _nd.grey_closing(   array, footprint=fp)
        
        elif key=='linear_map': array = linear_map(array, min=value[0], max=value[1])
        elif key=='normalize' : array = normalize( array, method = value)
        elif key=='adaptive'  : array = adaptive(  array, sigma  = value, kernel='gaussian')
        elif key=='adaptive_' : array = adaptive(  array, sigma  = value, kernel='uniform')
        else: 
            print '\033[031mUnrecognized filter :', key
            
    return array
Exemple #20
0
 def stretch_data(self,fits_data,pmin,pmax):
     #log_fits_data = np.log(fits_data-np.min(fits_data)+1,dtype="float32")
     # some statistics: min, max, mean
     print(np.min(fits_data),np.max(fits_data),np.mean(fits_data))
     #fits_data = median_filter(fits_data, 3)
     fits_data = uniform_filter(fits_data, 5)
     log_fits_data = np.arcsinh(fits_data-np.min(fits_data)+1.,dtype="float32")
     valuemin = np.percentile(log_fits_data,pmin)
     valuemax = np.percentile(log_fits_data,pmax)
     self.stretched_fits_data = log_fits_data.clip(valuemin,valuemax)
Exemple #21
0
def balanceimage(img, r, R):
    """Balance the brightness of an image by leveling.

    This is achieved here by applying a minimum filter over radius r
    and a uniform filter over radius R, and substracting the minimum
    of the two from the original image."""

    img_min = ndimage.minimum_filter(img, r)
    img_uni = ndimage.uniform_filter(img, R)
    return img - numpy.minimum(img_min, img_uni)
Exemple #22
0
def select_region_slices(sci_data, badpix_mask, box_size=256, num_boxes=10):
    """
    Find the optimal regions for calculating the noise autocorrelation (areas
    with the fewest objects/least signal)

    :param sci_data: Science image data array
    :param badpix_mask: Boolean array that is True for bad pixels
    :param box_size: Size of the (square) boxes to select
    :param num_boxes: Number of boxes to select. If insufficient good pixels can
        be found, will return as many boxes as possible
    :return: List of 2D slices, tuples of (slice_y, slice_x)
    """
    # TODO: For large images this is pretty slow, due to 3 full-size filters
    img_boxcar = ndimg.uniform_filter(sci_data, size=box_size)

    # Smooth over mask with min filter, to ignore small areas of bad pixels
    # One tenth of box size in each dimension means ignoring bad pixel regions
    # comprising <1% of total box pixels
    smooth_size = box_size // 10
    badpix_mask = ndimg.minimum_filter(badpix_mask, size=smooth_size,
                                       mode='constant', cval=False)
    # Expand zone of avoidance of bad pixels, so we don't pick boxes that
    # contain them. mode=constant, cval=True means treat all borders as
    # if they were masked-out pixels
    badpix_mask = ndimg.maximum_filter(badpix_mask, size=smooth_size + box_size,
                                       mode='constant', cval=True)
    img_boxcar = np.ma.array(img_boxcar, mask=badpix_mask)

    box_slices = []
    for box in range(num_boxes):
        # Find the location of the minimum value of the boxcar image, excluding
        # masked areas. This will be a pixel with few nearby sources within one
        # box width
        min_loc = img_boxcar.argmin()
        min_loc = np.unravel_index(min_loc, img_boxcar.shape)
        lower_left = tuple(int(x - box_size / 2) for x in min_loc)
        # Negative values of lower_left mean argmin ran out of unmasked pixels
        if lower_left[0] < 0 or lower_left[1] < 0:
            warn('Ran out of good pixels when placing RMS calculation regions '
                 'for file {}. Only {:d} boxes selected.'.format(sci_data, box))
            break
        min_slice = tuple(slice(x, x + box_size) for x in lower_left)
        box_slices += [min_slice]

        # Zone of avoidance (for center) is twice as big, since we are picking
        # box centers. Use clip to ensure avoidance slice stays within array
        # bounds
        lower_left = tuple(int(x - box_size) for x in min_loc)
        avoid_slice = tuple(slice(np.clip(x, 0, extent),
                                  np.clip(x + 2 * box_size, 0, extent))
                            for x, extent in zip(lower_left, img_boxcar.shape))

        # Add this box to the mask
        img_boxcar[avoid_slice] = np.ma.masked
    return box_slices
def neighbor_average(label, arr):
    """Helper function to produce focal average for cells

    Args:
      label (str): label to use for tuple to return
      arr (array): array to calculate focal average of

    Returns
      (str, array): label and new array produced
    """
    return (label, ndimage.uniform_filter(arr, size=1))
Exemple #24
0
def ex2salt():
    test = plt.imread("test.png")
    plot_img(test)
    test_noise = add_saltpepper_noise(test, 0.05)
    plot_img(test_noise)
    t1 = ndimage.uniform_filter(test_noise, size=3)
    plot_img(t1)
    t1 = ndimage.gaussian_filter(test_noise, sigma=1)
    plot_img(t1)
    t1 = ndimage.median_filter(test_noise, size=3)
    plot_img(t1)
Exemple #25
0
def hog_feature(im):
    """Compute Histogram of Gradient (HOG) feature for an image

         Modified from skimage.feature.hog
         http://pydoc.net/Python/scikits-image/0.4.2/skimage.feature.hog

       Reference:
         Histograms of Oriented Gradients for Human Detection
         Navneet Dalal and Bill Triggs, CVPR 2005

      Parameters:
        im : an input grayscale or rgb image

      Returns:
        feat: Histogram of Gradient (HOG) feature

    """

    # convert rgb to grayscale if needed
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)

    sx, sy = image.shape  # image size
    orientations = 9  # number of gradient bins
    cx, cy = (8, 8)  # pixels per cell

    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)
    gx[:, :-1] = np.diff(image, n=1, axis=1)  # compute gradient on x-direction
    gy[:-1, :] = np.diff(image, n=1, axis=0)  # compute gradient on y-direction
    grad_mag = np.sqrt(gx ** 2 + gy ** 2)  # gradient magnitude
    grad_ori = np.arctan2(gy, (gx + 1e-15)) * \
        (180 / np.pi) + 90  # gradient orientation

    n_cellsx = int(np.floor(sx / cx))  # number of cells in x
    n_cellsy = int(np.floor(sy / cy))  # number of cells in y
    # compute orientations integral images
    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))
    for i in range(orientations):
        # create new integral image for this orientation
        # isolate orientations in this range
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1),
                            grad_ori, 0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i,
                            temp_ori, 0)
        # select magnitudes for those orientations
        cond2 = temp_ori > 0
        temp_mag = np.where(cond2, grad_mag, 0)
        orientation_histogram[:, :, i] = uniform_filter(temp_mag, size=(cx, cy))[
            cx / 2::cx, cy / 2::cy].T

    return orientation_histogram.ravel()
Exemple #26
0
def fcnoise(flst):
    for fl in flst:
        hir = pyfits.open(fl.split('/')[0]+'/instrument_response_0001.fits')
        std = hir[0].data.flatten()
        w = np.where(std == 0)
        std[std == 0] = np.median(std[np.min(w)-10:np.min(w)-5])
        sstd = ndi.uniform_filter(ndi.median_filter(std,500,mode='nearest'),500)
        hc = pyfits.open(fl)
        hc[0].data = hc[0].data/sstd[:,np.newaxis,np.newaxis]
        hc.writeto(fl.replace('_ss.fits','_fc.fits'))
        hir.close()
        hc.close()
def getSaliencyMapNumpy(labImage, scales=3):
  (height, width, channels) = labImage.shape
  minimumDimension = min(width, height)

  # saliency map
  saliencyMap = np.zeros( shape=(height,width) )

  # calculate neighbourhood means for every scale and channel
  for s in range(0, scales):
    # TODO: make function for radius calculation parameter
    offset = np.round(minimumDimension/(2**(s+1))).astype(int)
    radius = offset*2+1
    # to correct the values near borders, see http://stackoverflow.com/questions/10683596/efficiently-calculating-boundary-adapted-neighbourhood-average
    filterMask = np.pad(np.ones((height,width)), offset, mode='constant', constant_values=0)
    filterFix = ndimage.uniform_filter(filterMask,radius,mode='constant',cval=0.0)
    filterFix = filterFix[offset:-offset, offset:-offset]
    for c in range(0, channels):
      # TODO: here R_1=1 is fixed (as in the original algorithm), make variable
      saliencyMap+=(labImage[:,:,c]-ndimage.uniform_filter(labImage[:,:,c],radius,mode='constant',cval=0.0)/filterFix)**2

  return saliencyMap
Exemple #28
0
def get_segments(feature_vectors, timestamps, kernel_width, peak_range, filter_width, save_image=False,
                 image_filename="similarity_matrix.png", subarray_size=None):

    sm = calculate_similarity_matrix(feature_vectors, subarray_size=subarray_size)

    sm = uniform_filter(sm, filter_width)

    peaks, convolution_values = checkerboard_matrix_filtering(sm, kernel_width, peak_range)

    if save_image:
        draw_similarity_matrix(sm, peaks, convolution_values, image_filename, timestamps)

    return calculate_segment_start_end_times_from_peak_positions(peaks, timestamps)
Exemple #29
0
    def filter_anomalies(self):
        """Find anomalous data and set to nan.

        You should run this either before filter_zeroes or after
        interpolate_nan, or you get lots more nans.

        After running this you should run interpolate_nan.
        """
        # TODO: write me!
        smoothed = ndi.uniform_filter(self.U, size=3)
        thresh = 0.05  # TODO: set more generally
        bad = np.abs(self.U - smoothed) > thresh
        self.U[bad] = np.nan
def rms_filter(image_data, filt_size=7):
    """
    Runs an 'RMS filter' on image data. Really this is the square root of the
    (appropriately scaled) difference of the squared uniform-filtered image and
    the uniform-filtered squared image. i.e. f*sqrt(boxcar(i**2) - boxcar(i)**2)

    Parameters
    ----------
    image_data : numpy 2-D array
        Image data to be filtered
    filt_size : integer
        Size of uniform filter kernel to use

    Returns
    -------
    filtered : numpy 2-D array
        RMS-filtered version of input image
    """
    medianSqr = uniform_filter(image_data ** 2, size=filt_size)
    sqrMedian = uniform_filter(image_data, size=filt_size) ** 2
    sqrDiffScale = filt_size ** 2 / (filt_size ** 2 - 1)
    return np.sqrt(np.abs(sqrDiffScale * (sqrMedian - medianSqr)))
Exemple #31
0
    def average_over_sliding_window(self,
                                    raw_data,
                                    width,
                                    hop,
                                    dtype='float32'):
        """
		Average groups of consecutive frames.
		"""
        frames = raw_data.shape[0]
        channels = raw_data.shape[1]
        win_hops = range(0, frames, hop)
        chunked_data = np.empty_like(raw_data)
        print "chunked: ", chunked_data.shape
        chunked_data[:] = raw_data
        print "chunked: ", chunked_data.shape
        print "rds: ", raw_data.shape
        ndimage.uniform_filter(raw_data, (width, channels),
                               output=chunked_data)

        print "WH: ", win_hops[-1]
        print "hop: ", hop
        chunked_data = chunked_data[win_hops, :]
        print chunked_data.shape
        return np.reshape(chunked_data, (-1, channels))
    def __call__(self, sample):
        """
        Args:
            img (PIL Image): Image to be flipped.

        Returns:
            PIL Image: Randomly flipped image.
        """

        if random.random() < self.p:
            new_sample = deepcopy(sample)
            new_sample['img'] = ndimage.uniform_filter(new_sample['img'], size=(4, 4, .4))
            #show_images(sample, new_sample)
            return new_sample
        return sample
def localRGBnormalDistributions(image, windowRadius=1, epsilon=1e-8):
    result = image.shape
    h = result[0]
    w = result[1]
    N = h * w
    windowSize = 2 * windowRadius + 1

    #meanImage = imboxfilt(image, windowSize)
    #meanImage = boxfilter(image, windowSize)
    meanImage = np.zeros(image.shape)
    meanImage[:, :, 0] = ndimage.uniform_filter(image[:, :, 0], windowSize)
    meanImage[:, :, 1] = ndimage.uniform_filter(image[:, :, 1], windowSize)
    meanImage[:, :, 2] = ndimage.uniform_filter(image[:, :, 2], windowSize)
    # temp_image=image*255
    # meanImage= Image.fromarray(temp_image.astype(np.uint8)).filter(ftr.Kernel((3,3),(1,1,1,1,1,1,1,1,1),9))
    # meanImage=np.array(meanImage)
    covarMat = np.zeros((3, 3, N))

    for r in range(0, 3):
        for c in range(r, 3):
            # temp_image = image[:,:,r]*image[:,:,c]*255
            # temp_image = Image.fromarray(temp_image.astype(np.uint8)).filter(ftr.Kernel((3, 3), (1, 1, 1, 1, 1, 1, 1, 1, 1), 1/9))
            # temp = np.array(temp_image)-meanImage[:,:,r]*meanImage[:,:,c]
            temp = ndimage.uniform_filter(
                image[:, :, r] * image[:, :, c],
                windowSize) - meanImage[:, :, r] * meanImage[:, :, c]
            #temp = imboxfilt(image(:,:, r).*image(:,:, c), windowSize) - meanImage(:,:, r).*meanImage(:,:, c)
            covarMat[r, c, :] = temp.T.flatten()

    for i in range(0, 3):
        covarMat[i, i, :] = covarMat[i, i, :] + epsilon

    for r in range(1, 3):
        for c in range(0, r):
            covarMat[r, c, :] = covarMat[c, r, :]
    return meanImage, covarMat
Exemple #34
0
    def _make_binarr(self):
        """
        Lightly process mstrace

        Returns:
            ndarray: The processed image

        """
        #  Only filter in the spectral dimension, not spatial!
        self.binarr = ndimage.uniform_filter(self.mstrace,
                                             size=(3, 1),
                                             mode='mirror')
        # Step
        self.steps.append(inspect.stack()[0][3])
        return self.binarr
    def __spectral_residual_saliency_map(self, image_src, filter_size, mode, sigma):
        # Spectral Residual
        image = io.imread(image_src)
        image = img_as_float(rgb2gray(image))
        fft = fftpack.fft2(image)
        logAmplitude = np.log(np.abs(fft))
        phase = np.angle(fft)
        avgLogAmp = uniform_filter(logAmplitude, size=filter_size, mode=mode) 
        spectralResidual = logAmplitude - avgLogAmp
        sm = np.abs(fftpack.ifft2(np.exp(spectralResidual + 1j * phase))) ** 2

        # After Effect
        saliencyMap = ndimage.gaussian_filter(sm, sigma=sigma)

        return saliencyMap
Exemple #36
0
def saliency(img):
    """Calculate saliency map of the image"""
    smaps = []
    for n in range(img.shape[2]):
        band = img[:, :, n]
        h, w = band.shape
        fft = np.fft.fft2(resize(band, (64, 64)))
        log_amplitude, phase = np.log(np.absolute(fft)), np.angle(fft)
        spectral_residual = log_amplitude - nd.uniform_filter(
            log_amplitude, size=3, mode='nearest')
        smap = np.absolute(
            np.fft.ifft2(np.exp(spectral_residual + 1.j * phase)))
        smap = nd.gaussian_filter(smap, sigma=3)
        smaps.append(normalize(resize(smap, (w, h))))
    return np.sum(np.dstack(smaps), axis=2)
Exemple #37
0
def getHistogram1(orientation,magnitude,n_cellsx,n_cellsy,cx=8,cy=8,orientations=18):
    orientation_histogram = np.zeros((n_cellsy, n_cellsx, orientations))
    subsample = np.index_exp[cy // 2:cy * n_cellsy:cy, cx // 2:cx * n_cellsx:cx]
    for i in range(orientations-1):
        temp_ori = np.where(orientation < 360 / orientations * (i + 1),
                            orientation, -1)
        temp_ori = np.where(orientation >= 360 / orientations * i,
                            temp_ori, -1)
        # select magnitudes for those orientations
        cond2 = (temp_ori > -1)
        temp_mag = np.where(cond2, magnitude, 0)

        temp_filt = uniform_filter(temp_mag, size=(cy, cx))
        orientation_histogram[:, :, int(i)] = temp_filt[subsample]
    return orientation_histogram
Exemple #38
0
def scale_labelmask(labelmask, scale):
    #assert (scaling % 1) == 0, "only integer scaling supported!"

    # rescale
    trans_labs = transform.rescale(labelmask + 1,
                                   scale=scale,
                                   preserve_range=True)

    trans_labs[(trans_labs % 1) > 0] = 1
    trans_labs = np.round(trans_labs) - 1
    #
    tmplabels = ndi.uniform_filter(trans_labs, size=2)
    trans_labs[trans_labs != tmplabels] = 0

    return trans_labs.astype(np.int)
Exemple #39
0
    def smooth(self, radius, kernel='gauss', **kwargs):
        """
        Smooth the image (works on a 2D image and returns a copy).

        The definition of the smoothing parameter radius is equivalent to the
        one that is used in ds9 (see `ds9 smoothing <http://ds9.si.edu/doc/ref/how.html#Smoothing>`_).

        Parameters
        ----------
        radius : `~astropy.units.Quantity` or float
            Smoothing width given as quantity or float. If a float is given it
            interpreted as smoothing width in pixels. If an (angular) quantity
            is given it converted to pixels using `geom.wcs.wcs.cdelt`.
        kernel : {'gauss', 'disk', 'box'}
            Kernel shape
        kwargs : dict
            Keyword arguments passed to `~scipy.ndimage.uniform_filter`
            ('box'), `~scipy.ndimage.gaussian_filter` ('gauss') or
            `~scipy.ndimage.convolve` ('disk').

        Returns
        -------
        image : `WcsNDMap`
            Smoothed image (a copy, the original object is unchanged).
        """
        from scipy.ndimage import gaussian_filter, uniform_filter, convolve

        if not self.geom.is_image:
            raise ValueError('Only supported on 2D maps')

        if isinstance(radius, Quantity):
            radius = (radius.to('deg') / self.geom.pixel_scales.mean()).value

        if kernel == 'gauss':
            width = radius / 2.
            data = gaussian_filter(self.data, width, **kwargs)
        elif kernel == 'disk':
            width = 2 * radius + 1
            disk = Tophat2DKernel(width)
            disk.normalize('integral')
            data = convolve(self.data, disk.array, **kwargs)
        elif kernel == 'box':
            width = 2 * radius + 1
            data = uniform_filter(self.data, width, **kwargs)
        else:
            raise ValueError('Invalid option kernel = {}'.format(kernel))

        return self._init_copy(data=data)
Exemple #40
0
def hog(image, orientations=9, pixels_per_cell=(9,9), cells_per_block=(2,2), normalise=True):
    if not isinstance(image, np.ndarray):
        image = misc.imread(image)
    image = np.atleast_2d(image)
    if image.ndim >= 3:
        image = np.mean(image, 2)
    if normalise: # gamma
        image = sqrt(image)
    if image.dtype.kind == 'u':
        image = image.astype('float')

    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)
    gx[:, 1:-1] = np.diff(image, n=2, axis=1)
    gy[1:-1, :] = np.diff(image, n=2, axis=0)

    magnitude = sqrt(gx**2 + gy**2)
    orientation = arctan2(gy, (gx + 1e-15)) * (180 / pi) % 180 

    sy, sx = image.shape
    cx, cy = pixels_per_cell
    bx, by = cells_per_block

    n_cellsx = int(np.floor(sx // cy))
    n_cellsy = int(np.floor(sy // cy))

    orientation_histogram = np.zeros((n_cellsy, n_cellsx, orientations))
    subsample = np.index_exp[int(cy/2):cy*n_cellsy:cy, int(cx/2):cx*n_cellsx:cx]
    for i in range(orientations):
        temp_ori = np.where(orientation < 180 / orientations * (i+1), orientation, -1)
        temp_ori = np.where(orientation >= 180 / orientations * i, temp_ori, -1)
        cond2 = temp_ori > -1
        temp_mag = np.where(cond2, magnitude, 0)

        temp_filt = uniform_filter(temp_mag, size=(cy, cx))
        orientation_histogram[:,:,i] = temp_filt[subsample]
    
    n_blocksx = (n_cellsx - bx) + 1
    n_blocksy = (n_cellsy - by) + 1
    normalised_blocks = np.zeros((n_blocksy, n_blocksx, by*bx*orientations))

    for x in range(n_blocksx):
        for y in range(n_blocksy):
            block = orientation_histogram[y:y+by, x:x+bx, :]
            eps = 1e-5
            block = block / sqrt(block.sum() ** 2 + eps)
            normalised_blocks[y, x, :] = block.ravel()
    return normalised_blocks
Exemple #41
0
def blur(data, repetitions=10, size=10):
    """ Blurs image using uniform filtering.
    Args:
        image_array: The image to be processed. A 2d Numpy array.
        repetitions: Defines how many time uniform filtering is applied
        size: The size of the uniform filter kernel.
    Returns:
        The filtered image.
    """
    image_array = data["image_array"]
    image_array = image_array.copy()

    for x in range(repetitions):
        image_array = uniform_filter(image_array, 10)
    data["image_array"] = image_array
    return data
def mean_filter(corrected_path, rolling_duration=10):
    corrected_files = natsorted(os.listdir(corrected_path))
    filtered_plane_list = []
    print 'Reading planes.'
    for plane in corrected_files:
        plane_path = os.path.join(corrected_path, plane)
        stack = io.imread(plane_path, plugin='tifffile')
        filtered_plane_list.append(
            nd.uniform_filter(stack, (1, 1, rolling_duration)))
    new_list = []
    print 'Filtering planes.'
    for plane in np.arange(filtered_plane_list[0].shape[0]):
        for array in filtered_plane_list:
            new_list.append(array[plane])
    recombined = np.stack(new_list, axis=0)
    return recombined
Exemple #43
0
def ublur_and_threshold(image, radius=2):
    """
    Blur an image with a Uniform blur kernel of supplied radius
    (the uniform filter better preserves edges/boundaries

    :param image: data
    :param radius: kernel radius

    :type image: :py:class:`numpy.ndarray`
    :type radius: float

    :return: modified image data
    :rtype: :py:class:`numpy.ndarray`
    """
    output = ndimage.uniform_filter(image, size=radius)
    return threshold(output)
Exemple #44
0
def square_filter_normalization(data, aggregate_type, win_size):
    '''
    '''
    # Filter locally, for staining variation
    if aggregate_type == M_MEDIAN:
        normalization_values = median_filter(data, (win_size, win_size))
    elif aggregate_type == M_MEAN:
        normalization_values = uniform_filter(data, (win_size, win_size))
    else:
        raise ValueError('Programming Error: Unknown window type supplied.')

    try:
        res = data / normalization_values
    except:
        logging.error("Division by zero, replace value with 0")
        res = 0
Exemple #45
0
def mask_hull(mask):
    """ find and return binary `mask` image hull, i.e. a list of pixels position """
    from scipy import spatial, sparse
    # find border pixels
    px = _np.transpose(
        (mask != _nd.uniform_filter(mask, size=(3, 3))).nonzero())

    # compute hull (sort indices using csgraph stuff)
    hull = spatial.Delaunay(px).convex_hull
    graf = sparse.csr_matrix((_np.ones(hull.shape[0]), hull.T),
                             shape=(hull.max() + 1, ) * 2)
    hull = sparse.csgraph.depth_first_order(graf, hull[0, 0],
                                            directed=False)[0]
    hull = px[hull]

    return hull
Exemple #46
0
def find_paws(data, smooth_radius=5, threshold=0.0001):
    # http://stackoverflow.com/questions/4087919/how-can-i-improve-my-paw-detection
    """Detects and isolates contiguous regions in the input array"""
    # Blur the input data a bit so the paws have a continous footprint
    data = ndimage.uniform_filter(data, smooth_radius)
    # Threshold the blurred data (this needs to be a bit > 0 due to the blur)
    thresh = data > threshold
    # Fill any interior holes in the paws to get cleaner regions...
    filled = ndimage.morphology.binary_fill_holes(thresh)
    # Label each contiguous paw
    coded_paws, num_paws = ndimage.label(filled)
    # Isolate the extent of each paw
    # find_objects returns a list of 2-tuples: (slice(...), slice(...))
    # which represents a rectangular box around the object
    data_slices = ndimage.find_objects(coded_paws)
    return data_slices
Exemple #47
0
def get_horiz_angle(lines):
    weighted_angles = [get_angle(line[0]) for line in lines]
    possible = np.zeros(90)
    for a, w in weighted_angles:
        possible[int(a)] += w
    possible = uniform_filter(possible, size=2)
    final_int_angle = np.argmax(possible)

    weight_sum, final_angle = 0, 0
    for a, w in weighted_angles:
        if angle_diff(final_int_angle, a) < 2:
            final_angle += (a if a < 45 else -(90 - a)) * w
            weight_sum += w
    final_angle = (final_angle / weight_sum) % 90
    # print(f"{final_int_angle} {final_angle}")
    return final_angle
Exemple #48
0
 def block(img):
     # FIXME: grid searchowac ten fragment?
     """ agressive
     from skimage.morphology import disk
     from skimage.filters import rank
     selem = disk(30)
     img = rank.equalize(img, selem=selem)
     img = exposure.equalize_adapthist(img)
     img = exposure.adjust_gamma(img)
     img = unsharp_mask(img, radius=5, amount=2)
     img = ndimage.uniform_filter(img, size=4)"""
     img = exposure.equalize_adapthist(img)
     img = exposure.adjust_gamma(img)
     img = unsharp_mask(img, radius=3, amount=2)
     img = ndimage.uniform_filter(img, size=2)
     return (img * 255).astype(np.uint8)
Exemple #49
0
    def resize(self, batches_array):
        """This function averages the values of pixel of 'batches_array' with the windows of
        size 'self.step' by the help of uniform_filter of scipy.
        for more information look at:
        https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.uniform_filter.html

        Next step of calculation is slicing in order to get rid of values that are belong to
        overlapping area in the filter result.
        """
        if self.step != 1:
            batches_array = uniform_filter(batches_array,
                                           size=(self.step, self.step),
                                           origin=(-(self.step // 2),
                                                   -(self.step // 2)))
            batches_array = super().resize(batches_array)
        return batches_array
Exemple #50
0
def adaptive_threshold(data, sigma=None, size=3, threshold=0):
    """
    Compute adaptive thresholding on data
        data >= mu(data) * (1 + threshold)
    
    Where mu is one of the fonctions (from scipy.ndimage):
        - gaussian_filter() with sigma as parameter    -  if sigma is not None (1st tested option)
        - uniform_filter()  with size  as parameter    -  otherwise 
        
    'threshold' can be view as a percentage of strengthening (for positve value) or
    relaxation (negative) of the inquality thresholding over the local average value (mu)
    """
    if sigma is not None: mu = _nd.gaussian_filter(data, sigma=sigma)
    else: mu = _nd.uniform_filter(data, size=size)

    return data >= mu * (1 + threshold)
Exemple #51
0
def hog_feature(im):
    """Compute Histogram of Gradient (HOG) feature for an image
         Modified from skimage.feature.hog
         http://pydoc.net/Python/scikits-image/0.4.2/skimage.feature.hog
       Reference:
         Histograms of Oriented Gradients for Human Detection
         Navneet Dalal and Bill Triggs, CVPR 2005
      Parameters:
        im : an input grayscale or rgb image
      Returns:
        feat: Histogram of Gradient (HOG) feature
    """

    # convert rgb to grayscale if needed
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)

    sx, sy = image.shape  # image size
    orientations = 9  # number of gradient bins
    cx, cy = (8, 8)  # pixels per cell

    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)
    gx[:, :-1] = np.diff(image, n=1, axis=1)  # compute gradient on x-direction
    gy[:-1, :] = np.diff(image, n=1, axis=0)  # compute gradient on y-direction
    grad_mag = np.sqrt(gx ** 2 + gy ** 2)  # gradient magnitude
    grad_ori = np.arctan2(gy, (gx + 1e-15)) * (180 / np.pi) + 90  # gradient orientation

    n_cellsx = int(np.floor(sx / cx))  # number of cells in x
    n_cellsy = int(np.floor(sy / cy))  # number of cells in y
    # compute orientations integral images
    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))
    for i in range(orientations):
        # create new integral image for this orientation
        # isolate orientations in this range
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1),
                            grad_ori, 0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i,
                            temp_ori, 0)
        # select magnitudes for those orientations
        cond2 = temp_ori > 0
        temp_mag = np.where(cond2, grad_mag, 0)
        orientation_histogram[:, :, i] = uniform_filter(temp_mag, size=(cx, cy))[cx // 2::cx, cy // 2::cy].T

    return orientation_histogram.ravel()
Exemple #52
0
    def smooth(self, width, kernel="gauss", **kwargs):
        """Smooth the map.

        Iterates over 2D image planes, processing one at a time.

        Parameters
        ----------
        width : `~astropy.units.Quantity`, str or float
            Smoothing width given as quantity or float. If a float is given it
            interpreted as smoothing width in pixels. If an (angular) quantity
            is given it converted to pixels using ``geom.wcs.wcs.cdelt``.
            It corresponds to the standard deviation in case of a Gaussian kernel,
            the radius in case of a disk kernel, and the side length in case
            of a box kernel.
        kernel : {'gauss', 'disk', 'box'}
            Kernel shape
        kwargs : dict
            Keyword arguments passed to `~ndi.uniform_filter`
            ('box'), `~ndi.gaussian_filter` ('gauss') or
            `~ndi.convolve` ('disk').

        Returns
        -------
        image : `WcsNDMap`
            Smoothed image (a copy, the original object is unchanged).
        """
        if isinstance(width, (u.Quantity, str)):
            width = u.Quantity(width) / self.geom.pixel_scales.mean()
            width = width.to_value("")

        smoothed_data = np.empty(self.data.shape, dtype=float)

        for img, idx in self.iter_by_image():
            img = img.astype(float)
            if kernel == "gauss":
                data = ndi.gaussian_filter(img, width, **kwargs)
            elif kernel == "disk":
                disk = Tophat2DKernel(width)
                disk.normalize("integral")
                data = ndi.convolve(img, disk.array, **kwargs)
            elif kernel == "box":
                data = ndi.uniform_filter(img, width, **kwargs)
            else:
                raise ValueError(f"Invalid kernel: {kernel!r}")
            smoothed_data[idx] = data

        return self._init_copy(data=smoothed_data)
Exemple #53
0
    def smooth(self, width, kernel="gauss", **kwargs):
        """
        Smooth the image (works on a 2D image and returns a copy).


        Parameters
        ----------
        width : `~astropy.units.Quantity` or float
            Smoothing width given as quantity or float. If a float is given it
            interpreted as smoothing width in pixels. If an (angular) quantity
            is given it converted to pixels using ``geom.wcs.wcs.cdelt``.
            It corresponds to the standard deviation in case of a Gaussian kernel,
            the radius in case of a disk kernel, and the side length in case
            of a box kernel.
        kernel : {'gauss', 'disk', 'box'}
            Kernel shape
        kwargs : dict
            Keyword arguments passed to `~scipy.ndimage.uniform_filter`
            ('box'), `~scipy.ndimage.gaussian_filter` ('gauss') or
            `~scipy.ndimage.convolve` ('disk').

        Returns
        -------
        image : `WcsNDMap`
            Smoothed image (a copy, the original object is unchanged).
        """
        from scipy.ndimage import gaussian_filter, uniform_filter, convolve

        if isinstance(width, u.Quantity):
            width = (width.to("deg") / self.geom.pixel_scales.mean()).value

        smoothed_data = np.empty_like(self.data)

        for img, idx in self.iter_by_image():
            if kernel == "gauss":
                data = gaussian_filter(img, width, **kwargs)
            elif kernel == "disk":
                disk = Tophat2DKernel(width)
                disk.normalize("integral")
                data = convolve(img, disk.array, **kwargs)
            elif kernel == "box":
                data = uniform_filter(img, width, **kwargs)
            else:
                raise ValueError("Invalid kernel: {!r}".format(kernel))
            smoothed_data[idx] = data

        return self._init_copy(data=smoothed_data)
def extract_hog(im):
    """
    Extract Histogram of Gradient (HOG) features for an image
    
    Inputs:
        im : an input grayscale or rgb image
    
    Returns:
        feat: Histogram of Gradient (HOG) feature
    """

    # convert rgb to grayscale
    image = rgb2gray(im)

    sx, sy = image.shape  # image size
    orientations = 9  # number of gradient bins
    cx, cy = (8, 8)  # pixels per cell

    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)
    gx[:, :-1] = np.diff(image, n=1, axis=1)  # compute gradient on x-direction
    gy[:-1, :] = np.diff(image, n=1, axis=0)  # compute gradient on y-direction
    grad_mag = np.sqrt(gx**2 + gy**2)  # gradient magnitude
    grad_ori = np.arctan2(
        gy, (gx + 1e-15)) * (180 / np.pi) + 90  # gradient orientation

    n_cellsx = int(np.floor(sx / cx))  # number of cells in x
    n_cellsy = int(np.floor(sy / cy))  # number of cells in y

    # compute orientations integral images
    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))
    for i in range(orientations):
        # create new integral image for this orientation
        # isolate orientations in this range
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1), grad_ori,
                            0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i, temp_ori, 0)
        # select magnitudes for those orientations
        cond2 = temp_ori > 0
        temp_mag = np.where(cond2, grad_mag, 0)
        orientation_histogram[:, :,
                              i] = uniform_filter(temp_mag,
                                                  size=(cx, cy))[cx / 2::cx,
                                                                 cy / 2::cy].T
    feat = orientation_histogram.ravel()
    dim = n_cellsx * n_cellsy * orientations
    return feat, dim
Exemple #55
0
 def to_figure(self, save_path, uniform_filter_size=11, loss_clip_max=10):
     pp = PdfPages(save_path)
     fig = plt.figure(figsize=(16, 9))
     losses = numpy.array(self.ctx.losses).clip(max=loss_clip_max)
     lrs = numpy.array(self.ctx.lrs)
     ax = fig.add_subplot(1, 1, 1)
     ax.plot(lrs, losses, color="#969696", label="Original Loss")
     uniform_losses = uniform_filter(losses, uniform_filter_size)
     plt.plot(lrs, uniform_losses, color="#0066FF", label="Gaussian Smoothing Loss")
     plt.title("Loss varying with learning rate from {:.6f} to {:.6f}.".format(self.ctx.lr_min, self.ctx.lr_max))
     plt.ylabel("loss")
     plt.xlabel("learning rate")
     plt.legend(loc=2)
     ax.set_xscale('log')
     pp.savefig(fig)
     pp.close()
     plt.close()
Exemple #56
0
def numb(skel_mat):
    """
    Counts the number of neighboring 1 for every nonzero
    element in 3D.
    
    Parameters
    ------
    skel_mat : 3d binary array
    
    Returns
    ------
    arr : The array of uint8 of the same size as skel_mat 
    with the elements equal to the number of neighbors 
    for the current point.
    
    Examples
    ------
    >>> a = np.random.random_integers(0,1,(3,3,3))
    >>> a 
    array([[[0, 0, 1],
            [0, 1, 1],
            [1, 0, 1]],        
           [[1, 0, 1],
            [1, 0, 1],
            [1, 1, 0]],        
           [[1, 1, 1],
            [1, 0, 0],
            [1, 1, 0]]])
    >>> neigh = numb(a)
    >>> neigh 
    array([[[ 0,  0,  4],
            [ 0, 10,  6],
            [ 4,  0,  4]],            
           [[ 5,  0,  6],
            [10,  0,  9],
            [ 7, 10,  0]],            
           [[ 4,  7,  3],
            [ 8,  0,  0],
            [ 5,  6,  0]]], dtype=uint8)
    """
    c_pad = util.pad(skel_mat, ((1, 1), (1, 1), (1, 1)), 'constant')
    mask = c_pad > 0
    fil = 3**3 * ndimage.uniform_filter(c_pad.astype('float'),
                                        size=(3, 3, 3)) - 1
    arr = (fil * mask)[1:-1, 1:-1, 1:-1].astype('uint8')
    return arr
def fit_gaussian_func(max_row, radiance_data, wavelen_val, spatial_pixel):
    """ Function to fit the Gaussian fit to the wavelength Vs. Amplitude Data
    """
    spatial_index = 'Spatial Index: ' + str(spatial_pixel)

    radiance_data = uniform_filter(radiance_data, size=2, mode='reflect')
    radiance_data = radiance_data[max_row - 12:max_row + 12]
    #radiance_data = moving_average(radiance_data)
    pixel_loc = np.arange(max_row - 12, max_row + 12)
    radiance_data = radiance_data / np.max(radiance_data)
    n = len(pixel_loc)
    #print(n)
    mean_val = sum(radiance_data * pixel_loc) / n
    #print(mean_val)
    sigma_val = np.sqrt(sum(radiance_data * (pixel_loc - mean_val)**2) / n)

    #    center = max_row
    #    amp = 1
    #    init_vals = [amp, center, width]
    popt, pcov = curve_fit(gauss_function,
                           pixel_loc,
                           radiance_data,
                           p0=[1, mean_val, sigma_val])
    sampled_pixel_loc = np.arange(min(pixel_loc), max(pixel_loc), 0.01)
    fitted_data = gauss_function(sampled_pixel_loc, *popt)
    loc_gauss = sampled_pixel_loc[np.where(fitted_data == max(fitted_data))]
    loc_data = pixel_loc[np.where(radiance_data == max(radiance_data))]
    popt = None

    FWHM = full_width_half_max(sampled_pixel_loc, fitted_data)

    #fitted_data = fitted_data/np.max(fitted_data)
    #full_width_half_max(wavelength, radiance_data)
    #    plt.plot(sampled_pixel_loc, fitted_data, 'g.-', markersize= 1, label=' Gaussian fit')
    #    plt.plot(pixel_loc, radiance_data, 'ro--', label = 'Original Data')
    #    plt.grid(True, linestyle=':')
    #    plt.xlabel('Spectral Pixel Index')
    #    plt.ylabel('Normalized Image Counts')
    #    plt.text(max_row+4,0.5, 'Smile = ' +str(round((loc_gauss- loc_data)[0], 3)) + ' pixel', bbox={'facecolor':'yellow', 'alpha':0.6, 'pad':4})
    #    plt.title('Signal Vs Spectral Index (' + str(wavelen_val) +'nm' +', '+ spatial_index +')')
    #    plt.legend(loc='best')

    #plt.ylim(0, 1)
    #plt.yscale('log')
    #plt.show()
    return loc_gauss, FWHM
Exemple #58
0
def image_transformation(X, method_type='blur', **kwargs):
    # https://www.kaggle.com/tomahim/image-manipulation-augmentation-with-skimage
    q = kwargs['percentile'] if 'percentile' in kwargs else (0.2, 99.8)
    angle = kwargs['angle'] if 'angle' in kwargs else 60
    transformation_dict = {
        'blur': normalize(ndimage.uniform_filter(X)),
        'invert': normalize(util.invert(X)),
        'rotate': rotate(X, angle=angle),
        'rescale_intensity': _rescale_intensity(X, q=q),
        'gamma_correction': exposure.adjust_gamma(X, gamma=0.4, gain=0.9),
        'log_correction': exposure.adjust_log(X),
        'sigmoid_correction': exposure.adjust_sigmoid(X),
        'horizontal_flip': X[:, ::-1],
        'vertical_flip': X[::-1, :],
        'rgb2gray': skimage.color.rgb2gray(X)
    }
    return transformation_dict[method_type]
 def on_validation_end(self, logs, outputs, gt):
     losses = to_numpy(self.loss_fn(from_numpy(outputs), from_numpy(gt)))
     losses = losses.mean(axis=0)
     losses = uniform_filter(losses, size=6, mode='nearest')
     x, y = np.meshgrid(np.arange(losses.shape[0]),
                        np.arange(losses.shape[1]))
     fig = plt.figure()
     ax = fig.gca(projection='3d')
     ax.plot_surface(x,
                     y,
                     losses,
                     linewidth=0,
                     antialiased=True,
                     cmap=plt.get_cmap('viridis'),
                     edgecolor='none')
     ax.view_init(60, 35)
     self.image_logger(fig)
def convertSpec(spec):
    #A rough approximation of what Isaac is doing
    #Just takes the envoronment map, removes black pixels and blurs it
    for i in range(spec.shape[0]):
        for j in range(spec.shape[1]):
            u = i / 127.5 - 1  #convert to [-1,1]
            v = j / 127.5 - 1
            r = np.sqrt(u**2 + v**2)
            theta = np.arctan2(u, v)
            if r > 1:
                u = (np.sin(theta) + 1) * 127.5
                v = (np.cos(theta) + 1) * 127.5
                spec[i, j] = spec[int(u), int(v)]
    spec = ndimage.uniform_filter(spec, size=(50, 50, 1))
    # plt.imshow(spec)
    # plt.show()
    return spec