2016-04-22 3 views
1

NumPyにはいくつかの黒(= 1)と白(= 0)の画像があります。 は、すべての2D方向で直線が並んでいる回数をカウントしたいと思います。Numpy Array:(画像からの)行内のカウント値

もう1行にないとすぐにカウントを停止したいと思います。私はラインの両側から数える必要があります。この考え方は、一方の側から直線がどれだけ長く、他方の側から真っ直ぐであるかを抽出することができるようにすることです。

以下の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 私の問題は、私はGoogleでさえするのか分からないということです。

ありがとうございます!

+1

、あなたは8「明白な」方向を意味するか、またはあなたも他の中間方向を含めるようにしたいですか? (それらは、関連するピクセルの中心が文字通り直線上にある場合、それらの8つの基本的な方向とは異なります)。 –

+1

出力は正確には何ですか?さまざまな長さの行をさまざまな方向に入力すると、最も長い行の長さ、または各方向の最長行の長さ、または*すべての*行の長さ、または何を必要としますか? –

+1

何が必要なのか教えてください。 (それは、どれほどの効率が重要であるかを変えるかもしれません)。 –

答えて

0

Gareth(私の質問のコメントを参照)のおかげで、これを理解することができました。ハフ変換は、私が探していた解決策です。

Scipyにはこれを行う機能があります。ドキュメントはhereであり、素敵なデモンストレーションと説明はhereです。

上記の質問に解決策を実装しました。私はまた、ピクセル/ 1の1つが直線ではなく、「まっすぐ」であるという1つの例を追加しました。これは、probabilistic_hough_line関数のパラメータの影響を受ける可能性があります。あなたは「すべての2D方向」を言うとき

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

関連する問題