def calculate_sift_grid(self,image,gridH,gridW):
        '''
        This function calculates the unnormalized sift features
        It is called by process_image().
        '''
        H,W = image.shape
        Npatches = gridH.size
        feaArr = np.zeros((Npatches,Nsamples*Nangles))

        # calculate gradient
        GH,GW = Gen_DGauss(self.sigma)
        IH = signal.convolve2d(image,GH,mode='same')
        IW = signal.convolve2d(image,GW,mode='same')
        Imag = np.sqrt(IH**2+IW**2)
        Itheta = np.arctan2(IH,IW)
        Iorient = np.zeros((Nangles,H,W))
        for i in range(Nangles):
            Iorient[i] = Imag * np.maximum(np.cos(Itheta - angles[i])**alpha,0)
            #pyplot.imshow(Iorient[i])
            #pyplot.show()
        for i in range(Npatches):
            currFeature = np.zeros((Nangles,Nsamples))
            for j in range(Nangles):
                currFeature[j] = np.dot(self.weights,\
                        Iorient[j,gridH[i]:gridH[i]+self.pS, gridW[i]:gridW[i]+self.pS].flatten())
            feaArr[i] = currFeature.flatten()
        return feaArr
Example #2
0
	def ssim(self, img1, img2, K = (0.01, 0.03), L = 255):

		img1 = img1.astype(float)
		img2 = img2.astype(float)

		C1 = (K[0]*L) ** 2
		C2 = (K[1]*L) ** 2
		
		window = fspecial()
		window /= window.sum()

		mu1 = sc.convolve2d( img1, window, mode ='valid')
		mu2 = sc.convolve2d( img2, window, mode = 'valid')

		mu1_sq = mu1 * mu1
		mu2_sq = mu2 * mu2
		mu1_mu2 = mu1 * mu2
		
		sigma1_sq = sc.convolve2d( img1*img1, window,  mode = 'valid') - mu1_sq;
		sigma2_sq = sc.convolve2d( img2*img2, window,  mode = 'valid') - mu2_sq;
		sigma12 = sc.convolve2d( img1*img2, window, mode = 'valid') - mu1_mu2;

		ssim_map = 	((2*mu1_mu2 + C1)*(2*sigma12 + C2))/((mu1_sq + mu2_sq + C1)*(sigma1_sq + sigma2_sq + C2))

		return ssim_map.mean()
Example #3
0
    def test_conv_halide(self):
        """Test convolution lin op in halide.
        """

        #Load image
        testimg_filename = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'data', 'angela.jpg' )
        img = Image.open(testimg_filename)  # opens the file using Pillow - it's not an array yet
        np_img = np.asfortranarray( im2nparray(img) )

        #Convert to gray
        np_img = np.mean( np_img, axis=2)

        #Test problem
        output = np.zeros_like(np_img);
        K = np.ones((11,11), dtype=np.float32, order='FORTRAN')
        K /= np.prod( K.shape )

        #Convolve in halide
        Halide('A_conv.cpp').A_conv(np_img, K, output)

        #Convolve in scipy
        output_ref = signal.convolve2d(np_img,K, mode='same', boundary='wrap')

        #Transpose
        output_corr = np.zeros_like(np_img);
        Halide('At_conv.cpp').At_conv(np_img, K, output_corr) #Call

        output_corr_ref = signal.convolve2d(np_img, np.flipud(np.fliplr(K)), mode='same', boundary='wrap')

        self.assertItemsAlmostEqual(output, output_ref)
        self.assertItemsAlmostEqual(output_corr, output_corr_ref)
Example #4
0
def deformed_patch_based(lim_patch_list, rim_patch_list, image_patch, patch_list):
    deformed_list = []
    #print len(image_patch)
    #print len(patch_list)
    #print np.mean((np.asarray(image_patch)-np.asarray(patch_list))**2)**0.5

    fx = np.asarray([[1.0, -1.0]], dtype='float')
    fy = np.asarray([[1.0], [-1.0]], dtype='float')

    for i in range(len(lim_patch_list)):
        grad_x = convolve2d(patch_list[i], fx, mode='same')*0.1
        grad_y = convolve2d(patch_list[i], fy, mode='same')*0.1

        grad_x[:, 0] = grad_x[:, 0]*0+1
        grad_y[0, :] = grad_y[0, :]*0+1
        # ====================== 这里手动构造梯度 ===================
        Def = Deformed.deformed_patch()
        cp, c = Def.deform(patch_list[i], image_patch[i],grad_x,grad_y)
        deformed_list.append(cp)
        #print np.mean((patch_list[i]-image_patch[i])**2)**0.5
        #print np.mean((cp-image_patch[i])**2)**0.5

        print "变形完成:%0.2f %%" % (i*1.0/len(lim_patch_list)*100)
    print np.mean((np.asarray(deformed_list)-np.asarray(patch_list))**2)**0.5
    return deformed_list
Example #5
0
def sharpen(img, k=11, lo_pass=True, min_diff=0.05, alpha=1.0):
    """
    Sharpens the input image with unsharp masking. See Wikipedia
    (http://en.wikipedia.org/wiki/Unsharp_masking) for details. Note that
    this function only changes pixel values by a maximum of max_difference
    at each iteration.

    Optionally applies a low-pass Gaussian filter at the end, 
    to reduce the effect of high frequency noise.
    """
    
    # sharpen each color channel separately
    out = np.zeros(img.shape)
    sharp_mask = bf.gauss_mask(k)
    blur_mask = bf.gauss_mask(2 * int(1.0 + (k - 1) / 4.0) + 1) 
    for i in range(0, img.shape[2]):
        blurred = convolve2d(img[:, :, i], sharp_mask, 
                             mode="same", boundary="symm")
        diff = img[:, :, i] - blurred
        scaled = alpha * diff

        if lo_pass:
            
            # if necessary, blur each color channel separately
            diff = convolve2d(diff, blur_mask, 
                              mode="same", boundary="symm")

        diff[np.abs(diff) < min_diff] = 0.0
        out[:, :, i] = img[:, :, i] + scaled


    # truncate to [0, 1]
    out = bf.truncate(out)  
    
    return out
Example #6
0
def sobelFilter(gray,threshold):
    sobelX = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
    sobelY = np.array([[1,1,1],[0,0,0],[-1,-1,-1]])

    gradX = signal.convolve2d(gray,sobelX,boundary='wrap',mode='same')
    gradY = signal.convolve2d(gray,sobelY,boundary='wrap',mode='same')
    edgeStrength = 0.5*(np.abs(gradX) + np.abs(gradY))
    edgeStrength *= 255/np.max(edgeStrength)

    #Applies threshold transformation to edges strength
    T = np.arange(256)
    T[T < threshold] = 0
    edgeStrength = edgeStrength.astype(np.uint8)
    edgeStrength = T[edgeStrength]

    directAngle = np.zeros(gray.shape)
    rows, columns = gray.shape
    for i in range(rows):
        for j in range(columns):
            if(gradX[i,j] != 0):
                directAngle[i,j] = np.arctan(gradY[i,j]/gradX[i,j])
            else:
                directAngle[i,j] = np.pi/2

    directAngle[directAngle < 0.0] += np.pi
    directAngle *= 180/np.pi
    directAngle = np.mod(directAngle.astype(np.uint8),180)

    return (edgeStrength,directAngle)
def convolucion_RGB(img, kernel):

    channelR = np.zeros((img.shape[0], img.shape[1]), "uint8")
    channelG = np.zeros((img.shape[0], img.shape[1]), "uint8")
    channelB = np.zeros((img.shape[0], img.shape[1]), "uint8")

    for x in range(img.shape[0]):
        for y in range(img.shape[1]):
            channelR[x, y] = img[x, y][0]
            channelG[x, y] = img[x, y][1]
            channelB[x, y] = img[x, y][2]

    matrixR = sg.convolve2d(channelR, kernel, "same")
    matrixG = sg.convolve2d(channelG, kernel, "same")
    matrixB = sg.convolve2d(channelB, kernel, "same")

    img_conv = np.zeros((matrixR.shape[0], matrixR.shape[1], 3), "double")

    for x in range(matrixR.shape[0]):
        for y in range(matrixR.shape[1]):
            img_conv[x, y, 0] = matrixR[x, y]
            img_conv[x, y, 1] = matrixG[x, y]
            img_conv[x, y, 2] = matrixB[x, y]

    return img_conv
Example #8
0
def run_cycle(vacancy_matrix, spin_matrix, non_vacants, vacants):
    global neighbour_count
    global spinup_count

    for k in range(N*N):
        # Pick an occupied site at random
        rand_occupied = random.randrange(len(non_vacants))
        (i,j) = non_vacants[rand_occupied]
        spin  = spin_matrix[i,j]
        happy = is_happy(spin, (i,j))
        #print ('%d,%d is happy?' % (i,j), happy)

        if not happy:
            # Not happy, trying to move
            rand_vacant = random.randrange(len(vacants))
            (newI,newJ) = vacants[rand_vacant]
         #   print('Trying to move to %d,%d' % (newI,newJ))
            happy = is_happy(spin, (newI, newJ))
            if (happy):
                #print('Yay he is happy, moving')
                vacancy_matrix[i,j] = 0
                vacancy_matrix[newI, newJ] = 1
                spin_matrix[i,j] = 0
                spin_matrix[newI, newJ] = spin

                non_vacants.pop(rand_occupied)
                vacants.pop(rand_vacant)
                non_vacants.append((newI,newJ))
                vacants.append((i,j))

                neighbour_count = signal.convolve2d(vacancy_matrix, NEIGHBORS, mode='same')
                spinup_count = signal.convolve2d(spin_matrix, NEIGHBORS, mode='same')
Example #9
0
 def feedforward(self, x=None):
     n = len(self.layers);
     if x is None:
         x = self.x;
     self.layers[0].a = [np.reshape(np.array(x) / 255.0, (self.inputsize[1], self.inputsize[2]))];
     inputmaps = self.inputsize[0];
     
     for l in range(1, n):
         self.layers[l].a = [];
         if self.layers[l].type == 'c':
             for j in range(self.layers[l].outputmaps):
                 rs, cs = np.shape(self.layers[l - 1].a[0]);
                 z = np.zeros((rs - self.layers[l].kernelsize + 1, cs - self.layers[l].kernelsize + 1));
                 for i in range(inputmaps):
                     z += convolve2d(self.layers[l - 1].a[i], self.layers[l].kernels[i][j], mode='valid');
                 self.layers[l].a.append(sigmoid(z + self.layers[l].b[j]));
             inputmaps = self.layers[l].outputmaps;
         elif self.layers[l].type == 's':
             for j in range(inputmaps):
                 z = convolve2d(self.layers[l - 1].a[j], np.ones((self.layers[l].scale, self.layers[l].scale)) / np.square(self.layers[l].scale), mode='valid');
                 self.layers[l].a.append(z[::self.layers[l].scale, ::self.layers[l].scale]);
         elif self.layers[l].type == 'r':
             fv = [];
             for j in range(len(self.layers[l - 1].a)):
                 rs, cs = np.shape(self.layers[l - 1].a[j]);
                 if j == 0:
                     fv = np.reshape(self.layers[l - 1].a[j], (rs * cs));
                 else:
                     fv = np.append(fv, np.reshape(self.layers[l - 1].a[j], (rs * cs)), axis = 1);
             self.layers[l].a = fv;
         elif self.layers[l].type == 'o':
             self.layers[l].a = sigmoid(np.mat(self.layers[l - 1].a) * np.mat(self.layers[l - 1].w).T + np.mat(self.layers[l - 1].b).T);
     
     return self.layers[n - 1].a;
def el_distor(im,  kernel_dim, Sigma, alpha):
    
    out = np.zeros(im.shape)
    
    displace_x = np.array([[random_integers(-1, 1) for x in xrange(im.shape[0])] \
                            for y in xrange(im.shape[1])]) * alpha
    displace_y = np.array([[random_integers(-1, 1) for x in xrange(im.shape[0])] \
                            for y in xrange(im.shape[1])]) * alpha
                   
    kernel = create_gaussian_kernel(kernel_dim, Sigma)
    #kernel = cv2.getGaussianKernel(kernel_dim,Sigma)
    displace_x = convolve2d(displace_x, kernel)
    displace_y = convolve2d(displace_y, kernel)
    
    for row in xrange(im.shape[1]):
        for col in xrange(im.shape[0]):
            low_x = row + int(math.floor(displace_x[row, col]))
            high_x = row + int(math.ceil(displace_x[row, col]))

            low_y = col + int(math.floor(displace_y[row, col]))
            high_y = col + int(math.ceil(displace_y[row, col]))

            if  high_x >= im.shape[1] -1 or high_y >= im.shape[0] - 1:
                continue

            res = im[low_x, low_y]/4 + im[low_x, high_y]/4 + \
                    im[high_x, low_y]/4 + im[high_x, high_y]/4

            out[row, col] = res   
    n,m = out.shape
    out = out.reshape(n*m,)    
    return out
