首页 文章

如何使用Sobel算子在图像中找到基本形状(砖块,圆柱体,球体)?

提问于
浏览
2

Sample 1- Brick

Sample 2- Sphere

我计算了Sobel梯度的大小和方向 . 但我仍然坚持如何进一步使用它进行形状检测 .

图像> Grayscaled> Sobel过滤> Sobel梯度和方向计算>下一步?

使用的Sobel内核是:

Kx = ([[1, 0, -1],[2, 0, -2],[1, 0, -1]]) 
Ky = ([[1, 2, 1],[0, 0, 0],[-1, -2, -1]])

(我有限制只使用Numpy,没有其他库使用Python语言 . )

import numpy as np
def classify(im):

   #Convert to grayscale
   gray = convert_to_grayscale(im/255.)

   #Sobel kernels as numpy arrays

   Kx = np.array([[1, 0, -1],[2, 0, -2],[1, 0, -1]]) 
   Ky = np.array([[1, 2, 1],[0, 0, 0],[-1, -2, -1]])

   Gx = filter_2d(gray, Kx)
   Gy = filter_2d(gray, Ky)

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

   #labels = ['brick', 'ball', 'cylinder']
   #Let's guess randomly! Maybe we'll get lucky.
   #random_integer = np.random.randint(low = 0, high = 3)

   return labels[random_integer]

def filter_2d(im, kernel):
   '''
   Filter an image by taking the dot product of each 
   image neighborhood with the kernel matrix.
   '''

    M = kernel.shape[0] 
    N = kernel.shape[1]
    H = im.shape[0]
    W = im.shape[1]

    filtered_image = np.zeros((H-M+1, W-N+1), dtype = 'float64')

    for i in range(filtered_image.shape[0]):
        for j in range(filtered_image.shape[1]):
            image_patch = im[i:i+M, j:j+N]
            filtered_image[i, j] = np.sum(np.multiply(image_patch, kernel))

    return filtered_image

def convert_to_grayscale(im):
    '''
    Convert color image to grayscale.
    '''
    return np.mean(im, axis = 2)

1 回答

  • 0

    您可以使用以下形状的独特特征:

    • 砖有几条直边(从四到六,取决于观点);

    • 球体有一个弯曲的边缘;

    • 圆柱体有两个弯曲边缘和直边(尽管它们可以完全隐藏) .

    使用二值化(基于亮度和/或饱和度)并提取轮廓 . 然后找到直线部分,可能使用Douglas-Peucker简化算法 . 最后,分析直边和弯边的序列 .


    解决最终分类任务的一种可能方法是将轮廓表示为一串直线或曲线的块,并粗略指示长度(短/中/长) . 对于不完美的分割,每个形状将对应于一组图案 .

    您可以使用训练阶段来学习最多的模式,然后使用字符串匹配(字符串被视为循环) . 可能会有仲裁关系 . 另一种选择是近似字符串匹配 .

相关问题