Numpy数组:计算一行中的值(来自图像)

时间:2016-04-22 13:52:33

标签: python numpy image-processing scipy

我在NumPy中有几个黑色(= 1)和白色(= 0)图像。我希望计算所有2D方向上直线的次数

一旦这些不再连续,我想停止计数。我需要从线的两侧算一下。这个想法是能够从一侧提取直线的直线长度与从另一侧直线的长度相比。

请看下面的MWE,我希望能更好地解释我的问题。

import numpy as np
import matplotlib.pylab as plt
from matplotlib import gridspec

background = np.zeros((60,60))

one = background.copy() # straight line
one[10,5:30:1] = 1
# Here I'd like to count 25 from both directions

two = background.copy()    # diagonal line, this stands for all angles the diagonal line could have, not just 45degrees!
two[[range(10,35)],[range(5,30)]] = 1
# Here I'd like to count 25 from both directions

three = background.copy()   # line with right angle
three[10,5:15:1] = 1
three[10:25:1,15] = 1
# Here I'd like to count 10 from one end and 15 from the other

gs = gridspec.GridSpec(2,2)
fig = plt.figure()
line = fig.add_subplot(gs[0,0])
diag = fig.add_subplot(gs[0,1])
angle = fig.add_subplot(gs[1,0])
line.imshow(one), line.set_title('Count 25 from both sides')
diag.imshow(two), diag.set_title('Count 25 from both sides')
angle.imshow(three), angle.set_title('Count 10 from one side and 15 from the other')

enter image description here 我的问题是,我甚至不知道谷歌要做什么。

非常感谢!

1 个答案:

答案 0 :(得分:0)

感谢Gareth(见我的问题的评论)我能够弄清楚这一点。霍夫变换是我正在寻找的解决方案。

Scipy有这样做的功能。可以找到文档here,可以找到一个很好的演示和解释here

我在上面的问题中实现了解决方案。我还添加了一个例子,其中一个像素/一个不在一条直线上,但是足够直接'。这可能受probabilistic_hough_line函数的参数影响。

import numpy as np
import matplotlib.pylab as plt
from matplotlib import gridspec
from skimage.transform import probabilistic_hough_line

background = np.zeros((60,60))

one = background.copy() # straight line
one[10,5:30:1] = 1
# Here I'd like to count 25 from both directions

two = background.copy()    # diagonal line, this stands for all angles the diagonal line could have, not just 45degrees!
two[[range(10,35)],[range(5,30)]] = 1
# Here I'd like to count 25 from both directions

three = background.copy()   # line with right angle
three[10,5:15:1] = 1
three[10:25:1,15] = 1
# Here I'd like to count 10 from one end and 15 from the other

four = background.copy()    # line with one value out of place and a right angle
four[10,5:8:1] = 1
four[11,8] = 1
four[10,9:15] = 1
four[10:25:1,15] = 1
# Here I'd like to count 10 from one side and 15 from the other side

gs = gridspec.GridSpec(2,2)
fig = plt.figure()
line_plt = fig.add_subplot(gs[0,0])
diag = fig.add_subplot(gs[0,1])
angle = fig.add_subplot(gs[1,0])
angle_dirty = fig.add_subplot(gs[1,1])
line_plt.imshow(one), line_plt.set_title('Count 25 from both sides')
diag.imshow(two), diag.set_title('Count 25 from both sides')
angle.imshow(three), angle.set_title('Count 10 from one side and 15 from the other')
angle_dirty.imshow(four), angle_dirty.set_title('Count 10 from one side and 15 from the other')

# This function returns the  List of lines identified, lines in format ((x0, y0), (x1, y0)), indicating line start and end.
lines_one = probabilistic_hough_line(one, threshold=10, line_length=5, line_gap=3)
# Now to the plotting on top of the original image
for line in lines_one:
    p0, p1 = line
    line_plt.plot((p0[0], p1[0]), (p0[1], p1[1]))

lines_two = probabilistic_hough_line(two, threshold=10, line_length = 5, line_gap = 3)
for line in lines_two:
    p0, p1 = line
    diag.plot((p0[0], p1[0]), (p0[1], p1[1]))

lines_three = probabilistic_hough_line(three, threshold=10, line_length = 5, line_gap = 3)
for line in lines_three:
    p0, p1 = line
    angle.plot((p0[0], p1[0]), (p0[1], p1[1]))

lines_four = probabilistic_hough_line(four, threshold=10, line_length = 5, line_gap = 3)
for line in lines_four:
    p0, p1 = line
    angle_dirty.plot((p0[0], p1[0]), (p0[1], p1[1]))

enter image description here