Пример #1
0
    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)
def plot_sobel(im):

    # plot grayscale
    im = color.rgb2gray(im)
    # sobel derivative filters
    imx = np.zeros(im.shape)
    filters.sobel(im,1,imx)

    imy = np.zeros(im.shape)
    filters.sobel(im,0,imy)

    magnitude = np.sqrt(imx**2+imy**2)
    plt.figure()
    plt.suptitle("gradients on x, y, and magnitude")
    ax = plt.subplot("141")
    ax.imshow(im)
    ax.set_title("image")
    ax = plt.subplot("142")
    ax.imshow(imx)
    ax.set_title("gx")
    ax = plt.subplot("143")
    ax.imshow(imy)
    ax.set_title("gy")
    ax = plt.subplot("144")
    ax.imshow(magnitude)
    ax.set_title("mag")
    plt.show()
Пример #3
0
    def _find_edges(self, image, edge_filter):
        """ Method for handling selection of edge_filter and some more
            pre-processing. """

        if edge_filter.lower() == "sobel":
            print "[*] Using Sobel-filter for edge detection."
            image = filters.sobel(image, 0)**2 + filters.sobel(image, 1)**2 
        elif edge_filter.lower() == "canny":
            print "[*] Using Canny-filter for edge detection."
            warn("[WARN] Actually... no... Couldn't find it.")
            #image = filters.canny(image)
            print "[*] Using Sobel-filter for edge detection."
            image = filters.sobel(image, 0)**2 + filters.sobel(image, 1)**2 
        elif edge_filter is None:
            print "[*] I sure hope you've done your own edge detection..."
        else:
            print "[ERROR] Unknonw option:", edge_filter, "Try: 'sobel', 'canny' or None!"
            print "[*] I sure hope you've done your own edge detection..."

        image -= image.min()

        if self._binary:
            image = image > image.max()*self._threshold
            image.dtype = np.int8
        elif self._threshold > 0:
            image = np.where(image > image.max()*self._threshold, image, 0)
        
        return image 
Пример #4
0
def edge_strength(input_image):
    grayscale = array(input_image.convert('L'))
    filtered_y = zeros(grayscale.shape)
    filters.sobel(grayscale, 0, filtered_y)
    print (type(filtered_y))
    print (filtered_y ** 2)
    return filtered_y ** 2
Пример #5
0
    def SobelTest(self, img):
        '''
        Sobel算子的测试
         |-1 0 1|   |-1 -2 -1|
         |-2 0 2|   | 0  0  0|
         |-1 0 1|   | 1  2  1|
         Dx         Dy
         使用Sobel滤波器计算x和y方向的倒数,以及梯度的大小
        '''
        im = np.array(img.convert('L'))

        #Sobel 导数滤波器
        pl.figure()
        imx = np.zeros(im.shape)
        filters.sobel(im, 1, imx)  #save in the third var
        img_x = Image.fromarray(imx)
        pl.title("X-Image")
        pl.imshow(img_x)

        #As same as X
        pl.figure()
        imy = np.zeros(im.shape)
        filters.sobel(im, 0, imy)
        img_y = Image.fromarray(imy)
        pl.title("Y-Image")
        pl.imshow(img_y)

        pl.figure()
        magnitude = np.sqrt(imx**2 + imy**2)
        img_m = Image.fromarray(magnitude)
        pl.title("Magnitude-Image")
        pl.imshow(img_m)

        pl.show()
        return
Пример #6
0
    def harris_corner_3d(volume, min_distance=10, threshold=0.1, eps=1e-6):
        """Finds corners in volume by extending harris corner detection to 3D.
        Special thanks to scikit-image!"""

        # compute harris response
        # derivatives
        v_x = sobel(volume, axis=0, mode='constant')
        v_y = sobel(volume, axis=1, mode='constant')
        v_z = sobel(volume, axis=2, mode='constant')

        w_xx = gaussian_filter(v_x * v_x, 1.5, mode='constant')
        w_xy = gaussian_filter(v_x * v_y, 1.5, mode='constant')
        w_xz = gaussian_filter(v_x * v_z, 1.5, mode='constant')
        w_yy = gaussian_filter(v_y * v_y, 1.5, mode='constant')
        w_yz = gaussian_filter(v_y * v_z, 1.5, mode='constant')
        w_zz = gaussian_filter(v_z * v_z, 1.5, mode='constant')

        # determinant and trace
        w_det = w_xx * w_yy * w_zz + 2 * w_xy * w_yz * w_xz - w_yy * w_xz**2 - w_zz * w_xy**2 - w_xx * w_yz**2
        w_tr = w_xx + w_yy + w_zz

        # Alison Noble, "Descriptions of Image Surfaces", PhD thesis (1989)
        harris_vol = w_det / (w_tr + eps)

        # calculate final corners by local maximum
        coordinates = peak_local_max(harris_vol,
                                     min_distance=min_distance,
                                     threshold_rel=threshold)

        return coordinates.astype(np.float32)
Пример #7
0
def computeHarrisValues(Image):

    height, width = Image.shape[:2]

    harrisImage = np.zeros(Image.shape[:2], dtype=float)
    orientationImage = np.zeros(Image.shape[:2], dtype=float)

    sobx = np.zeros(Image.shape[:2], dtype=float)
    filters.sobel(Image, 1, sobx)
    soby = np.zeros(Image.shape[:2], dtype=float)
    filters.sobel(Image, 0, soby)
    # sobx = filters.convolve(srcImage,sx,mode='reflect')
    # soby = filters.convolve(srcImage,sy,mode='reflect')
    Ix = sobx * sobx
    Iy = soby * soby
    Ixy = sobx * soby

    Wxx = filters.gaussian_filter(Ix, sigma=0.5)
    Wyy = filters.gaussian_filter(Iy, sigma=0.5)
    Wxy = filters.gaussian_filter(Ixy, sigma=0.5)

    harrisImage = Wxx * Wyy - Wxy * Wxy - 0.1 * (Wxx + Wyy) * (Wxx + Wyy)
    orientationImage = np.arctan2(soby, sobx) * (180) / np.pi

    return harrisImage, orientationImage
