2017-04-16 17 views
0

で抽出点は、私はこのようなイメージを持っていると仮定する。 Pythonでこれを行う方法はありますか?私が画像を行列としてロードすると、行列のいくつかの要素が「黒」であることを示します。問題は、行にある幅がある可能性があるため、行の同じ点に対応する多くの行列要素が存在することです。これをどうすれば解決できますか?私が必要なことをするための簡単な方法はありますか?機能の画像:私はラインを構成する点(x、y)のリストを取得したい</p> <p><a href="https://i.stack.imgur.com/qKAk5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qKAk5.png" alt="enter image description here"></a></p> <p>:パイソン

ありがとうございます。

+0

あなたは "行の同じポイント" とはどういう意味ですか? (水平/垂直)線の太さが2または4ピクセル(偶数なので、 "中"の線はありません)の場合、正しい "点"はどれですか?そして、厚さが1ピクセルの場合、「接続されている」とは何を考えますか? – Claudio

答えて

1

これは実際には複雑な問題です。イメージ内の黒の点をすべて抽出し、パスをおおよそトレースするより限定されたデータセットにそれらを凝縮する方法を見つける必要があります。

import requests 
from PIL import Image 
import numpy as np 
import io 
from sklearn.cluster import mean_shift 

# get the image 
url = 'https://i.stack.imgur.com/qKAk5.png' 
res = requests.get(url) 
# set the content as a file pointer object 
fp = io.BytesIO(res.content) 
# load the image to PIL 
img = Image.open(fp) 

# convert the image to gray-scale and load it to a numpy array 
# need to transpose because the X and Y are swapped in PIL 
# need [:,::-1] because pngs are indexed from the upper left 
arr = np.array(img.convert('L')).T[:,::-1] 
# get indices where there line is the pixel values is dark, ie <100 
indices = np.argwhere(arr < 100) 

これまでのところ、暗いピクセルが発生するインデックスまたは(x、y)の場所があります。しかし、必要以上のものがあります。この数を減らすために、クラスタリング手法を使用してポイント数を減らすことができます。ここではmean_shiftクラスタリング手法が適切です。カーネルをポイントのセットに割り当て、次に隣接するポイントをゆっくり一緒に引っ張ります。主なパラメータは、カーネルの帯域幅です。これは、プルがどのように「ワイド」であるかです。

# this shows how many clusters are formed per bandwidth 
for x in np.arange(.5,5.6,.25): 
    print('{:.2f}: '.format(x), end='') 
    print(len(mean_shift(indices, bandwidth=x)[0])) 

# returns: 
0.50: 1697 
0.75: 1697 
1.00: 539 
1.25: 397 
1.50: 364 
1.75: 343 
2.00: 277 
2.25: 247 
2.50: 232 
2.75: 221 
3.00: 194 
3.25: 175 
3.50: 165 
3.75: 160 
4.00: 156 
4.25: 138 
4.50: 139 
4.75: 133 
5.00: 120 
5.25: 111 
5.50: 112 

したがって、約200ポイントの近似では、帯域幅3.0を使用できます。

points, labels = mean_shift(indices, bandwidth=3.0) 

# lets plot the points 
import matplotlib.pyplot as plt 
plt.scatter(points[:,0], points[:,1]) 

enter image description here

+0

ありがとうm8、私は間違いなく困難を認識していましたが、これを実装したモジュールを見つけることを望んでいました。ご挨拶 –

0

あなたは、このためのOpenCVを使用することができます。

import cv2 
import numpy as np 
import matplotlib.pyplot as plt 

image = cv2.imread('squiggly.png', cv2.IMREAD_GRAYSCALE) 
ret, image = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY) 

scatter = np.where(image==0) 
x,y = scatter[1],-scatter[0] 

plt.scatter(x,y,color='blue',marker='.',s=0.2) 
plt.show() 

enter image description here

+0

ご協力ありがとうございます。しかし、これは多くのポイントを生み出しますね。 –

関連する問題

 関連する問題