Ejemplo n.º 1
0
def edgeDetectionSobel(img: np.ndarray,
                       thresh: float = 0.2) -> (np.ndarray, np.ndarray):
    """
    Detects edges using the Sobel method
    :param img: Input image
    :param thresh: The minimum threshold for the edge response
    :return: opencv solution, my implementation
    """
    s = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]])

    thresh *= 255
    my_res = np.sqrt((conv2D(img, s)**2 + conv2D(img, s.transpose())**2))
    my = np.ndarray(my_res.shape)
    my[my_res > thresh] = 1
    my[my_res < thresh] = 0
    plt.imshow(my, cmap='gray')
    plt.show()

    cv_res = cv.magnitude(cv.Sobel(img, -1, 1, 0), cv.Sobel(img, -1, 0, 1))
    v = np.ndarray(cv_res.shape)
    v[cv_res > thresh] = 1
    v[cv_res < thresh] = 0

    plt.imshow(v, cmap='gray')
    plt.show()
    return cv_res, my_res
Ejemplo n.º 2
0
def mag_thresh(img, sobel_kernel=3, mag_thresh=(0, 255)):
    """
    Filter by global color change
    Parameter:
        img:
        sobel_kernel:
        mag_thresh:
    Return:
    """
    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Take both Sobel x and y gradients
    sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
    sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
    # Calculate the gradient magnitude
    gradmag = np.sqrt(sobelx**2 + sobely**2)
    # Rescale to 8 bit
    scale_factor = np.max(gradmag) / 255
    gradmag = (gradmag / scale_factor).astype(np.uint8)
    # Create a binary image of ones where threshold is met, zeros otherwise
    binary_output = np.zeros_like(gradmag)
    binary_output[(gradmag >= mag_thresh[0]) & (gradmag <= mag_thresh[1])] = 1

    # Return the binary image
    return binary_output
Ejemplo n.º 3
0
def Sobel_Filter(img):
    sobel_x = cv2.Sobel(img, cv2.CV_16S, 1, 0, ksize=5)
    sobel_y = cv2.Sobel(img, cv2.CV_16S, 0, 1, ksize=5)
    sobel_x = cv2.convertScaleAbs(sobel_x)
    sobel_y = cv2.convertScaleAbs(sobel_y)
    sobel_xy = cv2.addWeighted(sobel_x, 0.5, sobel_y, 0.5, 0)
    return sobel_xy
Ejemplo n.º 4
0
def sobel_extract(img):
    x = cv2.Sobel(img, cv2.CV_16S, 1, 0)  # 1,0代表只计算x方向计算边缘
    y = cv2.Sobel(img, cv2.CV_16S, 0, 1)  # 0,1代表只在y方向计算边缘
    absX = cv2.convertScaleAbs(x)
    absY = cv2.convertScaleAbs(y)
    dst = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
    return dst
Ejemplo n.º 5
0
def edgeDetectionSobel(img: np.ndarray, thresh: float = 0.2) -> (np.ndarray, np.ndarray):
    """
    Detects edges using the Sobel method
    :param img: Input image
    :param thresh: The minimum threshold for the edge response
    :return: opencv solution, my implementation
    """
    s = np.array([[1, 0, -1],
                  [2, 0, -2],
                  [1, 0, -1]])

    thresh *= 255
    # my_res = np.sqrt((conv2D(img, s) ** 2 + conv2D(img, s.transpose()) ** 2))
    my_res = np.sqrt((cv.filter2D(img, -1, s, borderType=cv.BORDER_REPLICATE) ** 2 + cv.filter2D(img, -1, s.transpose(),
                                                                                                 borderType=cv.BORDER_REPLICATE) ** 2))
    my = np.ndarray(my_res.shape)
    my[my_res > thresh] = 1
    my[my_res < thresh] = 0

    cv_res = cv.magnitude(cv.Sobel(img, -1, 1, 0), cv.Sobel(img, -1, 0, 1))
    v = np.ndarray(cv_res.shape)
    v[cv_res > thresh] = 1
    v[cv_res < thresh] = 0

    return cv_res, my_res