Пример #8
0
def compute_harris_response(im, sigma=1):
    imx = zeros(im.shape)
    # filters.gaussian_filter(im, (sigma, sigma), (0, 1), imx)
    figure(1)
    gray()
    imshow(im)  #原图
    print(im)
    ll = filters.sobel(im)  #原图sobel
    print(ll)
    figure(2)
    gray()
    imshow(ll)
    imy = zeros(im.shape)
    filters.gaussian_filter(im, (sigma, sigma), (1, 0), imy)  #原图模糊
    figure(3)
    gray()
    imshow(imy)
    print(imy)
    figure(4)
    lk = filters.sobel(imy)  #原图模糊sobel
    imshow(lk)
    gray()
    Wxx = filters.gaussian_filter(imx * imx, sigma)
    Wxy = filters.gaussian_filter(imx * imy, sigma)
    Wyy = filters.gaussian_filter(imy * imy, sigma)

    Wdet = Wxx * Wyy - Wxy**2
    Wtr = Wxx + Wyy
    # imshow(Wdet)
    # show()
    return Wdet / Wtr
Пример #9
0
def test():
	#新建一个图像
	figure();
	im = array(Image.open('image.jpeg').convert('L'));
	#计算x方向的导
	imx = zeros(im.shape);
	filters.sobel(im,1,imx);

	#计算y方向的导
	imy = zeros(im.shape);
	filters.sobel(im,0,imy);
	
	# 计算梯度
	magnitude = sqrt(imx**2+imy**2);

	subplot(2,3,1);
	imx = Image.fromarray(imx)
	imshow(imx);

	subplot(2,3,2);
	imy = Image.fromarray(imy)
	imshow(imy);

	subplot(2,3,3);
	magnitude = Image.fromarray(magnitude)
	imshow(magnitude);
	show();
	pass;
Пример #10
0
    def _find_edges(self, image, edge_filter):
        """ Method for handling selection of edge_filter and some more
            pre-processing. """

        if edge_filter.lower() == "sobel":
            print "[*] Using Sobel-filter for edge detection."
            image = filters.sobel(image, 0)**2 + filters.sobel(image, 1)**2
        elif edge_filter.lower() == "canny":
            print "[*] Using Canny-filter for edge detection."
            warn("[WARN] Actually... no... Couldn't find it.")
            #image = filters.canny(image)
            print "[*] Using Sobel-filter for edge detection."
            image = filters.sobel(image, 0)**2 + filters.sobel(image, 1)**2
        elif edge_filter is None:
            print "[*] I sure hope you've done your own edge detection..."
        else:
            print "[ERROR] Unknonw option:", edge_filter, "Try: 'sobel', 'canny' or None!"
            print "[*] I sure hope you've done your own edge detection..."

        image -= image.min()

        if self._binary:
            image = image > image.max() * self._threshold
            image.dtype = np.int8
        elif self._threshold > 0:
            image = np.where(image > image.max() * self._threshold, image, 0)

        return image
Пример #11
0
    def compute_corner_response(self, image):
        # TODO: Compute the Harris corner response
        # https://www.mathworks.com/matlabcentral/answers/65593-what-is-wrong-with-my-code-harris-corner-detector?requestedDomain=www.mathworks.com
        # Compute gradients
        image_x = sobel(image, axis=1)
        image_y = sobel(image, axis=0)

        image_x_2 = image_x * image_x
        image_y_2 = image_y * image_y
        image_x_y = image_x * image_y

        # Smooth the gradient images
        image_x_2_smooth = gaussian_filter(image_x_2, self.gaussian_sigma)
        image_y_2_smooth = gaussian_filter(image_y_2, self.gaussian_sigma)
        image_x_y_smooth = gaussian_filter(image_x_y, self.gaussian_sigma)

        # Compute M and R
        det = image_x_2_smooth * image_y_2_smooth - image_x_y_smooth * image_x_y_smooth
        trace = image_x_2_smooth + image_y_2_smooth
        R = det - self.harris_corner_k * (trace * trace)

        # Non-maximal suppression
        R_nms = maximum_filter(R, (self.maxfilter_window_size, self.maxfilter_window_size))

        return R_nms
def plot_sobel_hist(args, im=None):


    dirs = [#'/home/neale/repos/adversarial-toolbox/images/cifar/train',
            #'/home/neale/repos/adversarial-toolbox/images/adversarials/lbfgs/resnet50'
            '/home/neale/repos/adversarial-toolbox/images/adversarials/lbfgs/imagenet/symmetric/adv',
            '/home/neale/repos/adversarial-toolbox/images/adversarials/fgsm/imagenet/symmetric/adv',
            '/home/neale/repos/adversarial-toolbox/images/adversarials/deepfool/imagenet/symmetric/adv',
            '/home/neale/repos/adversarial-toolbox/images/adversarials/lbfgs/imagenet/symmetric/real'
            ]
    mag_all = []
    for i, d in enumerate(dirs):
        images = glob(d+'/*.png')[:2000]
        print len(images)
        gx, gy, mag = [], [], []
        for image in images:
            im = imread(image)
            im = np.array(im)
            # plot grayscale
            im = color.rgb2gray(im)
            # sobel derivative filters
            imx = np.zeros(im.shape)
            filters.sobel(im, 1, imx)
            imy = np.zeros(im.shape)
            filters.sobel(im, 0, imy)
            magnitude = np.sqrt(imx**2 + imy**2)
            gx.append(imx)
            gy.append(imx)
            mag.append(magnitude)

        mag_all.append(mag)
    labels = ["L-BFGS", "FGSM", "DeepFool", "Imagenet Validation Set"]
    plot_pdf(mag_all, labels)
def first_sobel(im):
    sobel_x = np.zeros(im.shape)
    filters.sobel(im, 1, sobel_x)
    sobel_y = np.zeros(im.shape)
    filters.sobel(im, 0, sobel_y)
    sobel = np.sqrt(sobel_x*sobel_x + sobel_y*sobel_y) 
    return sobel
