边缘Python的渐变 [英] Gradient of edges Python

查看:47
本文介绍了边缘Python的渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两类图像的数据集:城市景观和景观.我要做的是计算每个图像边缘的梯度(方向),并显示城市景观的图像比横向图像具有更多的垂直/水平边缘.

I have a dataset with two classes of images: Cityscape and Landscape. What I want to do is calculate the gradient(orientation) of the edges of each image and show that images of cityscapes have more vertical/horizontal edges than landscape images.

我所做的是计算垂直,水平,45度和135度边缘.我对图像应用了Canny滤镜,计算了x,y梯度,还对图像应用了阈值,以表明其边缘高于该阈值.在此处看到此阈值的结果:

What I've done is calculated vertical, horizontal, 45 degree and 135 degree edges. I've applied a Canny filter to the images, calculated the x,y gradients and also applied a threshold to the images show it shows edges above that threshold. The result of this thresholding is seen here:

这是我用于图像处理以及计算梯度的代码:

This is my code for this image manipulation as well as calculating the gradients:

def gradient(image):    

    # Step 1
    img = image
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    # Step 2
    bi = cv2.bilateralFilter(gray, 15, 75, 75)

    # Step 3
    dst = cv2.Canny(bi, 100, 200)
    #print(np.count_nonzero(dst))  #--> make sure it's not all zeroes

    # Step 4
    #--- create a black image to see where those edges occur ---
    mask = np.zeros_like(gray)

    #--- applying a threshold and turning those pixels above the threshold to white ---           
    mask[dst > 0.1 * dst.max()] = 255

    # Step 5
    img[dst > 0.1 * dst.max()] = [255, 0, 0]   #--- [255, 0, 0] --> Red ---

    Gx = cv2.Sobel(mask,cv2.CV_64F,1,0,ksize=5)
    Gy = cv2.Sobel(mask,cv2.CV_64F,0,1,ksize=5)

    #orientation of the edges
    theta = np.arctan2(Gy, Gx)

    #magnitude
    M = np.sqrt(Gx*Gx + Gy*Gy)

    #Vertical edges: 
    v = abs(Gy)

    #Horizontal edges: 
    h = abs(Gx)

    #45 Degree edges: 
    deg45 = M*abs(np.cos(theta - np.pi/4))

    #135 Degree edges: 
    deg135 = M*abs(np.cos(theta - 3*np.pi/4))

    print('Vertical:')
    #print(v)
    print(np.count_nonzero(v))
    print('Horizontal:')
    #print(h)
    print(np.count_nonzero(h))

我想要的是为上图中显示为红色的边缘计算 v,h,deg45,deg135 (第5步).如果不可能,则对具有白色边缘的图像执行此操作(步骤4).有人可以帮忙吗?

What I want is to calculate the v,h,deg45,deg135 for the edges shown as red in the image above (Step 5). If that is not possible, then do that for the image with the white edges (Step 4). Can anyone help?

为避免混淆,我要做的是获取给定图像中的垂直,水平等边的数量,以便我可以比较城市景观与景观的这些数字图片.

So as to avoid confusion, what I want to do is to get the amount of vertical, horizontal etc edges in a given image, so that I can compare those numbers for cityscapes vs landscape images.

推荐答案

如果想要的是包括水平边缘与垂直边缘的像素总数,则建议为水平边缘与垂直边缘定义一些阈值(例如15度).因此,您可以计算 theta 的元素数量 abs(theta)<pi/12 (水平)或 abs(theta)>pi-pi/12 (水平)或 pi/2-pi/12<abs(theta)<pi/2 + pi/12 (垂直)

If what you want is the total number of pixels comprising horizontal vs vertical edges, I would suggest defining some threshold for horizontal vs vertical (say 15 degrees). So you can count the number of elements of theta for which abs(theta) < pi/12 (horizontal) or abs(theta) > pi-pi/12 (horizontal) or pi/2 - pi/12 < abs(theta) < pi/2+pi/12 (vertical)

v h 中存储的是每个点上渐变的垂直和水平分量,您需要比较的值> v h 来确定每个点的梯度矢量是水平还是垂直.比较 theta 可能是最直观的方法.

What you're storing in v and h are the vertical and horizontal components of the gradient at each point and what you need is to compare the values of v and h to determine for each point if the gradient vector should count as horizontal or vertical. Comparing theta is probably the most intuitive way to do this.

为了获得满足特定条件的theta元素数,我建议使用

In order to get the number of elements of theta that satisfy a particular condition, I would suggest using a generator expression:

sum(1 for i in theta if (abs(i)<pi/12) or (abs(i)>pi-pi/12))

例如,将为您提供水平边缘像素的数量.

would give you the number of horizontal edge pixels for example.

这篇关于边缘Python的渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