コード例 #1
2
ファイル: detect_features.py プロジェクト: sapphireJYT/cv
def detect_features(image):
    """
    Computer Vision 600.461/661 Assignment 2
    Args:
    	image (numpy.ndarray): The input image to detect features on. Note: this is NOT the image name or image path.
    Returns:
    	pixel_coords (list of tuples): A list of (row,col) tuples of detected feature locations in the image
    """  
    
    pixel_coords = list()
  
    # Compute Gaussian derivatives at each pixel
    (m, n) = image.shape
    Ix = np.zeros((m, n))
    Iy = np.zeros((m, n))
    sigma = 3
    filters.gaussian_filter(image, (sigma, sigma), (0, 1), Ix)
    filters.gaussian_filter(image, (sigma, sigma), (1, 0), Iy)
    
    A = Ix * Ix
    B = Ix * Iy
    C = Iy * Iy
    
    R = np.zeros((m, n))
    r = 3 # Gaussian window radius
    k = 0.05

    # Compute corner response function R
    for j in xrange(r, n-r-1):
        for i in xrange(r, m-r-1):
            # Make a Gaussian window around each pixel
            a = np.sum(A[i-r:i+r+1, j-r:j+r+1])
            b = np.sum(B[i-r:i+r+1, j-r:j+r+1])
            c = np.sum(C[i-r:i+r+1, j-r:j+r+1])

            # Compute second moment matrix H in a 
            # H = [[a, b],[b, c]]

            # Compute eigenvalues of H and calculate response
            # [l1, l2] = np.linalg.eigvals(H)
            # R[i][j] = l1 * l2 - k * (l1 + l2)**2
    
            # Alternately, R = det(H) - k * trace(H)^2 (faster)
            R[i][j] = a * c - b * b  - k * (a + c)**2

    # Normalize R map 
    min_R = np.min(R)
    R = (R - min_R) * 10.0 / (np.max(R) - min_R)

    # Print R map
    # plt.axis('off')
    # plt.imshow(R)
    # plt.show()
    
    # Find local maxima of response function
    radius = 5
    thres = np.mean(R) * 1.2
    pixel_coords = nonmaxsuppts(R, radius, thres)

    return pixel_coords
コード例 #2
0
ファイル: pageseg.py プロジェクト: mittagessen/kraken
def compute_gradmaps(binary, scale, gauss=False):
    """
    Use gradient filtering to find baselines

    Args:
        binary (numpy.array):
        scale (float):
        gauss (bool): Use gaussian instead of uniform filtering

    Returns:
        (bottom, top, boxmap)
    """
    # use gradient filtering to find baselines
    logger.debug('Computing gradient maps')
    boxmap = compute_boxmap(binary, scale)
    cleaned = boxmap*binary
    if gauss:
        grad = gaussian_filter(1.0*cleaned, (0.3*scale, 6*scale), order=(1, 0))
    else:
        grad = gaussian_filter(1.0*cleaned, (max(4, 0.3*scale),
                                             scale), order=(1, 0))
        grad = uniform_filter(grad, (1, 6*scale))
    bottom = norm_max((grad < 0)*(-grad))
    top = norm_max((grad > 0)*grad)
    return bottom, top, boxmap
コード例 #3
0
ファイル: words.py プロジェクト: caoym/odr
def separate_words_from_image(src):
    '''
    从单行文本图片中切割出每一个字
    :param src:
    :return:
    '''
    '''figure()
    subplot(211)
    imshow(src)
    subplot(212)
    imshow(tmp)
    show()'''
    orisum = src.sum(axis=0)/255.0
    sum = filters.gaussian_filter(orisum,8)
    sep = get_separaters_from_image(sum,filters.gaussian_filter(orisum,2))

    words = []
    pos = 0
    h = len(src)
    for i in sep:
        #words += [[i[0],0,i[1]-i[0],h]]
        word = src[:,i[0]:i[1]]
        x,y,w,h = bounding_box(word)
        words.append([i[0]+x,y,w,h])
        #if word is not None:
       #     words += [word]
    return words
コード例 #4
0
ファイル: binarization.py プロジェクト: tianyaqu/kraken
def nlbin(im, threshold=0.5, zoom=0.5, escale=1.0, border=0.1, perc=80,
          range=20, low=5, high=90):
    """
    Performs binarization using non-linear processing.

    Args:
        im (PIL.Image):
        threshold (float):
        zoom (float): Zoom for background page estimation
        escale (float): Scale for estimating a mask over the text region
        border (float): Ignore this much of the border
        perc (int): Percentage for filters
        range (int): Range for filters
        low (int): Percentile for black estimation
        high (int): Percentile for white estimation

    Returns:
        PIL.Image containing the binarized image
    """
    if im.mode == '1':
        return im
    raw = pil2array(im)
    # rescale image to between -1 or 0 and 1
    raw = raw/np.float(np.iinfo(raw.dtype).max)
    if raw.ndim == 3:
        raw = np.mean(raw, 2)
    # perform image normalization
    if np.amax(raw) == np.amin(raw):
        raise KrakenInputException('Image is empty')
    image = raw-np.amin(raw)
    image /= np.amax(image)

    m = interpolation.zoom(image, zoom)
    m = filters.percentile_filter(m, perc, size=(range, 2))
    m = filters.percentile_filter(m, perc, size=(2, range))
    m = interpolation.zoom(m, 1.0/zoom)
    w, h = np.minimum(np.array(image.shape), np.array(m.shape))
    flat = np.clip(image[:w, :h]-m[:w, :h]+1, 0, 1)

    # estimate low and high thresholds
    d0, d1 = flat.shape
    o0, o1 = int(border*d0), int(border*d1)
    est = flat[o0:d0-o0, o1:d1-o1]
    # by default, we use only regions that contain
    # significant variance; this makes the percentile
    # based low and high estimates more reliable
    v = est-filters.gaussian_filter(est, escale*20.0)
    v = filters.gaussian_filter(v**2, escale*20.0)**0.5
    v = (v > 0.3*np.amax(v))
    v = morphology.binary_dilation(v, structure=np.ones((escale*50, 1)))
    v = morphology.binary_dilation(v, structure=np.ones((1, escale*50)))
    est = est[v]
    lo = np.percentile(est.ravel(), low)
    hi = np.percentile(est.ravel(), high)

    flat -= lo
    flat /= (hi-lo)
    flat = np.clip(flat, 0, 1)
    bin = np.array(255*(flat > threshold), 'B')
    return array2pil(bin)
コード例 #5
0
ファイル: clouds.py プロジェクト: vbonvin/POUET
 def get_observability_map(self, x, y, threshold=40, filter_sigma=3, max_pxval=180):
     """
     Returns an observability map (an image of the sky)
     
     :param x: x coordinates of detected stars
     :param y: y coordinates of detected stars
     :param threshold: distance threshold in px of stars such that we have observations
     :param filter_sigma: sigma of the Gaussian kernel, default=2 
     :param max_pxval: px value above which the visibility is considered to be 0, default=170
     
     :return: an observability map with the same dimension as the input image.
     """
     logger.debug("Creating an observability map...")
     observability = copy.copy(self.im_masked) * 0.
     
     if len(x) > 0:
         notnans = np.where(np.isnan(self.im_masked) == False)
         notnans = list(zip(notnans[0], notnans[1]))
         pts = np.array([x,y]).T
         tree = cKDTree(pts)    
         for nx, ny in notnans:
             obs = len(tree.query_ball_point((ny,nx), threshold))
             if obs > 2 : observability[nx,ny] = 1. 
             elif obs >= 1 : observability[nx,ny] = 0.5
         observability[filters.gaussian_filter(np.nan_to_num(self.im_masked), 10) > max_pxval] = 0
         observability = filters.gaussian_filter(observability, filter_sigma)
     
     self.observability_map = observability.T
     return observability
コード例 #6
0
ファイル: theanoCortex04.py プロジェクト: jypuigbo/TheanoNNs
def gkern2(kern_shape, sigma, show_image=False):
    """Returns a 2D Gaussian kernel array. 
    size is kernlen, gaussian is centered 
    in the middle and std is 'nsig' units"""

    # create nxn zeros
    inp = np.zeros((kern_shape[0], kern_shape[0]))
    # set element at the middle to one, a dirac delta
    inp[kern_shape[0]//2, kern_shape[1]//2] = 1
    # gaussian-smooth the dirac, resulting in a gaussian filter mask
    aux =  fi.gaussian_filter(inp, sigma[0]/2)
    aux*=aux
    aux = aux/np.max(aux)
    aux1 = aux[:,kern_shape[0]//2]
    aux =  fi.gaussian_filter(inp, sigma[1]/2)
    aux = aux/np.max(aux)
    aux2 = aux[kern_shape[0]//2,:]
    #print np.max(aux)
    aux = sg.convolve(aux1.reshape(1,kern_shape[0]),aux2.reshape(kern_shape[0],1))
    
    #aux[aux<0.1]=0
    print aux.shape
    print "Remember to always check how is the kernel!!!"
    if show_image:
        plt.imshow(aux)
        plt.show()
    return aux
コード例 #7
0
ファイル: a5.py プロジェクト: makifcetinkaya/6.815
def toneMap(im, targetBase=100, detailAmp=3, useBila=False, maxLum = .99):
    lum, chrom = lumiChromi(im)
    h,w = im.shape[0:2]
    min = numpy.min(lum[lum>0])
    assert min>0 and targetBase>0
    
    lum[lum<=0] = min
    lum_log = numpy.log10(lum)
    sigmaS = getSigma(im)
    
    if useBila:
        sigmaR = .4
        bila = bilaGrid(lum_log[:,:,0], sigmaS, sigmaR)
        base = bila.doit(lum_log)
    else:
        base = numpy.zeros((h,w,3))
        gaussian_filter(lum_log, [sigmaS, sigmaS, 0], output=base)
        
    detail = lum_log - base  
    large_range = numpy.max(base)-numpy.min(base)
    
    scaled_base = (log10(targetBase)/large_range)*(base - numpy.max(base))
    
    log_lum = detail*detailAmp + scaled_base
    
    lum = numpy.power(log_lum, 10)
    return lum*chrom
コード例 #8
0
    def set_filters(self):

        # create filters
        self.filters_conv = self.get_random_filtersconv(self.n_filters)
        # self.fbstochastic = self.get_stochastic_filters(self.n_filters)

        self.filters_norm = []
        self.filters_pooling = []

        aux = np.zeros(self.shape_norm[0])
        aux[(aux.shape[0] - 1) / 2, (aux.shape[1] - 1) / 2] = 1
        gf = gaussian_filter(aux, sigma=1).astype(np.float32)

        # gf.shape = gf.shape+(1,1)
        # self.filters_norm.append(gf)
        for n_layer in range(len(self.n_filters)):
            aux = np.zeros(self.shape_norm[n_layer])
            aux[(aux.shape[0] - 1) / 2, (aux.shape[1] - 1) / 2] = 1
            gf = gaussian_filter(aux, sigma=1).astype(np.float32)
            gf.shape = gf.shape + (1,)
            gf = np.repeat(gf, self.n_filters[n_layer], axis=2)
            # gf = np.repeat(gf,self.n_filters[n_layer],axis=3)
            self.filters_norm.append(gf)
            self.filters_pooling.append(np.ones(self.shape_pool[n_layer], dtype=np.float32))

        self.n_layers = len(self.n_filters)
コード例 #9
0
	def our_track_points(self):
		if self.features != []:
			self.step() #move to the next frame - surprising too eh!

		#load the images and create grayscale
		self.image = cv2.imread(self.imnames[self.current_frame])
		self.gray = cv2.cvtColor(self.image,cv2.COLOR_BGR2GRAY)

		#reshape to fit input format
		tmp = float32(self.features).reshape(-1, 1, 2)
		tmpf = []

		tmpf=[]
		ims1 = filters.gaussian_filter(self.prev_gray,self.sigma)
		ims2 = filters.gaussian_filter(self.gray,self.sigma)
		for elem in tmp:
			inner = []
			inner = self.lk(ims1,ims2,elem[0][0],elem[0][1],winsize)
			tmpf.append(inner)

		tmp = array(tmp).reshape((-1,2))
		for i,f in enumerate(tmp):
			self.tracks[i].append(f)
		
		for i in range(len(tmpf)):
			self.features[i][0] = tmp[i][0]+tmpf[i][0]
			self.features[i][1] = tmp[i][1]+tmpf[i][1]

		self.prev_gray = self.gray
コード例 #10
0
    def propagateBelief(self):
        delta_t = rospy.get_time() - self.t_last_update

        d_t = self.d + self.v_current*delta_t*np.sin(self.phi)
        phi_t = self.phi + self.w_current*delta_t

        p_beliefRV = np.zeros(self.beliefRV.shape)

        for i in range(self.beliefRV.shape[0]):
            for j in range(self.beliefRV.shape[1]):
                if self.beliefRV[i,j] > 0:
                    if d_t[i,j] > self.d_max or d_t[i,j] < self.d_min or phi_t[i,j] < self.phi_min or phi_t[i,j] > self.phi_max:
                        continue
                    i_new = floor((d_t[i,j] - self.d_min)/self.delta_d)
                    j_new = floor((phi_t[i,j] - self.phi_min)/self.delta_phi)
                    p_beliefRV[i_new,j_new] += self.beliefRV[i,j]

        s_beliefRV = np.zeros(self.beliefRV.shape)
        gaussian_filter(100*p_beliefRV, self.cov_mask, output=s_beliefRV, mode='constant')

        if np.sum(s_beliefRV) == 0:
            return
        self.beliefRV = s_beliefRV/np.sum(s_beliefRV)

    	#bridge = CvBridge()
        #prop_img = bridge.cv2_to_imgmsg((255*self.beliefRV).astype('uint8'), "mono8")
        #self.pub_prop_img.publish(prop_img)
                
        return
コード例 #11
0
ファイル: volumes.py プロジェクト: mdschirmer/pyniftitools
def gaussian_blur(infile, outfile, sigma):
    from scipy.ndimage.filters import gaussian_filter
    if type(sigma) is str:
        sigma = ast.literal_eval(sigma)
    in_nii = nib.load(infile)

    in_arr = in_nii.get_data()
    out_arr = np.zeros_like(in_arr)
    if len(in_arr.shape) == 5:
        assert in_arr.shape[3] == 1
        assert in_arr.shape[4] == 3
        # Warp: blur x,y,z separately
        for i in xrange(3):
            gaussian_filter(
                    in_arr[:,:,:,0,i],
                    sigma=sigma,
                    output=out_arr[:,:,:,0,i])
    elif len(in_arr.shape) == 3:
            gaussian_filter(
                    in_arr[:,:,:],
                    sigma=sigma,
                    output=out_arr[:,:,:])

    out_nii = nib.Nifti1Image(out_arr, header=in_nii.get_header(), affine=in_nii.get_affine())
    out_nii.to_filename(outfile)
コード例 #12
0
ファイル: pageseg.py プロジェクト: tianyaqu/kraken
def compute_colseps_conv(binary, scale=1.0, minheight=10, maxcolseps=2):
    """Find column separators by convolution and thresholding.

    Args:
        binary (numpy.array):
        scale (float):
        minheight (int):
        maxcolseps (int):

    Returns:
        Separators
    """

    h, w = binary.shape
    # find vertical whitespace by thresholding
    smoothed = gaussian_filter(1.0*binary, (scale, scale*0.5))
    smoothed = uniform_filter(smoothed, (5.0*scale, 1))
    thresh = (smoothed < np.amax(smoothed)*0.1)
    # find column edges by filtering
    grad = gaussian_filter(1.0*binary, (scale, scale*0.5), order=(0, 1))
    grad = uniform_filter(grad, (10.0*scale, 1))
    grad = (grad > 0.5*np.amax(grad))
    # combine edges and whitespace
    seps = np.minimum(thresh, maximum_filter(grad, (int(scale), int(5*scale))))
    seps = maximum_filter(seps, (int(2*scale), 1))
    # select only the biggest column separators
    seps = morph.select_regions(seps, sl.dim0, min=minheight*scale,
                                nbest=maxcolseps+1)
    return seps
コード例 #13
0
def ingaramo_deconvolve(n_iterations=100):
    pass

    print "Deconvolving noisy object..."

    for i in range(n_iterations):
        print " Iteration", i

        for t in range(num_timepoints):
            #
            # Take the estimated true image and apply the PSF
            #
            blurred_estimate[t, :, :] = gaussian_filter(signal_range[t] * estimate, sigma=sigma_range[t])
        #
        # Compute correction factor
        #
        correction_factor = np.divide(measurement, blurred_estimate)

        print " Blurring correction ratio..."
        for t in range(num_timepoints):
            #
            # Update the correction factor
            #
            correction_factor[t, :, :] = gaussian_filter(signal_range[t] * correction_factor[t, :, :], sigma=sigma_range[t])

        estimate = np.multiply(estimate, correction_factor.mean(axis=0))

        # Save the evolution of the estimate
        estimate_history[i+1, :, :] = estimate


    print "Done deconvolving"
コード例 #14
0
def detect_features(image):
	"""
	Computer Vision 600.461/661 Assignment 2
	Args:
		image (numpy.ndarray): The input image to detect features on. Note: this is NOT the image name or image path.
	Returns:
		pixel_coords (list of tuples): A list of (row,col) tuples of detected feature locations in the image
	"""

	harris_threshold = 0.5

	sobel_vertical_kernel = [[-1, 0, 1],
                         [-2, 0, 2],
                         [-1, 0, 1]
                        ]

	sobel_horizontal_kernel = np.rot90(sobel_vertical_kernel)

	I_x = convolve2d(image, sobel_vertical_kernel, mode='same', boundary='symm')
	I_y = convolve2d(image, sobel_horizontal_kernel, mode='same', boundary='symm')

	I_xx = I_x * I_x
	I_yy = I_y * I_y
	I_xy = I_x * I_y

	I_xx = gaussian_filter(I_xx, 3)
	I_yy = gaussian_filter(I_yy, 3)
	I_xy = gaussian_filter(I_xy, 3)

	R = (I_xx * I_yy - I_xy**2) - K*(I_xx + I_yy)**2

	corners = nonmaxsuppts(R, 5, harris_threshold)

	return corners
コード例 #15
0
ファイル: Segmentation.py プロジェクト: szbokhar/2014USRA-nhm
    def loadImage(self, fname):
        self._imageFilename = fname
        self.templateBox((0,0,0,0))
        self._boxes = []
        self._currentBox = None
        logging.info("Load image %s" % fname)

        self._image = array(Image.open(fname).convert('L'), dtype='f')/255
        (h,w) = shape(self._image)
        self._imageSize = (w,h)
        im1 = filters.gaussian_filter(self._image, 2)
        im2 = filters.gaussian_filter(self._image, 3)
        im4 = filters.gaussian_filter(self._image, 4)
        im8 = filters.gaussian_filter(self._image, 5)

        features = []
        features.append(im1)
        features.append(im2)
        features.append(im4)
        features.append(im8)
        features.append(filters.gaussian_laplace(self._image, 2))
        features.append(filters.gaussian_laplace(self._image, 3))
        features.append(filters.gaussian_laplace(self._image, 4))
        features.append(filters.gaussian_laplace(self._image, 5))
        features.append(filters.sobel(im4, 0))
        features.append(filters.sobel(im8, 0))
        features.append(filters.sobel(im4, 1))
        features.append(filters.sobel(im8, 1))
        self._features = dstack(features)
コード例 #16
0
def elastic_distortion(ex, size=28, alpha=8, sigma=3):

	Xdis = np.random.uniform(-1,1,(size*size)).reshape((size,size))
	Ydis = np.random.uniform(-1,1,(size*size)).reshape((size,size))

	Xdis = gaussian_filter(Xdis, sigma=sigma, mode="constant")
	Ydis = gaussian_filter(Ydis, sigma=sigma, mode="constant")
	Xdis = (Xdis/ np.linalg.norm(Xdis)) * alpha
	Ydis = (Ydis/ np.linalg.norm(Ydis)) * alpha
	ex_new = np.zeros((size,size))
	k=0
	for i in range(size):
		for j in range(size):
			s=0
			xp, scale_x = divmod(Xdis[i, j] + i, 1)
			yp, scale_y = divmod(Ydis[i, j] +j,1)
			xp, yp = int(xp), int(yp)
			try:
				ex_yp_xp=ex[yp,xp] 
			except Exception, e:
				ex_yp_xp = 0
				s += 1
			try:
				ex_yp_xp1 = ex[yp, xp+ 1]
			except Exception, e:
				ex_yp_xp1 = 0
				s += 1
			try:
			 	ex_yp1_xp1 = ex[yp+1, xp+ 1]
			except Exception, e:
				ex_yp1_xp1 = 0
				s += 1
コード例 #17
0
def chi_profs(bulge_prof, disk_prof, mask, weight, resid, hrad, 
              smoothed = False, smooth_scale = 2.0):
    galprof = bulge_prof + disk_prof
    if smoothed:
        resid = filters.gaussian_filter(resid**2, smooth_scale)
        weight = filters.gaussian_filter(weight**2, smooth_scale)
    else:
        resid = resid**2
        weight = weight**2

        
    new_mask = np.where(galprof > np.max(galprof)/100.0, 1,0)    
    galprof = image_info(galprof, mask=new_mask)
    profile = image_info(resid/weight, mask = mask, x_ctr = galprof.x_ctr, y_ctr = galprof.y_ctr, ell = galprof.ba, pa= galprof.pa, zoom =-1 )
    profile.profile()

    rads = np.array(profile.rads)*0.396/hrad
    profs = np.array(profile.prof)
    cum_profs = np.array(profile.aperflux)/np.array(profile.included_pix)
    
    cum_profs = np.extract(rads>0, cum_profs)
    profs = np.extract(rads>0, profs)
    rads = np.extract(rads>0, rads)

    x = np.arange(0.1, 4.0,0.1)
    s = interp.InterpolatedUnivariateSpline(rads,cum_profs)
    ynew = s(x)
    
    return x, ynew 
コード例 #18
0
ファイル: stereo.py プロジェクト: PhilomontPhlea/PhleaBytesCV
def plane_sweep_gauss(im_l, im_r, start, steps, wid):
  '''Find disparity image using normalized cross-correlation with Gaussian
  weighted neighborhoods.'''

  m, n = im_l.shape  # Must match im_r.shape.

  mean_l = numpy.zeros(im_l.shape)
  mean_r = numpy.zeros(im_l.shape)
  s = numpy.zeros(im_l.shape)
  s_l = numpy.zeros(im_l.shape)
  s_r = numpy.zeros(im_l.shape)

  dmaps = numpy.zeros((m, n, steps))

  filters.gaussian_filter(im_l, wid, 0, mean_l)
  filters.gaussian_filter(im_r, wid, 0, mean_r)

  norm_l = im_l - mean_l
  norm_r = im_r - mean_r

  for disp in range(steps):
    filters.gaussian_filter(numpy.roll(norm_l, -disp - start) *
                            norm_r, wid, 0, s)
    filters.gaussian_filter(numpy.roll(norm_l, -disp - start) *
                            numpy.roll(norm_l, -disp - start), wid, 0, s_l)
    filters.gaussian_filter(norm_r * norm_r, wid, 0, s_r)

    dmaps[:, :, disp] = s / numpy.sqrt(s_l * s_r)

  return numpy.argmax(dmaps, axis=2)
コード例 #19
0
ファイル: scalar.py プロジェクト: Ryo-Ito/image_registration
    def change_resolution(self, resolution, sigma, order=1):
        """
        change image's resolution

        Parameters
        ----------
        resolution : int
            how much to magnify
            if resolution is 2, the shape of the image will be halved
        sigma : float
            standard deviation of gaussian filter for smoothing
        order : int
            order of interpolation

        Returns
        -------
        img : ScalarImage
            zoomed scalar image
        """
        if resolution != 1:
            blurred_data = gaussian_filter(self.data, sigma)
            ratio = [1 / float(resolution)] * self.ndim
            data = zoom(blurred_data, ratio, order=order)
        elif resolution == 1:
            data = gaussian_filter(self.data, sigma)
        img = ScalarImage(data=data)
        return img
コード例 #20
0
def elastic_transform(image, alpha, sigma, alpha_affine, random_state=None):
    """Elastic deformation of images as described in [Simard2003]_ (with modifications).
    .. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
         Convolutional Neural Networks applied to Visual Document Analysis", in
         Proc. of the International Conference on Document Analysis and
         Recognition, 2003.

     Based on https://gist.github.com/erniejunior/601cdf56d2b424757de5
    """
    if random_state is None:
        random_state = np.random.RandomState(None)

    shape = image.shape
    shape_size = shape[:2]
    
    # Random affine
    center_square = np.float32(shape_size) // 2
    square_size = min(shape_size) // 3
    pts1 = np.float32([center_square + square_size, [center_square[0]+square_size, center_square[1]-square_size], center_square - square_size])
    pts2 = pts1 + random_state.uniform(-alpha_affine, alpha_affine, size=pts1.shape).astype(np.float32)
    M = cv2.getAffineTransform(pts1, pts2)
    image = cv2.warpAffine(image, M, shape_size[::-1], borderMode=cv2.BORDER_REFLECT_101)

    dx = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha
    dy = gaussian_filter((random_state.rand(*shape) * 2 - 1), sigma) * alpha
    dz = np.zeros_like(dx)

    x, y, z = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]), np.arange(shape[2]))
    indices = np.reshape(y+dy, (-1, 1)), np.reshape(x+dx, (-1, 1)), np.reshape(z, (-1, 1))

    return map_coordinates(image, indices, order=1, mode='reflect').reshape(shape)