def matchTable(im, table):
    """
    :param im: input binary image, for searching template
    :param table: table for template
    :return:
        accumulator with searched votes
    """
    # matches the reference table with the given input
    # image for testing generalized Hough Transform
    m, n = im.shape
    acc = np.zeros((m, n))  # acc array requires some extra space

    #  这里是用梯度来作为key, 然后r作为后面的value这样确实是比较说的通的做法!!!!
    dx = cv.Sobel(im, -1, 1, 0)
    dy = cv.Sobel(im, -1, 0, 1)
    gradient = np.arctan2(dy, dx) * 180 / np.pi

    for (i, j), value in np.ndenumerate(im):
        if value:
            for r in table[gradient[i, j]]:
                accum_i, accum_j = i + r[0], j + r[1]
                if accum_i < acc.shape[0] and accum_j < acc.shape[1]:
                    acc[int(accum_i), int(accum_j)] += 1

    return acc
Ejemplo n.º 7
0
def apply_sobel(img, *params):
    ddepth = cv2.CV_16S
    scale = 1
    delta = 0
    kernel_size = 3

    img = cv2.GaussianBlur(img, (5, 5), cv2.BORDER_DEFAULT)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

    grad_x = cv2.Sobel(img,
                       ddepth,
                       1,
                       0,
                       ksize=kernel_size,
                       scale=scale,
                       delta=delta,
                       borderType=cv2.BORDER_DEFAULT)
    grad_y = cv2.Sobel(img,
                       ddepth,
                       0,
                       1,
                       ksize=kernel_size,
                       scale=scale,
                       delta=delta,
                       borderType=cv2.BORDER_DEFAULT)
    abs_grad_x = cv2.convertScaleAbs(grad_x)
    abs_grad_y = cv2.convertScaleAbs(grad_y)

    new_image = cv2.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)
    #new_image = cv2.Sobel(img, cv2.CV_16S, 1, 0, ksize=3)

    return new_image
Ejemplo n.º 8
0
def sobleForCanny(img: np.ndarray):
    """
    A simple sobel that uses CV's functions.
    """
    G = np.sqrt(np.power(cv.Sobel(img, -1, 0, 1), 2) + np.power(cv.Sobel(img, -1, 1, 0), 2))
    theta = np.arctan2(cv.Sobel(img, -1, 0, 1), cv.Sobel(img, -1, 1, 0))
    return G, theta
Ejemplo n.º 9
0
def abs_sobel_thresh(img, orient='x', thresh_min=0, thresh_max=255):
    """
    Filter by gradient threshold(gradient of color change)
    Parameter:
        img:
        orient:
        thresh_min:
        thresh_max:
    Return:
    """
    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Apply x or y gradient with the OpenCV Sobel() function
    # and take the absolute value
    if orient == 'x':
        abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 1, 0))
    if orient == 'y':
        abs_sobel = np.absolute(cv2.Sobel(gray, cv2.CV_64F, 0, 1))
    # Rescale back to 8 bit integer
    scaled_sobel = np.uint8(255 * abs_sobel / np.max(abs_sobel))
    # Create a copy and apply the threshold
    binary_output = np.zeros_like(scaled_sobel)
    # Here I'm using inclusive (>=, <=) thresholds, but exclusive is ok too
    binary_output[(scaled_sobel >= thresh_min)
                  & (scaled_sobel <= thresh_max)] = 1

    # Return the result
    return binary_output
Ejemplo n.º 10
0
def edge_demo(image):  #边缘提取
    blurred = cv.GaussianBlur(image, (3, 3), 0)
    gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
    xgrad = cv.Sobel(gray, cv.CV_16SC1, 1, 0)
    ygrad = cv.Sobel(gray, cv.CV_16SC1, 0, 1)
    edge = cv.Canny(xgrad, ygrad, 50, 150)
    dst = cv.bitwise_and(image, image, mask=edge)
    cv.imshow("edge", dst)