Пример #14
0
    def calculate_integral_HOG(self, image):
        # X, Y方向に微分
        xsobel = np.zeros(image.shape)
        filters.sobel(image,1,xsobel) # 1はaxis=1のこと = x方向
        
        ysobel = np.zeros(image.shape)
        filters.sobel(image,0,ysobel) # 1はaxis=0のこと = y方向
        
        # 角度別の画像を生成しておく
        bins = np.zeros((N_BIN, image.shape[0], image.shape[1]))
         
        # X, Y微分画像を勾配方向と強度に変換
        Imag, Iang = cv2.cartToPolar(xsobel, ysobel, None, None, True) # outputs are magnitude, angle
        # 勾配方向を[0, 180)にする(181~360は0~180に統合する。x軸をまたいだ方向は考えない)
        Iang = (Iang>180)*(Iang-180) + (Iang<=180)*Iang 
        Iang[Iang==360] = 0; Iang[Iang==180] = 0 
        # 勾配方向を[0, 1, ..., 8]にする準備(まだfloat)
        Iang /= THETA
        # 勾配方向を強度で重みをつけて、角度別に投票する
        ind = 0

        for ind in xrange(N_BIN):
            bins[ind] += (np.int8(Iang) == ind)*Imag
        
        # 角度別に積分画像生成
        """ !ここの計算があやしい! """
        integrals = np.array([cv2.integral(bins[i]) for i in xrange(N_BIN)])
        
        return integrals
Пример #15
0
 def depth_discontinuity_adjustment(label, cost):
     # Detect edges and replace the label with lower cost
     # horizontal
     result = np.empty_like(label)
     sobel_filter = sobel(label, axis=0)
     for y in range(h):
         for x in range(w):
             result[y, x] = label[y, x]
             if sobel_filter[y, x] > 10 and 1 <= x < w - 1:
                 disp = label[y, x]
                 if cost[label[y, x - 1], y, x] < cost[disp, y, x]:
                     disp = label[y, x - 1]
                 if cost[label[y, x + 1], y, x] < cost[disp, y, x]:
                     disp = label[y, x + 1]
                 result[y, x] = disp
     # vertical
     label = result
     result = np.empty_like(label)
     sobel_filter = sobel(label, axis=1)
     for y in range(h):
         for x in range(w):
             result[y, x] = label[y, x]
             if sobel_filter[y, x] > 10 and 1 <= y < h - 1:
                 disp = label[y, x]
                 if cost[label[y - 1, x], y, x] < cost[disp, y, x]:
                     disp = label[y - 1, x]
                 if cost[label[y + 1, x], y, x] < cost[disp, y, x]:
                     disp = label[y + 1, x]
                 result[y, x] = disp
     return result
Пример #16
0
    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)
Пример #17
0
def detectBorders(img, show=False):
    #Se aumenta el tamaño para hacer efectos mejores adaptado
    resize = cv2.resize(np.asarray(img),
                        None,
                        fx=6,
                        fy=6,
                        interpolation=cv2.INTER_CUBIC)

    #Filtro para aplicar blur y facilitar la detección de bordes evitando ruido
    gaussian = cv2.GaussianBlur(resize, (51, 51), 0)

    # Detección de bordes mediante filtro Sobel
    imx = zeros(gaussian.shape)
    imx = filters.sobel(gaussian, 1, imx)
    imy = zeros(gaussian.shape)
    imy = filters.sobel(gaussian, 0, imy)
    im_sobel = sqrt(imx**2 + imy**2)

    #Para visualizar, los muestra en colores invertidos por lo cual al invertirlos nuevamente
    #mostrará los colores originales
    inverso = 255 - im_sobel

    #Si show es true, mostrará el resultado de la detección de bordes
    if show:
        cv2_imshow(im_sobel)

    #Devolverá la imagen con los filtros aplicados
    return im_sobel
def gradient_orientation(image):
    '''
    Calculate the gradient orientation for edge point in the image
    '''
    #scipy.ndimage.sobel
    dx = sobel(image, axis=0, mode='constant')
    dy = sobel(image, axis=1, mode='constant')
    dz = sobel(image, axis=1, mode='constant')

    #For 3D instead of a single gradient value, we need two angles that define a normal vector
    #Phi is the angle between the positive x-axis to the projection of the normal vector the x-y plane (around +z)
    #Psi is the angle between the positive z-axis to the normal vector

    phi = np.arctan2(dy, dx) * 180 / np.pi
    psi = np.arctan2(np.sqrt(dx * dx + dy * dy), dz) * 180 / np.pi

    print("dx: ", dx.shape)
    print("dy: ", dy.shape)
    print("phi: ", phi.shape)
    print("psi:", psi.shape)
    print(np.max(phi))
    print(np.min(phi))
    print(np.max(psi))
    print(np.min(psi))

    gradient = np.zeros(image.shape)

    return phi, psi
