【问题标题】:compute the gradient line of edge pixel计算边缘像素的梯度线
【发布时间】:2021-10-14 20:57:18
【问题描述】:

我想计算检测到的轮廓中某些像素的梯度线。检测到的轮廓示例是 detected contour figure。 为此,我首先使用以下代码计算图像梯度

img_sobelx = cv2.Sobel(grey_img,cv2.CV_16S,1,0,ksize=3)
img_sobely = cv2.Sobel(grey_img,cv2.CV_16S,0,1,ksize=3)
m = np.hypot(img_sobelx,img_sobelx)

然后我使用下面的代码在轮廓中找到一些像素的渐变线 注意:为了检测轮廓,我使用 canny edge 方法和连接组件

px,py =np.where(img > 0)   # img: is the example image (after detecting the contour)
# pick points from the curve
index = np.array(range(0,py.shape[0],int(py.shape[0]/10)))
for i in index[:-1]:
     #first line
     m1 = m[px[i],py[i]]    #slope
     b1 = py[i] - (m1*px[i]) # intercept
     for j in index[k:]:
          m2 = m[px[j],py[j]]
          b2 = py[j] - (m2*px[j])
          # intersection point
          if (m2-m1) == 0:
               continue
          xi = (b1-b2) / (m2-m1)
          yi = m1 * xi + b1
          start_point = (py[i],px[i])
          end_point = (int(yi),int(xi))
          img= cv2.line(img,start_point,end_point,(255,255,255),1)
          start_point = (py[j],px[j])
          end_point = (int(yi),int(xi))
          img= cv2.line(img,start_point,end_point,(255,255,255),1)

上述代码的输出是 output: gradient line

结果似乎不正确。你能帮我找到正确的方法吗? 谢谢

【问题讨论】:

    标签: python opencv image-processing computer-vision


    【解决方案1】:

    您需要使用img_sobelximg_sobely 作为向量的两个分量。斜边不是这个向量的斜率,而是幅度。幅度表示梯度的强度,而不是它的方向。

    如果您有一个点 (px[i],py[i]),那么该点 (px[i]+img_sobelx[i],py[i]+img_sobely[i]) 就是您要查找的直线上的第二个点。

    【讨论】:

      猜你喜欢
      • 2016-11-27
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多