コード例 #21
0
def elastic_transform(image, gt, alpha, sigma, random_state=None):
    """
    :param image: image
    :param gt: ground truth
    :param alpha: deformation coefficient (high alpha -> strong deformation)
    :param sigma: std of the gaussian filter. (high sigma -> smooth deformation)
    :param random_state:
    :return: deformation of the pair [image,mask]
    """

    if random_state is None:
        random_state = np.random.RandomState(None)

    shape = image.shape

    d = 4
    sub_shape = (shape[0]/d, shape[0]/d)

    deformations_x = random_state.rand(*sub_shape) * 2 - 1
    deformations_y = random_state.rand(*sub_shape) * 2 - 1

    deformations_x = np.repeat(np.repeat(deformations_x, d, axis=1), d, axis = 0)
    deformations_y = np.repeat(np.repeat(deformations_y, d, axis=1), d, axis = 0)

    dx = gaussian_filter(deformations_x, sigma, mode="constant", cval=0) * alpha
    dy = gaussian_filter(deformations_y, sigma, mode="constant", cval=0) * alpha

    x, y = np.meshgrid(np.arange(shape[0]), np.arange(shape[1]))
    indices = np.reshape(y+dy, (-1, 1)), np.reshape(x+dx, (-1, 1))

    elastic_image = map_coordinates(image, indices, order=1).reshape(shape)
    elastic_gt = map_coordinates(gt, indices, order=1).reshape(shape)
    elastic_gt = preprocessing.binarize(np.array(elastic_gt), threshold=0.5)

    return [elastic_image, elastic_gt]
コード例 #22
0
ファイル: feature.py プロジェクト: adam410/cs365p4
	def generateData(self):
		image = self.getInput(0).getData()
		
		image = image.astype(numpy.float32)
	
		# compute derivatives in x
		Ix = filters.gaussian_filter1d(image, self.sigmaD, 0, 0)
		Ix = filters.gaussian_filter1d(Ix, self.sigmaD, 1, 1)
	
		# compute derivatives in y
		Iy = filters.gaussian_filter1d(image, self.sigmaD, 1, 0)
		Iy = filters.gaussian_filter1d(Iy, self.sigmaD, 0, 1)
	
		# compute components of the structure tensor
		# 2nd derivative in x
		Ixx = filters.gaussian_filter(Ix**2, self.sigmaI, 0)
		# 2nd derivative in y
		Iyy = filters.gaussian_filter(Iy**2, self.sigmaI, 0)
		# IxIy
		Ixy = filters.gaussian_filter(Ix * Iy, self.sigmaI, 0)
		
		self.getOutput(0).setData(Ix)
		self.getOutput(1).setData(Iy)
		self.getOutput(2).setData(Ixx)
		self.getOutput(3).setData(Iyy)
		self.getOutput(4).setData(Ixy)
コード例 #23
0
	def svm(self):
		img = self.training_set[0]
		img.image_data = gaussian_filter(img.image_data, 15)
		# img_average = numpy.average(img.image_data)
		training_set = self.generate_feature(img)
		img = self.training_set[1]
		img.image_data = gaussian_filter(img.image_data, 15)
		test_set = self.generate_feature(img)

		pca = PCA(n_components = 20)
		pca.fit([item[0] for item in training_set]+[item[0] for item in test_set])
		pca_training = pca.transform([item[0] for item in training_set])
		# for img in training_set:
		# 	print_image(img[0].reshape(2*self.MAT_SIZE[0]+1,2*self.MAT_SIZE[1]+1), '{}_fig_{}_{}.png'.format(img[1], img[2][0], img[2][1]))
		# training_set = training_set.map(lambda x: (x[0]-img_average, x[1]))
		model = svm.SVC()
		# model = tree.DecisionTreeClassifier()
		model.fit(pca_training,numpy.array([item[1] for item in training_set]))

		training_result = model.predict(pca_training)
		hit = 0
		for index, tag in enumerate(training_result):
			if tag == training_set[index][1]:
				hit += 1
		print(float(hit) / float(len(training_set)))

		pca_test = pca.transform([item[0] for item in test_set])
		# test_set = test_set.map(lambda x: (x[0]-img_average, x[1]))
		predicted = model.predict(pca_test)

		hit = 0
		for index, tag in enumerate(predicted):
			if tag == test_set[index][1]:
				hit += 1
		print(float(hit) / float(len(test_set)))
コード例 #24
0
def computeCurvature(xPos, yPos, time, sigmaVal):
    from scipy.ndimage.filters import gaussian_filter

    nPts = len(xPos)

    # Compute first and second derivatives of x and y w.r.t. to t
    dxdt = np.zeros(nPts)
    dydt = np.zeros(nPts)

    # Smooth position and partial derivatives with gaussian kernel before taking numerical derivative
    sigma = sigmaVal
    x_filt = gaussian_filter(xPos, sigma, mode='reflect')
    y_filt = gaussian_filter(yPos, sigma, mode='reflect')

    dxdt[1:] = np.diff(x_filt)/np.diff(time)
    dydt[1:] = np.diff(y_filt)/np.diff(time)

    ddxdt = np.zeros(nPts)
    ddydt = np.zeros(nPts)

    sigma = sigmaVal
    dxdt_filt = gaussian_filter(dxdt, sigma, mode='reflect')
    dydt_filt = gaussian_filter(dydt, sigma, mode='reflect')

    ddxdt[1:] = np.diff(dxdt_filt)/np.diff(time)
    ddydt[1:] = np.diff(dydt_filt)/np.diff(time)

    # Compute and return curvature
    curvature = (dxdt*ddydt - dydt*ddxdt) / (dxdt*dxdt + dydt*dydt)**(3.0/2.0)

    return curvature
コード例 #25
0
def polarCurvature(theta, objdist):

    from scipy.ndimage.filters import gaussian_filter

    # unwrap theta before taking derivatives
    # thetaU = np.copy(theta)
    # thetaU[~np.isnan(theta)] = np.unwrap(theta[~np.isnan(theta)],discont=np.pi)

    # first derivatives of the distance and angle
    d_theta = np.hstack((0, np.diff(theta)))
    d_theta[d_theta > np.pi] -= 2*np.pi
    d_theta[d_theta <= -np.pi] += 2*np.pi
    d_theta[np.where(np.isnan(d_theta))[0]] = 0

    # filter derivatives
    sigma = 2
    d_theta_filt = gaussian_filter(d_theta, sigma, mode='reflect')

    d_objdist = np.hstack((0, np.diff(objdist)))/d_theta_filt

    d_objdist_filt = gaussian_filter(d_objdist, sigma, mode='reflect')

    # second derivative
    dd_objdist = np.hstack((0, np.diff(d_objdist_filt)))/d_theta_filt

    # compute curvature
    polarCurv = (objdist**2 + 2*(d_objdist**2) - objdist*dd_objdist)/(np.sqrt(objdist**2 + d_objdist**2)**3)

    return polarCurv, d_theta, d_objdist
コード例 #26
0
    def generateData(self):
        inpt = self.getInput().getData().astype(numpy.float32)

        gI1 = filters.gaussian_filter(inpt, 1.2, 0)
        gI2 = filters.gaussian_filter(inpt, 2.0, 0)
        output = gI2 - gI1
        self.getOutput().setData(output)
コード例 #27
0
def smoothGeometry(landFraction, floatingFraction, bed, draft, filterSigma):
  import scipy.ndimage.filters as filters

  # Smoothing is performed using only the topography in the portion of the grid that is ocean.
  # This prevents the kink in the ice draft across the grounding line or regions of bare bedrock
  # from influencing the smoothed topography.  (Parts of the Ross ice shelf near the Trans-Antarctic
  # Mountains are particularly troublesome if topogrpahy is smoothed across the grounding line.)

  # Unlike in POP, the calving front is smoothed as well because MPAS-O does not support a
  # sheer calving face

  threshold = 0.01 # we won't normalize bed topography or ice draft where the mask is below this threshold

  oceanFraction = 1. - landFraction
  smoothedMask = filters.gaussian_filter(oceanFraction,filterSigma,mode='constant',cval=0.)
  mask = smoothedMask > threshold

  draft = filters.gaussian_filter(draft*oceanFraction,filterSigma,mode='constant',cval=0.)
  draft[mask] /= smoothedMask[mask]
  bed = filters.gaussian_filter(bed*oceanFraction,filterSigma,mode='constant',cval=0.)
  bed[mask] /= smoothedMask[mask]

  smoothedDraftMask = filters.gaussian_filter(floatingFraction,filterSigma,mode='constant',cval=0.)
  smoothedDraftMask[mask] /= smoothedMask[mask]

  return (bed,draft,smoothedDraftMask)
コード例 #28
0
ファイル: morphing.py プロジェクト: Jeronics/cac-segmenter
def smoothen_image(image_obj, sigma):
    image = gaussian_filter(image_obj.image, sigma=sigma, order=0)
    image_obj.image = image

    gray_image = gaussian_filter(image_obj.gray_image, sigma=sigma, order=0)
    image_obj.gray_image = gray_image

    return image_obj
コード例 #29
0
ファイル: segmenter.py プロジェクト: jblsmith/msaf
def gaussian_filter(X, M=8, axis=0):
    """Gaussian filter along the first axis of the feature matrix X."""
    for i in range(X.shape[axis]):
        if axis == 1:
            X[:, i] = filters.gaussian_filter(X[:, i], sigma=M / 2.)
        elif axis == 0:
            X[i, :] = filters.gaussian_filter(X[i, :], sigma=M / 2.)
    return X