Пример #19
0
 def run(self):
     wx.CallAfter(self.pb.update, 'Retrieving coral region')
     #load in original dicom pixel data and normalize
     coral_slab = self.model.ds.pixel_array.astype(np.double)
     coral_slab = self.model.normalize_intensity(coral_slab)
     #Cut down dicom pixel data to user defined coral slab region
     x, y, dx, dy = self.dicom_controller.coral_slab
     coral_slab = coral_slab[y:dy, x:dx]
     iht, iwd = coral_slab.shape
     wx.CallAfter(self.pb.update, 'Zero padding overlay 1')
     #zero pad image to a power of 2 to speed up FFT
     vpad = 2**(int(math.ceil(math.log(iht, 2))))
     hpad = 2**(int(math.ceil(math.log(iwd, 2))))
     fImg = np.zeros(shape=(vpad, hpad), dtype=np.double, order='C')
     fImg[0:iht, 0:iwd] = coral_slab
     wx.CallAfter(self.pb.update, 'Applying fast fourier transformation to overlay 1')
     #FFT
     fImg = np.fft.fftshift(np.fft.fft2(fImg))
     #find center row and column
     cr = fImg.shape[0]/2
     cc = fImg.shape[1]/2
     #compute distance for every pixel from center
     wx.CallAfter(self.pb.update, 'Computing distance matrix for overlay 1')
     D = np.ones(shape=(vpad, hpad), dtype=np.double, order='C')
     for row in range(iht):
         for col in range(iwd):
             if row==cr and col==cc:
                 wx.CallAfter(self.pb.update, 'Computing distance matrix for overlay 1')
             D[row][col] = math.sqrt((col-cc)**2 + (row-cr)**2)
     D[cr][cc] = 0.00000001  # avoid zero-division warning 
     #Create the Butterworth, high-pass filter
     wx.CallAfter(self.pb.update, 'Creating Butterworth HPF for overlay 1')
     Do = 25
     p = 2
     H = 1/(1 + (Do/D)**(2*p))
     #Highpass filter the source image & strip off the zero-padded regions
     wx.CallAfter(self.pb.update, 'Applying Butterworth HPF to overlay 1')
     fi = H*fImg
     fi = np.fft.ifftshift(fi)
     fi = np.abs(np.fft.ifft2(fi))
     fi = fi[0:iht, 0:iwd]
     #create the overlay
     self.controller.overlays[0] = (coral_slab-fi)
     
     ov2 = np.empty(shape=(iht, iwd), dtype=np.double, order='C')
     wx.CallAfter(self.pb.update, 'Creating sobel filter for overlay 2')
     sp.sobel(coral_slab-fi, output=ov2, mode='nearest')
     ov2 = self.model.normalize_intensity(ov2)
     wx.CallAfter(self.pb.update, 'Applying sobel filter to overlay 2')
     self.controller.overlays[1] = ov2
     self.controller.overlays[2] = coral_slab
     wx.CallAfter(self.pb.finish, 'Finishing up')
     wx.CallAfter(self.controller.add_overlay)
     if self.controller.show:
         self.controller.alphas = [50, 50, 0]
         wx.CallAfter(self.controller.display)
         wx.CallAfter(self.controller.view.Show)
     else:
         wx.CallAfter(self.controller.display)
def getGradientCrop(image, point):
    grad_crop = image.crop((point[0]-(patch_size+1)/2, point[1]-(patch_size+1)/2, point[0]+(patch_size+3)/2, point[1]+(patch_size+3)/2))
    grad_crop = numpy.array(grad_crop).astype('float32')
    dx = sobel(grad_crop, 0)/4.0
    dy = sobel(grad_crop, 1)/4.0
    grad_crop = numpy.hypot(dx, dy)
    grad_crop = grad_crop[1:patch_size+1, 1:patch_size+1]
    return grad_crop
Пример #21
0
def _gradientImage(image):
    """
    Obtain a gradient image (in both x and y directions)
    """
    gradient = np.sqrt(filters.sobel(image, 0)**2 + filters.sobel(image, 1)**2)
    gradient -= gradient.min()

    return gradient 
Пример #22
0
def Sobel_feature(img_name, image_size):
  im = array(Image.open(img_name).resize(image_size).convert('L'))
  imx = zeros(im.shape)
  filters.sobel(im,1,imx)
  imy = zeros(im.shape)
  filters.sobel(im,0,imy)
  
  return sqrt(imx**2 + imy**2).flatten()
Пример #23
0
def _gradientImage(image):
    """
    Obtain a gradient image (in both x and y directions)
    """
    gradient = np.sqrt(filters.sobel(image, 0)**2 + filters.sobel(image, 1)**2)
    gradient -= gradient.min()

    return gradient 
Пример #24
0
    def sobel_y(self):
        pil_img = Image.open('Chihiro.jpg').convert('L')
        img = np.array(pil_img)

        result = np.zeros(img.shape)
        filters.sobel(img, 0, result)
        plt.imshow(result, cmap='gray', vmin=0, vmax=255)
        plt.show()
Пример #25
0
def Sobel_feature(img_name, image_size):
    im = array(Image.open(img_name).resize(image_size).convert('L'))
    imx = zeros(im.shape)
    filters.sobel(im, 1, imx)
    imy = zeros(im.shape)
    filters.sobel(im, 0, imy)

    return sqrt(imx**2 + imy**2).flatten()
Пример #26
0
def getGradientCrop(image, point):
	grad_crop = image.crop((point[0]-(patch_size+1)/2, point[1]-(patch_size+1)/2, point[0]+(patch_size+3)/2, point[1]+(patch_size+3)/2))
	grad_crop = numpy.array(grad_crop).astype('float32')
	dx = sobel(grad_crop, 0)/4.0
	dy = sobel(grad_crop, 1)/4.0
	grad_crop = numpy.hypot(dx, dy)
	grad_crop = grad_crop[1:patch_size+1, 1:patch_size+1]
	return grad_crop
Пример #27
0
def gradient_orientation(image):
    '''
    Calculate the gradient orientation for edge point in the image
    '''
    dx = sobel(image, axis=0, mode='constant')
    dy = sobel(image, axis=1, mode='constant')
    gradient = np.arctan2(dy, dx) * 180 / np.pi
    return gradient
Пример #28
0
def gradient(image):
    imx = zeros(image.shape)
    imy = zeros(image.shape)

    filters.sobel(image, 1, imx)
    filters.sobel(image, 0, imy)

    angles = arctan2(imy, imx)
    return (sqrt(imx ** 2 + imy ** 2), angles)
Пример #29
0
def computeEnergy(im):
    imx = zeros(im.shape)
    filters.sobel(im, 1, imx)

    imy = zeros(im.shape)
    filters.sobel(im, 0, imy)

    magnitude = sqrt(imx**2 + imy**2)
    return magnitude
Пример #30
0
def gradient_orientation(image):
    '''
    Calculate the gradient orientation for edge point in the image
    '''
    dx = sobel(image, axis=0, mode='constant')
    dy = sobel(image, axis=1, mode='constant')
    gradient = np.arctan2(dy,dx) * 180 / np.pi
    
    return gradient
