Python 寻找图像中相应边缘的边缘矩阵 我在Andrew Ng的计算机视觉上追随这一点,我试图弄清楚他是如何找到对应于特定线的矩阵的。 他说垂直边缘检测矩阵是: [1 0 -1 1 0 -1 1 0 -1]

Python 寻找图像中相应边缘的边缘矩阵 我在Andrew Ng的计算机视觉上追随这一点,我试图弄清楚他是如何找到对应于特定线的矩阵的。 他说垂直边缘检测矩阵是: [1 0 -1 1 0 -1 1 0 -1],python,machine-learning,computer-vision,conv-neural-network,edge-detection,Python,Machine Learning,Computer Vision,Conv Neural Network,Edge Detection,我怎样才能知道矩阵会给我什么样的角线? 例如,如果我正在寻找一条水平线,或者一条45/30度的线,我将如何找到矩阵 更新: 我用3个不同的角度测试了Amird的函数:45、90和180 #Convolve function for convolving a filter on an image # import the necessary packages from skimage.exposure import rescale_intensity import numpy as np impo

我怎样才能知道矩阵会给我什么样的角线? 例如,如果我正在寻找一条水平线,或者一条45/30度的线,我将如何找到矩阵

更新:
我用3个不同的角度测试了Amird的函数:45、90和180

#Convolve function for convolving a filter on an image
# import the necessary packages
from skimage.exposure import rescale_intensity
import numpy as np
import argparse
import cv2
def convolve(image, kernel):
    # grab the spatial dimensions of the image, along with
    # the spatial dimensions of the kernel
    (iH, iW) = image.shape[:2]
    (kH, kW) = kernel.shape[:2]
    # allocate memory for the output image, taking care to
    # "pad" the borders of the input image so the spatial
    # size (i.e., width and height) are not reduced
    pad = (kW - 1) // 2
    image = cv2.copyMakeBorder(image, pad, pad, pad, pad,
        cv2.BORDER_REPLICATE)
    output = np.zeros((iH, iW), dtype="float32")
    # loop over the input image, "sliding" the kernel across
    # each (x, y)-coordinate from left-to-right and top to
    # bottom
    for y in np.arange(pad, iH + pad):
        for x in np.arange(pad, iW + pad):
            # extract the ROI of the image by extracting the
            # *center* region of the current (x, y)-coordinates
            # dimensions
            roi = image[y - pad:y + pad + 1, x - pad:x + pad + 1]
            # perform the actual convolution by taking the
            # element-wise multiplicate between the ROI and
            # the kernel, then summing the matrix
            k = (roi * kernel).sum()
            # store the convolved value in the output (x,y)-
            # coordinate of the output image
            output[y - pad, x - pad] = k
    # rescale the output image to be in the range [0, 255]
    output = rescale_intensity(output, in_range=(0, 255))
    output = (output * 255).astype("uint8")
    # return the output image
    return output


import matplotlib.pyplot as plt
#original image
image = cv2.imread('circle.jpg')

plt.figure(figsize=(10,10))
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# as opencv loads in BGR format by default, we want to show it in RGB.
plt.show()
180度输出:

90度输出:


45度角的输出图像看起来相同。我试了几张照片,看起来也一样。因此,我不确定是否遗漏了什么,但该函数似乎输出了45、90和180度角的类似结果。

考虑黑白图像。什么是线?在垂直线上缩放时,会出现从左到右(或从右到左)的对比度

因此,在图像中的实体区域(没有边缘),该卷积滤波器生成0(因为1s和-1s相互抵消)。但是当滤波器到达直线时,卷积产生一个大的正值或负值

它可用于使用其中一个过滤器检测水平线(假设过滤器的尺寸为3x3):

为了概括这个想法,一个
3x3
窗口对于其他度数来说不够大。最好将卷积滤波器的尺寸扩大到其他程度。例如,对于大小为5的45度卷积滤波器,可以使用:

  0  0  0  1  0 
  0  0  1  0 -1
  0  1  0 -1  0
  1  0 -1  0  0
  0  -1 0  0  0
要使用随机度值创建过滤器,可以使用以下函数:

将numpy导入为np
def创建过滤器(过滤器大小、度):
过滤器=np.零((2*过滤器大小+1,2*过滤器大小+1))
特萨=(度/180)*np.pi
对于范围内的x(-filter\u size,filter\u size+1):
y=int(x*np.tan(特塔))

如果np.abs(y)+1考虑黑白图像。什么是线?在垂直线上缩放时,会出现从左到右(或从右到左)的对比度

因此,在图像中的实体区域(没有边缘),该卷积滤波器生成0(因为1s和-1s相互抵消)。但是当滤波器到达直线时,卷积产生一个大的正值或负值

它可用于使用其中一个过滤器检测水平线(假设过滤器的尺寸为3x3):

为了概括这个想法,一个
3x3
窗口对于其他度数来说不够大。最好将卷积滤波器的尺寸扩大到其他程度。例如,对于大小为5的45度卷积滤波器,可以使用:

  0  0  0  1  0 
  0  0  1  0 -1
  0  1  0 -1  0
  1  0 -1  0  0
  0  -1 0  0  0
要使用随机度值创建过滤器,可以使用以下函数:

将numpy导入为np
def创建过滤器(过滤器大小、度):
过滤器=np.零((2*过滤器大小+1,2*过滤器大小+1))
特萨=(度/180)*np.pi
对于范围内的x(-filter\u size,filter\u size+1):
y=int(x*np.tan(特塔))

如果np.abs(y)+1感谢您的帮助!你如何从任何角度概括这个答案?从您的回答中,我发现我可能需要增加过滤器的尺寸(从3x3增加到更大的尺寸)。我明白了,但是如果我在找30度线呢?有没有一种方法可以在不放大图像的情况下找到它们,并找到过滤器的外观?@MaxK I刚刚为45度卷积过滤器添加了另一个示例。但对于其他度数,最好扩大过滤器的大小,以获得更好的分辨率。谢谢!请问你是怎么制作的?为了概括它,我试图理解这个过程。所以,如果我想要31度,我可以这样做。@MaxK你可以使用这个函数,你给出过滤器大小和度数,它会创建过滤器。看起来很棒!但如果可以的话,请看我的更新。还有,有没有一种方法可以找到更小的过滤器维度呢?谢谢你的帮助!你如何从任何角度概括这个答案?从您的回答中,我发现我可能需要增加过滤器的尺寸(从3x3增加到更大的尺寸)。我明白了,但是如果我在找30度线呢?有没有一种方法可以在不放大图像的情况下找到它们,并找到过滤器的外观?@MaxK I刚刚为45度卷积过滤器添加了另一个示例。但对于其他度数,最好扩大过滤器的大小,以获得更好的分辨率。谢谢!请问你是怎么制作的?为了概括它,我试图理解这个过程。所以,如果我想要31度,我可以这样做。@MaxK你可以使用这个函数,你给出过滤器大小和度数,它会创建过滤器。看起来很棒!但如果可以的话,请看我的更新。还有,有没有办法找到更小的过滤器尺寸?
 [1  1  1
  0  0  0
 -1 -1 -1]

#OR

 [-1 -1 -1
  0  0  0
  1  1  1] 
  0  0  0  1  0 
  0  0  1  0 -1
  0  1  0 -1  0
  1  0 -1  0  0
  0  -1 0  0  0