Example #11
0
def depthwise_conv2d_python_nhwc(input_np, filter_np, stride, padding):
    """Depthwise convolution operator in nchw layout.

    Parameters
    ----------
    input_np : numpy.ndarray
        4-D with shape [batch, in_height, in_width, in_channel]

    filter_np : numpy.ndarray
        4-D with shape [filter_height, filter_width, in_channel, channel_multiplier]

    stride : list / tuple of 2 ints
        [stride_height, stride_width]

    padding : str
        'VALID' or 'SAME'

    Returns
    -------
    output_np : np.ndarray
        4-D with shape [batch, out_height, out_width, out_channel]
    """
    batch, in_height, in_width, in_channel = input_np.shape
    filter_height, filter_width, _, channel_multiplier = filter_np.shape
    if isinstance(stride, int):
        stride_h = stride_w = stride
    else:
        stride_h, stride_w = stride

    # calculate output shape
    if padding == 'VALID':
        out_channel = in_channel * channel_multiplier
        out_height = (in_height - filter_height) // stride_h + 1
        out_width = (in_width - filter_width) // stride_w + 1
        output_np = np.zeros((batch, out_height, out_width, out_channel))
        for i in range(batch):
            for j in range(out_channel):
                output_np[i, :, :, j] = signal.convolve2d(input_np[i, :, :, j//channel_multiplier], \
                    np.rot90(filter_np[:, :, j//channel_multiplier, j%channel_multiplier], 2), \
                    mode='valid')[0:(in_height - filter_height + 1):stride_h, 0:(in_width - filter_height + 1):stride_w]
    if padding == 'SAME':
        out_channel = in_channel * channel_multiplier
        out_height = np.int(np.ceil(float(in_height) / float(stride_h)))
        out_width = np.int(np.ceil(float(in_width) / float(stride_w)))
        output_np = np.zeros((batch, out_height, out_width, out_channel))
        pad_along_height = np.int(np.max((out_height - 1) * stride_h + filter_height - in_height, 0))
        pad_along_width = np.int(np.max((out_width - 1) * stride_w + filter_width - in_width, 0))
        pad_top_tvm = np.int(np.ceil(float(pad_along_height) / 2))
        pad_left_tvm = np.int(np.ceil(float(pad_along_width) / 2))
        pad_top_scipy = np.int(np.ceil(float(filter_height - 1) / 2))
        pad_left_scipy = np.int(np.ceil(float(filter_width - 1) / 2))
        index_h = pad_top_scipy - pad_top_tvm
        index_w = pad_left_scipy - pad_left_tvm
        for i in range(batch):
            for j in range(out_channel):
                output_np[i, :, :, j] = signal.convolve2d(input_np[i, :, :, j//channel_multiplier], \
                    np.rot90(filter_np[:, :, j//channel_multiplier, j%channel_multiplier], 2), \
                    mode='same')[index_h:in_height:stride_h, index_w:in_width:stride_w]

    return output_np
def log_likelihoodC(theta, x, y, data, var, size=21):
    """
    Logarithm of the likelihood function.
    """
    #unpack the parameters
    amplitude, center_x, center_y, radius, focus, width_x, width_y, width_d = theta

    #1)Generate a model Airy disc
    airy = models.AiryDisk2D(amplitude, center_x, center_y, radius)
    adata = airy.eval(x, y, amplitude, center_x, center_y, radius).reshape((size, size))

    #2)Apply Focus
    f = models.Gaussian2D(1., center_x, center_y, focus, focus, 0.)
    focusdata = f.eval(x, y, 1., center_x, center_y, focus, focus, 0.).reshape((size, size))
    focusmodel = signal.convolve2d(adata, focusdata, mode='same')

    #3)Apply CCD diffusion kernel
    kernel = np.array([[width_d, width_y, width_d],
                       [width_x, 1., width_x],
                       [width_d, width_y, width_d]])
    kernel /= kernel.sum()
    #model = ndimage.convolve(focusmodel, kernel)
    model = signal.convolve2d(focusmodel, kernel, mode='same').flatten()

    #true for Gaussian errors, but not really true here because of mixture of Poisson and Gaussian noise
    lnL = - 0.5 * np.sum((data - model)**2 / var)

    return lnL
def log_likelihood(theta, x, y, data, var, size):
    """
    Logarithm of the likelihood function.
    """
    #unpack the parameters
    peak, center_x, center_y, radius, focus, width_x, width_y = theta

    #1)Generate a model Airy disc
    amplitude = _amplitudeFromPeak(peak, center_x, center_y, radius, x_0=int(size[0]/2.-0.5), y_0=int(size[1]/2.-0.5))
    airy = models.AiryDisk2D(amplitude, center_x, center_y, radius)
    adata = airy.eval(x, y, amplitude, center_x, center_y, radius).reshape(size)

    #2)Apply Focus
    f = models.Gaussian2D(1., center_x, center_y, focus, focus, 0.)
    focusdata = f.eval(x, y, 1., center_x, center_y, focus, focus, 0.).reshape(size)
    model = signal.convolve2d(adata, focusdata, mode='same')

    #3)Apply CCD diffusion, approximated with a Gaussian
    CCDdata = np.array([[0.0, width_y, 0.0],
                        [width_x, (1.-width_y-width_y-width_x-width_x), width_x],
                        [0.0, width_y, 0.0]])
    model = signal.convolve2d(model, CCDdata, mode='same').flatten()

    #true for Gaussian errors
    #lnL = - 0.5 * np.sum((data - model)**2 / var)
    #Gary B. said that this should be from the model not data so recompute var (now contains rn**2)
    var += model.copy()
    lnL = - (np.size(var)*np.sum(np.log(var))) - (0.5 * np.sum((data - model)**2 / var))

    return lnL
def sobelEdgeFeaturesDigit(datum):
  from scipy import signal
  import numpy

  features = util.Counter()
  opx = numpy.array([[1, 0, -1],
                     [2, 0, -2],
                     [1, 0, -1]])
  opy = numpy.array([[1, 2, 1],
                     [0, 0, 0],
                     [-1, -2, -1]])
  pixels = numpy.empty([DIGIT_DATUM_WIDTH, DIGIT_DATUM_HEIGHT])

  for x in range(DIGIT_DATUM_WIDTH):
    for y in range(DIGIT_DATUM_HEIGHT):
      pixels[x, y] = datum.getPixel(x, y)

  gx = signal.convolve2d(pixels, opx)
  gy = signal.convolve2d(pixels, opy)
  gx = numpy.absolute(gx)
  gy = numpy.absolute(gy)

  for x in range(DIGIT_DATUM_WIDTH):
    for y in range(DIGIT_DATUM_HEIGHT):
      mag = gx[x, y] + gy[x, y]
      features[(x, y, "edge")] = 1 if mag >= 3 else 0

  return features
Example #15
0
	def prewitt(self, gray_image):
		# Construct the mask
		kx = np.matrix('\
			-1 -1 -1;\
			 0  0  0;\
			 1  1  1 \
			') / 6.0

		ky = np.matrix('\
			-1  0  1;\
			-1  0  1;\
			-1  0  1 \
			') / 6.0

		# Convolute
		gx = convolve2d(gray_image, kx, 'same')
		gy = convolve2d(gray_image, ky, 'same')

		height,width = gray_image.shape[:2]
		result = gray_image.copy()
		max_p = 0
		for y in range(0, height):
			for x in range(0, width):
				result[y,x] = math.sqrt(gx[y,x]**2 + gy[y,x]**2)
				max_p = max(max_p, result[y,x])

		return np.uint8(result > 0.08995 * max_p) * 255
def log_likelihoodG(theta, x, y, data, var, size=21):
    """
    Logarithm of the likelihood function.
    """
    #unpack the parameters
    amplitude, center_x, center_y, radius, focus, width_x, width_y = theta

    #1)Generate a model Airy disc
    airy = models.AiryDisk2D(amplitude, center_x, center_y, radius)
    adata = airy.eval(x, y, amplitude, center_x, center_y, radius).reshape((size, size))

    #2)Apply Focus
    f = models.Gaussian2D(1., center_x, center_y, focus, focus, 0.)
    focusdata = f.eval(x, y, 1., center_x, center_y, focus, focus, 0.).reshape((size, size))
    model = signal.convolve2d(adata, focusdata, mode='same')

    #3)Apply CCD diffusion, approximated with a Gaussian
    CCD = models.Gaussian2D(1., size/2.-0.5, size/2.-0.5, width_x, width_y, 0.)
    CCDdata = CCD.eval(x, y, 1., size/2.-0.5, size/2.-0.5, width_x, width_y, 0.).reshape((size, size))
    model = signal.convolve2d(model, CCDdata, mode='same').flatten()

    #true for Gaussian errors, but not really true here because of mixture of Poisson and Gaussian noise
    lnL = - 0.5 * np.sum((data - model)**2 / var)

    return lnL
def test_GaussianKernel():
    # Gaussian kernel tests
    sourceImage = readImageFileRGB("ship-at-sea.jpg")
    grayImage = color.rgb2gray(sourceImage)
    
    xWindow = 9
    yWindow = 9
    sigma = 1.4
    xRange, yRange = createKernalWindowRanges(xWindow, yWindow, increment)
     
    g_kernel = gaussian_kernel(xWindow, yWindow, sigma)
    print "Gaussian kernel range:: ", np.min(g_kernel), np.max(g_kernel)
    plotKernel(xRange, yRange, g_kernel, "Gaussian kernel, sigma= + " + str(sigma) + ", window=(" + str(xWindow) + "," + str(yWindow) + ")")
    filteredImage = signal.convolve2d(grayImage, g_kernel, mode='same')
    plotImageComparison(grayImage, filteredImage)
   
    log_kernel = laplacianOfGaussian_kernel(xWindow, yWindow, sigma)
    print "Laplacian of Gaussian kernel range:: ", np.min(log_kernel), np.max(log_kernel)
    plotKernel(xRange, yRange, log_kernel, "LOG kernel, sigma= + " + str(sigma) + ", window=(" + str(xWindow) + "," + str(yWindow) + ")")
    filteredImage = signal.convolve2d(grayImage, log_kernel, mode='same')
    plotImageComparison(grayImage, filteredImage)
    
    g_dx_kernel = gaussian_1xDerivative_kernel(xWindow, yWindow, sigma)
    print "Gaussian X derivative kernel range:: ", np.min(g_dx_kernel), np.max(g_dx_kernel)
    plotKernel(xRange, yRange, g_dx_kernel, "G_dx kernel, sigma= + " + str(sigma) + ", window=(" + str(xWindow) + "," + str(yWindow) + ")")
    filteredImage = signal.convolve2d(grayImage, g_dx_kernel, mode='same')
    plotImageComparison(grayImage, filteredImage)

    g_dy_kernel = gaussian_1yDerivative_kernel(xWindow, yWindow, sigma)
    print "Gaussian Y derivative kernel range:: ", np.min(g_dy_kernel), np.max(g_dy_kernel)
    plotKernel(xRange, yRange, g_dy_kernel, "G_dy kernel, sigma= + " + str(sigma) + ", window=(" + str(xWindow) + "," + str(yWindow) + ")")
    filteredImage = signal.convolve2d(grayImage, g_dy_kernel, mode='same')
    plotImageComparison(grayImage, filteredImage)
Example #18
0
def gradient_magnitude(image):
    """
    Returns the L1 gradient magnitude of the given image.
    The result is an n x m numpy array of floating point values,
    where n is the number of rows in the image and m is the number of columns.
    """
    # First, convert the input image to a 32-bit floating point grayscale.
    # Be sure to scale it such that the intensity varies between 0 and 1.
    Is = np.mean(image,axis=2,dtype=np.float64)
    Is = normalize2D(Is)
    
    # Next, compute the graient in the x direction using the sobel operator with a kernel size of 5
    sobelX = [[2.0,1.0,0.0,-1.0,-2.0],
              [3.0,2.0,0.0,-2.0,-3.0],
              [4.0,3.0,0,-3.0,-4.0],
              [3.0,2.0,0.0,-2.0,-3.0],
              [2.0,1.0,0.0,-1.0,-2.0]]
    #gradientX = applyConvolution(Is,sobelX) #apparently there's a np.convolute2d - and it is so much faster!
    gradientX = signal.convolve2d(Is,sobelX,mode='same')
    # Compute the graient in the y direction using the sobel operator with a kernel size of 5
    gradientY = signal.convolve2d(Is,np.transpose(sobelX),mode='same')
    #gradientY = applyConvolution(Is,np.transpose(sobelX))    

    # Finally, compute the l1 norm of the x and y gradients at each pixel value.
    # The l1 norm is the sum of their absolute values.
    # convert the final image from a double-precision floating point to single.
    gradientX = np.absolute(gradientX)
    gradientY = np.absolute(gradientY)
    energy = gradientX + gradientY
    
    print 'found energy ',energy.shape

    # and return the result
    return energy
Example #19
0
def run_edges(image):
  ''' This function finds and colors all edges in the given image.
  '''

  # Convert image to gray
  if len(image.shape) > 2:
    grayimage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  else:
    grayimage = image

  # blur so the gradient operation is less noisy.
  # uses a gaussian filter with sigma = 2
  grayimage = gaussian_filter(grayimage, 2).astype(float)

  # Filter with x and y sobel filters
  dx = convolve2d(grayimage, sobel_filter_x())
  dy = convolve2d(grayimage, sobel_filter_y())

  # Convert to orientation and magnitude images
  theta = transform_xy_theta(dx, dy)
  mag = transform_xy_mag(dx, dy)

  outimg = np.zeros((image.shape[0], image.shape[1], 3), dtype = np.uint8)

  # Fill with corresponding color.
  for r in range(outimg.shape[0]):
    for c in range(outimg.shape[1]):
      outimg[r,c,:] = get_color(theta[r,c], mag[r,c])

  return outimg
Example #20
0
def extract(im, keypoints):
	for keypoint in keypoints:
		position = keypoint["position"]
		scale = keypoint["scale"]

		s = 20*scale
		x = position[0]
		y = position[1]

		window = im[y-s/2:y+s/2, x-s/2:x+s/2]

		ks = int(2*scale)
		kdx = np.ones((ks, ks), dtype=np.double)
		kdx[:,:ks/2] = -1
		kdy = np.ones((ks, ks), dtype=np.double)
		kdy[:ks/2,:] = -1

		des = []
		ws = s/4

		for i in range(4):
			for j in range(4):
				subwindow = window[i*ws:(i+1)*ws, j*ws:(j+1)*ws]

				dx = signal.convolve2d(subwindow, kdx, mode='valid')
				dy = signal.convolve2d(subwindow, kdy, mode='valid')

				des+=[np.sum(dx), np.sum(dy), np.sum(np.abs(dx)), np.sum(np.abs(dy))]

		sumsq=math.sqrt(sum(x*x for x in des))

		for i in range(len(des)):
			des[i] = des[i]/sumsq

		keypoint["des"] = des
Example #21
0
    def convolve(self, npImage, filters):
        """
        Return the 2D convolution of each colorband by its respective filter.

        Parameters
        ----------
        npImage : 3D numpy array where the dimensions represent respectively
        the height, the width and the colorbands. There must be 3 colorbands.
            The image to filter
        filters : a triplet of filters of the same size. The filters are
        2D array like structure (usually numpy array).
            The respective filters. They are applied in the same order to the
            colorbands.

        Return
        ------
        filtered :3D numpy array where the dimensions represent respectively
        the height, the width and the colorbands
            The result of the convolution of each colorband by its respective
            filter
        """
        redFilter, greenFilter, blueFilter = filters
        red, green, blue = npImage[:,:,0], npImage[:,:,1], npImage[:,:,2]

        newRed = sg.convolve2d(red, redFilter, "same")
        newGreen = sg.convolve2d(green, greenFilter, "same")
        newBlue = sg.convolve2d(blue, blueFilter, "same")

        return np.dstack((newRed, newGreen, newBlue))
Example #22
0
    def __init__(self, image, shape, sig):
        self.sig = sig
        self.image = cv2.imread(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
        self.g_masks = [cv2.GaussianBlur(self.image, (x, 1), sig) for x in range(1,31,4)]
        #print self.g_masks
        #print self.k_masks
        #return 0
        # criar convultions da imagem para cada uma das masks
        self.k_convultions = [signal.convolve2d(self.image, self.k_masks[x]) for x in range(len(self.k_masks))]
        self.g_convultions = [signal.convolve2d(self.image, self.g_masks[x]) for x in range(len(self.g_masks))]
        self.ldnk = np.array([[0 for x in self.image] for x in self.image[0]])
        self.ldng = np.array([[0 for x in self.image] for x in self.image[0]])
        for x in range(len(self.image)):
            for y in range(len(self.image[0])):
                self.ldnk[x][y] = self.LDNk(x,y)
                self.ldng[x][y] = self.LDNg(x,y)

        # histogramas
        lenx = len(self.ldnk)
        leny = len(self.ldnk[0])
        self.ldnk = []
        
        for i in range(5):
            for j in range(5):
                cur = self.ldnk[i*lenx/5:i*lenx/5 + lenx/5, j*leny/5: j*leny/5 + leny/5]
                hist, bin_edges = np.histogram(cur, 5, density=True, weights=cur)
                ret = np.frombuffer(hist * np.diff(bin_edges))
                self.ldnk = np.concatenate((self.ldnk, ret))
                cur = ldng[i*lenx/5:i*lenx/5 + lenx/5, j*leny/5: j*leny/5 + leny/5]
                hist, bin_edges = np.histogram(cur, 5, density=True, weights=cur)
                ret = np.frombuffer(hist * np.diff(bin_edges))
                self.ldng = np.concatenate((self.ldng, ret))
Example #23
0
def twoDsmooth(mat,ker):
    try:
        len(ker)
        kmat = ker
        
    except:
        kmat = np.ones((ker,ker))
        kmat = kmat / pow(ker, 2)

    [kr,kc] = list(kmat.shape)
    if (kr%2 == 0):
        conmat = np.ones((2,1))
        kmat = signal.convolve2d(kmat,conmat,'symm','same')
        kr = kr + 1

    if (kc%2 == 0):
        conmat = np.ones((2,1))
        kmat = signal.convolve2d(kmat,conmat,'symm','same')
        kc = kc + 1

    [mr,mc] = list(mat.shape)
    fkr = math.floor(kr/2)
    fkc = math.floor(kc/2)
    rota = np.rot90(kmat,2)
    mat=signal.convolve2d(mat,rota,'same','symm')
    return mat
Example #24
0
    def __init__(self, phasemap, thre):
        assert thre > 0 and thre < 1.0

        super(BrayPSMap, self).__init__(*phasemap.data.shape)
        
        nablaX = np.array([ [-0.5,  0, 0.5], [ -1, 0, 1], [-0.5,  0, 0.5] ], dtype = np.float32)
        nablaY = np.array([ [ 0.5,  1, 0.5], [  0, 0, 0], [-0.5, -1,-0.5] ], dtype = np.float32)
        
        def phaseComplement(phase_diff):
            out = np.copy(phase_diff)
            mask = (phase_diff > np.pi )*1
            out -= mask*(2*np.pi)
            mask = (phase_diff < -np.pi )*1
            out += mask*(2*np.pi)
            return out
        
        diffX = np.zeros_like(phasemap.data)
        diffY = np.zeros_like(phasemap.data)
        diffX[:,:,1:] = phaseComplement( phasemap.data[:,:,1:] - phasemap.data[:,:,0:-1])
        diffY[:,1:,:] = phaseComplement( phasemap.data[:,1:,:] - phasemap.data[:,0:-1,:])
        
        for frame in range(self.data.shape[0]):
            img_dx = diffX[frame, :,:]
            img_dy = diffY[frame, :,:]
            img_x = convolve2d(img_dx, nablaY, boundary='symm', mode='same')
            img_y = convolve2d(img_dy, nablaX, boundary='symm', mode='same')
            self.data[frame, :, :] = ((np.abs(img_y+img_x)/(2*np.pi))>thre)*1.0

        self.roi = np.copy(phasemap.roi)
        self.roi = scipy.ndimage.binary_erosion(self.roi, structure=np.ones((2,2))).astype(phasemap.roi.dtype)
        self.data *= self.roi

        self.vmin = 0.0
        self.vmax = 1.0
        self.cmap = 'gray'
Example #25
0
def ldp(image):
	x,y = image[:,:,0].shape
	#image = image[:,:,2]	
	image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY);
	ldp_image = np.zeros((x,y,8),dtype='int')
	mask1 = np.array([[-3,-3,5],[-3,0,5],[-3,-3,5]],dtype=int);
	mask2 = np.array([[-3,5,5],[-3,0,5],[-3,-3,-3]],dtype=int);
	for i in xrange(4):
		m1 = mask1;
		m2 = mask2;
		for j in xrange(i):
			m1 = np.rot90(m1);
			m2 = np.rot90(m2);
		ldp_image[:,:,2*i] = convolve2d(image,m1,mode='same');
		ldp_image[:,:,2*i+1] = convolve2d(image,m2,mode='same');
	ldp_hist = np.zeros(image.shape,dtype='uint8');
	max_image = np.zeros(image.shape,dtype='uint8')
	for i in xrange(x):	
		for j in xrange(y):
			a = ldp_image[i,j,:];
			a = abs(a);
			b = np.sort(a);
			thresh = b[4];
			a = a -thresh;
			a[a>0] = 1;
			a[a<=0] = 0;
			c = np.packbits(a,axis=None);
			ldp_hist[i,j] = int(c);
			max_image[i,j] = b[0];
	return(ldp_image,ldp_hist,max_image);
Example #26
0
    def __calcHarrisMat__(self):
        self.__findGradients__()
        # M = SUM( 3X3 window )
        # H = det(M) - k*(trace(M))*(trace(M))
        # k = 0.04 <=> 0.06 , we will assume it is 0.05
        # det(M) = (Ix*Ix)*(Iy*Iy) - (Ix*Iy)*(Ix*Iy) ,  trace(M) = (Ix*Ix) + (Iy*Iy)
        gaussianKernel1D = cv2.getGaussianKernel(3, self.sigma)
        window = gaussianKernel1D * gaussianKernel1D.transpose()
        # window = np.array([ [ 1 , 1 , 1 ] ,
        #                     [ 1 , 1 , 1 ] ,
        #                     [ 1 , 1 , 1 ] ])

        gradXSquared = self.gradientX * self.gradientX
        gradYSquared = self.gradientY * self.gradientY
        gradXgradY = self.gradientX * self.gradientY

        # Calculate the summation of the window's value ( IxIx, IyIy, IxIy)

        mIxIx = convolve2d(gradXSquared, window, mode='same')
        mIyIy = convolve2d(gradYSquared, window, mode='same')
        mIxIy = convolve2d(gradXgradY, window, mode='same')

        # Calculate the |M|
        detOfMatrix = (mIxIx * mIyIy) - (mIxIy * mIxIy)
        # Calculate the trace()
        traceOfMatrix = mIxIx + mIyIy

        self.responseMat = detOfMatrix - 0.05 * traceOfMatrix * traceOfMatrix
Example #27
0
def fresh():
    URL = 'http://img2.timeinc.net/people/i/2014/sandbox/news/140630/1994/will-smith-600x450.jpg'
    file = cStringIO.StringIO(urllib2.urlopen(URL).read())
    firstIm = Image.open(file)
    im = firstIm.convert('L')
    #im.show()
    im_array = np.asarray(im)
    im = im.convert('RGB')
    fresh_array = im_array.copy()
    #using 2d gaussian filter
    prince_convolved = gaussconvolve2d(fresh_array, 3)
    prince = Image.fromarray(prince_convolved)
    prince = prince.convert('RGB')
    #prince.show()
    #using box filter
    princebox = signal.convolve2d(fresh_array,boxfilter(19),'same')
    princebox = Image.fromarray(princebox)
    princebox = princebox.convert('RGB')
    #sharpening box filter
    princesharp = signal.convolve2d(fresh_array,sharpen(19),'same')
    princesharp = Image.fromarray(princesharp)
    princesharp = princesharp.convert('RGB')
    #display images side by side comparing gaussian and box filter at the bottom
    dims = (im.size[0]*2, im.size[1]*2)
    compare = Image.new('RGB', dims)
    compare.paste(im, (0,0))
    compare.paste(princesharp, (im.size[0],0))
    compare.paste(prince, (0, im.size[1]))
    compare.paste(princebox, (im.size[0], im.size[1]))
    return compare
Example #28
0
 def _get_riesz_triple(self, img):
     """:param img: 2D input image of shape (h, w)
     :return: 3D image of shape (h, w, 3), where the 3 channels correspond to
     amplitude, phase * cos(orient), phase * sin(orient)
     """
     assert img.ndim == 2
     img = img.astype(floatX)
     r1 = signal.convolve2d(img, self.riesz_kernel, mode='valid')
     r2 = signal.convolve2d(img, self.riesz_kernel.T, mode='valid')
     kh, kw = self.riesz_kernel.shape
     assert kh % 2 == 1 and kw % 2 == 1 and kh == kw
     img = img[kh/2:-(kh/2), kw/2:-(kw/2)]
     amp = np.sqrt(np.square(img) + np.square(r1) + np.square(r2))
     phase = np.arccos(img / (amp + self.EPS))
     t = np.sqrt(np.square(r1) + np.square(r2)) + self.EPS
     v0 = phase * r1 / t
     v1 = phase * r2 / t
     if self.spatial_blur:
         amp_blur = cv2.GaussianBlur(
             amp, self.spatial_ksize, self.spatial_blur)
         def blur(v):
             a = cv2.GaussianBlur(
                 amp * v, self.spatial_ksize, self.spatial_blur)
             return a / amp_blur
         v0 = blur(v0)
         v1 = blur(v1)
     rst = np.concatenate(map(lambda a: np.expand_dims(a, axis=2),
                              (amp, v0, v1)), axis=2)
     assert np.all(np.isfinite(rst))
     return rst
Example #29
0
def simple_sift(patch, grid_number):

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

    sift_orientation_histogram_bins = np.linspace(-np.pi, np.pi, 9)

    magnitude_weights = np.sqrt(I_x**2 + I_y**2)

    orientation = np.arctan2(I_y, I_x)

    magnitude_weights_blocks = split_matrix_into_blocks(magnitude_weights, grid_number)
    orientation_blocks = split_matrix_into_blocks(orientation, grid_number)

    descriptor = []

    for magnitude_weight_block, orientation_block in zip(magnitude_weights_blocks, orientation_blocks):

        block_descriptor, _ = np.histogram(orientation_block, bins=sift_orientation_histogram_bins, weights=magnitude_weight_block)

        descriptor.extend(block_descriptor)

    descriptor = np.asarray(descriptor)
    descriptor = descriptor / norm(descriptor)
    descriptor[descriptor > 0.2] = 0.2
    descriptor = descriptor / norm(descriptor)

    return descriptor
Example #30
0
def cnnbp(net, y):
    n = len(net.layers);
    
    net.e = net.o - y; # error
    net.L = 0.5 * np.sum(np.square(net.e)) / np.shape(net.e)[1];
    
    net.od = np.multiply(net.e, np.multiply(net.o, 1 - net.o));
    net.fvd = np.mat(net.od) * np.mat(net.ffw); # (50, 192)

    if net.layers[n-1]['type'] == 'c':
        net.fvd = np.multiply(net.fvd, np.multiply(net.fv, (1 - net.fv)));
    
    tm, tr, tc = np.shape(net.layers[n - 1]['a'][0]);
    fvnum = tr * tc;
    
    net.layers[n - 1]['d'] = [];
    for j in range(len(net.layers[n - 1]['a'])):
        net.layers[n - 1]['d'].append(np.array(net.fvd[:, j * fvnum : (j + 1) * fvnum]).reshape(tm, tr, tc));
    
    for l in range(n - 2, 0, -1):
        if net.layers[l]['type'] == 'c':
            net.layers[l]['d'] = [];
            for j in range(len(net.layers[l]['a'])):
                dtmp = [];
                for dl in range(len(net.layers[l]['a'][j])):
                    dtmp.append(np.multiply(np.multiply(net.layers[l]['a'][j][dl], 1 - net.layers[l]['a'][j][dl]),
                                (kronecker(net.layers[l + 1]['d'][j][dl], np.ones((net.layers[l + 1]['scale'],net.layers[l + 1]['scale'])) / np.square(float(net.layers[l + 1]['scale']))))));
                net.layers[l]['d'].append(dtmp);
        elif net.layers[l]['type'] == 's':
            net.layers[l]['d'] = [];
            for i in range(len(net.layers[l]['a'])):
                tm, tr, tc = np.shape(net.layers[l]['a'][0]);
                z = np.zeros((tm, tr, tc));
                for j in range(len(net.layers[l + 1]['a'])):
                    ztmp = [];
                    for dl in range(len(net.layers[l + 1]['d'][j])):
                        ztmp.append(convolve2d(net.layers[l + 1]['d'][j][dl], np.rot90(net.layers[l + 1]['k'][i][j], 2), mode='full'));
                    z += ztmp;
                net.layers[l]['d'].append(z);
    
    for l in range(1, n):
        if net.layers[l]['type'] == 'c':
            dk = [];
            for i in range(len(net.layers[l - 1]['a'])):
                dkj = [];
                for j in range(len(net.layers[l]['a'])):
                    tdk = [];
                    for dl in range(len(net.layers[l - 1]['a'][i])):
                        tdk.append(convolve2d(np.rot90(net.layers[l - 1]['a'][i][dl], 2), net.layers[l]['d'][j][dl], mode='valid'));
                    dkj.append(np.sum(tdk, 0) / np.shape(tdk)[0]);
                dk.append(dkj);
            net.layers[l]['dk'] = dk;
            
            net.layers[l]['db'] = [];
            for j in range(len(net.layers[l]['a'])):
                net.layers[l]['db'].append(np.sum(net.layers[l]['d'][j]) / np.shape(net.layers[l]['d'][j])[0]);
    
    net.dffw = np.mat(net.od).T * np.mat(net.fv) / np.shape(net.od)[0];
    net.dffb = np.mean(net.od, 0).T;
    return net;
Example #31
0
def _fast_kde_2d(x, y, gridsize=(128, 128), circular=False):
    """
    2D fft-based Gaussian kernel density estimate (KDE).

    The code was adapted from https://github.com/mfouesneau/faststats

    Parameters
    ----------
    x : Numpy array or list
    y : Numpy array or list
    gridsize : tuple
        Number of points used to discretize data. Use powers of 2 for fft optimization
    circular: bool
        If True, use circular boundaries. Defaults to False
    Returns
    -------
    grid: A gridded 2D KDE of the input points (x, y)
    xmin: minimum value of x
    xmax: maximum value of x
    ymin: minimum value of y
    ymax: maximum value of y
    """
    x = np.asarray(x, dtype=float)
    x = x[np.isfinite(x)]
    y = np.asarray(y, dtype=float)
    y = y[np.isfinite(y)]

    xmin, xmax = x.min(), x.max()
    ymin, ymax = y.min(), y.max()

    len_x = len(x)
    weights = np.ones(len_x)
    n_x, n_y = gridsize

    d_x = (xmax - xmin) / (n_x - 1)
    d_y = (ymax - ymin) / (n_y - 1)

    xyi = _stack(x, y).T
    xyi -= [xmin, ymin]
    xyi /= [d_x, d_y]
    xyi = np.floor(xyi, xyi).T

    scotts_factor = len_x**(-1 / 6)
    cov = _cov(xyi)
    std_devs = np.diag(cov**0.5)
    kern_nx, kern_ny = np.round(scotts_factor * 2 * np.pi * std_devs)

    inv_cov = np.linalg.inv(cov * scotts_factor**2)

    x_x = np.arange(kern_nx) - kern_nx / 2
    y_y = np.arange(kern_ny) - kern_ny / 2
    x_x, y_y = np.meshgrid(x_x, y_y)

    kernel = _stack(x_x.flatten(), y_y.flatten())
    kernel = _dot(inv_cov, kernel) * kernel
    kernel = np.exp(-kernel.sum(axis=0) / 2)
    kernel = kernel.reshape((int(kern_ny), int(kern_nx)))

    boundary = "wrap" if circular else "symm"

    grid = coo_matrix((weights, xyi), shape=(n_x, n_y)).toarray()
    grid = convolve2d(grid, kernel, mode="same", boundary=boundary)

    norm_factor = np.linalg.det(2 * np.pi * cov * scotts_factor**2)
    norm_factor = len_x * d_x * d_y * norm_factor**0.5

    grid /= norm_factor

    return grid, xmin, xmax, ymin, ymax
Example #32
0
        actual_image[x:x + 8, y:y + 8] = puz[(i, j)][0][1:-1, 1:-1]
print(actual_image)

with open('d20_actual_image.npy', 'wb') as f:
    np.save(f, actual_image)

with open('d20_actual_image.npy', 'rb') as f:
    acutal_image = np.load(f)

sea_monster_str = """                  # 
#    ##    ##    ###
 #  #  #  #  #  #   """
sea_monster = np.array([[1 if c == "#" else 0 for c in line]
                        for line in sea_monster_str.split('\n')])
sea_monster_count = np.sum(sea_monster)

from scipy.signal import convolve2d

searched_image = convolve2d(actual_image, sea_monster, mode='valid')

for oriented_monster in all_orientations(sea_monster):
    searched_image = convolve2d(actual_image, oriented_monster, mode='valid')
    sea_monster_squares = np.sum(
        searched_image == sea_monster_count) * sea_monster_count
    print(np.sum(actual_image) - sea_monster_squares)
    if np.max(searched_image) == sea_monster_count:
        sea_monster_squares = np.sum(
            searched_image == sea_monster_count) * sea_monster_count
        print(np.sum(actual_image) - sea_monster_squares)
        # break
Example #33
0
def lacosmic(sciframe,
             saturation,
             nonlinear,
             varframe=None,
             maxiter=1,
             grow=1.5,
             remove_compact_obj=True,
             sigclip=5.0,
             sigfrac=0.3,
             objlim=5.0):
    """
    Identify cosmic rays using the L.A.Cosmic algorithm
    U{http://www.astro.yale.edu/dokkum/lacosmic/}
    (article : U{http://arxiv.org/abs/astro-ph/0108003})
    This routine is mostly courtesy of Malte Tewes

    Args:
        sciframe:
        saturation:
        nonlinear:
        varframe:
        maxiter:
        grow:
        remove_compact_obj:
        sigclip (float):
            Threshold for identifying a CR
        sigfrac:
        objlim:

    Returns:
        ndarray: mask of cosmic rays (0=no CR, 1=CR)

    """
    msgs.info("Detecting cosmic rays with the L.A.Cosmic algorithm")
    #    msgs.work("Include these parameters in the settings files to be adjusted by the user")
    # Set the settings
    scicopy = sciframe.copy()
    crmask = np.cast['bool'](np.zeros(sciframe.shape))
    sigcliplow = sigclip * sigfrac

    # Determine if there are saturated pixels
    satpix = np.zeros_like(sciframe)
    #    satlev = settings_det['saturation']*settings_det['nonlinear']
    satlev = saturation * nonlinear
    wsat = np.where(sciframe >= satlev)
    if wsat[0].size == 0: satpix = None
    else:
        satpix[wsat] = 1.0
        satpix = np.cast['bool'](satpix)

    # Define the kernels
    laplkernel = np.array([[0.0, -1.0, 0.0], [-1.0, 4.0, -1.0],
                           [0.0, -1.0, 0.0]])  # Laplacian kernal
    growkernel = np.ones((3, 3))
    for i in range(1, maxiter + 1):
        msgs.info("Convolving image with Laplacian kernel")
        # Subsample, convolve, clip negative values, and rebin to original size
        subsam = utils.subsample(scicopy)
        conved = signal.convolve2d(subsam,
                                   laplkernel,
                                   mode="same",
                                   boundary="symm")
        cliped = conved.clip(min=0.0)
        lplus = utils.rebin_evlist(cliped, np.array(cliped.shape) / 2.0)

        msgs.info("Creating noise model")
        # Build a custom noise map, and compare  this to the laplacian
        m5 = ndimage.filters.median_filter(scicopy, size=5, mode='mirror')
        if varframe is None:
            noise = np.sqrt(np.abs(m5))
        else:
            noise = np.sqrt(varframe)
        msgs.info("Calculating Laplacian signal to noise ratio")

        # Laplacian S/N
        s = lplus / (2.0 * noise
                     )  # Note that the 2.0 is from the 2x2 subsampling

        # Remove the large structures
        sp = s - ndimage.filters.median_filter(s, size=5, mode='mirror')

        msgs.info("Selecting candidate cosmic rays")
        # Candidate cosmic rays (this will include HII regions)
        candidates = sp > sigclip
        nbcandidates = np.sum(candidates)

        msgs.info("{0:5d} candidate pixels".format(nbcandidates))

        # At this stage we use the saturated stars to mask the candidates, if available :
        if satpix is not None:
            msgs.info("Masking saturated pixels")
            candidates = np.logical_and(np.logical_not(satpix), candidates)
            nbcandidates = np.sum(candidates)

            msgs.info(
                "{0:5d} candidate pixels not part of saturated stars".format(
                    nbcandidates))

        msgs.info("Building fine structure image")

        # We build the fine structure image :
        m3 = ndimage.filters.median_filter(scicopy, size=3, mode='mirror')
        m37 = ndimage.filters.median_filter(m3, size=7, mode='mirror')
        f = m3 - m37
        f /= noise
        f = f.clip(min=0.01)

        msgs.info("Removing suspected compact bright objects")

        # Now we have our better selection of cosmics :

        if remove_compact_obj:
            cosmics = np.logical_and(candidates, sp / f > objlim)
        else:
            cosmics = candidates
        nbcosmics = np.sum(cosmics)

        msgs.info("{0:5d} remaining candidate pixels".format(nbcosmics))

        # What follows is a special treatment for neighbors, with more relaxed constains.

        msgs.info("Finding neighboring pixels affected by cosmic rays")

        # We grow these cosmics a first time to determine the immediate neighborhod  :
        growcosmics = np.cast['bool'](signal.convolve2d(
            np.cast['float32'](cosmics),
            growkernel,
            mode="same",
            boundary="symm"))

        # From this grown set, we keep those that have sp > sigmalim
        # so obviously not requiring sp/f > objlim, otherwise it would be pointless
        growcosmics = np.logical_and(sp > sigclip, growcosmics)

        # Now we repeat this procedure, but lower the detection limit to sigmalimlow :

        finalsel = np.cast['bool'](signal.convolve2d(
            np.cast['float32'](growcosmics),
            growkernel,
            mode="same",
            boundary="symm"))
        finalsel = np.logical_and(sp > sigcliplow, finalsel)

        # Unmask saturated pixels:
        if satpix is not None:
            msgs.info("Masking saturated stars")
            finalsel = np.logical_and(np.logical_not(satpix), finalsel)

        ncrp = np.sum(finalsel)

        msgs.info("{0:5d} pixels detected as cosmics".format(ncrp))

        # We find how many cosmics are not yet known :
        newmask = np.logical_and(np.logical_not(crmask), finalsel)
        nnew = np.sum(newmask)

        # We update the mask with the cosmics we have found :
        crmask = np.logical_or(crmask, finalsel)

        msgs.info(
            "Iteration {0:d} -- {1:d} pixels identified as cosmic rays ({2:d} new)"
            .format(i, ncrp, nnew))
        if ncrp == 0: break
    # Additional algorithms (not traditionally implemented by LA cosmic) to remove some false positives.
    msgs.work(
        "The following algorithm would be better on the rectified, tilts-corrected image"
    )
    filt = ndimage.sobel(sciframe, axis=1, mode='constant')
    filty = ndimage.sobel(filt / np.sqrt(np.abs(sciframe)),
                          axis=0,
                          mode='constant')
    filty[np.where(np.isnan(filty))] = 0.0

    sigimg = cr_screen(filty)

    sigsmth = ndimage.filters.gaussian_filter(sigimg, 1.5)
    sigsmth[np.where(np.isnan(sigsmth))] = 0.0
    sigmask = np.cast['bool'](np.zeros(sciframe.shape))
    sigmask[np.where(sigsmth > sigclip)] = True
    crmask = np.logical_and(crmask, sigmask)
    msgs.info("Growing cosmic ray mask by 1 pixel")
    crmask = grow_masked(crmask.astype(np.float), grow, 1.0)

    return crmask.astype(bool)
Example #34
0
2. 加過雜訊的用較小的gaussian去抹掉雜訊
平滑化 Gaussian Smooth
雜點消失
細節消失
'''
I, W, H, data = loadImage(filename)
#M = np.ones((20,20))/400
x, y = np.meshgrid(np.linspace(-1, 1, 10), np.linspace(-1, 1, 10))
d = np.sqrt(x * x + y * y)
sigma, mu = 0.2, 0.0
M = np.exp(-((d - mu)**2 / (2.0 * sigma**2)))
M = M / np.sum(M[:])
R = data[:, :, 0]
G = data[:, :, 1]
B = data[:, :, 2]
R2 = signal.convolve2d(R, M, boundary='symm', mode='same')
G2 = signal.convolve2d(G, M, boundary='symm', mode='same')
B2 = signal.convolve2d(B, M, boundary='symm', mode='same')

data2 = data.copy()
data2[:, :, 0] = R2.astype('uint8')
data2[:, :, 1] = G2.astype('uint8')
data2[:, :, 2] = B2.astype('uint8')

I2 = Image.fromarray(data2, 'RGB')
I2.show()

#%%
'''
3.
用大一點的gaussian去坐失焦圖
Example #35
0
def lpq(img_filepath, winSize=3, freqestim=1, mode='im'):

    img = cv2.imread(img_filepath, 0)
    rho = 0.90

    STFTalpha = 1 / winSize  # alpha in STFT approaches (for Gaussian derivative alpha=1)
    sigmaS = (winSize - 1
              ) / 4  # Sigma for STFT Gaussian window (applied if freqestim==2)
    sigmaA = 8 / (
        winSize - 1
    )  # Sigma for Gaussian derivative quadrature filters (applied if freqestim==3)

    convmode = 'valid'  # Compute descriptor responses only on part that have full neigborhood. Use 'same' if all pixels are included (extrapolates np.image with zeros).

    img = np.float64(img)  # Convert np.image to double
    r = (winSize - 1) / 2  # Get radius from window size
    x = np.arange(-r, r + 1)[np.newaxis]  # Form spatial coordinates in window

    if freqestim == 1:  #  STFT uniform window
        #  Basic STFT filters
        w0 = np.ones_like(x)
        w1 = np.exp(-2 * np.pi * x * STFTalpha * 1j)
        w2 = np.conj(w1)

    ## Run filters to compute the frequency response in the four points. Store np.real and np.imaginary parts separately
    # Run first filters
    filterResp1 = convolve2d(convolve2d(img, w0.T, convmode), w1, convmode)
    filterResp2 = convolve2d(convolve2d(img, w1.T, convmode), w0, convmode)
    filterResp3 = convolve2d(convolve2d(img, w1.T, convmode), w1, convmode)
    filterResp4 = convolve2d(convolve2d(img, w1.T, convmode), w2, convmode)

    # Initilize frequency domain matrix for four frequency coordinates (np.real and np.imaginary parts for each frequency).
    freqResp = np.dstack([
        filterResp1.real, filterResp1.imag, filterResp2.real, filterResp2.imag,
        filterResp3.real, filterResp3.imag, filterResp4.real, filterResp4.imag
    ])

    ## Perform quantization and compute LPQ codewords
    inds = np.arange(freqResp.shape[2])[np.newaxis, np.newaxis, :]
    LPQdesc = ((freqResp > 0) * (2**inds)).sum(2)

    ## Switch format to uint8 if LPQ code np.image is required as output
    if mode == 'im':
        LPQdesc = np.uint8(LPQdesc)

        pyplot.figure(figsize=(20, 10))
        pyplot.subplot(121)
        pyplot.imshow(img, cmap='gray')
        pyplot.title("Original image")

        pyplot.subplot(122)
        pyplot.imshow(LPQdesc, cmap='gray')
        pyplot.title("lpq")
        pyplot.show()

    ## Histogram if needed
    if mode == 'nh' or mode == 'h':
        LPQdesc = np.histogram(LPQdesc.flatten(), range(256))[0]

    ## Normalize histogram if needed
    if mode == 'nh':
        LPQdesc = LPQdesc / LPQdesc.sum()

    print(LPQdesc)
Example #36
0
# 2-d convolution-*-
"""
Created on Sun Aug 19 19:34:11 2018

@author: SRIKANT
"""


from scipy import signal

x=([1,0,0],[0,0,0],[0,0,0])
h=([1,1,0],[1,0,1],[1,0,0])
convolution=signal.convolve2d(x,h)
print(convolution)
Example #37
0
def sobel(img):
    r = np.array([[3, 0, -3], [10, 0, -10], [3, 0, -3]]) / 32
    d = r + r.T * 1j
    sob1 = signal.convolve2d(img, r, mode='valid')
    sob2 = signal.convolve2d(img, r.T, mode='valid')
    return sob1, sob2
Example #38
0
def elastic_transform(image, kernel_dim=13, sigma=6, alpha=36, negated=False):
    """
    Source: https://github.com/vsvinayak/mnist-helper/blob/master/mnist_helpers.py
    This method performs elastic transformations on an image by convolving
    with a gaussian kernel.
    NOTE: Image dimensions should be a sqaure image

    :param image: the input image
    :type image: a numpy nd array
    :param kernel_dim: dimension(1-D) of the gaussian kernel
    :type kernel_dim: int
    :param sigma: standard deviation of the kernel
    :type sigma: float
    :param alpha: a multiplicative factor for image after convolution
    :type alpha: float
    :param negated: a flag indicating whether the image is negated or not
    :type negated: boolean
    :returns: a nd array transformed image
    """
    # TEMP FIX
    import math

    from numpy.random import random_integers
    from scipy.signal import convolve2d
    import cv2

    # convert the image to single channel if it is multi channel one
    if image.ndim == 3:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # check if the image is a negated one
    if not negated:
        image = 255 - image

    # check if the image is a square one
    if image.shape[0] != image.shape[1]:
        raise ValueError("Image should be of sqaure form")

    # check if kernel dimesnion is odd
    if kernel_dim % 2 == 0:
        raise ValueError("Kernel dimension should be odd")

    # create an empty image
    result = np.zeros(image.shape)

    # create random displacement fields
    displacement_field_x = np.array(
        [[random_integers(-1, 1) for x in xrange(image.shape[0])]
         for y in xrange(image.shape[1])]) * alpha
    displacement_field_y = np.array(
        [[random_integers(-1, 1) for x in xrange(image.shape[0])]
         for y in xrange(image.shape[1])]) * alpha

    # create the gaussian kernel
    kernel = create_2d_gaussian(kernel_dim, sigma)

    # convolve the fields with the gaussian kernel
    displacement_field_x = convolve2d(displacement_field_x, kernel)
    displacement_field_y = convolve2d(displacement_field_y, kernel)

    # make the distortrd image by averaging each pixel value to the neighbouring
    # four pixels based on displacement fields

    for row in xrange(image.shape[1]):
        for col in xrange(image.shape[0]):
            low_ii = row + int(math.floor(displacement_field_x[row, col]))
            high_ii = row + int(math.ceil(displacement_field_x[row, col]))

            low_jj = col + int(math.floor(displacement_field_y[row, col]))
            high_jj = col + int(math.ceil(displacement_field_y[row, col]))

            if low_ii < 0 or low_jj < 0 or high_ii >= image.shape[1] - 1 \
               or high_jj >= image.shape[0] - 1:
                continue

            res = image[low_ii, low_jj]/4 + image[low_ii, high_jj]/4 + \
                image[high_ii, low_jj]/4 + image[high_ii, high_jj]/4

            result[row, col] = res

    # if the input image was not negated, make the output image also a non
    # negated one
    if not negated:
        result = 255 - result

    return result
def LoG(image,sigma,kSize,_boundary='fill',_fillValue = 0):
    #构建 LoG 卷积核
    loGKernel = createLoGKernel(sigma,kSize)
    #图像与 LoG 卷积核卷积
    img_conv_log = signal.convolve2d(image,loGKernel,'same',boundary =_boundary)
    return img_conv_log
def myCanny(image, tl, th):
    '''Canny edge detection algorithm 
    inputs : Grayscale image , tl : low threshold, th : high threshold
    output : Edge image or Canny image
    Basic steps of canny are : 
        1. Image smoothing using gaussian kernel for de-noising 
        2. Getting gradient magnitude image
        3. None maxima suppression: 
            Suppression of week edges at the same direction to have a thin edge
        4. Double thresholding : 
            Suppress globally weak edges that bellow tl, and keep that above th 
        5. Edge tracking:
            track remaining pixels with values in between tl and th. Suppress them
            if they haven't a strong edge in its neighbors.
    '''
    # 1. Image smoothing
    sigma = 1.4
    im1 = filters.gaussian_filter(image, (sigma, sigma))

    # 2. Getting gradient magnitude image
    sobelx = np.array([[-1, 0, 1],
                       [-2, 0, 2],
                       [-1, 0, 1]])
    sobely = sobelx.T

    Gx = signal.convolve2d(im1, sobelx, "same")
    Gy = signal.convolve2d(im1, sobely, "same")

    G = np.sqrt(Gx**2 + Gy**2)

    # 3. None maxima suppression
    # Getting gradient direction at first
    theta = np.arctan2(Gy, Gx)
    theta = 180 + (180/np.pi)*theta
    x0, y0 = np.where(((theta < 22.5)+(theta > 157.5)*(theta < 202.5)
                       + (theta > 337.5)) == True)
    x45, y45 = np.where(((theta > 22.5)*(theta < 67.5)
                         + (theta > 202.5)*(theta < 247.5)) == True)
    x90, y90 = np.where(((theta > 67.5)*(theta < 112.5)
                         + (theta > 247.5)*(theta < 292.5)) == True)
    x135, y135 = np.where(((theta > 112.5)*(theta < 157.5)
                           + (theta > 292.5)*(theta < 337.5)) == True)
    # Apply none-max suppression
    theta[x0, y0] = 0
    theta[x45, y45] = 1
    theta[x90, y90] = 2
    theta[x135, y135] = 3
    dirs = theta
    edgeCoords = np.array(G.nonzero()).T
    for c in edgeCoords:
        gradDir = dirs[c[0], c[1]]
        try:
            if gradDir == 0:
                idx = [[c[0], c[1]+1],
                       [c[0],  c[1]-1],
                       [c[0], c[1]+2],
                       [c[0], c[1]-2]]
            elif gradDir == 1:
                idx = [[c[0]+1, c[1]+1],
                       [c[0]-1,  c[1]-1],
                       [c[0]+2, c[1]+2],
                       [c[0]-2, c[1]-2]]
            elif gradDir == 2:
                idx = [[c[0]+1, c[1]],
                       [c[0]-1,  c[1]],
                       [c[0]+2, c[1]],
                       [c[0]-2, c[1]]]
            elif gradDir == 3:
                idx = [[c[0]+1, c[1]-1],
                       [c[0]-1,  c[1]+1],
                       [c[0]+2, c[1]-2],
                       [c[0]-2, c[1]+2]]
            for i in idx:
                if G[i[0], i[1]] > G[c[0], c[1]]:
                    G[c[0], c[1]] = 0
        except:
            pass

    # 4. Double Thresholding
    remainingEdges = np.array(G.nonzero()).T
    for e in remainingEdges:
        if G[e[0], e[1]] < tl:
            G[e[0], e[1]] = 0
        elif G[e[0], e[1]] > th:
            G[e[0], e[1]] = 255

    # 5. Edge tracking by hysteresis
    remEdges = np.array(G.nonzero()).T
    for re in remEdges:
        if G[re[0], re[1]] != 255:
            try:
                neighbors = G[re[0]-1:re[0]+2, re[1]-1:re[1]+2].flatten()
                if np.max(neighbors) == 255:
                    G[re[0], re[1]] = 255
                else:
                    G[re[0], re[1]] = 0
            except:
                G[re[0], re[1]] = 0
                continue
    return G
Example #41
0
def laplacian(img):
    lablacian_kernal = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]])

    image_laplacian = signal.convolve2d(img, lablacian_kernal, 'same')
    plotoutput(image_laplacian)
Example #42
0
    gauss_blur_filter[1][0] = 1 / 8
    gauss_blur_filter[1][1] = 1 / 4
    gauss_blur_filter[1][2] = 1 / 8
    gauss_blur_filter[2][0] = 1 / 16
    gauss_blur_filter[2][1] = 1 / 8
    gauss_blur_filter[2][2] = 1 / 16

    image = cv2.imread('hough.jpg')

    image_1 = cv2.imread('hough.jpg')

    image_2 = cv2.imread('hough.jpg')

    grey_image = cv2.imread('hough.jpg', 0)

    sharpen_image = signal.convolve2d(grey_image, gauss_blur_filter, 'same')

    image_edge_filtered = edge_detection_sobel(copy.deepcopy(sharpen_image))

    threshold_image = threshold_operation(copy.deepcopy(image_edge_filtered))

    cv2.imwrite('threshold.jpg', threshold_image)

    accumulator_Cells = houghTransform(copy.deepcopy(threshold_image))

    detect_red_lines_and_blue_lines(accumulator_Cells, image, 91, 87,
                                    'red_line.jpg', 15)

    detect_red_lines_and_blue_lines(accumulator_Cells, image_1, 58, 53,
                                    'blue_lines.jpg', 100)
Example #43
0
def prewit(img):
    image_prewit_h = signal.convolve2d(img, prewitt_h, 'same')
    image_prewit_v = signal.convolve2d(img, prewitt_v, 'same')
    gradient = np.sqrt(image_prewit_h * image_prewit_h +
                       image_prewit_v * image_prewit_v)
    plotoutput(gradient)
Example #44
0
def sharpen(img):
    sharpen_kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
    im_sharpened = np.ones(img.shape)
    im_sharpened = signal.convolve2d(img, sharpen_kernel, mode='same')
    plotoutput(im_sharpened)
def norm_bv( data_p , data_n , norm_type='None' , smooth='None' , sigma='None',cutoff='None' , xi='None' , xe='None' , yi='None' , ye='None', zi='None', ze='None'):
#Computes the bv in a similar way as the breeding fortran module does.
#Sigma is the standard deviation of the gaussian filter in units of grid points.
#Smooth is a filter option. Currently gaussian filter is supported.

    if norm_type == 'None' : #UVT, UV or T 
       norm_type = 'UVT'

    if smooth == 'None'    : #None, Gaussian or Lanczos
       filter_norm=False
    else                   :
       filter_norm=True 

    if sigma == 'None' or sigma==0  : 
       sigma = 1
       print('Warning : Sigma = 1 ')

    if cutoff == 'None'    : #Convolution function size for the Gaussian filter 
       cutoff=10

    tmp=np.array( data_p['U'] )
    tmp_shape = np.shape( tmp )
    nx=tmp_shape[0]
    ny=tmp_shape[1]
    nz=tmp_shape[2]
   
    norm=np.zeros((nx,ny,nz))

    if norm_type == 'UVT' :
       
       norm= norm + np.power( data_p['U'] - data_n['U'] , 2. )

       norm= norm + np.power( data_p['V'] - data_n['V'] , 2. )

       norm= norm + np.power( data_p['T'] - data_n['T'] , 2. )

    if norm_type == 'UV' :

       norm= norm + np.power( data_p['U'] - data_n['U'] , 2. )

       norm= norm + np.power( data_p['V'] - data_n['V'] , 2. )

    if norm_type == 'W' :

       norm= norm + np.power( data_p['W'] - data_n['W'] , 2. )

    if norm_type == 'T'  :

       norm= norm + np.power( data_p['T'] - data_n['T'] , 2. )

    if norm_type == 'QV'  :

       norm= norm + np.power( data_p['QV'] - data_n['QV'] , 2. )

    if norm_type == 'QHYD'  :

       norm= norm + np.power( data_p['QHYD'] - data_n['QHYD'] , 2. )

    if filter_norm :
      if smooth == 'Gaussian'  :
         filter_size=np.around( 2*cutoff*sigma , 0 ).astype(int)

         gaussian_conv_func=np.zeros([ 2*filter_size+1 , 2*filter_size+1 ])
         for ii in range(0,2*filter_size+1) : 
            for jj in range(0,2*filter_size+1) :
               gaussian_conv_func[ii,jj] = np.exp( -0.5*(np.power( ii-filter_size,2 ) + np.power( jj-filter_size, 2) ) /np.power(sigma,2)   )
       
         #Normalize the convolving function.
         gaussian_conv_func=gaussian_conv_func/np.sum( gaussian_conv_func)

         #a=plt.figure
         #plt.pcolor( gaussian_conv_func )
         #plt.show(a)


         for iz in range(0,nz) :

             norm[:,:,iz]=signal.convolve2d(norm[:,:,iz],gaussian_conv_func, boundary='symm', mode='same')

      #elif smooth == 'Lanczos'  :
      #   for iz in range(0,nz) :
      #       mask=np.ones((nx,ny))
      #       norm[:,:,iz]=s2d.filter_2d(inputvar2d=norm[:,:,iz],mask=mask,lambdaf=sigma,ctyp=smooth,nx=nx,ny=ny)
      else                      :   #TODO add lanczos 2D filter option.

         print('Smooth option not recognized, we will not smooth the norm')

    norm=np.power(norm,0.5)

    norm_mean , norm_max , norm_min = get_regional_average(norm,xi=xi,xe=xe,yi=yi,ye=ye,zi=zi,ze=ze)

    return norm_mean , norm_max , norm_min  , norm  #Generate a tuple as output.
Example #46
0
def setFilters(text):
    #prewit FILTER
    if dig.comboBox.currentIndex() == 1:
        prewit(dig.image)
#SOBEL FILTER
    if dig.comboBox.currentIndex() == 2:
        sobel(dig.image)
#laplacian FILTER
    if dig.comboBox.currentIndex() == 3:
        noisy1 = addnoise(dig.image)
        plotinput(noisy1)
        laplacian(dig.image)
#laplacian of gaussian FILTER
    if dig.comboBox.currentIndex() == 4:
        noisy2 = addnoise(dig.image)
        plotinput(noisy2)
        log(noisy2)
#difference of gaussian Filter
    if dig.comboBox.currentIndex() == 5:
        noisy = addnoise(dig.image)
        plotinput(noisy)
        dog(noisy)

#BOX FILTER
    if dig.comboBox.currentIndex() == 6:

        noisy = salt_n_pepper(dig.image)
        plotinput(salt_n_pepper(dig.image))

        filtered_img_box9 = signal.convolve2d(noisy, box_filter(9), 'same')
        plotoutput(filtered_img_box9)

#GAUSSIAN FILTER
    if dig.comboBox.currentIndex() == 7:

        noisy = salt_n_pepper(dig.image)
        plotinput(salt_n_pepper(dig.image))

        filtered_img_g7_std10 = signal.convolve2d(noisy,
                                                  gaussian_kernel(7,
                                                                  .3), 'same')
        plotoutput(filtered_img_g7_std10)

#MEDIAN FILTER
    if dig.comboBox.currentIndex() == 8:

        noisy = salt_n_pepper(dig.image)
        plotinput(salt_n_pepper(dig.image))

        med_image3 = ndimage.median_filter(noisy, (3, 3))
        plotoutput(med_image3)

#sharpen FILTER
    if dig.comboBox.currentIndex() == 9:
        sharpen(dig.image)

#sharpen FILTER
    if dig.comboBox.currentIndex() == 9:
        sharpen(dig.image)

#FT

    if dig.comboBox.currentIndex() == 10:
        dig.valueChannel = extractValueChannel(dig.image)
        dig.FT = fftpack.fft2(dig.valueChannel)
        v1 = np.log(1 + np.abs(dig.FT))
        plotoutput(v1)

#ShiftedFT
    if dig.comboBox.currentIndex() == 11:
        ShiftedFT = fftpack.fftshift(dig.FT)
        v2 = np.log(1 + np.abs(ShiftedFT))
        plotoutput(v2)

#Log effect on Shifted FT
    if dig.comboBox.currentIndex() == 12:
        dig.ShiftedFT = fftpack.fftshift(dig.FT)
        v3 = np.abs(dig.ShiftedFT)
        plotoutput(v3)

#LPF
    if dig.comboBox.currentIndex() == 13:
        LPF = generateFilter(dig.ShiftedFT, 0.5, 0.05, "LPF")
        plotoutput(LPF)


#HPF
    if dig.comboBox.currentIndex() == 14:
        HPF = generateFilter(dig.ShiftedFT, 0.025, 0.025, "HPF")
        plotoutput(HPF)
def Convolution1dy(Big, y_middle):
        Small = np.array([[1,y_middle,1]]).T
        return sig.convolve2d(Big,Small, mode='full', boundary='fill', fillvalue=0)
# importing necessary packages
from skimage import color
from skimage import exposure
import numpy as np
import imageio
import matplotlib.pyplot as plt
from scipy.signal import convolve2d

# load the image
pic = imageio.imread('<image location>')
plt.figure(figsize=(6, 6))
plt.imshow(pic)

# Convert the image to grayscale
img = color.rgb2gray(pic)

# outline kernel - used for edge detection
kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])

# we use 'valid' which means we do not add zero padding to our image
edges = convolve2d(img, kernel, mode='valid')

# Adjust the contrast of the filtered image by applying Histogram Equalization
edges_equalized = exposure.equalize_adapthist(edges / np.max(np.abs(edges)),
                                              clip_limit=0.03)

# plot the edges_clipped
plt.imshow(edges_equalized, cmap='gray')
plt.axis('off')
plt.show()
Example #49
0
    def test_convolution(self):
        #        print '\n\n*************************************************'
        #        print '           TEST CONVOLUTION'
        #        print '*************************************************'

        # fixed parameters
        bsize = 10  # batch size
        imshp = (28, 28)
        kshp = (5, 5)
        nkern = 5
        ssizes = ((1, 1), (2, 2), (3, 3), (4, 4))
        convmodes = ('full', 'valid')

        # symbolic stuff
        bias = tensor.dvector()
        kerns = tensor.dmatrix()
        input = tensor.dmatrix()
        rng = numpy.random.RandomState(3423489)
        filters = rng.randn(nkern, numpy.prod(kshp))
        biasvals = rng.randn(nkern)

        for mode in ('FAST_COMPILE', 'FAST_RUN'):  # , profmode):
            ttot, ntot = 0, 0
            for conv_mode in convmodes:
                for ss in ssizes:

                    output, outshp = sp.convolve(kerns, kshp, nkern, input,\
                            imshp, ss, bias=bias, mode=conv_mode)
                    f = function([kerns, bias, input], output, mode=mode)

                    # now test with real values
                    img2d = numpy.arange(bsize * numpy.prod(imshp)).reshape(( \
                                                            bsize,) + imshp)
                    img1d = img2d.reshape(bsize, -1)

                    # create filters (need to be flipped to use convolve2d)
                    filtersflipped = numpy.zeros((nkern, ) + kshp)
                    for k in range(nkern):
                        it = reversed(filters[k, :])
                        for i in range(kshp[0]):
                            for j in range(kshp[1]):
                                filtersflipped[k, i, j] = next(it)

                    # compute output with convolve2d
                    if conv_mode == 'valid':
                        fulloutshp = numpy.array(imshp) - numpy.array(kshp) + 1
                    else:
                        fulloutshp = numpy.array(imshp) + numpy.array(kshp) - 1
                    ntime1 = time.time()
                    refout = numpy.zeros((bsize, ) + tuple(fulloutshp) +
                                         (nkern, ))
                    for b in range(bsize):
                        for n in range(nkern):
                            refout[b, ...,
                                   n] = convolve2d(img2d[b, :, :],
                                                   filtersflipped[n, ...],
                                                   conv_mode)
                    ntot += time.time() - ntime1

                    # need to flatten images
                    bench1 = refout[:, 0::ss[0],
                                    0::ss[1], :].reshape(bsize, -1, nkern)
                    bench1 += biasvals.reshape(1, 1, nkern)

                    # swap the last two dimensions (output needs to be nkern x outshp)
                    bench1 = numpy.swapaxes(bench1, 1, 2)
                    ttime1 = time.time()
                    out1 = f(filters, biasvals, img1d)
                    ttot += time.time() - ttime1
                    temp = bench1.flatten() - out1.flatten()

                    assert (temp < 1e-5).all()
def Convolution(Big):
    Small = np.array([[1,90,1],[15,100,15],[1,90,1]])
    #Small = np.array([[1,1,1],[1,2,1],[1,1,1]])
    return sig.convolve2d(Big,Small, mode='full', boundary='fill', fillvalue=0)
Example #51
0
	def lacosmiciteration(self, verbose = None):
		"""
		Performs one iteration of the L.A.Cosmic algorithm.
		It operates on self.cleanarray, and afterwards updates self.mask by adding the newly detected
		cosmics to the existing self.mask. Cleaning is not made automatically ! You have to call
		clean() after each iteration.
		This way you can run it several times in a row to to L.A.Cosmic "iterations".
		See function lacosmic, that mimics the full iterative L.A.Cosmic algorithm.
		
		Returns a dict containing
			- niter : the number of cosmic pixels detected in this iteration
			- nnew : among these, how many were not yet in the mask
			- itermask : the mask of pixels detected in this iteration
			- newmask : the pixels detected that were not yet in the mask
	
		If findsatstars() was called, we exclude these regions from the search.
	
		"""
		
		if verbose is None:
			verbose = self.verbose

		if verbose:
			print "Convolving image with Laplacian kernel ..."
		
		# We subsample, convolve, clip negative values, and rebin to original size
		subsam = subsample(self.cleanarray)
		conved = signal.convolve2d(subsam, laplkernel, mode="same", boundary="symm")
		cliped = conved.clip(min=0.0)
		#cliped = np.abs(conved) # unfortunately this does not work to find holes as well ...
		lplus = rebin2x2(cliped)
		
		if verbose:
			print "Creating noise model ..."
			
		# We build a custom noise map, so to compare the laplacian to
 		m5 = ndimage.filters.median_filter(self.cleanarray, size=5, mode='mirror')
 		# We keep this m5, as I will use it later for the interpolation.
 		m5clipped = m5.clip(min=0.00001) # As we will take the sqrt
 		noise = (1.0/self.gain) * np.sqrt(self.gain*m5clipped + self.readnoise*self.readnoise)
 
 		if verbose:
			print "Calculating Laplacian signal to noise ratio ..."
 
 		# Laplacian signal to noise ratio :
 		s = lplus / (2.0 * noise) # the 2.0 is from the 2x2 subsampling
 		# This s is called sigmap in the original lacosmic.cl
 		
 		# We remove the large structures (s prime) :
 		sp = s - ndimage.filters.median_filter(s, size=5, mode='mirror')
 		
		if verbose:
			print "Selecting candidate cosmic rays ..."
			
 		# Candidate cosmic rays (this will include stars + HII regions)
 		candidates = sp > self.sigclip	
		nbcandidates = np.sum(candidates)
		
		if verbose:
			print "  %5i candidate pixels" % nbcandidates
 		
 		# At this stage we use the saturated stars to mask the candidates, if available :
 		if self.satstars is not None:
 			if verbose:
 				print "Masking saturated stars ..."
 			candidates = np.logical_and(np.logical_not(self.satstars), candidates)
 			nbcandidates = np.sum(candidates)
		
			if verbose:
				print "  %5i candidate pixels not part of saturated stars" % nbcandidates
 		
		if verbose:
			print "Building fine structure image ..."
			
 		# We build the fine structure image :
 		m3 = ndimage.filters.median_filter(self.cleanarray, size=3, mode='mirror')
		m37 = ndimage.filters.median_filter(m3, size=7, mode='mirror')
		f = m3 - m37
		# In the article that's it, but in lacosmic.cl f is divided by the noise...
		# Ok I understand why, it depends on if you use sp/f or L+/f as criterion.
		# There are some differences between the article and the iraf implementation.
		# So I will stick to the iraf implementation.
		f = f / noise
		f = f.clip(min=0.01) # as we will divide by f. like in the iraf version.
		
		if verbose:
			print "Removing suspected compact bright objects ..."
			
		# Now we have our better selection of cosmics :
		cosmics = np.logical_and(candidates, sp/f > self.objlim)
		# Note the sp/f and not lplus/f ... due to the f = f/noise above.
		
		nbcosmics = np.sum(cosmics)
		
		if verbose:
			print "  %5i remaining candidate pixels" % nbcosmics
		
		# What follows is a special treatment for neighbors, with more relaxed constains.
		
		if verbose:
			print "Finding neighboring pixels affected by cosmic rays ..."
			
		# We grow these cosmics a first time to determine the immediate neighborhod  :
		growcosmics = np.cast['bool'](signal.convolve2d(np.cast['float32'](cosmics), growkernel, mode="same", boundary="symm"))
		
		# From this grown set, we keep those that have sp > sigmalim
		# so obviously not requiring sp/f > objlim, otherwise it would be pointless
		growcosmics = np.logical_and(sp > self.sigclip, growcosmics)
		
		# Now we repeat this procedure, but lower the detection limit to sigmalimlow :
			
		finalsel = np.cast['bool'](signal.convolve2d(np.cast['float32'](growcosmics), growkernel, mode="same", boundary="symm"))
		finalsel = np.logical_and(sp > self.sigcliplow, finalsel)
		
		# Again, we have to kick out pixels on saturated stars :
		if self.satstars is not None:
 			if verbose:
 				print "Masking saturated stars ..."
 			finalsel = np.logical_and(np.logical_not(self.satstars), finalsel)
 			
		nbfinal = np.sum(finalsel)
		
		if verbose:
			print "  %5i pixels detected as cosmics" % nbfinal
		
		# Now the replacement of the cosmics...
		# we outsource this to the function clean(), as for some purposes the cleaning might not even be needed.
		# Easy way without masking would be :
		#self.cleanarray[finalsel] = m5[finalsel]
		
		# We find how many cosmics are not yet known :
		newmask = np.logical_and(np.logical_not(self.mask), finalsel)
		nbnew = np.sum(newmask)
		
		# We update the mask with the cosmics we have found :
		self.mask = np.logical_or(self.mask, finalsel)
	
		# We return
		# (used by function lacosmic)
		
		return {"niter":nbfinal, "nnew":nbnew, "itermask":finalsel, "newmask":newmask}
t = 280
b = t + 520
i = 680
o = i + 190
cropped = img[t:b, i:o]

# Create figure and axes
fig, ax = plt.subplots(1)

gblur = ((1 / 256., 4 / 256., 6 / 256., 4 / 256.,
          1 / 256.), (4 / 256., 16 / 256., 24 / 256., 16 / 256., 4 / 256.),
         (6 / 256., 24 / 256., 36 / 256., 24 / 256.,
          6 / 256.), (4 / 256., 16 / 256., 24 / 256., 16 / 256., 4 / 256.),
         (1 / 256., 4 / 256., 6 / 256., 4 / 256., 1 / 256.))
conv = signal.convolve2d(cropped, gblur)
#conv[conv>1.5] = 1
# edged = ((-1,-1,-1),(-1,8,-1),(-01,-1,-1))
# conv = signal.convolve2d(cropped, kernel)
# conv[conv>0] = 100

from BMMcontrols import DCM
dcm = DCM()
com = ndimage.measurements.center_of_mass(conv)
print "%7.1f  %5.1f  %5.1f  %5.1f  %5.1f" % (dcm.current_energy(), com[0] + t,
                                             com[1] + i, com[0], com[1])

with open("com.dat", "a") as myfile:
    myfile.write(
        "%7.1f  %5.1f  %5.1f  %5.1f  %5.1f\n" %
        (dcm.current_energy(), com[0] + t, com[1] + i, com[0], com[1]))
# load the image as grayscale
f = mpimg.imread("../data/lena.png")

# "Gaussian" kernel defined in Lecture 3b. Page3
g = 1.0 / 256 * np.array([[1, 4, 6, 4, 1], [2, 8, 12, 8, 2], [
    6, 24, 36, 24, 6
], [2, 8, 12, 8, 2], [1, 4, 6, 4, 1]])
# show image
plt.subplot(1, 2, 1)
plt.imshow(f, cmap='gray')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(g, cmap='gray')

h1 = signal.convolve2d(f, g, mode='full')
H1 = np.fft.fft2(h1)

# padding zeros in the end of f and g
padf = np.pad(f, ((0, 4), (0, 4)), 'constant')
padg = np.pad(g, ((0, 255), (0, 255)), 'constant')

# compute the Fourier transforms of f and g
F = np.fft.fft2(padf)
G = np.fft.fft2(padg)

# compute the product
H2 = np.multiply(F, G)

# inverse Fourier transform
h2 = np.fft.ifft2(H2)
def smooth(array, bin_size, room_shape):
    ''' Apply a filter to smooth rates.
    
        Convolve with the filters, then adjust 
        by the appropriate weight.'''

    assert (room_shape[0][1] - room_shape[0][0]) / bin_size == array.shape[0]
    assert (room_shape[1][1] - room_shape[1][0]) / bin_size == array.shape[1]

    ins_mask = inside(bin_size, room_shape)

    #assert np.sum(( 1-ins_mask)*array) == 0
    ''''''
    # Debug code
    if np.sum((1 - ins_mask) * array) != 0:
        print 'Something is going on outside!'
        import pdb
        pdb.set_trace()
        import matplotlib.pyplot as plt
        plt.figure()
        plt.pcolor((1 - ins_mask) * array)
        plt.figure()
        plt.pcolor(ins_mask)
        plt.show()
        import pdb
        pdb.set_trace()
        raise Exception('Something is going on outside!')

    filt = gauss_kernel().astype(np.complex)

    pre_adjusted_rates = np.real(
        convolve2d(array.astype(np.complex), filt, mode='same'))
    weight_adjustment = np.real(
        convolve2d(ins_mask.astype(np.complex), filt, mode='same'))

    # Adjust weight_adjustment so we don't get any division by 0 errors
    weight_adjustment += 1 - ins_mask

    adjusted_rates = pre_adjusted_rates * ins_mask / weight_adjustment
    '''
    if np.sum(adjusted_rates) != np.sum(rates):
        import pdb; pdb.set_trace()
        print 'Smoothing did not work properly.'
        #raise Exception('Smoothing did not work properly.')
    
    import matplotlib.pyplot as plt
    plt.figure()
    plt.pcolor(rates)
    plt.colorbar()
    
    plt.figure()
    plt.pcolor(weight_adjustment*inside(binsz))
    plt.colorbar()
    
    plt.figure()
    plt.pcolor(pre_adjusted_rates)
    plt.colorbar()
    plt.figure()
    plt.pcolor(adjusted_rates)
    plt.colorbar()
    plt.show()'''
    return np.real(adjusted_rates)
Example #55
0
    pmesh = ax.pcolormesh(x, y, z / np.pi,
                          cmap=cm, vmin=-1, vmax=1)
    plt.axis([x.min(), x.max(), y.min(), y.max()])
    cbar = fig.colorbar(pmesh)
    cbar.ax.set_ylabel('Phase [pi]')

    theta = np.pi * 0
    omega = 1

    filts = get_quadratures(25)
    theta = 90
    sinusoid = genSinusoid((100, 100), 1, (omega * np.sin(theta), omega * np.cos(theta)), 0)

    filt = filts[0]
    sin_conv = convolve2d(sinusoid, filt[0], mode='same')

    plt.imshow(sin_conv)

    filts = get_quadratures(5)
    for theta in np.arange(0, np.pi, np.pi / 8):
        sinusoid = genSinusoid((100, 100), 1, (omega * np.sin(theta), omega * np.cos(theta)), 0)
        magnitudes = []

        im = sinusoid
        for filt in filts:
            sin_conv = convolve2d(im, filt[1], mode='same')
            cos_conv = convolve2d(im, filt[0], mode='same')

            magnitudes.append(np.sqrt(sin_conv ** 2 + cos_conv ** 2))
        plt.subplot(121)
Example #56
0
def life_step_2(X):
    """Game of life step using scipy tools"""
    from scipy.signal import convolve2d
    nbrs_count = convolve2d(X, np.ones(
        (3, 3)), mode='same', boundary='wrap') - X
    return (nbrs_count == 3) | (X & (nbrs_count == 2))
Example #57
0
import cv2
import numpy as np
import scipy
from scipy import signal
from matplotlib import pyplot as plt
X = cv2.imread('UBCampus.jpg', 0)

X1 = np.array(X)
m3 = np.asmatrix([[0, 0, -1, -1, -1, 0, 0], [0, -2, -3, -3, -3, -2, 0],
                  [-1, -3, 5, 5, 5, -3, -1], [-1, -3, 5, 16, 5, -3, -1],
                  [-1, -3, 5, 5, 5, -3, -1], [0, -2, -3, -3, -3, -2, 0],
                  [0, 0, -1, -1, -1, 0, 0]])

x1 = signal.convolve2d(X1, m3, boundary='symm', mode='same')
cv2.imwrite('image.png', x1)
[row, col] = np.shape(x1)

cv2.imwrite('DoG.png', x1)
zero_cross = np.zeros(np.shape(x1))

for i in range(0, row - 1):
    for j in range(0, col - 1):
        if x1[i][j] * x1[i][j + 1] < 0:
            zero_cross[i][j] = 255
        if x1[i][j] * x1[i + 1][j] < 0:
            zero_cross[i][j] = 255

cv2.imwrite('zerocrossing_DoG.png', zero_cross)

dx = cv2.Sobel(X1, cv2.CV_64F, 1, 0, ksize=3)
dy = cv2.Sobel(X1, cv2.CV_64F, 0, 1, ksize=3)
Example #58
0
# histshow(new_im_np)
# histshow(new_im_np_2)

# plot_multi_img(['image1', 'mean1', 'image2', 'mean2'], [im_np, new_im_np, im_np_2, new_im_np_2], [2,2])

mask = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]])
#
# image = np.array([[15,15,5],
#                  [10,25,20],
#                  [5,20,11]])
# print(np.mean(im_np))
# im = laplacian_of_gaussian(im_np)
# print(np.mean(im))
# im2 = laplacian_of_gaussian(im)
# print(np.mean(im2))
# imshow(im2)