Пример #31
0
def filtering():
    im = np.array(Image.open('./dataimg/empire.jpg').convert('L'))
    im2 = filters.gaussian_filter(im, 15)
    imx = np.zeros(im.shape)
    filters.sobel(im, 1, imx)

    imshow(imx)
    gray() #use gray() when you use convert
    show()
Пример #32
0
def compute_color_gradmag(image_arr):
    """ Compute average gradient magnitude of a 3D image (2D image with
    multiple channels). """
    if image_arr.ndim != 3 or image_arr.shape[2] != 3:
        raise ValueError('The image should have 3 channels!')

    dy = sobel(image_arr, axis=0)
    dx = sobel(image_arr, axis=1)
    return np.mean(np.hypot(dx, dy), axis=2)
Пример #33
0
def take_filter():
    """ give filter show of saved images """
    for i in range(1,11):
        im   = np.array(Image.open('img/screenshot'+ str(10*i) +'.png').convert('L'));
        imx  = np.zeros(im.shape);filters.sobel(im,1,imx);
        imy  = np.zeros(im.shape);filters.sobel(im,0,imy);
        imxy = np.sqrt(imx**2+imy**2);
        Image.fromarray( np.append(np.append(imx,imy,axis=1),imxy,axis=1)  ).convert('RGB').save('img_filter/f'+str(10*i)+'.jpg');
        cv.imshow('time',cv.imread('img_filter/f'+str(10*i)+'.jpg'));cv.waitKey(52);
Пример #34
0
def get_gradient_magnitudes(array):
    """Get gradient magnitude array."""
    imx = numpy.zeros(array.shape)
    filters.sobel(array, 1, imx)

    imy = numpy.zeros(array.shape)
    filters.sobel(array, 0, imy)

    return numpy.sqrt(imx**2 + imy**2)
Пример #35
0
def gradient(image, type=0):
    if type == 0:
        Ix = filters.sobel(image, axis=1)
        Iy = filters.sobel(image, axis=0)
        return Ix, Iy
    else:
        Ix = filters.prewitt(image, axis=1)
        Iy = filters.prewitt(image, axis=0)
    return Ix, Iy
Пример #36
0
    def computeHarrisValues(self, srcImage):
        '''
        Input:
            srcImage -- Grayscale input image in a numpy array with
                        values in [0, 1]. The dimensions are (rows, cols).
        Output:
            harrisImage -- numpy array containing the Harris score at
                           each pixel.
            orientationImage -- numpy array containing the orientation of the
                                gradient at each pixel in degrees.
        '''
        height, width = srcImage.shape[:2]

        harrisImage = np.zeros(srcImage.shape[:2], dtype=float)
        orientationImage = np.zeros(srcImage.shape[:2], dtype=float)

        # TODO 1: Compute the harris corner strength for 'srcImage' at
        # each pixel and store in 'harrisImage'.  See the project page
        # for direction on how to do this. Also compute an orientation
        # for each pixel and store it in 'orientationImage.'
        # TODO-BLOCK-BEGIN

        # sx = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
        # sy = np.array([[1,2,1],[0,0,0],[-1,-2,-1]])

        sobx = np.zeros(srcImage.shape[:2], dtype=float)
        filters.sobel(srcImage, 1, sobx)
        soby = np.zeros(srcImage.shape[:2], dtype=float)
        filters.sobel(srcImage, 0, soby)
        # sobx = filters.convolve(srcImage,sx,mode='reflect')
        # soby = filters.convolve(srcImage,sy,mode='reflect')
        Ix = sobx * sobx
        Iy = soby * soby
        Ixy = sobx * soby

        Wxx = filters.gaussian_filter(Ix, sigma=0.5)
        Wyy = filters.gaussian_filter(Iy, sigma=0.5)
        Wxy = filters.gaussian_filter(Ixy, sigma=0.5)

        # for i in range(height):
        #     for j in range(width):
        #         M = np.array([[Wxx[i,j],Wxy[i,j]],[Wxy[i,j],Wyy[i,j]]])
        #         R = np.linalg.det((M)-0.1*np.trace(M)*np.trace(M))
        #         harrisImage[i,j] = R
        #         orientationImage[i, j] = np.arctan2(Ix[i, j], Iy[i, j]) * (180) / np.pi
        # orientationImage[i,j] = np.arctan2(Ix[i,j],Iy[i,j])
        harrisImage = Wxx * Wyy - Wxy * Wxy - 0.1 * (Wxx + Wyy) * (Wxx + Wyy)
        orientationImage = np.arctan2(soby, sobx) * (180) / np.pi
        # TODO-BLOCK-END

        # raise Exception("TODO in features.py not implemented")

        # Save the harris image as harris.png for the website assignment
        self.saveHarrisImage(harrisImage, srcImage)

        return harrisImage, orientationImage
Пример #37
0
def GRADfocus(img):

    Gx = nd.sobel(img, axis=0)
    Gy = nd.sobel(img, axis=1)

    Gmag = Gx**2 + Gy**2
    
    focusMeasure = numpy.mean(Gmag)
    
    return focusMeasure
Пример #38
0
def showImageGradient(im):
    #im = array(Image.open(’empire.jpg’).convert(’L’))
    #Sobel derivative filters
    imx = zeros(im.shape)
    filters.sobel(im,1,imx)
    imy = zeros(im.shape)
    filters.sobel(im,0,imy)
    magnitude = sqrt(imx**2+imy**2)
    #Need to blur with different colors
    return (imx, imy, magnitude)
Пример #39
0
def sobel_grad(image_array):

    grad_y = sobel(image_array.astype(np.float64), 0)
    grad_x = sobel(image_array.astype(np.float64), 1)

    #print "grad_x dtype = ", grad_x.dtype

    mag = np.sqrt(grad_y**2 + grad_x**2) + 1e-16

    return mag, grad_x, grad_y