Ejemplo n.º 11
0
def sobel():
    image = imageInit

    img_sobelx = cv2.Sobel(image, cv2.CV_8U, 1, 0, ksize=5)
    img_sobely = cv2.Sobel(image, cv2.CV_8U, 0, 1, ksize=5)
    edged = img_sobelx + img_sobely

    show_img(ImageTk.PhotoImage(Img.fromarray(edged)))
Ejemplo n.º 12
0
def sobel_gradient(image):
    grad_x = cv.Sobel(image, cv.CV_32F, 1, 0)           # 对x求一阶导
    grad_y = cv.Sobel(image, cv.CV_32F, 0, 1)           # 对y求一阶导
    gradx = cv.convertScaleAbs(grad_x)                  # 用convertScaleAbs()函数将其转回原来的uint8形式
    grady = cv.convertScaleAbs(grad_y)
    # cv.imshow("sobel_x", gradx)                         # x方向上的梯度
    # cv.imshow("sobel_y", grady)                         # y方向上的梯度
    gradxy = cv.addWeighted(gradx, 0.5, grady, 0.5, 0)  # 图片融合
    cv.imshow("sobel", gradxy)
Ejemplo n.º 13
0
 def describe(self, pattern_img, eps=1e-7):
     # Calculate Sobel gradient
     grad_x = cv2.Sobel(pattern_img, cv2.CV_32F, 1, 0, ksize=3)
     grad_y = cv2.Sobel(pattern_img, cv2.CV_32F, 0, 1, ksize=3)
     grad_mag, grad_ang = cv2.cartToPolar(grad_x,
                                          grad_y,
                                          angleInDegrees=True)
     hist = self.get_grad_features(grad_mag, grad_ang).astype(np.float32)
     return hist
Ejemplo n.º 14
0
def canny_edge(image):
    blurred = cv.GaussianBlur(image, (3, 3), 0)
    gray = cv.cvtColor(blurred, cv.COLOR_RGB2GRAY)
    xgrad = cv.Sobel(gray, cv.CV_16SC1, 1, 0) #x方向梯度
    ygrad = cv.Sobel(gray, cv.CV_16SC1, 0, 1) #y方向梯度
    edge_output = cv.Canny(xgrad, ygrad, 50, 150)
    # edge_output = cv.Canny(gray, 50, 150)
    cv.imshow("Canny Edge", edge_output)
    dst = cv.bitwise_and(image, image, mask= edge_output)
    cv.imshow("Color Edge", dst)
Ejemplo n.º 15
0
def getGradDiff(img1_Y, img2_Y):
    gradx = cv2.Sobel(img1_Y, cv2.CV_64F, 1, 0)
    grady = cv2.Sobel(img1_Y, cv2.CV_64F, 0, 1)

    grad_diff_mag = np.sqrt(gradx**2 + grady**2)
    gradx = cv2.Sobel(img2_Y, cv2.CV_64F, 1, 0)
    grady = cv2.Sobel(img2_Y, cv2.CV_64F, 0, 1)
    grad_diff_mag += np.sqrt(gradx**2 + grady**2)

    return grad_diff_mag
Ejemplo n.º 16
0
def hog(img):
    gx = cv2.Sobel(img, cv2.CV_32F, 1, 0)
    gy = cv2.Sobel(img, cv2.CV_32F, 0, 1)
    mag, ang = cv2.cartToPolar(gx, gy)
    bins = np.int32(bin_n*ang/(2*np.pi))    # quantizing binvalues in (0...16)
    bin_cells = bins[:10,:10], bins[10:,:10], bins[:10,10:], bins[10:,10:]
    mag_cells = mag[:10,:10], mag[10:,:10], mag[:10,10:], mag[10:,10:]
    hists = [np.bincount(b.ravel(), m.ravel(), bin_n) for b, m in zip(bin_cells, mag_cells)]
    hist = np.hstack(hists)     # hist is a 64 bit vector
    # cv2.imshow('hog',hist)
    return hist