コード例 #30
0
def compute_curvature(input_filepath, alpha, beta, method, new_centerlines, compute_original, region_of_interest,
                      region_points):
    """
    Primary collection of methods for computing curvature of a centerline.
    Five methods are currently implemented:
    1) VMTK - Factor variance (vmtkfactor)
    2) VMTK - Iteration variance (vmtkit)
    3) Discrete derivatives (disc)
    4) B-splines (spline)

    Args:
        input_filepath (str): Path to case folder.
        alpha (float): Extension / Compression factor in vertical direction.
        beta (float): Extension / Compression factor in horizontal direction.
        method (str): Method used to compute angle.
        new_centerlines (vtkPolyData): New centerline.
        compute_original (bool): Computes old curvature value if True.
        region_of_interest (str): Method for setting the region of interest ['manual' | 'commandline' | 'landmarking']
        region_points (list): If region_of_interest is 'commandline', this a flatten list of the start and endpoint

    Returns:
        new_maxcurv (float): Maximum curvature within the manipulated region of interest.
    Returns:
        old_maxcurv (float): Maximum curvature within the original region of interest.
    """
    # Input filename
    base_path = get_path_names(input_filepath)

    # Centerline filename
    centerline_path = base_path + "_centerline.vtp"

    # Clean and capp / uncapp surface
    surface, capped_surface = prepare_surface(base_path, input_filepath)

    # Extract old centerline
    if not path.exists(centerline_path):
        # Compute centerlines
        inlet, outlets = get_inlet_and_outlet_centers(surface, base_path)

        print("-- Compute centerlines and Voronoi diagram")
        centerlines, _, _ = compute_centerlines(inlet, outlets, centerline_path, capped_surface, resampling=0.1,
                                                smooth=False, base_path=base_path)
    else:
        centerlines = read_polydata(centerline_path)

    # Get region of interest
    point_path = base_path + "_anterior_bend.particles"
    if region_of_interest == "landmarking":
        if not path.exists(point_path):
            raise RuntimeError(("The given .particles file: %s does not exist. Please run" +
                                " landmarking with automated_landmarking.py first.") % point_path)
        region_points = np.loadtxt(point_path)
    else:
        _, _, _, region_points, _ = get_line_to_change(capped_surface, centerlines,
                                                       region_of_interest, "bend", region_points, 0)
        region_points = [[region_points[3 * i], region_points[3 * i + 1], region_points[3 * i + 2]]
                         for i in range(len(region_points) // 3)]
    p1 = region_points[0]
    p2 = region_points[1]

    if new_centerlines is None:
        print("-- Maniuplating centerline manually")
        centerlines, new_centerlines = get_new_centerlines(centerlines, region_points, alpha, beta, p1, p2)

    # Extract centerline points and ids
    new_centerline = extract_single_line(new_centerlines, 0)
    centerline = extract_single_line(centerlines, 0)
    new_locator = get_vtk_point_locator(new_centerline)
    old_locator = get_vtk_point_locator(centerline)
    id1 = old_locator.FindClosestPoint(p1)
    id2 = old_locator.FindClosestPoint(p2)
    id1_new = new_locator.FindClosestPoint(p1)
    id2_new = new_locator.FindClosestPoint(p2)

    # 1) VMTK - Factor variance
    if method == "vmtkfactor":
        factor = 0.5
        line_fac = vmtk_compute_geometric_features(new_centerline, smooth=True, iterations=100, factor=factor)
        curv_fac = get_point_data_array("Curvature", line_fac)
        new_curvature = gaussian_filter(curv_fac, 5)

        if compute_original:
            line_fac = vmtk_compute_geometric_features(centerline, smooth=True, iterations=100, factor=factor)
            curv_fac = get_point_data_array("Curvature", line_fac)
            curvature = gaussian_filter(curv_fac, 5)

    # 2) VMTK - Iteration variance
    elif method == "vmtkit":
        it = 150
        line_it = vmtk_compute_geometric_features(new_centerline, smooth=True, iterations=it, factor=1.0)
        curv_it = get_point_data_array("Curvature", line_it)
        new_curvature = gaussian_filter(curv_it, 5)

        if compute_original:
            line_it = vmtk_compute_geometric_features(centerline, smooth=True, iterations=it, factor=1.0)
            curv_it = get_point_data_array("Curvature", line_it)
            curvature = gaussian_filter(curv_it, 5)

    # 3) Splines
    elif method == "spline":
        nknots = 50
        _, siphon_curv = compute_splined_centerline(new_centerline, get_curv=True, isline=True, nknots=nknots)
        new_curvature = gaussian_filter(siphon_curv, 5)

        if compute_original:
            _, siphon_curv = compute_splined_centerline(centerline, get_curv=True, isline=True, nknots=nknots)
            curvature = gaussian_filter(siphon_curv, 5)

    # 4) Default: Discrete derivatives
    elif method == "disc":
        neigh = 20
        _, curv_di = compute_discrete_derivatives(new_centerline, neigh=neigh)
        new_curvature = gaussian_filter(curv_di, 5)

        if compute_original:
            _, curv_di = compute_discrete_derivatives(centerline, neigh=neigh)
            curvature = gaussian_filter(curv_di, 5)

    old_maxcurv = max(curvature[id1 + 10:id2 - 10]) if compute_original else None
    new_maxcurv = max(new_curvature[id1_new + 10:id2_new - 10])

    return new_maxcurv, old_maxcurv
コード例 #31
0
ap.add_argument("-d",
                "--min_distance",
                required=True,
                type=int,
                help="min distance in peak_local_max function")
args = vars(ap.parse_args())

# load the mask image_mask and perform pyramid mean shift filtering
image_mask = cv2.imread(args["image_mask"])

# apply meanshif filering
shifted = cv2.pyrMeanShiftFiltering(image_mask, 21, 51)
shifted_gray = cv2.cvtColor(shifted, cv2.COLOR_BGR2GRAY)

# apply gaussican filtering to smooth the edges
blurred = gaussian_filter(shifted_gray, sigma=args["sigma"])

# convert the mean shift image_mask to grayscale, then apply Otsu's thresholding
mask_output = cv2.threshold(blurred, 0, 255,
                            cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

from skimage import morphology
from skimage.morphology import disk
#from skimage.segmentation import clear_border

# remove artifacts connected to image border
#cleared = mask_output.copy()
#im_bw_cleared = clear_border(cleared)

im_bw_cleared = morphology.remove_small_objects(mask_output, min_size=350)
コード例 #32
0
    def train_model(self, image_dir, nb_images=50000, nb_epochs=1):
        datagen = ImageDataGenerator(rescale=1. / 255)
        img_width = self.img_width * 4
        img_height = self.img_height * 4

        early_stop = False
        iteration = 0
        prev_improvement = -1

        print("Training SR ResNet network")
        for i in range(nb_epochs):
            print()
            print("Epoch : %d" % (i + 1))

            for x in datagen.flow_from_directory(image_dir,
                                                 class_mode=None,
                                                 batch_size=self.batch_size,
                                                 target_size=(img_width,
                                                              img_height)):

                try:
                    t1 = time.time()

                    # resize images
                    x_temp = x.copy()
                    x_temp = x_temp.transpose((0, 2, 3, 1))

                    x_generator = np.empty(
                        (self.batch_size, self.img_width, self.img_height, 3))

                    for j in range(self.batch_size):
                        img = gaussian_filter(x_temp[j], sigma=0.5)
                        img = imresize(img, (self.img_width, self.img_height))
                        x_generator[j, :, :, :] = img

                    x_generator = x_generator.transpose((0, 3, 1, 2))

                    if iteration % 50 == 0 and iteration != 0:
                        print("Random Validation image..")
                        output_image_batch = self.model.predict_on_batch(
                            x_generator)

                        average_psnr = 0.0
                        for x_i in range(self.batch_size):
                            average_psnr += psnr(
                                x[x_i],
                                np.clip(output_image_batch[x_i] * 255, 0, 255)
                                / 255.)

                        average_psnr /= self.batch_size

                        iteration += self.batch_size
                        t2 = time.time()

                        print(
                            "Time required : %0.2f. Average validation PSNR over %d samples = %0.2f"
                            % (t2 - t1, self.batch_size, average_psnr))

                        for x_i in range(self.batch_size):
                            real_path = base_val_images_path + "epoch_%d_iteration_%d_num_%d_real_.png" % \
                                                               (i + 1, iteration, x_i + 1)

                            generated_path = base_val_images_path + \
                                             "epoch_%d_iteration_%d_num_%d_generated.png" % (i + 1,
                                                                                            iteration,
                                                                                            x_i + 1)

                            val_x = x[x_i].copy() * 255.
                            val_x = val_x.transpose((1, 2, 0))
                            val_x = np.clip(val_x, 0, 255).astype('uint8')

                            output_image = output_image_batch[x_i] * 255
                            output_image = output_image.transpose((1, 2, 0))
                            output_image = np.clip(output_image, 0,
                                                   255).astype('uint8')

                            imsave(real_path, val_x)
                            imsave(generated_path, output_image)
                        '''
                        Don't train of validation images for now.

                        Note that if nb_epochs > 1, there is a chance that
                        validation images may be used for training purposes as well.

                        In that case, this isn't strictly a validation measure, instead of
                        just a check to see what the network has learned.
                        '''
                        continue

                    hist = self.model.fit(x_generator,
                                          x,
                                          batch_size=self.batch_size,
                                          nb_epoch=1,
                                          verbose=0)
                    psnr_loss_val = hist.history['PSNRLoss'][0]

                    if prev_improvement == -1:
                        prev_improvement = psnr_loss_val

                    improvement = (prev_improvement -
                                   psnr_loss_val) / prev_improvement * 100
                    prev_improvement = psnr_loss_val

                    iteration += self.batch_size
                    t2 = time.time()

                    print(
                        "Iter : %d / %d | Improvement : %0.2f percent | Time required : %0.2f seconds | "
                        "PSNR : %0.3f" % (iteration, nb_images, improvement,
                                          t2 - t1, psnr_loss_val))

                    if iteration % 1000 == 0 and iteration != 0:
                        self.model.save_weights(self.weights_path,
                                                overwrite=True)

                    if iteration >= nb_images:
                        break

                except KeyboardInterrupt:
                    print("Keyboard interrupt detected. Stopping early.")
                    early_stop = True
                    break

            iteration = 0

            if early_stop:
                break

        print("Finished training SRGAN network. Saving model weights.")
コード例 #33
0
def get_finding_chart(
    source_ra,
    source_dec,
    source_name,
    image_source='desi',
    output_format='pdf',
    imsize=3.0,
    tick_offset=0.02,
    tick_length=0.03,
    fallback_image_source='dss',
    **offset_star_kwargs,
):
    """Create a finder chart suitable for spectroscopic observations of
       the source

    Parameters
    ----------
    source_ra : float
        Right ascension (J2000) of the source
    source_dec : float
        Declination (J2000) of the source
    source_name : str
        Name of the source
    image_source : {'desi', 'dss', 'ztfref'}, optional
        Survey where the image comes from "desi", "dss", "ztfref"
        (more to be added)
    output_format : str, optional
        "pdf" of "png" -- determines the format of the returned finder
    imsize : float, optional
        Requested image size (on a size) in arcmin. Should be between 2-15.
    tick_offset : float, optional
        How far off the each source should the tick mark be made? (in arcsec)
    tick_length : float, optional
        How long should the tick mark be made? (in arcsec)
    fallback_image_source : str, optional
        Where what `image_source` should we fall back to if the
        one requested fails
    **offset_star_kwargs : dict, optional
        Other parameters passed to `get_nearby_offset_stars`

    Returns
    -------
    dict
        success : bool
            Whether the request was successful or not, returning
            a sensible error in 'reason'
        name : str
            suggested filename based on `source_name` and `output_format`
        data : str
            binary encoded data for the image (to be streamed)
        reason : str
            If not successful, a reason is returned.
    """
    if (imsize < 2.0) or (imsize > 15):
        return {
            'success': False,
            'reason': 'Requested `imsize` out of range',
            'data': '',
            'name': '',
        }

    if image_source not in source_image_parameters:
        return {
            'success': False,
            'reason': f'image source {image_source} not in list',
            'data': '',
            'name': '',
        }

    matplotlib.use("Agg")
    fig = plt.figure(figsize=(11, 8.5), constrained_layout=False)
    widths = [2.6, 1]
    heights = [2.6, 1]
    spec = fig.add_gridspec(
        ncols=2,
        nrows=2,
        width_ratios=widths,
        height_ratios=heights,
        left=0.05,
        right=0.95,
    )

    # how wide on the side will the image be? 256 as default
    npixels = source_image_parameters[image_source].get("npixels", 256)
    # set the pixelscale in arcsec (typically about 1 arcsec/pixel)
    pixscale = 60 * imsize / npixels

    hdu = fits_image(source_ra,
                     source_dec,
                     imsize=imsize,
                     image_source=image_source)

    # skeleton WCS - this is the field that the user requested
    wcs = WCS(naxis=2)

    # set the headers of the WCS.
    # The center of the image is the reference point (source_ra, source_dec):
    wcs.wcs.crpix = [npixels / 2, npixels / 2]
    wcs.wcs.crval = [source_ra, source_dec]

    # create the pixel scale and orientation North up, East left
    # pixelscale is in degrees, established in the tangent plane
    # to the reference point
    wcs.wcs.cd = np.array([[-pixscale / 3600, 0], [0, pixscale / 3600]])
    wcs.wcs.ctype = ["RA---TAN", "DEC--TAN"]

    if hdu is not None:
        im = hdu.data

        # replace the nans with medians
        im[np.isnan(im)] = np.nanmedian(im)
        if source_image_parameters[image_source].get("reproject", False):
            # project image to the skeleton WCS solution
            log("Reprojecting image to requested position and orientation")
            im, _ = reproject_adaptive(hdu, wcs, shape_out=(npixels, npixels))
        else:
            wcs = WCS(hdu.header)

        if source_image_parameters[image_source].get("smooth", False):
            im = gaussian_filter(
                hdu.data,
                source_image_parameters[image_source]["smooth"] / pixscale)

        norm = ImageNormalize(im, interval=ZScaleInterval())
        watermark = source_image_parameters[image_source]["str"]

    else:
        # if we got back a blank image, try to fallback on another survey
        # and return the results from that call
        if fallback_image_source is not None:
            if fallback_image_source != image_source:
                log(f"Falling back on image source {fallback_image_source}")
                return get_finding_chart(
                    source_ra,
                    source_dec,
                    source_name,
                    image_source=fallback_image_source,
                    output_format=output_format,
                    imsize=imsize,
                    tick_offset=tick_offset,
                    tick_length=tick_length,
                    fallback_image_source=None,
                    **offset_star_kwargs,
                )

        # we dont have an image here, so let's create a dummy one
        # so we can still plot
        im = np.zeros((npixels, npixels))
        norm = None
        watermark = None

    # add the images in the top left corner
    ax = fig.add_subplot(spec[0, 0], projection=wcs)
    ax_text = fig.add_subplot(spec[0, 1])
    ax_text.axis('off')
    ax_starlist = fig.add_subplot(spec[1, 0:])
    ax_starlist.axis('off')

    ax.imshow(im, origin='lower', norm=norm, cmap='gray_r')
    ax.set_autoscale_on(False)
    ax.grid(color='white', ls='dotted')
    ax.set_xlabel(r'$\alpha$ (J2000)', fontsize='large')
    ax.set_ylabel(r'$\delta$ (J2000)', fontsize='large')
    obstime = offset_star_kwargs.get("obstime",
                                     datetime.datetime.utcnow().isoformat())
    ax.set_title(f'{source_name} Finder ({obstime})',
                 fontsize='large',
                 fontweight='bold')

    star_list, _, _, _ = get_nearby_offset_stars(source_ra, source_dec,
                                                 source_name,
                                                 **offset_star_kwargs)

    if not isinstance(star_list, list) or len(star_list) == 0:
        return {
            'success': False,
            'reason': 'failure to get star list',
            'data': '',
            'name': '',
        }

    ncolors = len(star_list)
    colors = sns.color_palette("colorblind", ncolors)

    start_text = [-0.35, 0.99]
    starlist_str = (
        "# Note: spacing in starlist many not copy/paste correctly in PDF\n" +
        f"#       you can get starlist directly from" +
        f" /api/sources/{source_name}/offsets?" +
        f"facility={offset_star_kwargs.get('facility', 'Keck')}\n" +
        "\n".join([x["str"] for x in star_list]))

    # add the starlist
    ax_starlist.text(
        0,
        0.50,
        starlist_str,
        fontsize="x-small",
        family='monospace',
        transform=ax_starlist.transAxes,
    )

    # add the watermark for the survey
    props = dict(boxstyle='round', facecolor='gray', alpha=0.5)

    if watermark is not None:
        ax.text(
            0.035,
            0.035,
            watermark,
            horizontalalignment='left',
            verticalalignment='center',
            transform=ax.transAxes,
            fontsize='medium',
            fontweight='bold',
            color="yellow",
            alpha=0.5,
            bbox=props,
        )

    ax.text(
        0.95,
        0.035,
        f"{imsize}\u2032 \u00D7 {imsize}\u2032",  # size'x size'
        horizontalalignment='right',
        verticalalignment='center',
        transform=ax.transAxes,
        fontsize='medium',
        fontweight='bold',
        color="yellow",
        alpha=0.5,
        bbox=props,
    )

    # compass rose
    # rose_center_pixel = ax.transAxes.transform((0.04, 0.95))
    rose_center = pixel_to_skycoord(int(npixels * 0.1), int(npixels * 0.9),
                                    wcs)
    props = dict(boxstyle='round', facecolor='gray', alpha=0.5)

    for ang, label, off in [(0, "N", 0.01), (90, "E", 0.03)]:
        position_angle = ang * u.deg
        separation = (0.05 * imsize * 60) * u.arcsec  # 5%
        p2 = rose_center.directional_offset_by(position_angle, separation)
        ax.plot(
            [rose_center.ra.value, p2.ra.value],
            [rose_center.dec.value, p2.dec.value],
            transform=ax.get_transform('world'),
            color="gold",
            linewidth=2,
        )

        # label N and E
        position_angle = (ang + 15) * u.deg
        separation = ((0.05 + off) * imsize * 60) * u.arcsec
        p2 = rose_center.directional_offset_by(position_angle, separation)
        ax.text(
            p2.ra.value,
            p2.dec.value,
            label,
            color="gold",
            transform=ax.get_transform('world'),
            fontsize='large',
            fontweight='bold',
        )

    for i, star in enumerate(star_list):

        c1 = SkyCoord(star["ra"] * u.deg, star["dec"] * u.deg, frame='icrs')

        # mark up the right side of the page with position and offset info
        name_title = star["name"]
        if star.get("mag") is not None:
            name_title += f", mag={star.get('mag'):.2f}"
        ax_text.text(
            start_text[0],
            start_text[1] - i / ncolors,
            name_title,
            ha='left',
            va='top',
            fontsize='large',
            fontweight='bold',
            transform=ax_text.transAxes,
            color=colors[i],
        )
        source_text = f"  {star['ra']:.5f} {star['dec']:.5f}\n"
        source_text += f"  {c1.to_string('hmsdms')}\n"
        if (star.get("dras") is not None) and (star.get("ddecs") is not None):
            source_text += f'  {star.get("dras")} {star.get("ddecs")} to {source_name}'
        ax_text.text(
            start_text[0],
            start_text[1] - i / ncolors - 0.06,
            source_text,
            ha='left',
            va='top',
            fontsize='large',
            transform=ax_text.transAxes,
            color=colors[i],
        )

        # work on making marks where the stars are
        for ang in [0, 90]:
            position_angle = ang * u.deg
            separation = (tick_offset * imsize * 60) * u.arcsec
            p1 = c1.directional_offset_by(position_angle, separation)
            separation = (tick_offset + tick_length) * imsize * 60 * u.arcsec
            p2 = c1.directional_offset_by(position_angle, separation)
            ax.plot(
                [p1.ra.value, p2.ra.value],
                [p1.dec.value, p2.dec.value],
                transform=ax.get_transform('world'),
                color=colors[i],
                linewidth=3 if imsize <= 4 else 2,
            )
        if star["name"].find("_off") != -1:
            # this is an offset star
            text = star["name"].split("_off")[-1]
            position_angle = 14 * u.deg
            separation = (tick_offset +
                          tick_length * 1.6) * imsize * 60 * u.arcsec
            p1 = c1.directional_offset_by(position_angle, separation)
            ax.text(
                p1.ra.value,
                p1.dec.value,
                text,
                color=colors[i],
                transform=ax.get_transform('world'),
                fontsize='large',
                fontweight='bold',
            )

    buf = io.BytesIO()
    fig.savefig(buf, format=output_format)
    buf.seek(0)

    return {
        "success": True,
        "name": f"finder_{source_name}.{output_format}",
        "data": buf.read(),
        "reason": "",
    }
コード例 #34
0
def LEstr(datafile=None):
    """ Plot streamlines """

    nm = "LE_Stream.LEstr: "

    if datafile == None:
        try:
            datafile = argv[1]
        except IndexError:
            raise IOError(nm + "Need an input file!")

    ## Analysis options
    for i in range(0, len(datafile)):
        if datafile[i] == "b":
            b = float(datafile[i + 1:i + 4])
            break
    for i in range(0, len(datafile)):
        if datafile[i] == "X": j = i
        if datafile[i] == "s" and datafile[i + 1] == "1":
            X = float(datafile[j + 1:i])
            break

    ## Output options
    saveplot = True
    showplot = False
    outfile = os.path.split(datafile)[0] + "/stream_" + os.path.split(
        datafile)[1][0:-4]

    t0 = time.time()
    ## Read eta (yy), xHO (x1) points from file
    yy, x1 = np.loadtxt(datafile, delimiter=" ", skiprows=1)[:, 1:3].T
    print nm + "Reading data", round(time.time() - t0, 1), "seconds"
    coords = zip(x1, yy)

    ## Set up grid of points in x-y
    gridsize = 20
    #x = np.linspace(-2*X,2*X, gridsize);	y = np.linspace(-2*b,2*b, gridsize)
    x = np.linspace(-15, 15, gridsize)
    y = np.linspace(-1.5, 1.5, gridsize)
    xi, yi = np.meshgrid(x, y)
    #print xi;return

    ## Calculate speeds (1D arrays)
    vx1 = np.diff(x1)
    vx1 = np.append(np.array(vx1[0]), vx1)
    vyy = np.diff(yy)
    vyy = np.append(np.array(vyy[0]), vyy)
    vyy /= 100  ## HACK TO RE-SCALE ETA -- MESSY!
    v1 = np.sqrt(vx1**2 + vyy**2)
    del x1, yy

    t0 = time.time()
    ## Interpolate data onto grid
    gvx11 = griddata(coords, vx1, (xi, yi), method='nearest')
    gvyy1 = griddata(coords, vyy, (xi, yi), method='nearest')
    gv1 = griddata(coords, v1, (xi, yi), method='nearest')
    print nm + "Gridding data", round(time.time() - t0, 1), "seconds"
    del coords

    if saveplot or showplot:
        ## Subplots for 1 and 2
        fig, ax1 = plt.subplots(1, 1)
        fig.suptitle(outfile)
        fig.set_facecolor("white")

        ## Smooth data
        smooth = 2
        gvyy1 = gaussian_filter(gvyy1, smooth)
        gvx11 = gaussian_filter(gvx11, smooth)
        gv1 = gaussian_filter(gv1, smooth)
        ## Line widths
        lw1 = 3.0 * gv1 / gv1.max()

        t0 = time.time()
        ## Make plots
        fs = 25
        ax1.contourf(xi, yi, gv1, 4, alpha=0.4)
        ax1.streamplot(x, y, gvx11, gvyy1, linewidth=lw1, cmap=plt.cm.jet)
        #ax1.plot([-X,-X],[-2*b,2*b],"k--",linewidth=2);ax1.plot([X,X],[-2*b,2*b],"k--",linewidth=2)
        ax1.plot([-X, -X], [-1.5, 1.5], "k--", linewidth=2)
        ax1.plot([X, X], [-1.5, 1.5], "k--", linewidth=2)
        ax1.set_xlabel("$x$", fontsize=fs)
        ax1.set_ylabel("$\eta$", fontsize=fs)
        print nm + "Plotting", round(time.time() - t0, 1), "seconds"
        ## NEED: colorbar

        if saveplot:
            fig.savefig(outfile + "TEST.png",
                        facecolor=fig.get_facecolor(),
                        edgecolor="none")
            print nm + "Plot saved", outfile + ".png"
        if showplot:
            plt.show()

    return
コード例 #35
0
def LoadImage(fname) :
    img = misc.imread(fname, flatten = True) # Grayscale
    img = gaussian_filter(img, 1, mode='nearest')
    img = (img[::-1, :])  / 255.
    img = np.swapaxes(img, 0,1 )
    return tensor( 1 - img )
コード例 #36
0
ファイル: gdal-test.py プロジェクト: tjturnage/gis
xgrid = np.arange(xmin, xmax, xres)
ygrid = np.arange(ymax, ymin, yres)
X, Y = np.meshgrid(xgrid, ygrid)

origin = [gt[0], gt[3]]
ext = GetExtent(gt, cols, rows)

src_srs = osr.SpatialReference()
src_srs.ImportFromWkt(ds.GetProjection())
tgt_srs = src_srs.CloneGeogCS()

geo_ext = ReprojectCoords(ext, src_srs, tgt_srs)

npRaster = gdal.Dataset.ReadAsArray(ds)
blurred = gaussian_filter(npRaster, sigma=4)
rasterFinal = 'C:\\data\\GIS\\blur.tif'
array2raster(rasterFinal, origin, xres, yres, blurred)

ds = None

# create a grid of xy coordinates in the original projection
xy_source = np.mgrid[xmin:xmax + xres:xres, ymax + yres:ymin:yres]

xx, yy = convertXY(xy_source, src_srs, tgt_srs)

# plot the data (first layer)
im1 = plt.pcolormesh(xgrid, ygrid, blurred)

# annotate
#plt.drawcountries()
コード例 #37
0
ファイル: ImageTransform.py プロジェクト: trasse/DRFNS
    def _apply_(self, *image):
        res = ()
        n_img = 0
        for img in image:
            shape = img.shape
            shape_size = shape[:2]
            if not hasattr(self, "M"):
                alpha = img.shape[1] * self.params["alpha"]
                alpha_affine = img.shape[1] * self.params["alpha_affine"]
                sigma = img.shape[1] * self.params["sigma"]
                # Random affine
                center_square = np.float32(shape_size) // 2
                square_size = min(shape_size) // 3
                random_state = np.random.RandomState(None)

                pts1 = np.float32([
                    center_square + square_size,
                    [
                        center_square[0] + square_size,
                        center_square[1] - square_size
                    ], center_square - square_size
                ])
                pts2 = pts1 + \
                    random_state.uniform(-alpha_affine, alpha_affine,
                                         size=pts1.shape).astype(np.float32)
                self.M = cv2.getAffineTransform(pts1, pts2)
                self.dx = gaussian_filter(
                    (random_state.rand(*shape) * 2 - 1), sigma) * alpha
                self.dy = gaussian_filter(
                    (random_state.rand(*shape) * 2 - 1), sigma) * alpha
            if shape[0:2] == self.dx.shape[0:2]:
                if len(shape) == 3:
                    x, y, z = np.meshgrid(np.arange(shape[1]),
                                          np.arange(shape[0]),
                                          np.arange(shape[2]),
                                          indexing='ij')
                    indices = np.reshape(x + self.dx, (-1, 1)), np.reshape(
                        y + self.dy, (-1, 1)), np.reshape(z, (-1, 1))
                elif len(shape) == 2:
                    x, y = np.meshgrid(np.arange(shape[1]),
                                       np.arange(shape[0]),
                                       indexing='ij')
                    if len(self.dx.shape) == 3:
                        indices = np.reshape(x + np.mean(self.dx, axis=2),
                                             (-1, 1)), np.reshape(
                                                 y + np.mean(self.dy, axis=2),
                                                 (-1, 1))
                    else:
                        indices = np.reshape(x + self.dx, (-1, 1)), np.reshape(
                            y + self.dy, (-1, 1))
                else:
                    print "Error"
            elif shape[0] > self.dx.shape[0]:
                print "not possible"
            else:
                offset = (self.dx.shape[0] - shape[0]) / 2
                if len(shape) == 3:
                    x, y, z = np.meshgrid(np.arange(shape[1]),
                                          np.arange(shape[0]),
                                          np.arange(shape[2]),
                                          indexing='ij')
                    indices = np.reshape(
                        x + self.dx[offset:-offset, offset:-offset],
                        (-1, 1)), np.reshape(
                            y + self.dy[offset:-offset, offset:-offset],
                            (-1, 1)), np.reshape(z, (-1, 1))
                elif len(shape) == 2:
                    x, y = np.meshgrid(np.arange(shape[1]),
                                       np.arange(shape[0]),
                                       indexing='ij')
                    if len(self.dx.shape) == 3:
                        indices = np.reshape(
                            x +
                            np.mean(self.dx[offset:-offset, offset:-offset],
                                    axis=2), (-1, 1)), np.reshape(
                                        y + np.mean(self.dy[offset:-offset,
                                                            offset:-offset],
                                                    axis=2), (-1, 1))
                    else:
                        indices = np.reshape(
                            x + self.dx[offset:-offset, offset:-offset],
                            (-1, 1)), np.reshape(
                                y + self.dy[offset:-offset, offset:-offset],
                                (-1, 1))
                else:
                    print "Error"
            if n_img == 1:
                order = 0
                flags = cv2.INTER_NEAREST
            else:
                order = 1
                flags = cv2.INTER_LINEAR
            img = cv2.warpAffine(img,
                                 self.M,
                                 shape_size[::-1],
                                 flags=flags,
                                 borderMode=cv2.BORDER_REFLECT_101)

            sub_res = map_coordinates(img,
                                      indices,
                                      order=order,
                                      mode='reflect').reshape(shape)
            sub_res = self.OutputType(sub_res)
            res += (sub_res, )
            n_img += 1
        return res
コード例 #38
0
def evaluate(upsampling_factor, residual_blocks, feature_size,
             checkpoint_dir_restore, path_volumes, nn, subpixel_NN, img_height,
             img_width, img_depth):

    # dataset & variables
    traindataset = Train_dataset(1)
    iterations = math.ceil(
        (len(traindataset.subject_list) *
         0.2))  # 817 subjects total. De 0 a 654 training. De 654 a 817 test.
    print(len(traindataset.subject_list))
    print(iterations)
    totalpsnr = 0
    totalssim = 0
    array_psnr = np.empty(iterations)
    array_ssim = np.empty(iterations)
    batch_size = 1
    div_patches = 4
    num_patches = traindataset.num_patches

    # define model
    t_input_gen = tf.placeholder('float32', [1, None, None, None, 1],
                                 name='t_image_input_to_SRGAN_generator')
    srgan_network = generator(input_gen=t_input_gen,
                              kernel=3,
                              nb=residual_blocks,
                              upscaling_factor=upsampling_factor,
                              feature_size=feature_size,
                              subpixel_NN=subpixel_NN,
                              img_height=img_height,
                              img_width=img_width,
                              img_depth=img_depth,
                              nn=nn,
                              is_train=False,
                              reuse=False)

    # restore g
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                            log_device_placement=False))

    saver = tf.train.Saver(
        tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="SRGAN_g"))
    saver.restore(sess, tf.train.latest_checkpoint(checkpoint_dir_restore))

    for i in range(0, iterations):
        # extract volumes
        xt_total = traindataset.data_true(
            654 + i)  # [[self.batch_size, 224, 224, 152]]
        xt_mask = traindataset.mask(654 + i)
        xg_generated = np.empty([1, 224, 224, 152, 1])
        normfactor = (np.amax(xt_total[0])) / 2
        x_generator = ((xt_total[0] - normfactor) / normfactor)
        res = 1 / upsampling_factor
        x_generator = x_generator[:, :, :, np.newaxis]
        x_generator = zoom(x_generator, [res, res, res, 1])
        x_generator = gaussian_filter(x_generator, sigma=1)
        xg_generated[0] = sess.run(srgan_network.outputs,
                                   {t_input_gen: x_generator[np.newaxis, :]})
        xg_generated[0] = ((xg_generated[0] + 1) * normfactor)
        volume_real = xt_total[0]
        volume_real = volume_real[:, :, :, np.newaxis]
        volume_generated = xg_generated[0]
        volume_mask = aggregate(xt_mask)
        # compute metrics
        max_gen = np.amax(volume_generated)
        max_real = np.amax(volume_real)
        if max_gen > max_real:
            val_max = max_gen
        else:
            val_max = max_real
        min_gen = np.amin(volume_generated)
        min_real = np.amin(volume_real)
        if min_gen < min_real:
            val_min = min_gen
        else:
            val_min = min_real
        val_psnr = psnr(np.multiply(volume_real, volume_mask),
                        np.multiply(volume_generated, volume_mask),
                        dynamic_range=val_max - val_min)
        array_psnr[i] = val_psnr

        totalpsnr += val_psnr
        val_ssim = ssim(np.multiply(volume_real, volume_mask),
                        np.multiply(volume_generated, volume_mask),
                        dynamic_range=val_max - val_min,
                        multichannel=True)
        array_ssim[i] = val_ssim
        totalssim += val_ssim
        print(val_psnr)
        print(val_ssim)
        # save volumes
        filename_gen = os.path.join(path_volumes, str(i) + 'gen.nii.gz')
        img_volume_gen = nib.Nifti1Image(volume_generated, np.eye(4))
        img_volume_gen.to_filename(filename_gen)
        filename_real = os.path.join(path_volumes, str(i) + 'real.nii.gz')
        img_volume_real = nib.Nifti1Image(volume_real, np.eye(4))
        img_volume_real.to_filename(filename_real)

    print('{}{}'.format('Mean PSNR: ', array_psnr.mean()))
    print('{}{}'.format('Mean SSIM: ', array_ssim.mean()))
    print('{}{}'.format('Variance PSNR: ', array_psnr.var()))
    print('{}{}'.format('Variance SSIM: ', array_ssim.var()))
    print('{}{}'.format('Max PSNR: ', array_psnr.max()))
    print('{}{}'.format('Min PSNR: ', array_psnr.min()))
    print('{}{}'.format('Max SSIM: ', array_ssim.max()))
    print('{}{}'.format('Min SSIM: ', array_ssim.min()))