Пример #40
0
    def compute_derivatives(prev_vol, next_vol):
        """Computes 3D derivatives between two 3D volume images."""

        vx = sobel(next_vol, axis=0, mode='constant')
        vy = sobel(next_vol, axis=1, mode='constant')
        vz = sobel(next_vol, axis=2, mode='constant')

        vt = prev_vol - next_vol

        return vx, vy, vz, vt
Пример #41
0
def main():
    fname = sys.argv[1]
    im = array(Image.open(fname).convert('L'))

    imx = zeros(im.shape)
    filters.sobel(im, 1, imx)
    imy = zeros(im.shape)
    filters.sobel(im, 0, imy)
    mag = sqrt(imx**2 + imy**2)
    showim(mag)

    saveim(mag, 'test.jpg')
Пример #42
0
def apply_sobel(img):

	imx = zeros(img.shape)
	filters.sobel(img,1,imx)
	
	imy = zeros(img.shape)
	filters.sobel(img,0,imy)

	#the magnitude
	grad = sqrt(imx**2+imy**2)

	return imx,imy,grad
Пример #43
0
def _detectEdges(image, threshold):
    """
    Sobel edge detection on the image
    """
    # sobel filter in x and y direction
    image = filters.sobel(image, 0)**2 + filters.sobel(image, 1)**2 
    image -= image.min()
    
    # make binary image
    image = image > image.max()*threshold
    image.dtype = np.int8
        
    return image
Пример #44
0
    def _calcD(frame):
        """ Calculate the 2d derivative of the frame """

        if method == 'sobel':
            from scipy.ndimage.filters import sobel
            dx = np.abs(sobel(frame, axis=-1))
            dy = np.abs(sobel(frame, axis=0))
                
        if method == 'roll':
            dx = frame-np.roll(frame, 1, axis=0)
            dy = frame-np.roll(frame, 1, axis=1)
    
        gradient = np.sqrt(dx**2+dy**2)**exponent
        
        return gradient*mask
def sobel_xy_derivative(img):
    """
    Applies Sobel filters to identify X and Y derivatives
    """
    # Sobel derivative filters
    imx = zeros(img.shape)
    filters.sobel(img, 1, imx)

    imy = zeros(img.shape)
    filters.sobel(img, 0, imy)

    magnitude = sqrt(imx**2 + imy**2)

    # Returning the filtered image
    return magnitude
def gaussian_xy_derivatives(img, standard_deviation=5):
    """
    Applies Gaussian filters to identify the X and Y derivatives
    """
    # Gaussian derivative filters
    imx = zeros(img.shape)
    filters.gaussian_filter(img, (standard_deviation, standard_deviation), (0,1), imx)

    imy = zeros(img.shape)
    filters.sobel(img, (standard_deviation, standard_deviation), (1,0), imx)

    magnitude = sqrt(imx**2 + imy**2)

    # Returning the filtered image
    return magnitude
Пример #47
0
def _acutance_calc(data,method,normalized,mask,exponent):
    assert data.ndim == 2, "data is wrong ndim"

    if method == 'sobel':
        from scipy.ndimage.filters import sobel
        dx = abs(sobel(data,axis=-1))
        dy = abs(sobel(data,axis=0))
                
    if method == 'roll':
        dx = data-numpy.roll(data,1,axis=0)
        dy = data-numpy.roll(data,1,axis=1)

    gradient = numpy.sqrt(dx**2+dy**2)**exponent
            
    a = numpy.sum(gradient*mask)
    if normalized: a *= 1./numpy.sum(data*mask)
    return a
Пример #48
0
def FindFeatures(infile):
	im = np.array(Image.open(infile).convert('L'))
	print 'FindAlphas infile', infile

	## GRADIENT 
	imx = np.zeros( im.shape ) 
	filters.sobel( im, 1, imx )
	imy = np.zeros( im.shape )
	filters.sobel( im, 0, imy )
	magnitude = np.sqrt ( imx**2 + imy**2 )

	## FILTER OUT ZEROS 
	magnitude = magnitude[np.where(magnitude > 1.0e-3)]

	## HISTOGRAM 16 bins/features returned, normed => values sum = 1 
	n, bins, patches = plt.hist(magnitude, 16,  normed = 1)
	plt.close() #close the hist plot before doing thumbnail plot
	return n
Пример #49
0
def main():
    
    # Parse command line arguments
    parser = argparse.ArgumentParser(description='Sobel filter 3D image')
    parser.add_argument('-i','--in_file', help="source image filename")
    parser.add_argument('-o','--out_file', help="Sobel filtered image filename")

    args = parser.parse_args()

    in_file = args.in_file
    out_file = args.out_file
        
    # Load the source image
    print('Opening %s' % in_file)
    in_nii = nib.load(in_file)
    
    # Load label image
    print('Loading image')
    src_img = in_nii.get_data()
    src_img = src_img.astype(float)
    
    # Smooth target label region
    print('  Sobel gradients')
    Sx = sobel(src_img, axis=0)
    Sy = sobel(src_img, axis=1)
    Sz = sobel(src_img, axis=2)
    
    # Take gradient magnitude
    print('  Sobel gradient magnitude')
    # out_img = np.sqrt(Sx**2 + Sy**2 + Sz**2)
    out_img = np.sqrt(Sx**2 + Sz**2)
    
    # Save Sobel image
    print('Saving Sobel image %s' % out_file)
    out_nii = nib.Nifti1Image(out_img, in_nii.get_affine())
    out_nii.to_filename(out_file)
    
    print('Done')
    
    # Clean exit
    sys.exit(0)