Ejemplo n.º 17
0
def sobel_demo(image):  #当边缘不明显时可用Scharr算子,但该算子受噪声影响较大
    grad_x = cv.Sobel(image, cv.CV_32F, 1, 0)
    grad_y = cv.Sobel(image, cv.CV_32F, 0, 1)

    gradx = cv.convertScaleAbs(grad_x)
    grady = cv.convertScaleAbs(grad_y)

    cv.imshow("x_dir", gradx)
    cv.imshow("y_dir", grady)

    gradxy = cv.addWeighted(gradx, 0.5, grady, 0.5, 0)
    cv.imshow("xy_dir", gradxy)
Ejemplo n.º 18
0
def abs_tenengrad(m, n, thres, l, sigma, mean, p, img, hist_range):
    sum1 = 0
    sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
    sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)

    sx = abs(np.int64(sobelx))
    sy = abs(np.int64(sobely))

    for i in range(1, m - 1):
        for j in range(1, n - 1):
            sum1 = sum1 + (sx[i][j] + sy[i][j])

    return sum1
Ejemplo n.º 19
0
def sobelFilter(image):
    # Use Sobel filter on abscisse X.
    filterX = cv.Sobel(image, cv.CV_64F, 1, 0, ksize=3)
    absoluteX = cv.convertScaleAbs(filterX)

    # Use Sobel filter on abscisse Y.
    filterY = cv.Sobel(image, cv.CV_64F, 0, 1, ksize=3)
    absoluteY = cv.convertScaleAbs(filterY)

    # Converge the output filter.
    filteredImage = np.hypot(absoluteX, absoluteY)
    filteredImage = filteredImage / filteredImage.max() * 255
    angle = np.rad2deg(np.arctan2(filterY, filterX))

    return (filteredImage, angle)
def detect_edge(image):
    """

    Args:
        image: (numpy.ndarray)

    Returns:
        detected_img: (numpy.ndarray)

    """
    detected_img = None
    sobel_x = cv2.Sobel(image, ddepth=cv2.CV_64F, dx=1, dy=0)
    sobel_y = cv2.Sobel(image, ddepth=cv2.CV_64F, dx=0, dy=1)
    sobel_x = numpy.uint8(numpy.absolute(sobel_x))
    sobel_y = numpy.uint8(numpy.absolute(sobel_y))
    detected_img = cv2.bitwise_or(sobel_x, sobel_y)
    return detected_img
Ejemplo n.º 21
0
def myHOG(img, cell_x, cell_y, cell_size):

    gradient_values_x = cv.Sobel(img, cv.CV_64F, 1, 0, ksize=5)
    gradient_values_y = cv.Sobel(img, cv.CV_64F, 0, 1, ksize=5)

    gradient_magnitude = np.sqrt(
        np.power(gradient_values_x, 2) + np.power(gradient_values_y, 2))
    gradient_angle = np.arctan2(gradient_values_x, gradient_values_y)

    grad_cell = div(gradient_magnitude, cell_x, cell_y, cell_size)
    ang_cell = div(gradient_angle, cell_x, cell_y, cell_size)
    bins = np.zeros((cell_x, cell_y, 9))
    for r in range(cell_x):
        for c in range(cell_y):
            bins[r, c] = get_bins(grad_cell[r, c], ang_cell[r, c])

    return bins