コード例 #39
0
    def constant_cylinder(self,
                          radius,
                          dvp=-5.0,
                          dvs=-5.0,
                          drho=-1.0,
                          start_depth=100.0,
                          **kwargs):
        '''
      generates a cylindrical anomaly with constant perturbation in Vp, Vs, and density
      use this *after* running 'velocity_conversion'
      args-----------------------------------------------------------------------
      #   radius: radius of cylinder in km
      #   dvp: vp perturbation in %
      #   dvs: vs perturbation in %
      #   rho: density perturbation in %
      #   start_depth: starting depth of cylinder

      #kwargs--------------------------------------------------------------------
      sigma_blur: sigma for gaussian smoothing (default = 3.0)
      '''
        self.dvp_rel = np.zeros((self.npts_rad, self.npts_theta))
        self.dvs_rel = np.zeros((self.npts_rad, self.npts_theta))
        self.drho_rel = np.zeros((self.npts_rad, self.npts_theta))
        self.dvp_abs = np.zeros((self.npts_rad, self.npts_theta))
        self.dvs_abs = np.zeros((self.npts_rad, self.npts_theta))
        self.drho_abs = np.zeros((self.npts_rad, self.npts_theta))
        sigma_blur = kwargs.get('sigma_blur', 3.0)
        sigma_blur_vert = kwargs.get('sigma_blur_vert', 0.5)

        #T_ref = self.T_adiabat[::-1]
        #T_here = np.zeros((self.npts_rad,self.npts_theta))

        for i in range(0, self.npts_rad):
            km_per_degree = self.rad_km[i] * 2 * np.pi / 360.0

            #get 1d values
            vp_here = self.vp1d[::-1][i]
            vs_here = self.vs1d[::-1][i]
            rho_here = self.rho1d[::-1][i]

            #calculate absolute values of perturbations
            dvp_abs = vp_here * (dvp / 100.0)
            dvs_abs = vs_here * (dvs / 100.0)
            drho_abs = rho_here * (drho / 100000.0)

            for j in range(0, self.npts_theta):

                cyl_th_here = radius / km_per_degree
                th_here = self.theta[j]
                depth_here = 6371.0 - self.rad_km[i]

                if th_here <= cyl_th_here and depth_here > start_depth:
                    self.dvp_abs[(self.npts_rad - 1) - i, j] += dvp_abs
                    self.dvs_abs[(self.npts_rad - 1) - i, j] += dvs_abs
                    self.drho_abs[(self.npts_rad - 1) - i, j] += drho_abs
                    self.dvp_rel[(self.npts_rad - 1) - i, j] += dvp
                    self.dvs_rel[(self.npts_rad - 1) - i, j] += dvs
                    self.drho_rel[(self.npts_rad - 1) - i, j] += drho

        self.dvp_abs = gaussian_filter(self.dvp_abs,
                                       sigma=[sigma_blur_vert, sigma_blur])
        self.dvs_abs = gaussian_filter(self.dvs_abs,
                                       sigma=[sigma_blur_vert, sigma_blur])
        self.drho_abs = gaussian_filter(self.drho_abs,
                                        sigma=[sigma_blur_vert, sigma_blur])
        self.dvp_rel = gaussian_filter(self.dvp_rel,
                                       sigma=[sigma_blur_vert, sigma_blur])
        self.dvs_rel = gaussian_filter(self.dvs_rel,
                                       sigma=[sigma_blur_vert, sigma_blur])
        self.drho_rel = gaussian_filter(self.drho_rel,
                                        sigma=[sigma_blur_vert, sigma_blur])
コード例 #40
0
from PIL import Image
from numpy import *
from scipy.ndimage import filters

im = array(Image.open('for_learn.jpeg').convert('L'))
im_blur_5 = filters.gaussian_filter(im, 5)
im_blur_10 = filters.gaussian_filter(im, 10)

# Image.fromarray(im_blur_5).save('./photo/im_blur_5.jpg')
# Image.fromarray(im_blur_10).save('./photo/im_blur_10.jpg')
コード例 #41
0
ファイル: body.py プロジェクト: eaoge/openpose_practice
    def __call__(self, oriImg):
        scale_search = [0.5, 1.0, 1.5, 2.0]
        #scale_search = [0.5]
        boxsize = 368
        stride = 8
        padValue = 128
        thre1 = 0.1
        thre2 = 0.05
        multiplier = [x * boxsize / oriImg.shape[0] for x in scale_search]
        heatmap_avg = np.zeros((oriImg.shape[0], oriImg.shape[1], 19))
        paf_avg = np.zeros((oriImg.shape[0], oriImg.shape[1], 38))

        for m in range(len(multiplier)):
            scale = multiplier[m]
            imageToTest = cv2.resize(oriImg, (0, 0),
                                     fx=scale,
                                     fy=scale,
                                     interpolation=cv2.INTER_CUBIC)
            imageToTest_padded, pad = util.padRightDownCorner(
                imageToTest, stride, padValue)
            im = np.transpose(
                np.float32(imageToTest_padded[:, :, :, np.newaxis]),
                (3, 2, 0, 1)) / 256 - 0.5
            im = np.ascontiguousarray(im)

            data = torch.from_numpy(im).float()
            if torch.cuda.is_available():
                data = data.cuda()
            # data = data.permute([2, 0, 1]).unsqueeze(0).float()
            with torch.no_grad():
                Mconv7_stage6_L1, Mconv7_stage6_L2 = self.model(data)
            Mconv7_stage6_L1 = Mconv7_stage6_L1.cpu().numpy()
            Mconv7_stage6_L2 = Mconv7_stage6_L2.cpu().numpy()

            # extract outputs, resize, and remove padding
            # heatmap = np.transpose(np.squeeze(net.blobs[output_blobs.keys()[1]].data), (1, 2, 0))  # output 1 is heatmaps
            heatmap = np.transpose(np.squeeze(Mconv7_stage6_L2),
                                   (1, 2, 0))  # output 1 is heatmaps
            heatmap = cv2.resize(heatmap, (0, 0),
                                 fx=stride,
                                 fy=stride,
                                 interpolation=cv2.INTER_CUBIC)
            heatmap = heatmap[:imageToTest_padded.shape[0] -
                              pad[2], :imageToTest_padded.shape[1] - pad[3], :]
            heatmap = cv2.resize(heatmap, (oriImg.shape[1], oriImg.shape[0]),
                                 interpolation=cv2.INTER_CUBIC)

            # paf = np.transpose(np.squeeze(net.blobs[output_blobs.keys()[0]].data), (1, 2, 0))  # output 0 is PAFs
            paf = np.transpose(np.squeeze(Mconv7_stage6_L1),
                               (1, 2, 0))  # output 0 is PAFs
            paf = cv2.resize(paf, (0, 0),
                             fx=stride,
                             fy=stride,
                             interpolation=cv2.INTER_CUBIC)
            paf = paf[:imageToTest_padded.shape[0] -
                      pad[2], :imageToTest_padded.shape[1] - pad[3], :]
            paf = cv2.resize(paf, (oriImg.shape[1], oriImg.shape[0]),
                             interpolation=cv2.INTER_CUBIC)

            heatmap_avg += heatmap_avg + heatmap / len(multiplier)
            paf_avg += +paf / len(multiplier)

        all_peaks = []
        peak_counter = 0

        for part in range(18):
            map_ori = heatmap_avg[:, :, part]
            one_heatmap = gaussian_filter(map_ori, sigma=3)

            map_left = np.zeros(one_heatmap.shape)
            map_left[1:, :] = one_heatmap[:-1, :]
            map_right = np.zeros(one_heatmap.shape)
            map_right[:-1, :] = one_heatmap[1:, :]
            map_up = np.zeros(one_heatmap.shape)
            map_up[:, 1:] = one_heatmap[:, :-1]
            map_down = np.zeros(one_heatmap.shape)
            map_down[:, :-1] = one_heatmap[:, 1:]

            peaks_binary = np.logical_and.reduce(
                (one_heatmap >= map_left, one_heatmap >= map_right,
                 one_heatmap >= map_up, one_heatmap >= map_down,
                 one_heatmap > thre1))
            peaks = list(
                zip(np.nonzero(peaks_binary)[1],
                    np.nonzero(peaks_binary)[0]))  # note reverse
            peaks_with_score = [x + (map_ori[x[1], x[0]], ) for x in peaks]
            peak_id = range(peak_counter, peak_counter + len(peaks))
            peaks_with_score_and_id = [
                peaks_with_score[i] + (peak_id[i], )
                for i in range(len(peak_id))
            ]

            all_peaks.append(peaks_with_score_and_id)
            peak_counter += len(peaks)

        # find connection in the specified sequence, center 29 is in the position 15
        limbSeq = [[2, 3], [2, 6], [3, 4], [4, 5], [6, 7], [7, 8], [2, 9], [9, 10], \
                   [10, 11], [2, 12], [12, 13], [13, 14], [2, 1], [1, 15], [15, 17], \
                   [1, 16], [16, 18], [3, 17], [6, 18]]
        # the middle joints heatmap correpondence
        mapIdx = [[31, 32], [39, 40], [33, 34], [35, 36], [41, 42], [43, 44], [19, 20], [21, 22], \
                  [23, 24], [25, 26], [27, 28], [29, 30], [47, 48], [49, 50], [53, 54], [51, 52], \
                  [55, 56], [37, 38], [45, 46]]

        connection_all = []
        special_k = []
        mid_num = 10

        for k in range(len(mapIdx)):
            score_mid = paf_avg[:, :, [x - 19 for x in mapIdx[k]]]
            candA = all_peaks[limbSeq[k][0] - 1]
            candB = all_peaks[limbSeq[k][1] - 1]
            nA = len(candA)
            nB = len(candB)
            indexA, indexB = limbSeq[k]
            if (nA != 0 and nB != 0):
                connection_candidate = []
                for i in range(nA):
                    for j in range(nB):
                        vec = np.subtract(candB[j][:2], candA[i][:2])
                        norm = math.sqrt(vec[0] * vec[0] + vec[1] * vec[1])
                        vec = np.divide(vec, norm)

                        startend = list(zip(np.linspace(candA[i][0], candB[j][0], num=mid_num), \
                                            np.linspace(candA[i][1], candB[j][1], num=mid_num)))

                        vec_x = np.array([score_mid[int(round(startend[I][1])), int(round(startend[I][0])), 0] \
                                          for I in range(len(startend))])
                        vec_y = np.array([score_mid[int(round(startend[I][1])), int(round(startend[I][0])), 1] \
                                          for I in range(len(startend))])

                        score_midpts = np.multiply(
                            vec_x, vec[0]) + np.multiply(vec_y, vec[1])
                        score_with_dist_prior = sum(score_midpts) / len(
                            score_midpts) + min(
                                0.5 * oriImg.shape[0] / norm - 1, 0)
                        criterion1 = len(np.nonzero(
                            score_midpts > thre2)[0]) > 0.8 * len(score_midpts)
                        criterion2 = score_with_dist_prior > 0
                        if criterion1 and criterion2:
                            connection_candidate.append([
                                i, j, score_with_dist_prior,
                                score_with_dist_prior + candA[i][2] +
                                candB[j][2]
                            ])

                connection_candidate = sorted(connection_candidate,
                                              key=lambda x: x[2],
                                              reverse=True)
                connection = np.zeros((0, 5))
                for c in range(len(connection_candidate)):
                    i, j, s = connection_candidate[c][0:3]
                    if (i not in connection[:, 3]
                            and j not in connection[:, 4]):
                        connection = np.vstack(
                            [connection, [candA[i][3], candB[j][3], s, i, j]])
                        if (len(connection) >= min(nA, nB)):
                            break

                connection_all.append(connection)
            else:
                special_k.append(k)
                connection_all.append([])

        # last number in each row is the total parts number of that person
        # the second last number in each row is the score of the overall configuration
        subset = -1 * np.ones((0, 20))
        candidate = np.array(
            [item for sublist in all_peaks for item in sublist])

        for k in range(len(mapIdx)):
            if k not in special_k:
                partAs = connection_all[k][:, 0]
                partBs = connection_all[k][:, 1]
                indexA, indexB = np.array(limbSeq[k]) - 1

                for i in range(len(connection_all[k])):  # = 1:size(temp,1)
                    found = 0
                    subset_idx = [-1, -1]
                    for j in range(len(subset)):  # 1:size(subset,1):
                        if subset[j][indexA] == partAs[i] or subset[j][
                                indexB] == partBs[i]:
                            subset_idx[found] = j
                            found += 1

                    if found == 1:
                        j = subset_idx[0]
                        if subset[j][indexB] != partBs[i]:
                            subset[j][indexB] = partBs[i]
                            subset[j][-1] += 1
                            subset[j][-2] += candidate[
                                partBs[i].astype(int),
                                2] + connection_all[k][i][2]
                    elif found == 2:  # if found 2 and disjoint, merge them
                        j1, j2 = subset_idx
                        membership = ((subset[j1] >= 0).astype(int) +
                                      (subset[j2] >= 0).astype(int))[:-2]
                        if len(np.nonzero(membership == 2)[0]) == 0:  # merge
                            subset[j1][:-2] += (subset[j2][:-2] + 1)
                            subset[j1][-2:] += subset[j2][-2:]
                            subset[j1][-2] += connection_all[k][i][2]
                            subset = np.delete(subset, j2, 0)
                        else:  # as like found == 1
                            subset[j1][indexB] = partBs[i]
                            subset[j1][-1] += 1
                            subset[j1][-2] += candidate[
                                partBs[i].astype(int),
                                2] + connection_all[k][i][2]

                    # if find no partA in the subset, create a new subset
                    elif not found and k < 17:
                        row = -1 * np.ones(20)
                        row[indexA] = partAs[i]
                        row[indexB] = partBs[i]
                        row[-1] = 2
                        row[-2] = sum(
                            candidate[connection_all[k][i, :2].astype(int),
                                      2]) + connection_all[k][i][2]
                        subset = np.vstack([subset, row])
        # delete some rows of subset which has few parts occur
        deleteIdx = []
        for i in range(len(subset)):
            if subset[i][-1] < 4 or subset[i][-2] / subset[i][-1] < 0.4:
                deleteIdx.append(i)
        subset = np.delete(subset, deleteIdx, axis=0)

        # subset: n*20 array, 0-17 is the index in candidate, 18 is the total score, 19 is the total parts
        # candidate: x, y, score, id
        return candidate, subset
コード例 #42
0
def nonMaximumSuppression(param,
                          heatmaps,
                          upsampFactor=1.,
                          bool_refine_center=True,
                          bool_gaussian_filt=False):
    """
    NonMaximaSuppression: find peaks (local maxima) in a set of grayscale images
    :param heatmaps: set of grayscale images on which to find local maxima (3d np.array,
    with dimensions image_height x image_width x num_heatmaps)
    :param upsampFactor: Size ratio between CPM heatmap output and the input image size.
    Eg: upsampFactor=16 if original image was 480x640 and heatmaps are 30x40xN
    :param bool_refine_center: Flag indicating whether:
     - False: Simply return the low-res peak found upscaled by upsampFactor (subject to grid-snap)
     - True: (Recommended, very accurate) Upsample a small patch around each low-res peak and
     fine-tune the location of the peak at the resolution of the original input image
    :param bool_gaussian_filt: Flag indicating whether to apply a 1d-GaussianFilter (smoothing)
    to each upsampled patch before fine-tuning the location of each peak.
    :return: a NUM_JOINTS x 4 np.array where each row represents a joint type (0=nose, 1=neck...)
    and the columns indicate the {x,y} position, the score (probability) and a unique id (counter)
    """
    # MODIFIED BY CARLOS: Instead of upsampling the heatmaps to heatmap_avg and
    # then performing NMS to find peaks, this step can be sped up by ~25-50x by:
    # (9-10ms [with GaussFilt] or 5-6ms [without GaussFilt] vs 250-280ms on RoG
    # 1. Perform NMS at (low-res) CPM's output resolution
    # 1.1. Find peaks using scipy.ndimage.filters.maximum_filter
    # 2. Once a peak is found, take a patch of 5x5 centered around the peak, upsample it, and
    # fine-tune the position of the actual maximum.
    #  '-> That's equivalent to having found the peak on heatmap_avg, but much faster because we only
    #      upsample and scan the 5x5 patch instead of the full (e.g.) 480x640

    joint_list_per_joint_type = []
    cnt_total_joints = 0
    # For every peak found, win_size specifies how many pixels in each
    # direction from the peak we take to obtain the patch that will be
    # upsampled. Eg: win_size=1 -> patch is 3x3; win_size=2 -> 5x5
    # (for BICUBIC interpolation to be accurate, win_size needs to be >=2!)

    win_size = 2
    for joint in range(NUM_JOINTS):
        map_orig = heatmaps[:, :, joint]
        peak_coords = find_peaks(param, map_orig)
        peaks = np.zeros((len(peak_coords), 4))

        for i, peak in enumerate(peak_coords):
            if bool_refine_center:
                x_min, y_min = np.maximum(0, peak - win_size)
                x_max, y_max = np.minimum(
                    np.array(map_orig.T.shape) - 1, peak + win_size)
                # Take a small patch around each peak and only upsample that
                # tiny region

                patch = map_orig[y_min:y_max + 1, x_min:x_max + 1]
                map_upsamp = cv2.resize(patch,
                                        None,
                                        fx=upsampFactor,
                                        fy=upsampFactor,
                                        interpolation=cv2.INTER_CUBIC)

                # Gaussian filtering takes an average of 0.8ms/peak (and there might be
                # more than one peak per joint!) -> For now, skip it (it's
                # accurate enough)
                map_upsamp = gaussian_filter(
                    map_upsamp, sigma=3) if bool_gaussian_filt else map_upsamp
                # Obtain the coordinates of the maximum value in the patch
                location_of_max = np.unravel_index(map_upsamp.argmax(),
                                                   map_upsamp.shape)
                # Remember that peaks indicates [x,y] -> need to reverse it for
                # [y,x]

                location_of_patch_center = compute_resized_coords(
                    peak[::-1] - [y_min, x_min], upsampFactor)
                # Calculate the offset wrt to the patch center where the actual
                # maximum is

                refined_center = (location_of_max - location_of_patch_center)
                peak_score = map_upsamp[location_of_max]
            else:
                refined_center = [0, 0]
                # Flip peak coordinates since they are [x,y] instead of [y,x]
                peak_score = map_orig[tuple(peak[::-1])]
            peaks[i, :] = tuple([
                int(round(x))
                for x in compute_resized_coords(peak_coords[i], upsampFactor) +
                refined_center[::-1]
            ]) + (peak_score, cnt_total_joints)
            cnt_total_joints += 1
        joint_list_per_joint_type.append(peaks)

    return joint_list_per_joint_type