Пример #50
0
def FindFeatures(infile):
	# im = np.array(Image.open(infile))
	im = infile.astype(np.float)
	histlist = []
	print "imshape", im.shape
	imderivatives = []
	for i in xrange(0,3):
		imTemp = im[:,:,i]
		print "imTemp", imTemp.shape
		imx = np.zeros( imTemp.shape ) 
		filters.sobel( imTemp, 1, imx )
		imy = np.zeros( imTemp.shape )
		filters.sobel( imTemp, 0, imy )
		magnitude = np.sqrt ( imx**2 + imy**2 )
		# magnitude = magnitude[np.where(magnitude > 1.0e-3)]
		imderivatives.append(magnitude)
		print "magshape", magnitude.shape
	print "len ImageDir", len(imderivatives)
	im = np.dstack((imderivatives))
	print "imderivatives Finished", im.shape 
	# magnitude = im
	## FILTER OUT ZEROS 
	# print "mag shape", magnitude.shape
	# print "magnitude", magnitude
	# print "im", np.max(im)
	# print "IMSHAPE", im.shape
	nc = im.shape #should be 3
	hist = [np.histogram(im[:,chan],
			bins = 16,
			range = (0., 255.),
			density = True)
		for chan in range(0,3)]
	# print "hist", len(hist)
	# print "hist", hist
	hist = np.hstack((hist[0][0],hist[1][0], hist[2][0]))
	# hist = np.hstack(hist[:][:])
	# print "New hist", hist
	# print "vsthist", np.hstack(hist)
	return hist
Пример #51
0
def gradient(image):
    ''' Given an image, this computes its gradient using
    sobel filters::

             [-1 0 1]        [ -1 -2 -1 ]
        Dx = [-2 0 2]   Dy = [  0  0  0 ]
             [-1 0 1]        [  1  2  1 ]

    The prewitt filters can also be used::

             [-1 0 1]        [ -1 -1 -1 ]
        Dx = [-1 0 1]   Dy = [  0  0  0 ]
             [-1 0 1]        [  1  1  1 ]
    '''
    im_x = pl.zeros(image.shape)
    im_y = pl.zeros(image.shape)

    filters.sobel(image, 1, im_x)
    filters.sobel(image, 0, im_y)
    magnitude = pl.sqrt(im_x**2 + im_y**2)
    angle = pl.arctan2(im_y, im_x)
    return magnitude, angle
Пример #52
0
def FindFeatures(infile):
	im = np.asarray(infile)
	# print "im", np.max(im)
	im = np.reshape(im, (-1, im.shape[-1]))
	# print 'FindAlphas infile', infile
	# print "imageColor Channel", im.shape
	## GRADIENT 
	imx = np.zeros( im.shape ) 
	filters.sobel( im, 1, imx )
	imy = np.zeros( im.shape )
	filters.sobel( im, 0, imy )
	magnitude = np.sqrt ( imx**2 + imy**2 )
	# magnitude = im
	# ## FILTER OUT ZEROS 
	# # print "mag shape", magnitude.shape
	# # print "magnitude", magnitude
	magnitude = magnitude[np.where(magnitude > 1.0e-3)]
	# magnitude = im
	## HISTOGRAM 16 bins/features returned, normed => values sum = 1 
	n, bins, patches = plt.hist(magnitude, 16,  normed = 1)
	# print "len n", len(n)
	plt.close() #close the hist plot before doing thumbnail plot
	return n