im = signal.convolve2d(im_np_2, -mask)
# im2 = signal.convolve2d(im, mask)
# im3 = signal.convolve2d(im2, mask)
# im4 = signal.convolve2d(im3, mask)
# im5 = signal.convolve2d(im4, mask)
imshow(im[1:-1, 1:-1] + im_np_2)
# im_ori = ft.img2fft(im_np_2)
# im_f = ft.img2fft(im)
# mag = ft.mag_resp(im_ori)
# mag1 = ft.mag_resp(im_f)
# imshow(mag)
# imshow(mag1)
# print(im_f[2,2])
Example #59
0
 def Train(self,numEpochs,learningRate,batchSize):
     """Train the CNN+NN network"""
     trainingError=0
     for i in range(numEpochs):
         trainingError=0
         self.RandomizeInputs()
         #iterate thorugh each sample
         for j in range(self.InputDataList.shape[0]):
             #do forward pass
             #print("Forward passing")
             data=self.InputDataList[j]
             res=self.Evaluate(data)
             #error is computed as (a-y)^2
             
             trainingError += np.sum(np.square(res - self.OutputLabels[j]) )
             #print("Back prop in NN")
             # ----------Back prop in NN layers--------------
             for count in range(len(self.LayerList)-1,-1,-1):
                 layer=self.LayerList[count]
                 if count==len(self.LayerList)-1:#last layer
                     layer.Delta=layer.A-self.OutputLabels[j] #Softmax by default
                     if layer.activationType==ActivationFunction.SIGMOID or layer.activationType==ActivationFunction.TANH :
                         layer.Delta=np.multiply(layer.Delta,layer.APrime)
                     elif layer.activationType==ActivationFunction.ReLU:
                         layer.Delta[layer.Sum<=0]=0
                         pass
                 else:#not the last layer
                     #(W^T*Deltas)_prevlayer * APRime_thisLayer
                     layer.Delta=np.dot(self.LayerList[count+1].W.T , self.LayerList[count+1].Delta)
                     if layer.activationType==ActivationFunction.SIGMOID or layer.activationType==ActivationFunction.TANH :
                         layer.Delta=np.multiply(layer.Delta,layer.APrime)
                     elif layer.activationType==ActivationFunction.ReLU:
                         layer.Delta[layer.Sum<=0]=0
                 #compute gradient of weights
                 layer.GradB+=layer.Delta
                 #compute gradient of weights
                 if count==0: #first layer
                     layer.GradW+= np.dot(layer.Delta, self.Flatten.T)
                 else:
                     layer.GradW+= np.dot(layer.Delta, self.LayerList[count-1].A.T)
             # compute delta on the output of SS layer of all feature maps
             deltaSSFlat=np.dot(self.LayerList[0].W.T , self.LayerList[0].Delta)
             #print("Back prop in CNN")
             #----------Back prop in CNN  layers------------------
             # do reverse flattening and distribute the deltas on
             # each feature map's SS
             index=0
             for fmp in self.CNNLayerList[-1].FeatureMapList:
                 fmp.DeltaSS=np.zeros((fmp.OutPutSS.shape[0],fmp.OutPutSS.shape[1]))
                 for m in range(fmp.OutPutSS.shape[0]):
                     for n in range(fmp.OutPutSS.shape[1]):
                         fmp.DeltaSS[m,n]=deltaSSFlat[index,0]
                         index+=1
                 pass
             
             #----iterate each CNN layers in reverse order
             for cnnCount in range(len(self.CNNLayerList)-1,-1,-1):
                 #compute deltas on C layers - distribute deltas from SS layer
                 #then multiply by activation function
                 
                 #------reverse subsampling, compute delta*fprime
                 # (2Nx2N) <-----(NxN)
                 for fmp in self.CNNLayerList[cnnCount].FeatureMapList:
                     indexm,indexn=0,0
                     fmp.DeltaCV=np.zeros((fmp.OutPutSS.shape[0]*2,fmp.OutPutSS.shape[1]*2))
                     for m in range(fmp.DeltaSS.shape[0]):
                         indexn=0
                         for n in range(fmp.DeltaSS.shape[1]):
                             if fmp.activationType==ActivationFunction.SIGMOID or fmp.activationType==ActivationFunction.TANH:
                                 fmp.DeltaCV[indexm, indexn] = (1 / 4.0) * fmp.DeltaSS[m, n] * fmp.APrime[indexm, indexn] 
                                 fmp.DeltaCV[indexm, indexn + 1] = (1 / 4.0) * fmp.DeltaSS[m, n] * fmp.APrime[indexm, indexn + 1]
                                 fmp.DeltaCV[indexm + 1, indexn] = (1 / 4.0) * fmp.DeltaSS[m, n] * fmp.APrime[indexm + 1, indexn]
                                 fmp.DeltaCV[indexm + 1, indexn + 1] = (1 / 4.0) * fmp.DeltaSS[m, n] * fmp.APrime[indexm + 1, indexn + 1]
                                 pass
                             elif fmp.activationType==ActivationFunction.ReLU:
                                 fmp.DeltaCV[indexm, indexn] = (1 / 4.0) * fmp.DeltaSS[m, n]  if fmp.Sum[indexm,indexn]>0 else 0
                                 fmp.DeltaCV[indexm, indexn + 1] = (1 / 4.0) * fmp.DeltaSS[m, n] if fmp.Sum[indexm,indexn+1]>0 else 0
                                 fmp.DeltaCV[indexm + 1, indexn] = (1 / 4.0) * fmp.DeltaSS[m, n] if fmp.Sum[indexm+1,indexn]>0 else 0
                                 fmp.DeltaCV[indexm + 1, indexn + 1] = (1 / 4.0) * fmp.DeltaSS[m, n] if fmp.Sum[indexm+1,indexn+1]>0 else 0
                                 pass
                             indexn+=2
                         indexm+=2
                         pass
                     pass
                 
                 #-------compute BiasGrad in current CNN Layer
                 for fmp in self.CNNLayerList[cnnCount].FeatureMapList:
                     fmp.BiasGrad+=np.sum(fmp.DeltaCV)
                 #----compute gradients for pxq kernels in current CNN layer
                 if cnnCount>0:# not first layer
                     for p in range(len(self.CNNLayerList[cnnCount-1].FeatureMapList)):
                         for q in range(len(self.CNNLayerList[cnnCount].FeatureMapList)):
                             inputRot180=np.rot90(self.CNNLayerList[cnnCount-1].FeatureMapList[p].OutPutSS,2,(1,0))
                             deltaCV=self.CNNLayerList[cnnCount].FeatureMapList[q].DeltaCV
                             self.CNNLayerList[cnnCount].KernelGrads[p][q]+= sc.convolve2d(inputRot180,deltaCV,"valid")
                             pass
                     pass
                     #back propagate to previous CNN layer
                     for p in range(len(self.CNNLayerList[cnnCount-1].FeatureMapList)):
                         size=self.CNNLayerList[cnnCount-1].FeatureMapList[p].OutPutSS.shape[0]
                         "TO-DO: make this work with rectangular matrix"
                         self.CNNLayerList[cnnCount-1].FeatureMapList[p].DeltaSS=np.zeros((size,size))
                         for q in range(len(self.CNNLayerList[cnnCount].FeatureMapList)):
                             kernelRot180=np.rot90(self.CNNLayerList[cnnCount].Kernels[p][q],2,(1,0))
                             deltaCV=self.CNNLayerList[cnnCount].FeatureMapList[q].DeltaCV
                             #full convol of delta with kernel rotated 180
                             self.CNNLayerList[cnnCount-1].FeatureMapList[p].DeltaSS+=sc.convolve2d(deltaCV,kernelRot180) 
                             pass
                         pass
                 else: 
                     #first layer connected to output
                     for p in range(1):
                         for q in range(len(self.CNNLayerList[cnnCount].FeatureMapList)):
                             inputRot180=np.rot90(self.InputDataList[j],2,(1,0))
                             deltaCV=self.CNNLayerList[cnnCount].FeatureMapList[q].DeltaCV
                             self.CNNLayerList[cnnCount].KernelGrads[p][q]+= sc.convolve2d(inputRot180,deltaCV,"valid")
                     pass
                 pass
             if j%batchSize==0:
                 self.UpdateKernelsWeightsBiases(learningRate,batchSize)
                 self.ClearGradients()
             pass
             
         if i%10==0:
             learningRate/=2
         print("epochs: %d train error: %f"%(i,trainingError))
         pass
     pass
Example #60
0
mnist = input_data.read_data_sets("data/", one_hot=True)

w_cov = np.random.randn(5, 5)
w_cov1 = np.random.randn(5, 5)
w_fc = np.random.randn(16, 10)
#b_cov=np.zeros(25)
b_fc = np.zeros(10)
rate = 0.8

for k in range(10000):
    x = mnist.train.images[k]
    y = mnist.train.labels[k]

    x = np.reshape(x, [28, 28])
    p = signal.convolve2d(x, w_cov, "valid")
    p = 1 / (1 + np.exp(-p))

    fc = np.zeros([12, 12])
    for i in range(12):
        for j in range(12):
            fc[i][j] = p[i * 2:(i + 1) * 2, j * 2:(j + 1) * 2].max()

    p = signal.convolve2d(fc, w_cov1, "valid")
    p = 1 / (1 + np.exp(-p))

    fc = np.zeros([4, 4])
    for i in range(4):
        for j in range(4):
            fc[i][j] = p[i * 2:(i + 1) * 2, j * 2:(j + 1) * 2].max()