コード例 #43
0
def compute_angle(input_filepath, alpha, beta, method, new_centerlines,
                  region_of_interest, region_points, projection=False):
    """
    Primary collection of methods for computing the angle of a vessel bend.
    Three main methods are currently implemented:
    1) ODR methods: odrline
    2) Tracing point methods: maxcurv, smooth, discrete, frac, MISR
    3) Relative tracing point methods: plane, itplane, itplane_clip

    Args:
        input_filepath (str): Path to case folder.
        alpha (float): Extension / Compression factor in vertical direction.
        beta (float): Extension / Compression factor in horizontal direction.
        method (str): Method used to compute angle.
        new_centerlines (vtkPolyData): New centerline.
        region_of_interest (str): Method for setting the region of interest ['manual' | 'commandline' | 'landmarking']
        region_points (list): If region_of_interest is 'commandline', this a flatten list of the start and endpoint
        projection (bool): True / False for computing 2D / 3D angle.

    Returns:
        new_deg (float): New angle of a vessel bend from a manipulated centerline.
    Returns:
        deg (float): Old angle of a vessel bend from a manipulated centerline.
    """
    # Get base path
    base_path = get_path_names(input_filepath)

    # Centerline path
    centerline_path = base_path + "_centerline.vtp"

    # Clean and capp / uncapp surface
    surface, capped_surface = prepare_surface(base_path, input_filepath)

    # Extract old centerline
    if not path.exists(centerline_path):

        # Compute centerlines
        inlet, outlets = get_inlet_and_outlet_centers(surface, base_path)

        print("-- Compute centerlines and Voronoi diagram")
        centerlines, _, _ = compute_centerlines(inlet, outlets, centerline_path,
                                                capped_surface, resampling=0.1,
                                                smooth=False, base_path=base_path)
    else:
        centerlines = read_polydata(centerline_path)

    # Get region of interest
    point_path = base_path + "_anterior_bend.particles"
    if region_of_interest == "landmarking":
        if not path.exists(point_path):
            raise RuntimeError(("The given .particles file: %s does not exist. Please run" +
                                " landmarking with automated_landmarking.py first.") % point_path)
        region_points = np.loadtxt(point_path)
    else:
        _, _, _, region_points, _ = get_line_to_change(capped_surface, centerlines,
                                                       region_of_interest, "bend", region_points, 0)
        region_points = [[region_points[3 * i], region_points[3 * i + 1], region_points[3 * i + 2]]
                         for i in range(len(region_points) // 3)]
    p1 = region_points[0]
    p2 = region_points[1]

    if new_centerlines is None:
        centerlines, new_centerlines = get_new_centerlines(centerlines, region_points, alpha, beta, p1, p2)

    # Get new siphon and prepare
    id1, id2, moved_id1, moved_id2, moved_p1, moved_p2 = get_moved_siphon(new_centerlines, centerlines, p1, p2)

    # Extract region of interest
    siphon = extract_single_line(centerlines, 1, start_id=id1, end_id=id2)
    moved_siphon = extract_single_line(new_centerlines, 0, start_id=moved_id1, end_id=moved_id2)
    id1, id2 = 0, siphon.GetNumberOfPoints() - 1
    moved_id1, moved_id2 = 0, moved_siphon.GetNumberOfPoints() - 1

    if method in ["maxcurv", "odrline", "smooth", "frac"]:
        nknots = 11
        siphon_splined, siphon_curv = compute_splined_centerline(siphon, get_curv=True, isline=True, nknots=nknots,
                                                                 get_misr=False)

        _, moved_siphon_curv = compute_splined_centerline(moved_siphon, get_curv=True, isline=True, nknots=nknots,
                                                          get_misr=False)
        siphon_curv = resample(siphon_curv, siphon_splined.GetNumberOfPoints())
        cutcurv = siphon_curv[id1:id2]
        newcutcurv = moved_siphon_curv[moved_id1:moved_id2]

    if method == "discrete":
        # Smooth line with discrete derivatives
        neigh = 30
        _, curv_d = compute_discrete_derivatives(siphon, neigh=neigh)
        _, newcurv_d = compute_discrete_derivatives(moved_siphon, neigh=neigh)
        cutcurv_d = curv_d[id1:id2]
        newcutcurv_d = newcurv_d[moved_id1:moved_id2]

    if method == "MISR":
        # Map MISR values to old and new splined anterior bend
        anterior_bend = extract_single_line(centerlines, 0, start_id=id1, end_id=id2)
        m = anterior_bend.GetNumberOfPoints()
        m1 = moved_siphon.GetNumberOfPoints()
        misr_array = get_vtk_array(radiusArrayName, 1, m)
        newmisr_array = get_vtk_array(radiusArrayName, 1, m1)
        misr_list = []
        for i in range(m):
            misr = anterior_bend.GetPointData().GetArray(radiusArrayName).GetTuple(i)
            misr_list.append(misr[0])
            misr_array.SetTuple(i, misr)

        misr_list = resample(misr_list, m1)
        for i in range(m1):
            newmisr_array.SetTuple(i, (misr_list[i],))

        siphon.GetPointData().AddArray(misr_array)
        moved_siphon.GetPointData().AddArray(newmisr_array)

    # Get direction to the point furthest away (dx)
    direction = "vertical"
    clipping_points_vtk = vtk.vtkPoints()
    for point in [p1, p2]:
        clipping_points_vtk.InsertNextPoint(point)
    middle_points, _, dx = get_direction_parameters(extract_single_line(centerlines, 0), 0.1, direction,
                                                    clipping_points_vtk)

    # Find adjusted clipping points (and tracing points)
    if method == "plane":
        _, max_id = get_most_distant_point(dx, siphon)
        _, newmax_id = get_most_distant_point(dx, moved_siphon)

    elif method in ["itplane", "itplane_clip"]:
        _, max_id = get_most_distant_point(dx, siphon)
        _, newmax_id = get_most_distant_point(dx, moved_siphon)

        siphon = vmtk_compute_geometric_features(siphon, False)

        frenet_t1 = get_point_data_array("FrenetTangent", siphon, k=3)
        frenet_t2 = get_point_data_array("FrenetTangent", moved_siphon, k=3)

        p1_1, p1_id = get_closest_point(frenet_t1[-1], 0, max_id, p2, siphon)
        p2_2, p2_id = get_closest_point(frenet_t1[0], max_id, siphon.GetNumberOfPoints(), p1, siphon)

        newp1, np1_id = get_closest_point(frenet_t2[-1], 0, newmax_id, moved_p2, moved_siphon)
        newp2, np2_id = get_closest_point(frenet_t2[0], newmax_id,
                                          moved_siphon.GetNumberOfPoints(), moved_p1,
                                          moved_siphon)

        n1 = get_point_data_array("FrenetBinormal", siphon, k=3)[p1_id]
        n2 = get_point_data_array("FrenetBinormal", moved_siphon, k=3)[np1_id]

        dp = p1_1 - p2_2
        dnewp = newp1 - newp2

        normal = np.cross(dp, n1)
        newnormal = np.cross(dnewp, n2)

        _, max_id = get_most_distant_point(normal, siphon)
        _, newmax_id = get_most_distant_point(newnormal, moved_siphon)

    elif method == "maxcurv":
        max_id, _ = max(enumerate(cutcurv), key=operator.itemgetter(1))
        newmax_id, _ = max(enumerate(newcutcurv), key=operator.itemgetter(1))

    elif method == "smooth":
        allmaxcurv = argrelextrema(cutcurv, np.greater)[0]
        allnewmaxcurv = argrelextrema(newcutcurv, np.greater)[0]

        tmpcurv = cutcurv
        while len(allmaxcurv) > 2:
            tmpcurv = gaussian_filter(tmpcurv, 2)
            allmaxcurv = argrelextrema(tmpcurv, np.greater)[0]

        tmpnewcurv = newcutcurv
        while len(allnewmaxcurv) > 2:
            tmpnewcurv = gaussian_filter(tmpnewcurv, 2)
            allnewmaxcurv = argrelextrema(tmpnewcurv, np.greater)[0]

        max_id = allmaxcurv[0]
        newmax_id = allnewmaxcurv[0]

    elif method == "discrete":
        max_id, _ = max(enumerate(cutcurv_d), key=operator.itemgetter(1))
        newmax_id, _ = max(enumerate(newcutcurv_d), key=operator.itemgetter(1))

    elif method == "maxdist":
        norm_p1 = [la.norm(np.array(p1) - np.array(siphon.GetPoint(i))) for i in range(siphon.GetNumberOfPoints())]
        norm_p2 = [la.norm(np.array(p2) - np.array(siphon.GetPoint(i))) for i in
                   range(siphon.GetNumberOfPoints() - 1, -1, -1)]
        max_id = 0
        max_dist = 0
        for i, n1 in enumerate(norm_p1):
            for n2 in norm_p2:
                dist = n1 ** 2 + n2 ** 2
                if dist > max_dist:
                    max_dist = dist
                    max_id = i

        newnorm_p1 = [la.norm(np.array(moved_p1) - np.array(moved_siphon.GetPoint(i))) for i in
                      range(moved_siphon.GetNumberOfPoints())]
        newnorm_p2 = [la.norm(np.array(moved_p2) - np.array(moved_siphon.GetPoint(i))) for i in
                      range(moved_siphon.GetNumberOfPoints() - 1, -1, -1)]
        newmax_id = 0
        new_max_dist = 0
        for i, n1 in enumerate(newnorm_p1):
            for j, n2 in enumerate(newnorm_p2):
                dist = n1 ** 2 + n2 ** 2
                if dist > new_max_dist:
                    new_max_dist = dist
                    newmax_id = i

    # Compute angles based on the classic formula for
    # angle between vectors in 3D
    if method == "odrline":
        limits = ["cumulative", "sd"]
        for limit in limits:
            d1, d2, _ = odr_line(id1, id2, siphon_splined, siphon_curv, limit)
            newd1, newd2, _ = odr_line(moved_id1, moved_id2, moved_siphon, moved_siphon_curv, limit)

            deg = find_angle_odr(d1, d2, projection)
            new_deg = find_angle_odr(newd1, newd2, projection)

    elif method == "MISR":
        multiplier = 1.5
        n1 = siphon.GetNumberOfPoints()
        n2 = moved_siphon.GetNumberOfPoints()
        rad1 = siphon.GetPointData().GetArray(radiusArrayName).GetTuple1(0)
        rad2 = siphon.GetPointData().GetArray(radiusArrayName).GetTuple1(n1 - 1)
        newrad1 = moved_siphon.GetPointData().GetArray(radiusArrayName).GetTuple1(0)
        newrad2 = moved_siphon.GetPointData().GetArray(radiusArrayName).GetTuple1(n2 - 1)

        pa, _ = move_past_sphere(siphon, p1, rad1, 0, step=1, stop=n1 - 1, scale_factor=multiplier)
        pb, _ = move_past_sphere(siphon, p2, rad2, n1 - 1, step=-1, stop=0, scale_factor=multiplier)
        new_pa, _ = move_past_sphere(moved_siphon, moved_p1, newrad1, 0, step=1, stop=n2 - 1, scale_factor=multiplier)
        new_pb, _ = move_past_sphere(moved_siphon, moved_p2, newrad2, n2 - 1, step=-1, stop=0, scale_factor=multiplier)

        deg, l1, _ = find_angle(pa, pb, p1, p2, projection)
        new_deg, nl1, _ = find_angle(new_pa, new_pb, moved_p1, moved_p2, projection)

    else:
        if method == "frac":
            n_values = [5]
            left = [2]
            right = [3]
            i = 0
            dx = 1. / n_values[i]
            ida = int(id1 + (id2 - id1) * left[i] * dx)
            idb = int(id1 + (id2 - id1) * right[i] * dx)
            pa = siphon_splined.GetPoints().GetPoint(ida)
            pb = siphon_splined.GetPoints().GetPoint(idb)

            ida = int(moved_id1 + (moved_id2 - moved_id1) * left[i] * dx)
            idb = int(moved_id1 + (moved_id2 - moved_id1) * right[i] * dx)
            new_pa = moved_siphon.GetPoints().GetPoint(ida)
            new_pb = moved_siphon.GetPoints().GetPoint(idb)

            deg, l1, l2 = find_angle(pa, pb, p1, p2, projection)
            new_deg, nl1, nl2 = find_angle(new_pa, new_pb, moved_p1, moved_p2, projection)

        elif method in ["plane", "itplane", "itplane_clip", "maxcurv", "smooth",
                        "discrete", "maxdist"]:
            frac = 4. / 5.
            if method == "itplane_clip":
                id_mid = (p2_id - p1_id) / 2.
                new_id_mid = (np2_id - np1_id) / 2.
                if max_id > id_mid:
                    ida = int((max_id - p1_id) * frac)
                    idb = int((max_id - p1_id) * (1 + (1 - frac)))
                    pa = siphon.GetPoints().GetPoint(ida + p1_id)
                    pb = siphon.GetPoints().GetPoint(idb + p1_id)
                else:
                    idb = int((p2_id - max_id) * (1 + (1 - frac)))
                    ida = int((p2_id - max_id) * frac)
                    pa = siphon.GetPoints().GetPoint(ida)
                    pb = siphon.GetPoints().GetPoint(idb)

                if newmax_id > new_id_mid:
                    ida = int((newmax_id - np1_id) * frac)
                    idb = int((newmax_id - np1_id) * (1 + (1 - frac)))
                    new_pa = moved_siphon.GetPoints().GetPoint(ida + np1_id)
                    new_pb = moved_siphon.GetPoints().GetPoint(idb + np1_id)
                else:
                    ida = int((np2_id - newmax_id) * frac)
                    idb = int((np2_id - newmax_id) * (1 + (1 - frac)))
                    new_pa = moved_siphon.GetPoints().GetPoint(ida)
                    new_pb = moved_siphon.GetPoints().GetPoint(idb)

                deg, l1, l2 = find_angle(pa, pb, p1, p2, projection)
                new_deg, nl1, nl2 = find_angle(new_pa, new_pb, moved_p1, moved_p2, projection)

            else:
                ida = int(max_id * frac)
                idb = int(max_id * (1 + (1 - frac)))
                pa = siphon.GetPoints().GetPoint(ida)
                pb = siphon.GetPoints().GetPoint(idb)

                ida = int(newmax_id * frac)
                idb = int(newmax_id * (1 + (1 - frac)))
                new_pa = moved_siphon.GetPoints().GetPoint(ida)
                new_pb = moved_siphon.GetPoints().GetPoint(idb)

                deg, l1, l2 = find_angle(pa, pb, p1, p2, projection)
                new_deg, nl1, nl2 = find_angle(new_pa, new_pb, moved_p1, moved_p2, projection)

    return new_deg, deg
コード例 #44
0
def gaussianblur(img, sigma):
	return filters.gaussian_filter(img,sigma)
コード例 #45
0
def postprocess(mask_out, images_train, scribbles_train=None):
    '''
    Postprocesses predictions of CNN to create ground truths for recursion
    :param data: Data of this recursion - e.g. if given data file for recursion n,
                 It will set up ground truths to be random walked for recursion n
    :param images_train: Numpy array of training images
    :param scribbles_train: Numpy array of weakly annotated images
    :return:
'''

    #get labels present
    labels = np.unique(scribbles_train)
    labels = labels[labels != 0]

    # use full segmentation of random walker as upper bound
    if exp_config.rw_intersection:
        rw_segmentation = random_walker.segment(images_train,
                                                scribbles_train,
                                                threshold=0,
                                                beta=exp_config.rw_beta)
        mask = mask_out[:]
        mask_out = np.zeros_like(mask_out)
        for label in labels:
            indices = (rw_segmentation == label)
            indices &= (mask == label)
            mask_out[indices] = label

    #revert to original random walked data for 'bad' prediction
    if exp_config.rw_reversion:
        mask = mask_out[:]
        mask_out = np.zeros_like(mask_out)
        for img_id in range(exp_config.batch_size):
            for label in labels:
                if np.sum(mask[img_id, ...] == label) < np.sum(
                        scribbles_train[img_id, ...] == label):
                    #If the prediction has predicted less than the original scribble, revert to
                    #the scribble
                    mask_out[img_id, scribbles_train[img_id,
                                                     ...] == label] = label
                else:
                    mask_out[img_id, mask[img_id, ...] == label] = label

    #keep only largest cluster for output
    if exp_config.keep_largest_cluster:
        for img_id in range(exp_config.batch_size):
            mask_out[img_id,
                     ...] = image_utils.keep_largest_connected_components(
                         np.squeeze(mask_out[img_id, ...]))

    if exp_config.smooth_edges:
        labels = labels[labels != np.max(labels)]
        for img_id in range(exp_config.batch_size):
            mask = mask_out[img_id, ...]
            new_mask = np.zeros_like(mask)
            for label in labels:
                struct = (mask == label).astype(np.float)
                blurred_struct = gaussian_filter(
                    struct, sigma=exp_config.edge_smoother_sigma)
                # ax = fig.add_subplot(161 + label)
                blurred_struct[
                    blurred_struct >= exp_config.edge_smoother_threshold] = 1
                blurred_struct[
                    blurred_struct < exp_config.edge_smoother_threshold] = 0
                new_mask[blurred_struct != 0] = label
            mask_out[img_id, ...] = new_mask

    return mask_out
コード例 #46
0
def apply_model(oriImg, model, multiplier,numPoints,roi_str):
    stride = 8
    roiPoint = roi_str.split('_')
    newImg = oriImg[int(roiPoint[0]):int(roiPoint[2]), int(roiPoint[1]):int(roiPoint[3])]

    height, width, _ = newImg.shape
    oriImg = newImg
    #height, width, _ = oriImg.shape
    #将图像转化成数组形式,类型是float32
    normed_img = np.array(oriImg, dtype=np.float32)
    #新建一个大小一样的0矩阵(图),另外这个图的通道数就是代预测点的个数
    heatmap_avg = np.zeros((oriImg.shape[0], oriImg.shape[1], numPoints), dtype=np.float32)
    #遍历尺度因数
    for m in range(len(multiplier)):
        scale = multiplier[m]
        imageToTest = cv2.resize(normed_img, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
        # imgToTest_padded, pad = util.padRightDownCorner(imageToTest, stride, 128)
        #将输入图片补全成标准大小(可能是384*384)
        imgToTest_padded, pad = util.padRightDownCorner(imageToTest, 32, 128)

        input_img = np.transpose(np.float32(imgToTest_padded[:, :, :, np.newaxis]),
                                 (3, 2, 0, 1)) / 255 - 0.5  # required shape (1, c, h, w)

        input_var = torch.autograd.Variable(torch.from_numpy(input_img).cuda())

        # get the features
        # heat1, heat2, heat3, heat4, heat5, heat6 = model(input_var)
        #怎么看是经历了几个阶段的特诊图
        heat = model(input_var)

        # get the heatmap
        heatmap = heat.data.cpu().numpy()
        heatmap = np.transpose(np.squeeze(heatmap), (1, 2, 0))  # (h, w, c)
        heatmap = cv2.resize(heatmap, (0, 0), fx=stride, fy=stride, interpolation=cv2.INTER_CUBIC)
        heatmap = heatmap[:imgToTest_padded.shape[0] - pad[2], :imgToTest_padded.shape[1] - pad[3], :]
        heatmap = cv2.resize(heatmap, (width, height), interpolation=cv2.INTER_CUBIC)
        heatmap_avg = heatmap_avg + heatmap / len(multiplier)

    all_peaks = []  # all of the possible points by classes.
    peak_counter = 0
    thre1 = 0.1
    for part in range(numPoints - 1):
        x_list = []
        y_list = []
        map_ori = heatmap_avg[:, :, part]
        map = gaussian_filter(map_ori, sigma=3)

        map_left = np.zeros(map.shape)
        map_left[1:, :] = map[:-1, :]
        map_right = np.zeros(map.shape)
        map_right[:-1, :] = map[1:, :]
        map_up = np.zeros(map.shape)
        map_up[:, 1:] = map[:, :-1]
        map_down = np.zeros(map.shape)
        map_down[:, :-1] = map[:, 1:]

        peaks_binary = np.logical_and.reduce(
            (map >= map_left, map >= map_right, map >= map_up, map >= map_down, map > thre1))
        peaks = list(zip(np.nonzero(peaks_binary)[1], np.nonzero(peaks_binary)[0])) # note reverse
        peaks_with_score = [x + (map_ori[x[1], x[0]],) for x in peaks]
        id = range(peak_counter, peak_counter + len(peaks))
        peaks_with_score_and_id = [peaks_with_score[i] + (id[i],) for i in range(len(id))]

        all_peaks.append(peaks_with_score_and_id)
        peak_counter += len(peaks)

    # sort by score
    for i in range(numPoints-1):
        all_peaks[i] = sorted(all_peaks[i], key=lambda ele : ele[2],reverse = True)

    keypoints = -1*np.ones((numPoints-1, 3))
    for i in range(numPoints-1):
        if len(all_peaks[i]) == 0:
            continue
        else:
            keypoints[i,0], keypoints[i,1], keypoints[i,2] = all_peaks[i][0][0], all_peaks[i][0][1], 1
    keypoints[:,0] =  keypoints[:,0] + int(roiPoint[1])
    keypoints[:,1] =  keypoints[:,1] + int(roiPoint[0])
    return  keypoints
コード例 #47
0
ファイル: align.py プロジェクト: lukasz-migas/pySPM
def AutoShift(Ref, Img, Delta = 50, shift=(0,0), step=5, gauss=5, mean=True, test=False, norm=False, normData=False):
    """Function to find the best shift between two images by using brute force
    It shift the two iumages and calculate a difference score between the two.
    The function will return the shift which gives the lowerst score (least difference)
    The score is the norm of the difference between the two images where all non-overlaping parts of the images
    due to the shifts are set to 0. The score is then normes by the effective area.
    In order to avoid the errors due to shot-noise, the images are gaussian blured.
  
    Delta: shifts between shift[0/1]-Delta and shift[0/1]+Delta will be tested
    step: The step between tested delta values
    gauss: For noisy image it is better to use a gaussian filter in order to improve the score accuracy.
           The value is the gaussian size.
    mean: If True, the mean value of each image are subtracted. This is prefered when the intensities of the two images does not match perfectly.
          Set it to False if you know that the intensities of your two images are identical
    Note: This function was developed as the maximum in FFT correlation does not seems to give very acurate
          result for images with low counts. If the shifts is expected to be big, the first guess shift can be calculated
          by FFT correlation. ie.:
          s = np.fft.fftshift( np.abs( np.fft.ifft2( np.fft.fft2(Reference) * np.conj(np.fft.fft2(Image)))))
          shift = [x-s.shape[i]/2 for i,x in enumerate(np.unravel_index(np.argmax(s), s.shape))]
    """
    assert Ref.shape[0] <= Img.shape[0]
    assert Ref.shape[1] <= Img.shape[1]
    if mean:
        Ref = Ref - np.mean(Ref)
        Img = Img - np.mean(Img)
    if normData:
       assert np.std(Ref)>0
       assert np.std(Img)>0
       Ref /= np.std(Ref)
       Img /= np.std(Img)
    # blur the images for score calculation
    if gauss in [0,None,False]:
        im1 = Ref
        im2 = Img
    else:
        im1 = gaussian_filter( Ref, gauss)
        im2 = gaussian_filter( Img, gauss)
        
    # the following two variables save the best score
    best = (0,0)
    Dbest = Ref.shape[0]*Ref.shape[1]*max(np.max(im2),np.max(im1))
    
    tested = np.zeros((int(2*Delta/step)+1,int(2*Delta/step)+1))
    # Sweep through all possible shifts (brute force)
    for iy,Dy in enumerate(np.arange(shift[1]-Delta, shift[1]+Delta+1, step)):
        dy = int(Dy)
        for ix,Dx in enumerate(np.arange(shift[0]-Delta, shift[0]+Delta+1, step)):
            dx = int(Dx)
            corr2 = ApplyShift(im2, (dx,dy))
            DSX = Img.shape[1] - Ref.shape[1]
            DSY = Img.shape[0] - Ref.shape[0]
            # Create a copy of the reference and erase parts which are not overlaping with img
            Or = np.copy(im1)
            if dy < 0:
                Or[:-dy, :] = 0
            elif DSY-dy < 0:
                Or[DSY-dy:, :] = 0
            if dx > 0:
                Or[:, :dx] = 0
            elif DSX+dx < 0:
                Or[:, dx+DSX:] = 0
            corr2 = corr2[:Ref.shape[0],:Ref.shape[1]]
            # calculate the score: absolute of the differendces normed by the overlaping area
            D = np.sum(np.abs( Or - corr2 ))
            if norm:
                D /= ((Ref.shape[0]-2*dy)*(Ref.shape[1]-2*dx))
            if test:
                tested[iy,ix] = D
            if D < Dbest:
                Dbest = D
                best = (dx,dy)
    if test:
        return best, Dbest, tested
    return best, Dbest
コード例 #48
0
import os
import numpy as np
from keras.models import load_model
from scipy.misc import imread, imresize
from scipy.ndimage.filters import gaussian_filter

model = load_model("saved_steps/weights-improvement-07-0.78.hdf5")
directory = "/home/alexander/PycharmProjects/car_detection/brand_image/validation/"

for path in os.listdir(directory):
    x = imread(directory + path)
    x = gaussian_filter(x, 3)
    x = imresize(x, (224, 224))
    x = np.reshape(x, [1, 224, 224, 3])
    x = x / 255
    print(path)
    print(
        sorted(list(enumerate(model.predict(x)[0].tolist())),
               key=lambda item: item[1])[-1][0])
    print(model.predict(x))
コード例 #49
0
def plot(projection, x, y, z, args):
    lat, lon = get_latlon_coords(projection, x, y, z)
    if len(lat.shape) == 1:
        lat, lon = np.meshgrid(lat, lon)

    dataset_name = args.get('dataset')
    config = DatasetConfig(dataset_name)
    variable = args.get('variable')

    variable = variable.split(',')

    depth = args.get('depth')

    scale = args.get('scale')
    scale = [float(component) for component in scale.split(',')]

    time = args.get('time')

    data = []
    with open_dataset(config, variable=variable, timestamp=time) as dataset:

        for v in variable:
            data.append(dataset.get_area(
                np.array([lat, lon]),
                depth,
                time,
                v,
                args.get('interp'),
                args.get('radius'),
                args.get('neighbours')
            ))

        vc = config.variable[dataset.variables[variable[0]]]
        variable_name = vc.name
        variable_unit = vc.unit
        cmap = colormap.find_colormap(variable_name)

        if depth != 'bottom':
            depthm = dataset.depths[depth]
        else:
            depthm = 0

    if len(data) == 1:
        data = data[0]

    if len(data) == 2:
        data = np.sqrt(data[0] ** 2 + data[1] ** 2)
        cmap = colormap.colormaps.get('speed')

    data = data.transpose()
    xpx = x * 256
    ypx = y * 256

    # Mask out any topography if we're below the vector-tile threshold
    if z < 8:
        with Dataset(current_app.config['ETOPO_FILE'] % (projection, z), 'r') as dataset:
            bathymetry = dataset["z"][ypx:(ypx + 256), xpx:(xpx + 256)]

        bathymetry = gaussian_filter(bathymetry, 0.5)

        data[np.where(bathymetry > -depthm)] = np.ma.masked

    sm = matplotlib.cm.ScalarMappable(
        matplotlib.colors.Normalize(vmin=scale[0], vmax=scale[1]), cmap=cmap)

    img = sm.to_rgba(np.ma.masked_invalid(np.squeeze(data)))
    im = Image.fromarray((img * 255.0).astype(np.uint8))

    buf = BytesIO()
    im.save(buf, format='PNG', optimize=True)
    return buf
コード例 #50
0
    if not (success1 and success2):
        continue

    else:
        image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
        image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)

        image1 = imresize(Image.fromarray(image1))
        image2 = imresize(Image.fromarray(image2))

        model_start_time = time.time()

        seg_map_left = model.run(image1)
        seg_map_left[seg_map_left > 0] = 255
        floatmap = seg_map_left.astype(np.float32) / 255.0
        blurmap = gaussian_filter(floatmap, sigma=3)
        cv2.imwrite("blurseg.png", blurmap * 255)

        model_ex_time = time.time() - model_start_time
        print(model_ex_time)

        resized_im_left = np.float32(image1)
        resized_im_right = np.float32(image2)

        print(resized_im_left.shape)
        print(resized_im_right.shape)

        resized_im_right = resized_im_right.copy()
        blurmap = blurmap[..., np.newaxis]

        frame = (resized_im_left * blurmap) + (resized_im_right *
コード例 #51
0
ファイル: MR.py プロジェクト: ahstewart/OASIS
def MR_other(location, mode='sos_abs', sigma_thresh=4, gauss_sigma=3, gauss_filter=False):
    print("\n-> Constructing master residual...")
    residual_list = []
    masks = []
    median_list = []
    means = []
    residuals = glob.glob("%s/residuals/*_residual_.fits" % (location))
    mResidual = glob.glob("%s/residuals/*MR.fits" % (location))
    #fill all masked values with zero
    zeros_mask(location)
    if len(mResidual) == 0:
        if len(residuals) != 0:
            if fits.getval(residuals[0], 'WEIGHT') == 'Y':
                mask_value = 1
            else:
                mask_value = 0
            for r in residuals:
                hdu = fits.open(r)
                residual_list.append(hdu[0].data)
                median_list.append(np.median(hdu[0].data))
                masks.append(hdu[1].data)
                hdu.close()
            master_residual = np.zeros(residual_list[0].shape)
            master_mask = np.zeros(residual_list[0].shape)
            for i in tqdm(range(residual_list[0].shape[0])):
                for j in range(residual_list[0].shape[1]):
                    pixels = []
                    for res in range(len(residual_list)):
                        if masks[res][i,j] == mask_value:
                            pixels.append(residual_list[res][i,j])
                    if pixels != []:
                        if mode == 'sigma_clip':
                            MR_mask_pixel = mask_value
                            median = np.median(pixels)
                            stdev = np.std(pixels)
                            if np.max(pixels) >= (median + sigma_thresh*stdev):
                                MR_pixel = np.max(pixels)
                            elif np.min(pixels) <= (median - sigma_thresh*stdev):
                                MR_pixel = np.min(pixels) * -1
                            else:
                                MR_pixel = np.median(pixels)
                        elif mode == 'sos_abs':
                            pixels = np.array(pixels)
                            MR_pixel = np.sum(pixels*abs(pixels))
                            MR_mask_pixel = mask_value
                        elif mode == 'sos':
                            pixels = np.array(pixels)
                            MR_pixel = np.sum(pixels*pixels)
                            MR_mask_pixel = mask_value
                        else:
                            print("\n-> Error: Unrecognized mode\n-> Please use either 'sos', 'sos_abs', or 'sigma_clip'\n-> Exiting...")
                            sys.exit()
                    else:
                        MR_pixel = np.median(median_list)
                        MR_mask_pixel = (mask_value-1)*-1
                    master_residual[i,j] = MR_pixel
                    master_mask[i,j] = MR_mask_pixel                    
            template = glob.glob("%s/templates/*.fits" % (location))
            if len(template) == 1:
                temp_hdu = fits.open(template[0])
                temp_header = temp_hdu[0].header
                temp_hdu.close()
                if gauss_filter == True:
                    master_residual = gaussian_filter(master_residual, gauss_sigma)
                hduData = fits.PrimaryHDU(master_residual, header=temp_header)
                hduMask = fits.ImageHDU(master_mask)
                hduList = fits.HDUList([hduData, hduMask])
                hduList.writeto("%s/residuals/MR.fits" % (location), overwrite=True)
            else:
                print("-> Error: Problem with number of template images, couldn't complete master residual construction")
    elif len(mResidual) == 1:
        print("-> Master residual already exists...")
    else:
        print("-> Error: Problem with number of master residuals")
    return means
コード例 #52
0
def main():

    ps = PlotSequence('dr3-summary')

    #fns = glob('/project/projectdirs/cosmo/data/legacysurvey/dr3.1/sweep/3.1/sweep-*.fits')
    #fns = glob('/project/projectdirs/cosmo/data/legacysurvey/dr3.1/sweep/3.1/sweep-240p005-250p010.fits')
    fns = ['dr3.1/sweep/3.1/sweep-240p005-250p010.fits',
#           '/project/projectdirs/cosmo/data/legacysurvey/dr3.1/sweep/3.1/sweep-240p010-250p015.fits',
#           '/project/projectdirs/cosmo/data/legacysurvey/dr3.1/sweep/3.1/sweep-230p005-240p010.fits',
#           '/project/projectdirs/cosmo/data/legacysurvey/dr3.1/sweep/3.1/sweep-230p010-240p015.fits'
    ]

    plt.figure(2, figsize=(6,4))
    plt.subplots_adjust(left=0.1, bottom=0.15, right=0.98, top=0.98)
    plt.figure(1)


    TT = []
    for fn in fns:
        T = fits_table(fn)
        print(len(T), 'from', fn)
        TT.append(T)
    T = merge_tables(TT)
    del TT

    print('Types:', Counter(T.type))

    T.flux_g = T.decam_flux[:,1]
    T.flux_r = T.decam_flux[:,2]
    T.flux_z = T.decam_flux[:,4]
    T.flux_ivar_g = T.decam_flux_ivar[:,1]
    T.flux_ivar_r = T.decam_flux_ivar[:,2]
    T.flux_ivar_z = T.decam_flux_ivar[:,4]
    
    T.mag_g, T.magerr_g = NanoMaggies.fluxErrorsToMagErrors(T.flux_g, T.flux_ivar_g)
    T.mag_r, T.magerr_r = NanoMaggies.fluxErrorsToMagErrors(T.flux_r, T.flux_ivar_r)
    T.mag_z, T.magerr_z = NanoMaggies.fluxErrorsToMagErrors(T.flux_z, T.flux_ivar_z)

    T.deflux_g = T.flux_g / T.decam_mw_transmission[:,1]
    T.deflux_r = T.flux_r / T.decam_mw_transmission[:,2]
    T.deflux_z = T.flux_z / T.decam_mw_transmission[:,4]
    T.deflux_ivar_g = T.flux_ivar_g * T.decam_mw_transmission[:,1]**2
    T.deflux_ivar_r = T.flux_ivar_r * T.decam_mw_transmission[:,2]**2
    T.deflux_ivar_z = T.flux_ivar_z * T.decam_mw_transmission[:,4]**2

    T.demag_g, T.demagerr_g = NanoMaggies.fluxErrorsToMagErrors(T.deflux_g, T.deflux_ivar_g)
    T.demag_r, T.demagerr_r = NanoMaggies.fluxErrorsToMagErrors(T.deflux_r, T.deflux_ivar_r)
    T.demag_z, T.demagerr_z = NanoMaggies.fluxErrorsToMagErrors(T.deflux_z, T.deflux_ivar_z)

    T.typex = np.array([t[0] for t in T.type])


    T.mag_w1, T.magerr_w1 = NanoMaggies.fluxErrorsToMagErrors(
        T.wise_flux[:,0], T.wise_flux_ivar[:,0])
    T.mag_w2, T.magerr_w2 = NanoMaggies.fluxErrorsToMagErrors(
        T.wise_flux[:,1], T.wise_flux_ivar[:,1])
    tt = 'P'
    I2 = np.flatnonzero((T.flux_ivar_g > 0) * (T.flux_ivar_r > 0) *
                        (T.flux_ivar_z > 0) *
                        (T.flux_g > 0) * (T.flux_r > 0) * (T.flux_z > 0) *
                        (T.magerr_g < 0.05) *
                        (T.magerr_r < 0.05) *
                        (T.magerr_z < 0.05) *
                        (T.typex == tt))
    print(len(I2), 'with < 0.05-mag errs and type PSF')
    plt.clf()
    plt.hist(T.mag_w1[I2], bins=100, range=[12,24], histtype='step', color='b')
    plt.hist(T.mag_w2[I2], bins=100, range=[12,24], histtype='step', color='g')
    plt.xlabel('WISE mag')
    ps.savefig()

    plt.clf()
    plt.hist(T.magerr_w1[I2], bins=100, range=[0,1], histtype='step', color='b')
    plt.hist(T.magerr_w2[I2], bins=100, range=[0,1], histtype='step', color='g')
    plt.xlabel('WISE mag errors')
    ps.savefig()

    I2 = np.flatnonzero((T.flux_ivar_g > 0) * (T.flux_ivar_r > 0) *
                        (T.flux_ivar_z > 0) *
                        (T.flux_g > 0) * (T.flux_r > 0) * (T.flux_z > 0) *
                        (T.magerr_g < 0.05) *
                        (T.magerr_r < 0.05) *
                        (T.magerr_z < 0.05) *
                        (T.typex == tt) *
                        (T.magerr_w1 < 0.05))
    #(T.magerr_w2 < 0.1) *
    print(len(I2), 'with < 0.05-mag errs and type PSF and W1 errors < 0.1')

    Q1 = fits_table('dr3.1/external/survey-dr3-DR12Q.fits')
    Q2 = fits_table('dr3.1/external/DR12Q.fits', columns=['z_pipe'])
    Q1.add_columns_from(Q2)
    I = np.flatnonzero(Q1.objid >= 0)
    Q1.cut(I)
    print(len(Q1), 'sources with DR12Q matches')
    Q = Q1
    Q.typex = np.array([t[0] for t in Q.type])
    Q.mag_w1, Q.magerr_w1 = NanoMaggies.fluxErrorsToMagErrors(
        Q.wise_flux[:,0], Q.wise_flux_ivar[:,0])
    Q.mag_w2, Q.magerr_w2 = NanoMaggies.fluxErrorsToMagErrors(
        Q.wise_flux[:,1], Q.wise_flux_ivar[:,1])
    Q.flux_g = Q.decam_flux[:,1]
    Q.flux_r = Q.decam_flux[:,2]
    Q.flux_z = Q.decam_flux[:,4]
    Q.flux_ivar_g = Q.decam_flux_ivar[:,1]
    Q.flux_ivar_r = Q.decam_flux_ivar[:,2]
    Q.flux_ivar_z = Q.decam_flux_ivar[:,4]
    Q.deflux_g = Q.flux_g / Q.decam_mw_transmission[:,1]
    Q.deflux_r = Q.flux_r / Q.decam_mw_transmission[:,2]
    Q.deflux_z = Q.flux_z / Q.decam_mw_transmission[:,4]
    Q.deflux_ivar_g = Q.flux_ivar_g * Q.decam_mw_transmission[:,1]**2
    Q.deflux_ivar_r = Q.flux_ivar_r * Q.decam_mw_transmission[:,2]**2
    Q.deflux_ivar_z = Q.flux_ivar_z * Q.decam_mw_transmission[:,4]**2
    Q.demag_g, Q.demagerr_g = NanoMaggies.fluxErrorsToMagErrors(Q.deflux_g, Q.deflux_ivar_g)
    Q.demag_r, Q.demagerr_r = NanoMaggies.fluxErrorsToMagErrors(Q.deflux_r, Q.deflux_ivar_r)
    Q.demag_z, Q.demagerr_z = NanoMaggies.fluxErrorsToMagErrors(Q.deflux_z, Q.deflux_ivar_z)
    I = np.flatnonzero((Q.flux_ivar_g > 0) * (Q.flux_ivar_r > 0) *
                        (Q.flux_ivar_z > 0) *
                        (Q.flux_g > 0) * (Q.flux_r > 0) * (Q.flux_z > 0) *
                        (Q.demagerr_g < 0.05) *
                        (Q.demagerr_r < 0.05) *
                        (Q.demagerr_z < 0.05) *
                        (Q.typex == tt) *
                        (Q.magerr_w1 < 0.05))    
    Q.cut(I)
    print(len(Q), 'DR12Q-matched entries of type PSF with g,r,z,W1 errs < 0.05')
    
    plt.clf()
    loghist(T.demag_g[I2] - T.demag_r[I2], T.demag_z[I2] - T.mag_w1[I2],
             nbins=200, range=((-0.5,2.0),(-3,4)))
    plt.xlabel('g - r (mag)')
    plt.ylabel('z - W1 (mag)')
    ps.savefig()

    #Q.cut(np.argsort(Q.z_pipe))
    plt.clf()
    plt.scatter(Q.demag_g - Q.demag_r, Q.demag_z - Q.mag_w1, c=Q.z_pipe, s=5)
    plt.colorbar(label='QSO redshift')
    plt.xlabel('g - r (mag)')
    plt.ylabel('z - W1 (mag)')
    ps.savefig()

    plt.figure(2)
    from scipy.ndimage.filters import gaussian_filter
    import matplotlib.cm
    xlo,xhi,ylo,yhi = [-0.25, 1.75, -2.5, 2.5]
    plt.clf()
    H,xe,ye = loghist(T.demag_g[I2] - T.demag_r[I2], T.demag_z[I2] - T.mag_w1[I2],
                      nbins=(400,200), range=((xlo,xhi),(ylo,yhi)), imshowargs=dict(cmap='Greys'),
                      hot=False, docolorbar=False)
    I = np.flatnonzero(Q.z_pipe < 10.)
    QH,nil,nil = np.histogram2d(Q.demag_g[I] - Q.demag_r[I], Q.demag_z[I] - Q.mag_w1[I],
                       bins=200, range=((xlo,xhi),(ylo,yhi)))
    QH = gaussian_filter(QH.T, 3.)
    c = plt.contour(QH, 8, extent=[xe.min(), xe.max(), ye.min(), ye.max()], colors='k')
    plt.axis([xlo,xhi,ylo,yhi])
    plt.xlabel('g - r (mag)')
    plt.ylabel('z - W1 (mag)')

    mx,my,mz = [],[],[]
    # I = np.argsort(Q.z_pipe)
    # for i in range(len(Q)/1000):
    #     J = I[i*1000:][:1000]
    #     mx.append(np.median(Q.demag_g[J] - Q.demag_r[J]))
    #     my.append(np.median(Q.demag_z[J] - Q.mag_w1[J]))
    #     mz.append(np.median(Q.z_pipe[J]))
    # plt.scatter(mx,my, c=mz)
    # plt.colorbar(label='QSO Redshift')
    # ps.savefig()

        
    zvals = np.arange(0, 3.61, 0.1)
    for zlo,zhi in zip(zvals, zvals[1:]):
         J = np.flatnonzero((Q.z_pipe >= zlo) * (Q.z_pipe < zhi))
         mx.append(np.median(Q.demag_g[J] - Q.demag_r[J]))
         my.append(np.median(Q.demag_z[J] - Q.mag_w1[J]))
         mz.append(np.median(Q.z_pipe[J]))
    # plt.scatter(mx,my, c=mz, marker='o-')
    # plt.colorbar(label='QSO Redshift')
    for i in range(len(mx)-1):
        c = matplotlib.cm.viridis(0.8 * i / (len(mx)-1))
        plt.plot([mx[i], mx[i+1]], [my[i],my[i+1]], '-', color=c, lw=4)
    plt.savefig('qso-wise.png')
    plt.savefig('qso-wise.pdf')

    xlo,xhi,ylo,yhi = [-0.25, 1.75, -0.25, 2.5]
    plt.clf()
    H,xe,ye = loghist(T.demag_g[I2] - T.demag_r[I2], T.demag_r[I2] - T.demag_z[I2],
                      nbins=(400,200), range=((xlo,xhi),(ylo,yhi)), imshowargs=dict(cmap='Greys'),
                      hot=False, docolorbar=False)

    #I3 = np.random.permutation(I2)[:1000]
    #plt.plot(T.demag_g[I3] - T.demag_r[I3], T.demag_r[I3] - T.demag_z[I3], 'r.')
    
    I = np.flatnonzero(Q.z_pipe < 10.)
    QH,nil,nil = np.histogram2d(Q.demag_g[I] - Q.demag_r[I], Q.demag_r[I] - Q.demag_z[I],
                       bins=200, range=((xlo,xhi),(ylo,yhi)))
    QH = gaussian_filter(QH.T, 3.)
    c = plt.contour(QH, 8, extent=[xe.min(), xe.max(), ye.min(), ye.max()], colors='k')
    plt.axis([xlo,xhi,ylo,yhi])
    plt.xlabel('g - r (mag)')
    plt.ylabel('r - z (mag)')
    mx,my,mz = [],[],[]
    zvals = np.arange(0, 3.61, 0.1)
    for zlo,zhi in zip(zvals, zvals[1:]):
         J = np.flatnonzero((Q.z_pipe >= zlo) * (Q.z_pipe < zhi))
         mx.append(np.median(Q.demag_g[J] - Q.demag_r[J]))
         my.append(np.median(Q.demag_r[J] - Q.demag_z[J]))
         mz.append(np.median(Q.z_pipe[J]))
    for i in range(len(mx)-1):
        c = matplotlib.cm.viridis(0.8 * i / (len(mx)-1))
        plt.plot([mx[i], mx[i+1]], [my[i],my[i+1]], '-', color=c, lw=4)

    plt.savefig('qso-optical.png')
    plt.savefig('qso-optical.pdf')
    plt.figure(1)


    # I = np.flatnonzero((T.flux_ivar_g > 0) * (T.flux_ivar_r > 0) * (T.flux_ivar_z > 0))
    # print(len(I), 'with g,r,z fluxes')
    # I = np.flatnonzero((T.flux_ivar_g > 0) * (T.flux_ivar_r > 0) * (T.flux_ivar_z > 0) *
    #                    (T.flux_g > 0) * (T.flux_r > 0) * (T.flux_z > 0))
    # print(len(I), 'with positive g,r,z fluxes')
    # I = np.flatnonzero((T.flux_ivar_g > 0) * (T.flux_ivar_r > 0) * (T.flux_ivar_z > 0) *
    #                    (T.flux_g > 0) * (T.flux_r > 0) * (T.flux_z > 0) *
    #                    (T.magerr_g < 0.2) * (T.magerr_r < 0.2) * (T.magerr_z < 0.2))
    # print(len(I), 'with < 0.2-mag errs')
    # I = np.flatnonzero((T.flux_ivar_g > 0) * (T.flux_ivar_r > 0) * (T.flux_ivar_z > 0) *
    #                    (T.flux_g > 0) * (T.flux_r > 0) * (T.flux_z > 0) *
    #                    (T.magerr_g < 0.1) * (T.magerr_r < 0.1) * (T.magerr_z < 0.1))
    # print(len(I), 'with < 0.1-mag errs')
    # 
    # I2 = np.flatnonzero((T.flux_ivar_g > 0) * (T.flux_ivar_r > 0) * (T.flux_ivar_z > 0) *
    #                     (T.flux_g > 0) * (T.flux_r > 0) * (T.flux_z > 0) *
    #                     (T.magerr_g < 0.05) * (T.magerr_r < 0.05) * (T.magerr_z < 0.05))
    # print(len(I2), 'with < 0.05-mag errs')

    #ha = dict(nbins=200, range=((-0.5, 2.5), (-0.5, 2.5)))
    ha = dict(nbins=400, range=((-0.5, 2.5), (-0.5, 2.5)))
    plt.clf()
    #plothist(T.mag_g[I] - T.mag_r[I], T.mag_r[I] - T.mag_z[I], 200)
    #loghist(T.mag_g[I] - T.mag_r[I], T.mag_r[I] - T.mag_z[I], **ha)
    loghist(T.demag_g[I] - T.demag_r[I], T.demag_r[I] - T.demag_z[I], **ha)
    plt.xlabel('g - r (mag)')
    plt.ylabel('r - z (mag)')
    ps.savefig()

    hists = []
    hists2 = []
    for tt,name in [('P', 'PSF'), ('E', 'EXP'), ('D', 'DeV')]:
        I = np.flatnonzero((T.flux_ivar_g > 0) * (T.flux_ivar_r > 0) *
                           (T.flux_ivar_z > 0) *
                           (T.flux_g > 0) * (T.flux_r > 0) * (T.flux_z > 0) *
                           (T.magerr_g < 0.1) * (T.magerr_r < 0.1) *(T.magerr_z < 0.1) *
                           (T.typex == tt))
        print(len(I), 'with < 0.1-mag errs and type', name)

        # plt.clf()
        # H,xe,ye = loghist(T.mag_g[I] - T.mag_r[I], T.mag_r[I] - T.mag_z[I], **ha)
        # plt.xlabel('g - r (mag)')
        # plt.ylabel('r - z (mag)')
        # plt.title('Type = %s' % name)
        # ps.savefig()

        plt.clf()
        H,xe,ye = loghist(T.demag_g[I] - T.demag_r[I], T.demag_r[I] - T.demag_z[I], **ha)

        # Keeping the dereddened color-color histograms
        hists.append(H.T)

        plt.xlabel('g - r (mag)')
        plt.ylabel('r - z (mag)')
        plt.title('Type = %s, dereddened' % name)
        ps.savefig()

        I2 = np.flatnonzero((T.flux_ivar_g > 0) * (T.flux_ivar_r > 0) *
                            (T.flux_ivar_z > 0) *
                            (T.flux_g > 0) * (T.flux_r > 0) * (T.flux_z > 0) *
                            (T.magerr_g < 0.05) *
                            (T.magerr_r < 0.05) *
                            (T.magerr_z < 0.05) *
                            (T.typex == tt))
        print(len(I2), 'with < 0.05-mag errs and type', name)

        plt.clf()
        H,xe,ye = loghist(T.demag_g[I2] - T.demag_r[I2], T.demag_r[I2] - T.demag_z[I2], **ha)
        hists2.append(H.T)
        plt.xlabel('g - r (mag)')
        plt.ylabel('r - z (mag)')
        plt.title('Type = %s, dereddened, 5%% errs' % name)
        ps.savefig()

    for H in hists:
        H /= H.max()
    #for H in hists2:
    #    H /= H.max()
    #hists2 = [np.clip(H / np.percentile(H.ravel(), 99), 0, 1) for H in hists2]
    hists2 = [np.clip(H / np.percentile(H.ravel(), 99.9), 0, 1) for H in hists2]

    rgbmap = np.dstack((hists[2], hists[0], hists[1]))
    plt.clf()
    ((xlo,xhi),(ylo,yhi)) = ha['range']
    plt.imshow(rgbmap, interpolation='nearest', origin='lower', extent=[xlo,xhi,ylo,yhi])
    plt.axis([0,2,0,2])
    plt.xlabel('g - r (mag)')
    plt.ylabel('r - z (mag)')
    plt.title('Red: DeV, Blue: Exp, Green: PSF')
    ps.savefig()

    plt.clf()
    plt.imshow(np.sqrt(rgbmap), interpolation='nearest', origin='lower', extent=[xlo,xhi,ylo,yhi])
    plt.axis([0,2,0,2])
    plt.xlabel('g - r (mag)')
    plt.ylabel('r - z (mag)')
    plt.title('Red: DeV, Blue: Exp, Green: PSF')
    ps.savefig()

    # For a white-background plot, mix colors like paint
    for hr,hg,hb in [(hists[2], hists[0], hists[1]),
                     (hists2[2], hists2[0], hists2[1]),]:
        H,W = hr.shape
        for mapping in [lambda x: x]: #, lambda x: np.sqrt(x), lambda x: x**2]:
            red = np.ones((H,W,3))
            red[:,:,1] = 1 - mapping(hr)
            red[:,:,2] = 1 - mapping(hr)
            # in matplotlib, 'green'->(0, 0.5, 0)
            green = np.ones((H,W,3))
            green[:,:,0] = 1 - mapping(hg)
            green[:,:,1] = 1 - 0.5*mapping(hg)
            green[:,:,2] = 1 - mapping(hg)
            blue = np.ones((H,W,3))
            blue[:,:,0] = 1 - mapping(hb)
            blue[:,:,1] = 1 - mapping(hb)
            plt.clf()
            plt.imshow(red * green * blue, origin='lower', interpolation='nearest',
                       extent=[xlo,xhi,ylo,yhi], aspect='auto')
            plt.xlabel('g - r (mag)')
            plt.ylabel('r - z (mag)')
            plt.xticks(np.arange(0, 2.1, 0.5))
            plt.yticks(np.arange(0, 2.1, 0.5))
            plt.axis([-0.25,2,0,2])
            plt.title('Red: DeV, Blue: Exp, Green: PSF')
            ps.savefig()

            plt.figure(2)
            plt.clf()
            plt.imshow(red * green * blue, origin='lower', interpolation='nearest',
                       extent=[xlo,xhi,ylo,yhi], aspect='auto')
            plt.xlabel('g - r (mag)')
            plt.ylabel('r - z (mag)')
            plt.xticks(np.arange(0, 2.1, 0.5))
            plt.yticks(np.arange(0, 2.1, 0.5))
            plt.axis([0,2,-0.25,2])
            plt.text(0.7, 0.1, 'PSF', ha='center', va='center', color='k')
            plt.text(0.6, 0.7, 'EXP', ha='center', va='center', color='k')
            plt.text(1.5, 0.6, 'DEV', ha='center', va='center', color='k')
            plt.savefig('color-type.pdf')
            plt.figure(1)

    sys.exit(0)

            
    np.random.seed(42)

    T.gr = T.mag_g - T.mag_r
    T.rz = T.mag_r - T.mag_z

    for tt,name in [('P', 'PSF'), ('E', 'EXP'), ('D', 'DeV')]:
        I = np.flatnonzero((T.flux_ivar_g > 0) * (T.flux_ivar_r > 0) *
                           (T.flux_ivar_z > 0) *
                           (T.flux_g > 0) * (T.flux_r > 0) * (T.flux_z > 0) *
                           (T.magerr_g < 0.1) * (T.magerr_r < 0.1) *(T.magerr_z < 0.1) *
                           # Bright cut
                           (T.mag_r > 16.) *

                           # Contamination cut
                           (T.decam_fracflux[:,2] < 0.5) *

                           # Mag range cut
                           (T.mag_r > 19.) *
                           (T.mag_r < 20.) *
                           
                           (T.typex == tt))
        print(len(I), 'with < 0.1-mag errs and type', name)

        # plt.clf()
        # ha = dict(histtype='step', range=(16, 24), bins=100)
        # plt.hist(T.mag_g[I], color='g', **ha)
        # plt.hist(T.mag_r[I], color='r', **ha)
        # plt.hist(T.mag_z[I], color='m', **ha)
        # plt.title('Type %s' % name)
        # ps.savefig()

        J = np.random.permutation(I)

        if tt == 'D':
            J = J[T.shapedev_r[J] < 3.]

        J = J[:100]
        rzbin = np.argsort(T.rz[J])
        rzblock = (rzbin / 10).astype(int)
        K = np.lexsort((T.gr[J], rzblock))
        #print('sorted rzblock', rzblock[K])
        #print('sorted rzbin', rzbin[K])
        J = J[K]

        # plt.clf()
        # plt.hist(T.decam_fracflux[J,2], label='fracflux_r', histtype='step', bins=20)
        # plt.hist(T.decam_fracmasked[J,2], label='fracmasked_r', histtype='step', bins=20)
        # plt.legend()
        # plt.title('Type %s' % name)
        # ps.savefig()

        #print('fracflux r', T.decam_fracflux[J,2])
        #print('fracmasked r', T.decam_fracmasked[J,2])
        #print('anymasked r', T.decam_anymask[J,2])

        imgs = []
        imgrow = []
        stackrows = []
        for i in J:
            fn = 'cutouts/cutout_%.4f_%.4f.jpg' % (T.ra[i], T.dec[i])
            if not os.path.exists(fn):
                url = 'http://legacysurvey.org/viewer/jpeg-cutout/?layer=decals-dr3&pixscale=0.262&size=25&ra=%f&dec=%f' % (T.ra[i], T.dec[i])
                print('URL', url)
                cmd = 'wget -O %s.tmp "%s" && mv %s.tmp %s' % (fn, url, fn, fn)
                os.system(cmd)
            img = plt.imread(fn)
            #print('Image', img.shape)
            extra = ''
            if tt == 'D':
                extra = ', shapedev_r %.2f' % T.shapedev_r[i]
            elif tt == 'E':
                extra = ', shapeexp_r %.2f' % T.shapeexp_r[i]

            print(name, 'RA,Dec %.4f,%.4f, g/r/z %.2f, %.2f, %.2f, fracflux_r %.3f, fracmasked_r %.3f, anymasked_r %4i%s' % (
                T.ra[i], T.dec[i], T.mag_g[i], T.mag_r[i], T.mag_z[i],
                T.decam_fracflux[i,2], T.decam_fracmasked[i,2], T.decam_anymask[i,2], extra))

            imgrow.append(img)
            if len(imgrow) == 10:
                stack = np.hstack(imgrow)
                print('stacked row:', stack.shape)
                stackrows.append(stack)

                imgs.append(imgrow)
                imgrow = []


        stack = np.vstack(stackrows)
        print('Stack', stack.shape)
        plt.clf()
        plt.imshow(stack, interpolation='nearest', origin='lower')
        plt.xticks([])
        plt.yticks([])
        plt.title('Type %s' % name)
        ps.savefig()
        

    # Wrap-around at RA=300
    # rax = T.ra + (-360 * T.ra > 300.)
    # 
    # rabins = np.arange(-60., 300., 0.2)
    # decbins = np.arange(-20, 35, 0.2)
    # 
    # print('RA bins:', len(rabins))
    # print('Dec bins:', len(decbins))
    #
    # for name,cut in [(None, 'All sources'),
    #                  (T.type == 'PSF '), 'PSF'),
    #                  (T.type == 'SIMP'), 'SIMP'),
    #                  (T.type == 'DEV '), 'DEV'),
    #                  (T.type == 'EXP '), 'EXP'),
    #                  (T.type == 'COMP'), 'COMP'),
    #                  ]:
    #                  
    # H,xe,ye = np.histogram2d(T.ra, T.dec, bins=(rabins, decbins))
    # print('H shape', H.shape)
    # 
    # H = H.T
    # 
    # H /= (rabins[1:] - rabins[:-1])[np.newaxis, :]
    # H /= ((decbins[1:] - decbins[:-1]) * np.cos(np.deg2rad((decbins[1:] + decbins[:-1]) / 2.))))[:, np.newaxis]
    # 
    # plt.clf()
    # plt.imshow(H, extent=(rabins[0], rabins[-1], decbins[0], decbins[-1]), interpolation='nearest', origin='lower',
    #            vmin=0)
    # cbar = plt.colorbar()
    # cbar.set_label('Source density per square degree')
    # plt.xlim(rabins[-1], rabins[0])
    # plt.xlabel('RA (deg)')
    # plt.ylabel('Dec (deg)')
    # ps.savefig()



    '''
コード例 #53
0
    for j in range(0, col - 1):
        Im_n60[i, j] = Im_n60[i, j] + np.random.uniform(0, 60)

# Imprimir imagen con ruido (0,60) en escala de grises
plt.figure()
plt.gray()
plt.imshow(Im_n60)
plt.title("Imagen con ruido (0,60)")

# MSE entre imagen original e imagen con filtro uniforme de 3 x 3 en escala de grises
Im_f1u = filters.uniform_filter(Im_n60, 3)
print("MSE para uniforme 3 x 3 con ruido (0,60): " +
      str(fun.my_mse(Im_f1u, Im_g)))

# MSE entre imagen original e imagen con filtro Gaussiano de 0.5 en escala de grises
Im_f1g = filters.gaussian_filter(Im_n60, 0.5)
print("MSE para gaussiano 1 con ruido (0,60): " +
      str(fun.my_mse(Im_f1g, Im_g)))

# Imprimir imagen con filtro uniforme 3 x 3 en escala de grises
plt.figure()
plt.gray()
plt.imshow(Im_f1u)
plt.title("Imagen con filtro uniforme de 3 x 3")

# Imprimir imagen con filtro Gaussiano 0.5 en escala de grises
plt.figure()
plt.gray()
plt.imshow(Im_f1u)
plt.title("Imagen con filtro Gaussiano 0.5")
コード例 #54
0
def optimize_image(layer_tensor,
                   image,
                   num_iterations=10,
                   step_size=3.0,
                   tile_size=400,
                   show_gradient=False):
    """
    Use gradient ascent to optimize an image so it maximizes the mean value of the given layer_tensor

    :param layer_tensor: Reference to a tensor that will be maximized.
    :param image: Input image used as the starting point.
    :param num_iterations: Number of optimization iterations to perform.
    :param step_size: Scale for each step of the gradient ascent.
    :param tile_size: Size of the tiles when calculating the gradient.
    :param show_gradient: Plot the gradient in each iterations.

    :return:
    """

    # Copy the image so we don't overwrite the original image.
    img = image.copy()

    print("Image before:")
    plot_image(img)

    print("Processing image:", end="")

    # Use TensorFlow to get the mathematical function for the
    # gradient of the given layer-tensor with regard to the
    # input image. This may cause TensorFlow to add the same
    # math-expressions to the graph each time this function is called.
    # It may use a lot of RAM and could be moved outside the function.

    gradient = model.get_gradient(layer_tensor)

    for i in range(num_iterations):
        # Calculate the value of the gradient.
        # This tells us how to change the image so as to
        # maximize the mean of the given layer-tensor.
        grad = tiled_gradient(gradient=gradient, image=img)

        # Blur the gradient with different amounts and add
        # them together. The blur amount is also increased
        # during the optimization. This was found to give
        # nice, smooth images. You can try and change the formulas.
        # The blur-amount is called sigma (0=no blur, 1=low blur, etc.)
        # We could call gaussian_filter(grad, sigma=(sigma, sigma, 0.0))
        # which would not blur the colour-channel. This tends to
        # give psychadelic / pastel colours in the resulting images.
        # When the colour-channel is also blurred the colours of the
        # input image are mostly retained in the output image.

        sigma = (i * 4.0) / num_iterations + 0.5
        grad_smooth1 = gaussian_filter(grad, sigma=sigma)
        grad_smooth2 = gaussian_filter(grad, sigma=sigma * 2)
        grad_smooth3 = gaussian_filter(grad, sigma=sigma * 0.5)
        grad = (grad_smooth1 + grad_smooth2 + grad_smooth3)

        # Scale the step-size according to the gradient-values.
        # This may not be necessary because the tiled-gradient
        # is already normalized

        step_size_scaled = step_size / (np.std(grad) + 1e-8)

        # Update the image by following the gradient.
        img += grad * step_size_scaled

        if show_gradient:
            # Print statistics for the gradient.
            msg = "Gradient min: {0:>9.6f}, max: {1:>9.6f}, stepsize: {2:>9.2f}"
            print(msg.format(grad.min(), grad.max(), step_size_scaled))

            # Plot the gradient.
            plot_gradient(grad)
        else:
            # Otherwise show a little progress-indicator.
            print(". ", end="")

    print()
    print("Image after:")
    plot_image(img)

    return img
コード例 #55
0
def main(args):
    if len(args) != 5:
        print("Usage: runmodel.py infile prior [-o/-d] [outfile/outdir]")
        print(
            "If -o is specified, then all predictions are dumped to a hickle.")
        print(
            "Else, all predictions are dumped to images in the directory specified."
        )
        print(
            "If 'prior' is 'extract', then this program will instead extract an existing predictions hickle into the specified output directory."
        )
        return

    infile = args[1]
    prior = args[2]
    outopt = args[3]
    outfile = args[4]

    if prior == "extract":
        predictions = hickle.load(infile)
        if not os.path.exists(outfile):
            os.makedirs(outfile)

        for p in range(predictions.shape[0]):
            #arr=predictions[p]-np.min(predictions[p])
            #arr=np.abs(predictions[p])
            #arr=np.abs(predictions[p])
            # Scale the array:
            #print(arr.shape)
            #arr=arr/arr.max()
            #arr=arr*255.0
            im = misc.imresize(predictions[p], (480, 640)).astype("float32")
            im = im / im.max()
            im = filters.gaussian_filter(im, 8).astype("float32")
            # We should probably now normalize the image:
            #im=im/np.max(im)
            #im=softmax(im)
            print(im.max())
            print(im.min())
            im[im < im.mean()] = 0
            im = im / im.max()
            # Todo: consider thresholding the image to remove all noticable large regions of activation.
            #im=softmax(im)
            print(im.max())
            print(im.min())
            misc.imsave(os.path.join(outfile, "%d.png" % p), im)
            print("Done")
        return

    indata = hickle.load(infile)
    m = model.PredSaliencyModel(
        None, prior
    )  # Prior is passed as a filename and the first argument has yet to be defined.

    # Todo: figure out if "indata" is actually formatted correctly for this application.
    predictions = m.predict(indata)

    if outopt == '-o':
        print('predictions.shape:')
        print(predictions.shape)
        hickle.dump(predictions, outfile)
    else:
        predictions = predictions[
            0]  # IIRC, there's a useless first dimension in each model output.
        if not os.path.exists(outfile):
            os.makedirs(outfile)

        for p in range(predictions.shape[0]):
            misc.imsave(os.path.join(outfile, "%d.png" % p), predictions[p])
コード例 #56
0
def recursive_optimize(layer_tensor,
                       image,
                       num_repeats=4,
                       rescale_facotr=0.7,
                       blend=0.2,
                       num_iterations=10,
                       step_size=3.0,
                       tile_size=400):
    """
    Recursively blur and downscale the input image.
    Each downscaled image is run through the optimize_image()
    function to amplify the patterns that the Inception model sess.

    :param Input: Input image used as the starting point.
    :param num_repeats: Number of times to downscale the image.
    :param rescale_facotr: Downscaling factor for the image.
    :param blend: Factor for blending the original and processed images.

    Parameters passed to optimize_image()
    :param num_iterations: Number of optimization iterations to perform.
    :param step_size: Scale for each step of the gradient ascent.
    :param tile_size: Size of the tiles when calculating the gradient.
    :param layer_tensor: Reference to a tensor that will be maximized.

    :return:
    """

    # Do a recursive step?
    if num_repeats > 0:
        # Blur the input image to prevent artifacts when downscaling.
        # The blur amount is controlled by sigma. Note that the
        # colour-channel is not blurred as it would make the image grey.
        sigma = 0.5
        img_blur = gaussian_filter(image, sigma=(sigma, sigma, 0.0))

        # Downscale the image.
        img_downscaled = resize_image(image=img_blur, factor=rescale_facotr)

        # Recursive call to this function.
        # Subtract one from num_repeats and use the downscaled image.
        img_result = recursive_optimize(layer_tensor=layer_tensor,
                                        image=img_downscaled,
                                        num_repeats=num_repeats - 1,
                                        rescale_facotr=rescale_facotr,
                                        blend=blend,
                                        num_iterations=num_iterations,
                                        step_size=step_size,
                                        tile_size=tile_size)

        # Up scale the resulting image back to its original size
        img_upscaled = resize_image(image=img_result, size=image.shape)

        # Blend the original and precessed images.
        image = blend * image + (1.0 - blend) * img_upscaled

    print("Recursive level:", num_repeats)

    img_result = optimize_image(layer_tensor=layer_tensor,
                                image=image,
                                num_iterations=num_iterations,
                                step_size=step_size,
                                tile_size=tile_size)

    return img_result
コード例 #57
0
def train(upscaling_factor,
          residual_blocks,
          feature_size,
          path_prediction,
          checkpoint_dir,
          img_width,
          img_height,
          img_depth,
          subpixel_NN,
          nn,
          batch_size=1,
          div_patches=4,
          epochs=12):
    traindataset = Train_dataset(batch_size)
    iterations_train = math.ceil(
        (len(traindataset.subject_list) * 0.8) / batch_size)
    num_patches = traindataset.num_patches

    # ##========================== DEFINE MODEL ============================##
    t_input_gen = tf.placeholder(
        'float32',
        [int((batch_size * num_patches) / div_patches), None, None, None, 1],
        name='t_image_input_to_SRGAN_generator')
    t_target_image = tf.placeholder('float32', [
        int((batch_size * num_patches) / div_patches), img_width, img_height,
        img_depth, 1
    ],
                                    name='t_target_image')
    t_input_mask = tf.placeholder('float32', [
        int((batch_size * num_patches) / div_patches), img_width, img_height,
        img_depth, 1
    ],
                                  name='t_image_input_mask')
    ae_real_z = tf.placeholder(
        'float32',
        [int((batch_size * num_patches) / div_patches), 16, 16, 12, 30],
        name='ae_real_z')
    ae_fake_z = tf.placeholder(
        'float32',
        [int((batch_size * num_patches) / div_patches), 16, 16, 12, 30],
        name='ae_fake_z')

    net_gen = generator(input_gen=t_input_gen,
                        kernel=3,
                        nb=residual_blocks,
                        upscaling_factor=upscaling_factor,
                        img_height=img_height,
                        img_width=img_width,
                        img_depth=img_depth,
                        subpixel_NN=subpixel_NN,
                        nn=nn,
                        feature_size=feature_size,
                        is_train=True,
                        reuse=False)
    net_d, disc_out_real = discriminator(input_disc=t_target_image,
                                         kernel=3,
                                         is_train=True,
                                         reuse=False)
    _, disc_out_fake = discriminator(input_disc=net_gen.outputs,
                                     kernel=3,
                                     is_train=True,
                                     reuse=True)

    # test
    gen_test = generator(t_input_gen,
                         kernel=3,
                         nb=residual_blocks,
                         upscaling_factor=upscaling_factor,
                         img_height=img_height,
                         img_width=img_width,
                         img_depth=img_depth,
                         subpixel_NN=subpixel_NN,
                         nn=nn,
                         feature_size=feature_size,
                         is_train=True,
                         reuse=True)

    # autoencoder
    ae_out = autoencoder(x_auto=t_target_image)

    # ###========================== DEFINE TRAIN OPS ==========================###

    # use disc_out_real in both cases because shape will be equal in disc_out_real and disc_out_fake
    # if not, problems for not specifying input shape for generator

    if np.random.uniform() > 0.1:
        # give correct classifications
        y_gan_real = tf.ones_like(disc_out_real)
        y_gan_fake = tf.zeros_like(disc_out_real)
    else:
        # give wrong classifications (noisy labels)
        y_gan_real = tf.zeros_like(disc_out_real)
        y_gan_fake = tf.ones_like(disc_out_real)

    d_loss_real = tf.reduce_mean(tf.square(disc_out_real -
                                           smooth_gan_labels(y_gan_real)),
                                 name='d_loss_real')
    d_loss_fake = tf.reduce_mean(tf.square(disc_out_fake -
                                           smooth_gan_labels(y_gan_fake)),
                                 name='d_loss_fake')
    d_loss = d_loss_real + d_loss_fake

    # use disc_out_real in both cases because shape will be equal in disc_out_real and disc_out_fake
    # if not, problems for not specifying input shape for generator

    g_gan_loss = 10e-2 * tf.reduce_mean(
        tf.square(disc_out_fake -
                  smooth_gan_labels(tf.ones_like(disc_out_real))),
        name='g_loss_gan')
    mse_loss = tf.reduce_sum(tf.square(net_gen.outputs - t_target_image),
                             axis=[0, 1, 2, 3, 4],
                             name='g_loss_mse')

    dx_real = t_target_image[:, 1:, :, :, :] - t_target_image[:, :-1, :, :, :]
    dy_real = t_target_image[:, :, 1:, :, :] - t_target_image[:, :, :-1, :, :]
    dz_real = t_target_image[:, :, :, 1:, :] - t_target_image[:, :, :, :-1, :]
    dx_fake = net_gen.outputs[:,
                              1:, :, :, :] - net_gen.outputs[:, :-1, :, :, :]
    dy_fake = net_gen.outputs[:, :,
                              1:, :, :] - net_gen.outputs[:, :, :-1, :, :]
    dz_fake = net_gen.outputs[:, :, :,
                              1:, :] - net_gen.outputs[:, :, :, :-1, :]

    gd_loss = tf.reduce_sum(tf.square(tf.abs(dx_real) - tf.abs(dx_fake))) + \
              tf.reduce_sum(tf.square(tf.abs(dy_real) - tf.abs(dy_fake))) + \
              tf.reduce_sum(tf.square(tf.abs(dz_real) - tf.abs(dz_fake)))

    g_loss_mse = mse_loss + g_gan_loss + gd_loss

    g_loss_ae = 10e-2 * tf.reduce_sum(tf.square(ae_real_z - ae_fake_z),
                                      axis=[0, 1, 2, 3, 4],
                                      name='ae_loss') + g_gan_loss

    g_vars = tl.layers.get_variables_with_name('SRGAN_g', True, True)
    d_vars = tl.layers.get_variables_with_name('SRGAN_d', True, True)

    with tf.variable_scope('learning_rate'):
        #lr_v_g = tf.Variable(1e-5, trainable=False)
        lr_v_g = tf.Variable(1e-4, trainable=False)
        lr_v_d = tf.Variable(1e-4, trainable=False)
    global_step = tf.Variable(0, trainable=False)
    decay_rate = 0.5
    decay_steps = 4920  # every 2 epochs (more or less)
    learning_rate_g = tf.train.inverse_time_decay(lr_v_g,
                                                  global_step=global_step,
                                                  decay_rate=decay_rate,
                                                  decay_steps=decay_steps)
    learning_rate_d = tf.train.inverse_time_decay(lr_v_d,
                                                  global_step=global_step,
                                                  decay_rate=decay_rate,
                                                  decay_steps=decay_steps)

    # Optimizers
    g_optim_mse = tf.train.AdamOptimizer(learning_rate_d).minimize(
        g_loss_mse, var_list=g_vars)
    #Adagrad probado no va mejor que sin ae, si converge (5e-5, 1e-5)
    #Adam/RMSprop no converge (5e-5)
    #Adadelta probado no va mejor que sin ae, si converge (5e-5, 1e-4 va peor) va mejor sin lrdecay
    g_optim_ae = tf.train.AdagradOptimizer(5e-5).minimize(g_loss_ae,
                                                          var_list=g_vars)
    d_optim = tf.train.AdamOptimizer(learning_rate_d).minimize(d_loss,
                                                               var_list=d_vars)

    session = tf.Session()
    tl.layers.initialize_global_variables(session)

    session_ae = tf.Session()
    session_ae.run(tf.global_variables_initializer())
    var_list = list()

    for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES):
        if 'SRGAN' not in v.name and '9' not in v.name and 'learning_rate' not in v.name and 'beta' not in v.name:
            var_list.append(v)
    saver_ae = tf.train.Saver(var_list)
    saver_ae.restore(session_ae,
                     tf.train.latest_checkpoint(AUTOENCODER_CHECPOINTS))
    initialize_uninitialized_vars(session_ae)

    step = 0

    for j in range(0, epochs):
        for i in range(0, iterations_train):
            ###====================== LOAD DATA ===========================###
            xt_total = traindataset.patches_true(i)
            xm_total = traindataset.mask(i)
            for k in range(0, div_patches):
                print('{}'.format(k))
                xt = xt_total[k *
                              int((batch_size * num_patches) / div_patches):
                              (int((batch_size * num_patches) / div_patches) *
                               k) +
                              int((batch_size * num_patches) / div_patches)]
                xm = xm_total[k *
                              int((batch_size * num_patches) / div_patches):
                              (int((batch_size * num_patches) / div_patches) *
                               k) +
                              int((batch_size * num_patches) / div_patches)]

                # NORMALIZING
                for t in range(0, xt.shape[0]):
                    normfactor = (np.amax(xt[t])) / 2
                    if normfactor != 0:
                        xt[t] = ((xt[t] - normfactor) / normfactor)

                # RESIZING, don't normalize, XT already normalized
                x_generator = zoom(xt, [
                    1, (1 / upscaling_factor), (1 / upscaling_factor),
                    (1 / upscaling_factor), 1
                ])
                x_generator = gaussian_filter(x_generator, sigma=1)
                xgenin = x_generator

                ###========================= train SRGAN =========================###
                # update D
                errd, _ = session.run([d_loss, d_optim], {
                    t_target_image: xt,
                    t_input_gen: xgenin
                })

                if j < 4:
                    errg, errmse, errgan, errgd, _ = session.run(
                        [
                            g_loss_mse, mse_loss, g_gan_loss, gd_loss,
                            g_optim_mse
                        ], {
                            t_input_gen: xgenin,
                            t_target_image: xt,
                            t_input_mask: xm
                        })
                    print(
                        "Epoch [%2d/%2d] [%4d/%4d] [%4d/%4d]: d_loss: %.8f g_loss: %.8f (mse: %.6f gdl: %.6f adv: %.6f)"
                        % (j, epochs, i, iterations_train, k, div_patches - 1,
                           errd, errg, errmse, errgd, errgan))
                else:
                    # loss autoencoder
                    x_pred_ae = session.run(gen_test.outputs,
                                            {t_input_gen: xgenin})
                    ae_fake_z_val = session_ae.run(ae_out['z'],
                                                   {t_target_image: x_pred_ae})
                    ae_real_z_val = session_ae.run(ae_out['z'],
                                                   {t_target_image: xt})
                    # update G
                    errg, errgan, _ = session.run(
                        [g_loss_ae, g_gan_loss, g_optim_ae], {
                            t_input_gen: xgenin,
                            t_target_image: xt,
                            t_input_mask: xm,
                            ae_real_z: ae_real_z_val,
                            ae_fake_z: ae_fake_z_val
                        })

                    print(
                        "Epoch [%2d/%2d] [%4d/%4d] [%4d/%4d]: d_loss: %.8f g_loss: %.8f (adv: %.6f)"
                        % (j, epochs, i, iterations_train, k, div_patches - 1,
                           errd, errg, errgan))

                ###========================= evaluate & save model =========================###

                if k == 1 and i % 20 == 0:
                    if j == 0:
                        x_true_img = xt[0]
                        if normfactor != 0:
                            x_true_img = (
                                (x_true_img + 1) * normfactor)  # denormalize
                        img_pred = nib.Nifti1Image(x_true_img, np.eye(4))
                        img_pred.to_filename(
                            os.path.join(path_prediction,
                                         str(j) + str(i) + 'true.nii.gz'))

                        x_gen_img = xgenin[0]
                        if normfactor != 0:
                            x_gen_img = (
                                (x_gen_img + 1) * normfactor)  # denormalize
                        img_pred = nib.Nifti1Image(x_gen_img, np.eye(4))
                        img_pred.to_filename(
                            os.path.join(path_prediction,
                                         str(j) + str(i) + 'gen.nii.gz'))

                    x_pred = session.run(gen_test.outputs,
                                         {t_input_gen: xgenin})
                    x_pred_img = x_pred[0]
                    if normfactor != 0:
                        x_pred_img = (
                            (x_pred_img + 1) * normfactor)  # denormalize
                    img_pred = nib.Nifti1Image(x_pred_img, np.eye(4))
                    img_pred.to_filename(
                        os.path.join(path_prediction,
                                     str(j) + str(i) + '.nii.gz'))

                    # x_auto = session_ae.run(ae_out['y'], {t_target_image: xt})
                    # x_auto_img = x_auto[0]
                    # if normfactor != 0:
                    #     x_auto_img = ((x_auto_img + 1) * normfactor)  # denormalize
                    # img_pred = nib.Nifti1Image(x_auto_img, np.eye(4))
                    # img_pred.to_filename(
                    #     os.path.join(path_prediction, str(j) + str(i) + 'yayauto.nii.gz'))

        saver = tf.train.Saver()
        saver.save(sess=session, save_path=checkpoint_dir, global_step=step)
        print("Saved step: [%2d]" % step)
        step = step + 1
コード例 #58
0
ファイル: circle.py プロジェクト: stacywebb/raveberry
    def draw(self) -> None:
        import numpy as np
        from scipy.ndimage.filters import gaussian_filter
        from pi3d.Camera import Camera
        from pi3d.constants import (
            opengles,
            GL_SRC_ALPHA,
            GL_ONE_MINUS_SRC_ALPHA,
            GLsizei,
            GLboolean,
            GLint,
            GL_FLOAT,
            GL_ARRAY_BUFFER,
            GL_UNSIGNED_SHORT,
            GL_TEXTURE_2D,
            GL_UNSIGNED_BYTE,
        )

        time_logging = False

        if self.should_prepare:
            self._prepare()

        if self.lights.alarm_program.factor != -1:
            self.alarm_factor = max(0.001, self.lights.alarm_program.factor)
        else:
            self.alarm_factor = 0

        then = time.time()

        self.display.loop_running()
        now = self.display.time
        self.time_delta = now - self.last_loop
        self.last_loop = now
        self.time_elapsed += self.time_delta

        if time_logging:
            print(f"{time.time() - then} main loop")
            then = time.time()

        # use a sliding window to smooth the spectrum with a gauss function
        # truncating does not save significant time (~3% for this step)

        # new_frame = np.array(self.cava.current_frame, dtype="float32")
        new_frame = gaussian_filter(self.cava.current_frame,
                                    sigma=1.5,
                                    mode="nearest")
        new_frame = new_frame[self.SPECTRUM_CUT:-self.SPECTRUM_CUT]
        new_frame = -0.5 * new_frame**3 + 1.5 * new_frame
        new_frame *= 255
        current_frame = new_frame

        if time_logging:
            print(f"{time.time() - then} spectrum smoothing")
            then = time.time()

        # Value used for circle shake and background color cycle
        # select the first few values and compute their average
        bass_elements = math.ceil(self.BASS_MAX * self.cava.bars)
        self.bass_value = sum(
            current_frame[0:bass_elements]) / bass_elements / 255
        self.bass_value = max(self.bass_value, self.alarm_factor)
        self.total_bass = self.total_bass + self.bass_value
        # the fraction of time that there was bass
        self.bass_fraction = self.total_bass / self.time_elapsed / self.lights.UPS

        self.uniform_values = {
            48: self.width / self.scale,
            49: self.height / self.scale,
            50: self.scale,
            51: self.FFT_HIST,
            52: self.NUM_PARTICLES,
            53: self.PARTICLE_SPAWN_Z,
            54: self.time_elapsed,
            55: self.time_delta,
            56: self.alarm_factor,
            57: self.bass_value,
            58: self.total_bass,
            59: self.bass_fraction,
        }

        # start rendering to the smaller OffscreenTexture
        # we decrease the size of the texture so it only allocates that much memory
        # otherwise it would use as much as the displays size, negating its positive effect
        self.post.ix = int(self.post.ix / self.scale)
        self.post.iy = int(self.post.iy / self.scale)
        opengles.glViewport(
            GLint(0),
            GLint(0),
            GLsizei(int(self.width / self.scale)),
            GLsizei(int(self.height / self.scale)),
        )
        self.post._start()
        self.post.ix = self.width
        self.post.iy = self.height

        self._set_unif(self.background, [48, 49, 54, 56, 58])
        self.background.draw()

        if time_logging:
            print(f"{time.time() - then} background draw")
            then = time.time()

        # enable additive blending so the draw order of overlapping particles does not matter
        opengles.glBlendFunc(1, 1)

        self._set_unif(self.particle_sprite, [53, 54, 59])

        # copied code from pi3d.Shape.draw()
        # we don't need modelmatrices, normals ord textures and always blend
        self.particle_sprite.load_opengl()
        camera = Camera.instance()
        if not camera.mtrx_made:
            camera.make_mtrx()
        self.particle_sprite.MRaw = self.particle_sprite.tr1
        self.particle_sprite.M[0, :, :] = self.particle_sprite.MRaw[:, :]
        self.particle_sprite.M[1, :, :] = np.dot(self.particle_sprite.MRaw,
                                                 camera.mtrx)[:, :]

        # Buffer.draw()
        buf = self.particle_sprite.buf[0]
        buf.load_opengl()
        shader = buf.shader
        shader.use()
        opengles.glUniformMatrix4fv(
            shader.unif_modelviewmatrix,
            GLsizei(2),
            GLboolean(0),
            self.particle_sprite.M.ctypes.data,
        )
        opengles.glUniform3fv(shader.unif_unif, GLsizei(20),
                              self.particle_sprite.unif)
        buf._select()
        opengles.glVertexAttribPointer(shader.attr_vertex, GLint(3), GL_FLOAT,
                                       GLboolean(0), buf.N_BYTES, 0)
        opengles.glEnableVertexAttribArray(shader.attr_vertex)
        opengles.glVertexAttribPointer(shader.attr_texcoord, GLint(2),
                                       GL_FLOAT, GLboolean(0), buf.N_BYTES, 24)
        opengles.glEnableVertexAttribArray(shader.attr_texcoord)
        buf.disp.last_shader = shader
        opengles.glUniform3fv(shader.unif_unib, GLsizei(5), buf.unib)

        opengles.glBindBuffer(GL_ARRAY_BUFFER, self.instance_vbo)
        opengles.glDrawElementsInstanced(
            buf.draw_method,
            GLsizei(buf.ntris * 3),
            GL_UNSIGNED_SHORT,
            0,
            self.NUM_PARTICLES,
        )

        # restore normal blending
        opengles.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        if time_logging:
            print(f"{time.time() - then} particle draw")
            then = time.time()

        # roll the history one further, insert the current one.
        # we use a texture with four channels eventhough we only need one, refer to this post:
        # https://community.khronos.org/t/updating-textures-per-frame/75020/3
        # basically the gpu converts it anyway, so other formats would be slower
        history = np.zeros(
            (self.FFT_HIST, self.cava.bars - 2 * self.SPECTRUM_CUT, 4),
            dtype="uint8")
        self.fft = np.roll(self.fft, 1, 0)
        self.fft[0] = current_frame
        history[:, :, 0] = self.fft

        if time_logging:
            print(f"{time.time() - then} spectrum roll")
            then = time.time()

        # change the spectrum part of the texture (the lower 256xFFT_HIST pixels)
        opengles.glBindTexture(GL_TEXTURE_2D, self.dynamic_texture._tex)
        iformat = self.dynamic_texture._get_format_from_array(
            history, self.dynamic_texture.i_format)
        opengles.glTexSubImage2D(
            GL_TEXTURE_2D,
            0,
            0,
            self.dynamic_texture.ix,
            history.shape[1],
            history.shape[0],
            iformat,
            GL_UNSIGNED_BYTE,
            history.ctypes.data_as(ctypes.POINTER(ctypes.c_ubyte)),
        )

        if time_logging:
            print(f"{time.time() - then} glTexImage2D")
            then = time.time()

        self._set_unif(self.spectrum, [48, 49, 51, 52, 53, 54, 55, 57, 58])
        self.spectrum.draw()

        if time_logging:
            print(f"{time.time() - then} spectrum draw")
            then = time.time()

        self._set_unif(self.logo, [48, 49, 51, 54, 57, 58])
        self.logo.draw()

        if time_logging:
            print(f"{time.time() - then} logo draw")
            then = time.time()

        self._set_unif(self.after, [48, 49, 54, 57])
        self.after.draw()

        if time_logging:
            print(f"{time.time() - then} after draw")
            then = time.time()

        self.post._end()

        opengles.glViewport(GLint(0), GLint(0), GLsizei(self.width),
                            GLsizei(self.height))
        self._set_unif(self.post_sprite, [50])
        self.post_sprite.draw()

        if time_logging:
            print(f"{time.time() - then} post draw")
            print(f"scale: {self.scale}")
            print("=====")
コード例 #59
0
def calculate_polygonal_environment(im: PIL.Image.Image,
                                    baselines: Sequence[Tuple[int, int]] = None,
                                    suppl_obj: Sequence[Tuple[int, int]] = None,
                                    im_feats: np.array = None,
                                    scale: Tuple[int, int] = None):
    """
    Given a list of baselines and an input image, calculates a polygonal
    environment around each baseline.

    Args:
        im (PIL.Image): grayscale input image (mode 'L')
        baselines (sequence): List of lists containing a single baseline per
                              entry.
        suppl_obj (sequence): List of lists containing additional polylines
                              that should be considered hard boundaries for
                              polygonizaton purposes. Can be used to prevent
                              polygonization into non-text areas such as
                              illustrations or to compute the polygonization of
                              a subset of the lines in an image.
        im_feats (numpy.array): An optional precomputed seamcarve energy map.
                                Overrides data in `im`. The default map is
                                `gaussian_filter(sobel(im), 2)`.
        scale (tuple): A 2-tuple (h, w) containing optional scale factors of
                       the input. Values of 0 are used for aspect-preserving
                       scaling. `None` skips input scaling.
    Returns:
        List of lists of coordinates. If no polygonization could be compute for
        a baseline `None` is returned instead.
    """
    if scale is not None and (scale[0] > 0 or scale[1] > 0):
        w, h = im.size
        oh, ow = scale
        if oh == 0:
            oh = int(h * ow/w)
        elif ow == 0:
            ow = int(w * oh/h)
        im = im.resize((ow, oh))
        scale = np.array((ow/w, oh/h))
        # rescale baselines
        baselines = [(np.array(bl) * scale).astype('int').tolist() for bl in baselines]
        # rescale suppl_obj
        if suppl_obj is not None:
            suppl_obj = [(np.array(bl) * scale).astype('int').tolist() for bl in suppl_obj]

    bounds = np.array(im.size, dtype=np.float)
    im = np.array(im)
    if im_feats is None:
         # compute image gradient
        im_feats = gaussian_filter(sobel(im), 2)

    def _ray_intersect_boundaries(ray, direction, aabb):
        """
        Simplified version of [0] for 2d and AABB anchored at (0,0).

        [0] http://gamedev.stackexchange.com/questions/18436/most-efficient-aabb-vs-ray-collision-algorithms
        """
        dir_fraction = np.empty(2, dtype=ray.dtype)
        dir_fraction[direction == 0.0] = np.inf
        dir_fraction[direction != 0.0] = np.divide(1.0, direction[direction != 0.0])

        t1 = (-ray[0]) * dir_fraction[0]
        t2 = (aabb[0] - ray[0]) * dir_fraction[0]
        t3 = (-ray[1]) * dir_fraction[1]
        t4 = (aabb[1] - ray[1]) * dir_fraction[1]

        tmin = max(min(t1, t2), min(t3, t4))
        tmax = min(max(t1, t2), max(t3, t4))

        t = min(x for x in [tmin, tmax] if x >= 0)
        return ray + (direction * t)

    def _extract_patch(env_up, env_bottom, baseline, dir_vec):
        """
        Calculate a line image patch from a ROI and the original baseline.
        """
        upper_polygon = np.concatenate((baseline, env_up[::-1]))
        bottom_polygon = np.concatenate((baseline, env_bottom[::-1]))
        angle = np.arctan2(dir_vec[1], dir_vec[0])

        def _calc_seam(polygon, bias=100):
            """
            Calculates seam between baseline and ROI boundary on one side.

            Adds a baseline-distance-weighted bias to the feature map, masks
            out the bounding polygon and rotates the line so it is roughly
            level.
            """
            MASK_VAL = 99999
            r, c = draw.polygon(polygon[:,1], polygon[:,0])
            c_min, c_max = int(polygon[:,0].min()), int(polygon[:,0].max())
            r_min, r_max = int(polygon[:,1].min()), int(polygon[:,1].max())
            patch = im_feats[r_min:r_max+2, c_min:c_max+2].copy()
            # bias feature matrix by distance from baseline
            mask = np.ones_like(patch)
            for l in zip(baseline[:-1] - (c_min, r_min), baseline[1:] - (c_min, r_min)):
                line_locs = draw.line(l[0][1], l[0][0], l[1][1], l[1][0])
                mask[line_locs] = 0
            dist_bias = distance_transform_cdt(mask)
            # absolute mask
            mask = np.ones_like(patch, dtype=np.bool)
            mask[r-r_min, c-c_min] = False
            # combine weights with features
            patch[mask] = MASK_VAL
            patch += (dist_bias*(np.mean(patch[patch != MASK_VAL])/bias))
            extrema = baseline[(0,-1),:] - (c_min, r_min)
            # scale line image to max 600 pixel width
            scale = min(1.0, 600/(c_max-c_min))
            tform, rotated_patch = _rotate(patch, angle, center=extrema[0], scale=scale, cval=MASK_VAL)
            # ensure to cut off padding after rotation
            x_offsets = np.sort(np.around(tform.inverse(extrema)[:,0]).astype('int'))
            rotated_patch = rotated_patch[:,x_offsets[0]+1:x_offsets[1]]
            # infinity pad for seamcarve
            rotated_patch = np.pad(rotated_patch, ((1, 1), (0, 0)),  mode='constant', constant_values=np.inf)
            r, c = rotated_patch.shape
            # fold into shape (c, r-2 3)
            A = np.lib.stride_tricks.as_strided(rotated_patch, (c, r-2, 3), (rotated_patch.strides[1],
                                                                             rotated_patch.strides[0],
                                                                             rotated_patch.strides[0]))
            B = rotated_patch[1:-1,1:].swapaxes(0, 1)
            backtrack = np.zeros_like(B, dtype='int')
            T = np.empty((B.shape[1]), 'f')
            R = np.arange(-1, len(T)-1)
            for i in np.arange(c-1):
                A[i].min(1, T)
                backtrack[i] = A[i].argmin(1) + R
                B[i] += T
            # backtrack
            seam = []
            j = np.argmin(rotated_patch[1:-1,-1])
            for i in range(c-2, 0, -1):
                seam.append((i+x_offsets[0]+1, j))
                j = backtrack[i, j]
            seam = np.array(seam)[::-1]
            # rotate back
            seam = tform(seam).astype('int')
            # filter out seam points in masked area of original patch/in padding
            seam = seam[seam.min(axis=1)>=0,:]
            m = (seam < mask.shape[::-1]).T
            seam = seam[np.logical_and(m[0], m[1]), :]
            seam = seam[np.invert(mask[seam.T[1], seam.T[0]])]
            seam += (c_min, r_min)
            return seam

        upper_seam = _calc_seam(upper_polygon).astype('int')
        bottom_seam = _calc_seam(bottom_polygon).astype('int')[::-1]
        polygon = np.concatenate(([baseline[0]], upper_seam, [baseline[-1]], bottom_seam))
        return approximate_polygon(polygon, 3).tolist()

    polygons = []
    if suppl_obj is None:
        suppl_obj = []
    for idx, line in enumerate(baselines):
        try:
            # find intercepts with image bounds on each side of baseline
            line = np.array(line, dtype=np.float)
            # calculate magnitude-weighted average direction vector
            lengths = np.linalg.norm(np.diff(line.T), axis=0)
            p_dir = np.mean(np.diff(line.T) * lengths/lengths.sum(), axis=1)
            p_dir = (p_dir.T / np.sqrt(np.sum(p_dir**2,axis=-1)))
            # interpolate baseline
            ls = geom.LineString(line)
            ip_line = [line[0]]
            dist = 10
            while dist < ls.length:
                ip_line.append(np.array(ls.interpolate(dist)))
                dist += 10
            ip_line.append(line[-1])
            ip_line = np.array(ip_line)
            upper_bounds_intersects = []
            bottom_bounds_intersects = []
            for point in ip_line:
                upper_bounds_intersects.append(_ray_intersect_boundaries(point, (p_dir*(-1,1))[::-1], bounds+1).astype('int'))
                bottom_bounds_intersects.append(_ray_intersect_boundaries(point, (p_dir*(1,-1))[::-1], bounds+1).astype('int'))
            # build polygon between baseline and bbox intersects
            upper_polygon = geom.Polygon(ip_line.tolist() + upper_bounds_intersects)
            bottom_polygon = geom.Polygon(ip_line.tolist() + bottom_bounds_intersects)

            # select baselines at least partially in each polygon
            side_a = [geom.LineString(upper_bounds_intersects)]
            side_b = [geom.LineString(bottom_bounds_intersects)]
            for adj_line in baselines[:idx] + baselines[idx+1:] + suppl_obj:
                adj_line = geom.LineString(adj_line)
                if upper_polygon.intersects(adj_line):
                    side_a.append(adj_line)
                elif bottom_polygon.intersects(adj_line):
                    side_b.append(adj_line)
            side_a = unary_union(side_a).buffer(1).boundary
            side_b = unary_union(side_b).buffer(1).boundary
            def _find_closest_point(pt, intersects):
                spt = geom.Point(pt)
                if intersects.type == 'MultiPoint':
                    return min([p for p in intersects], key=lambda x: spt.distance(x))
                elif intersects.type == 'Point':
                    return intersects
                elif intersects.type == 'GeometryCollection' and len(intersects) > 0:
                    t = min([p for p in intersects], key=lambda x: spt.distance(x))
                    if t == 'Point':
                        return t
                    else:
                        return nearest_points(spt, t)[1]
                else:
                    raise Exception('No intersection with boundaries. Shapely intersection object: {}'.format(intersects.wkt))
            # interpolate baseline
            env_up = []
            env_bottom = []
            # find orthogonal (to linear regression) intersects with adjacent objects to complete roi
            for point, upper_bounds_intersect, bottom_bounds_intersect in zip(ip_line, upper_bounds_intersects, bottom_bounds_intersects):
                upper_limit = _find_closest_point(point, geom.LineString([point, upper_bounds_intersect]).intersection(side_a))
                bottom_limit = _find_closest_point(point, geom.LineString([point, bottom_bounds_intersect]).intersection(side_b))
                env_up.append(upper_limit.coords[0])
                env_bottom.append(bottom_limit.coords[0])
            env_up = np.array(env_up, dtype='uint')
            env_bottom = np.array(env_bottom, dtype='uint')
            polygons.append(_extract_patch(env_up, env_bottom, line.astype('int'), p_dir))
        except Exception as e:
            polygons.append(None)

    if scale is not None:
        polygons = [(np.array(pol)/scale).astype('uint').tolist() if pol is not None else None for pol in polygons]
    return polygons
コード例 #60
-1
def elastic_distortion(image, alpha, sigma):
    s = image.shape
    dx = gaussian_filter(random_state.rand(*s) * 2 - 1, sigma, mode="constant", cval=0) * alpha
    dy = gaussian_filter(random_state.rand(*s) * 2 - 1, sigma, mode="constant", cval=0) * alpha
    x, y = np.meshgrid(np.arange(s[0]), np.arange(s[1]))
    indices = np.reshape(y+dy, (-1, 1)), np.reshape(x+dx, (-1, 1))
    return map_coordinates(image, indices, order=1).reshape(s)