Пример #53
0
 def find_jumps2(self,ds,threshold=30000):
     self._prepare_find_jumps()
     ds = self._hf[ds]
     offset=ds[0]
     # first we remove a bit of noise
     #flt = gaussian_filter1d(ds,10)
     flt = median_filter(ds,size=10)
     #flt = ds
     # the sobel filter finds the "jumps" 
     sb=sobel(flt)
     for i in sb:
         self.qps_jpn_hight.append(float(i))
         
     for i in flt: self.qps_jpn_spec.append(float(i))
     """    
Пример #54
0
def blur_example():
	""" Blurring image using Gaussian filtering. Blur = I (convol) G """
	figure()
	gray()
	subplot(1,3,1)
	axis('off')
	im = array(Image.open('empire.jpg').convert('L'))
	imshow(im)
	for i in range(3,5):
		subplot(1,3,i-1)
		axis('off')
		im2 = filters.gaussian_filter(im,i) # 5 is standard deviation
		im2 = array(im2, 'uint8')
		imshow(im2)
		
	figure()
	gray()
	subplot(2,2,1)
	imshow(im)
	
	# Sobel derivative filters
	''' Image derivatives shows how image changes over space. '''
	imx = zeros(im.shape)
	filters.sobel(im,1,imx)
	imy = zeros(im.shape)
	filters.sobel(im,0,imy)
	magnitude = sqrt(imx**2+imy**2)

	subplot(2,2,2)
	imshow(imx)
	subplot(2,2,3)
	imshow(imy)
	subplot(2,2,4)
	imshow(magnitude)
		
	show()
Пример #55
0
    def _findSlitPositions(self, slitImage, threshold=1000):
        """
        Finds slit positions from a slit image.

        This method uses the Sobel filter in scipy.ndimage.filters

        :todo: this is not ready!

        :param: filename

        """
        # sobel filter
        filtered = f.sobel(slitImage, axis=1)
        # create a mask above the threshold
        msk = filtered > threshold
        masked = filtered[msk]
        # indices
        y, x = np.indices(slitImage.shape)

        yargs = y[msk]

        return masked
Пример #56
0
 def split_traces(self,ds,threshold=30000):
     self._prepare_find_jumps()
     ds = self._hf[ds]
     # first we remove a bit of noise, size is the number of averages
     #flt = gaussian_filter1d(ds,10)
     flt = median_filter(ds,size=3)
     #flt = ds
     # the sobel filter finds the "jumps" 
     sb=sobel(flt)
     for i in sb:
         self.qps_jpn_hight.append(float(i))
         
     #for i in flt: self.qps_jpn_spec.append(float(i))
     offset=ds[0]
     tr_num = 0
     tr_name = "qps_tr_"+str(tr_num)
     tr_obj =  self._hf.add_value_vector(tr_name, 
                                         folder = 'analysis', 
                                         x = self._x_co, 
                                         unit = 'Hz')
     keepout = 4
     for i,tr in enumerate(flt):
         keepout += 1
         if abs(sb[i])>threshold and keepout>3:
             keepout = 0
             # new trace
             tr_num +=1
             tr_name = "qps_tr_"+str(tr_num)
             tr_obj =  self._hf.add_value_vector(tr_name, 
                                                 folder = 'analysis', 
                                                 x =  self._x_co, 
                                                 unit = 'Hz')
             print tr , i
             #tr_obj.append(float(tr))
         else:
             if keepout>2:
                 tr_obj.append(float(tr-offset))
Пример #57
0
    def gamma(self, comparison_image, doseTA=1, distTA=1, threshold=0.1):
        """Calculate the gamma between the current image (reference) and a comparison image.

        .. versionadded:: 1.2

        The gamma calculation is based on `Bakai et al
        <http://iopscience.iop.org/0031-9155/48/21/006/>`_ eq.6,
        which is a quicker alternative to the standard Low gamma equation.

        Parameters
        ----------
        comparison_image : {:class:`~pylinac.core.image.ArrayImage`, :class:`~pylinac.core.image.DicomImage`, or :class:`~pylinac.core.image.FileImage`}
            The comparison image. The image must have the same DPI/DPMM to be comparable.
            The size of the images must also be the same.
        doseTA : int, float
            Dose-to-agreement in percent; e.g. 2 is 2%.
        distTA : int, float
            Distance-to-agreement in mm.
        threshold : float
            The dose threshold percentage of the maximum dose, below which is not analyzed.
            Must be between 0 and 1.

        Returns
        -------
        gamma_map : numpy.ndarray
            The calculated gamma map.

        See Also
        --------
        :func:`~pylinac.core.image.equate_log_fluence_and_epid`
        """
        # error checking
        if not is_close(self.dpi, comparison_image.dpi, delta=0.1):
            raise AttributeError("The image DPIs to not match: {:.2f} vs. {:.2f}".format(self.dpi, comparison_image.dpi))
        same_x = is_close(self.shape[1], comparison_image.shape[1], delta=1.1)
        same_y = is_close(self.shape[0], comparison_image.shape[0], delta=1.1)
        if not (same_x and same_y):
            raise AttributeError("The images are not the same size: {} vs. {}".format(self.shape, comparison_image.shape))

        # set up reference and comparison images
        ref_img = ArrayImage(copy.copy(self.array))
        ref_img.check_inversion()
        ref_img.ground()
        ref_img.normalize()
        comp_img = ArrayImage(copy.copy(comparison_image.array))
        comp_img.check_inversion()
        comp_img.ground()
        comp_img.normalize()

        # invalidate dose values below threshold so gamma doesn't calculate over it
        ref_img.array[ref_img < threshold * np.max(ref_img)] = np.NaN

        # convert distance value from mm to pixels
        distTA_pixels = self.dpmm * distTA

        # construct image gradient using sobel filter
        img_x = spf.sobel(ref_img.as_type(np.float32), 1)
        img_y = spf.sobel(ref_img.as_type(np.float32), 0)
        grad_img = np.hypot(img_x, img_y)

        # equation: (measurement - reference) / sqrt ( doseTA^2 + distTA^2 * image_gradient^2 )
        subtracted_img = np.abs(comp_img - ref_img)
        denominator = np.sqrt((doseTA / 100.0 ** 2) + ((distTA_pixels ** 2) * (grad_img ** 2)))
        gamma_map = subtracted_img / denominator

        return gamma_map
Пример #58
0
from PIL import Image
from numpy import *
from scipy.ndimage import filters
from pylab import *

im = array(Image.open('1.jpg').convert('L'))

#Sobel derivative filters
imx = zeros(im.shape)
filters.sobel(im,1,imx)

imshow(imx)
show()

imy = zeros(im.shape)
filters.sobel(im,0,imy)
magnitude = sqrt(imx**2+imy**2)

imshow(imy)
show()

imshow(magnitude)
show()
Пример #59
0
def series_align(im_series,align_output=[],start='Mid',smooth=True,smooth_window='3',sobel=True):
    '''Function to align a series of images.'''
    if align_output == []:
        series_dim = len(im_series)
        
        filtered_series = []
        
        for i in range(series_dim):
            filtered_series.append(im_series[i].copy())
        
        align_output = []
        
        if smooth == True:
            for i in range(series_dim):
                filtered_series[i] = filters.gaussian_filter(filtered_series[i],3)
        
        if sobel == True:
            for i in range(series_dim):
                filtered_series[i] = filters.sobel(filtered_series[i])
        
        #Align from first image
        if start == 'First':
            align_output.append(register_translation(filtered_series[0], filtered_series[0],100))
            for i in range(series_dim-1):
                align_output.append(register_translation(filtered_series[i], filtered_series[i+1],100))
                align_output[i+1][0][0] = align_output[i+1][0][0] + align_output[i][0][0]
                align_output[i+1][0][1] = align_output[i+1][0][1] + align_output[i][0][1]
        
        #Align from mid-image
        if start == 'Mid':
            
            #Ensure compatibility with Pyton 2 and 3
            if series_dim % 2 == 0:
                mid_point = series_dim / 2
            else:
                mid_point = series_dim // 2
            
            align_output.append(register_translation(filtered_series[mid_point], filtered_series[mid_point], 100))
            
            for i in range(mid_point,0,-1):
                align_output.append(register_translation(filtered_series[i], filtered_series[i-1], 100))
                align_output[mid_point-i+1][0][0] = align_output[mid_point-i+1][0][0] + align_output[mid_point-i][0][0]
                align_output[mid_point-i+1][0][1] = align_output[mid_point-i+1][0][1] + align_output[mid_point-i][0][1]
                
            align_output = list(reversed(align_output))
            
            for i in range(mid_point,series_dim-1):
                align_output.append(register_translation(filtered_series[i], filtered_series[i+1], 100))
                align_output[i+1][0][0] = align_output[i+1][0][0] + align_output[i][0][0]
                align_output[i+1][0][1] = align_output[i+1][0][1] + align_output[i][0][1]
        
    #Apply calculated shifts to the image series
    shifted_im_series = []
    im_count = 0
    for im in im_series:
        shifted_im_series.append(interpolation.shift(im,align_output[im_count][0]))
        im_count = im_count + 1
        
    shifted_im_series = np.asarray(shifted_im_series)
        
    return(shifted_im_series, align_output)