Ejemplo n.º 22
0
def grad(img):
    laplacian = cv2.Laplacian(img, cv2.CV_64F)
    sobelx = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
    sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)

    plt.subplot(2, 2, 1), plt.imshow(img, cmap='gray')
    plt.title('orignal'), plt.xticks([]), plt.yticks([])

    plt.subplot(2, 2, 2), plt.imshow(laplacian, cmap='gray')
    plt.title('Laplacian'), plt.xticks([]), plt.yticks([])

    plt.subplot(2, 2, 3), plt.imshow(sobelx, cmap='gray')
    plt.title('sobel X'), plt.xticks([]), plt.yticks([])

    plt.subplot(2, 2, 4), plt.imshow(sobely, cmap='gray')
    plt.title('sobel Y'), plt.xticks([]), plt.yticks([])

    plt.show()
Ejemplo n.º 23
0
 def sobelYDetection(self):
     try:
         img = cv2.imread(self.filename)
         sobely = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
         cv2.imwrite('img/dist/sobely.jpg', sobely,
                     [cv2.IMWRITE_JPEG_QUALITY, 100])
         cv2.waitKey()
     except:
         pass
Ejemplo n.º 24
0
    def _calculate_gradients(self, image):

        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        H_size, W_size = gray.shape

        # Output dtype = cv2.CV_64F. Then take its absolute and convert to cv2.CV_8U
        sobelx64f = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5)
        abs_sobel64f = np.absolute(sobelx64f)
        sobel_8u_x = np.uint8(abs_sobel64f).reshape(H_size, W_size, 1)

        # Output dtype = cv2.CV_64F. Then take its absolute and convert to cv2.CV_8U
        sobely64f = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5)
        abs_sobel64f = np.absolute(sobely64f)
        sobel_8u_y = np.uint8(abs_sobel64f).reshape(H_size, W_size, 1)

        sobel_8u_x = self._convert2cell(sobel_8u_x)
        sobel_8u_y = self._convert2cell(sobel_8u_y)
        return np.dstack((sobel_8u_x, sobel_8u_y))
Ejemplo n.º 25
0
    def _image_gradient(self, orignal_img, edged_img):
        """
        Function to find gradient vectors (θ) of the image. 
        Using sobel operators to find the  gradient in dx 
        and dy direction using 5x5 kernel θ matrix is formed
        and only those indexex are retained where the edged_img!=0. 

        parameters
        --------------------------------------
        orignal_img : np.ndarray, required
            Orignal Grayscale Image
        
        edged_img : np.ndarray, required
            Edge image of  Grayscale Image
        
        returns
        --------------------------------------
        tuple - (gray_image, image_edge)

        Returns Grayscale Image and Edged Image
        """
        rows, columns = orignal_img.shape[:2]
        dx = cv2.Sobel(orignal_img,
                       cv2.CV_32F,
                       1,
                       0,
                       ksize=5,
                       scale=-1,
                       delta=1,
                       borderType=cv2.BORDER_DEFAULT)
        dy = cv2.Sobel(orignal_img,
                       cv2.CV_32F,
                       0,
                       1,
                       ksize=5,
                       scale=-1,
                       delta=1,
                       borderType=cv2.BORDER_DEFAULT)

        theta_mat = np.arctan2(dy, dx)
        edgesbool = (edged_img != 0).astype(int)
        theta_mat = theta_mat * edgesbool

        return theta_mat
Ejemplo n.º 26
0
def calc_hog(gray):
    ''' 计算梯度 '''
    dx = cv.Sobel(gray, cv.CV_16S, 1, 0)
    dy = cv.Sobel(gray, cv.CV_16S, 0, 1)
    sigma = 1e-3
    # 计算角度
    angle = np.int32(np.arctan(dy / (dx + sigma)) * 180 / np.pi) + 90
    dx = cv.convertScaleAbs(dx)
    dy = cv.convertScaleAbs(dy)
    # 计算梯度大小
    mag = cv.addWeighted(dx, 0.5, dy, 0.5, 0)

    print('angle\n', angle[:8, :8])
    print('mag\n', mag[:8, :8])
    ''' end of 计算梯度 '''

    # 将图像切成多个cell
    cell_size = 8
    bin_size = 9
    img_h, img_w = gray.shape[:2]
    cell_h, cell_w = (img_h // cell_size, img_w // cell_size)

    cells = np.zeros((cell_h, cell_w, bin_size), dtype=np.int32)
    for i in range(cell_h):
        cell_row = cell_size * i
        for j in range(cell_w):
            cell_col = cell_size * j
            cells[i, j] = calc_hist(
                mag[cell_row:cell_row + cell_size,
                    cell_col:cell_col + cell_size],
                angle[cell_row:cell_row + cell_size,
                      cell_col:cell_col + cell_size], bin_size)

    # 多个cell融合成block
    block_size = 2
    block_h, block_w = (cell_h - block_size + 1, cell_w - block_size + 1)
    blocks = np.zeros((block_h, block_w, block_size * block_size * bin_size),
                      dtype=np.float32)
    for i in range(block_h):
        for j in range(block_w):
            blocks[i, j] = l2_norm(cells[i:i + block_size, j:j + block_size])

    return blocks.flatten()
Ejemplo n.º 27
0
def edge_detection():
    img = cv2.imread('2.jpg', cv2.IMREAD_GRAYSCALE)
    lap = cv2.Laplacian(img, cv2.CV_64F, ksize=3)
    lap = np.uint8(np.absolute(lap))
    sobelX = cv2.Sobel(img, cv2.CV_64F, 1, 0)
    sobelY = cv2.Sobel(img, cv2.CV_64F, 0, 1)

    sobelX = np.uint8(np.absolute(sobelX))
    sobelY = np.uint8(np.absolute(sobelY))

    sobelCombined = cv2.bitwise_or(sobelX, sobelY)

    titles = ['image', 'Laplacian', 'sobelX', 'sobelY', 'sobelCombined']
    images = [img, lap, sobelX, sobelY, sobelCombined]
    for i in range(5):
        plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray')
        plt.title(titles[i])
        plt.xticks([]), plt.yticks([])

    plt.show()
Ejemplo n.º 28
0
def edge_diff(img_1, img_2):
    """
    https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_gradients/py_gradients.html
    """

    if (img_1.shape != img_2.shape):
        raise ValueError("Images must have same dimensions.")

    gray_1 = cv2.cvtColor(img_1, cv2.COLOR_BGR2GRAY)
    gray_2 = cv2.cvtColor(img_2, cv2.COLOR_BGR2GRAY)

    sobel_1 = cv2.Sobel(gray_1, cv2.CV_64F, 1, 0, None, ksize=5)
    sobel_2 = cv2.Sobel(gray_2, cv2.CV_64F, 1, 0, None, ksize=5)

    diff = 0
    for i in range(sobel_1.shape[0]):
        for j in range(sobel_1.shape[1]):
            diff += abs(sobel_1[i][j] - sobel_2[i][j])

    return (diff / sobel_1.size) / 255
Ejemplo n.º 29
0
def dir_threshold(img, sobel_kernel=3, thresh=(0, np.pi / 2)):
    """
    Parameter:
        img:
        sobel_kernel:
        thresh:
    Return:
    """
    # Grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # Calculate the x and y gradients
    sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=sobel_kernel)
    sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=sobel_kernel)
    # Take the absolute value of the gradient direction,
    # apply a threshold, and create a binary image result
    absgraddir = np.arctan2(np.absolute(sobely), np.absolute(sobelx))
    binary_output = np.zeros_like(absgraddir)
    binary_output[(absgraddir >= thresh[0]) & (absgraddir <= thresh[1])] = 1

    # Return the binary image
    return binary_output
Ejemplo n.º 30
0
def sobel_y(img: np.ndarray, kernel_size=3) -> np.ndarray:
    """
    sobel operator in y direction to identify horizontal gradients

    Args:
        img: image as numpy array
        kernel_size: size of NxN matrix

    Returns:
        img
    """
    return cv2.Sobel(to_gray(img), cv2.CV_64F, 0, 1, ksize=kernel_size)