私は異なるクラスタの周りに楕円を合わせたいと思います。これまで、私はcv2.fit Ellipseを使いました。しかし、私は1つの楕円(100以上のうち)の問題を発見しました。 予期しない動作を再現するコードのサンプルを次に示します。cv2.fitEllipseは、はっきりとは異なる楕円を作成します
import numpy as np
import cv2
import itertools
import matplotlib.pyplot as mpl
test=np.zeros((40,40))
test[27,12]=1
test[28,11:13]=1
test[29,11:14]=1
test[30,10:14]=1
test[31,11:14]=1
test[32,12:14]=1
test[33,13]=1
def getContours(clusters):
map_contour=np.ones(clusters.shape).astype(np.uint8)
int_cnts=[]
for label in np.unique(clusters):
# if the label is zero, we are examining the 'background'
if label == 0:
continue
mask = np.zeros(test.shape, dtype="uint8")
mask[clusters == label] = 255
cnts, hierarchy = \
cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
int_cnts.append(cnts)
c = max(cnts, key=cv2.contourArea)
cv2.drawContours(map_contour, cnts, 0, (0, 255, 0), 1)
list_contours=list(itertools.chain.from_iterable(int_cnts))
return list_contours, map_contour
list_test,map_test=getContours(test)
info=np.zeros((len(list_test),5))
map_ellipse=np.ones(test.shape).astype(np.uint8)
for index,cnt in enumerate(list_test):
ellipse=cv2.fitEllipse(cnt)
info[index,0:2],info[index,2:4],info[index,4] = ellipse
cv2.ellipse(map_ellipse,ellipse,(255,0,0))
print cnt
print ellipse
mpl.contourf(map_ellipse)
mpl.contour(test)
mpl.show()
cntができます:
array([[[12, 27]],
[[11, 28]],
[[11, 29]],
[[10, 30]],
[[13, 33]],
[[13, 29]],
[[12, 28]]], dtype=int32)
楕円は次のとおりです。
((23.71430015563965, -20.397018432617188),
(8.957982063293457, 98.63383483886719),
0.0)
あなたは/貼り付けをコピーするかどうかを確認し、上記のコードを実行し、楕円の上を(でカットすることができたよう半分)は、それが表すと思われるゾーン(左上)から完全に切り離されているように見えます。 私は何が間違っているのか分かりませんが、特に同じ絵の中の他のフォームのために働いています。 従って、どんな助けも非常に高く評価されるでしょう!
あなたが面白い質問だと思ったら、それに答えることができませんでしたので、より多くの可視性が得られるようにupvoteしてください。どうもありがとうございました!
編集:何らかの理由で、test[30,10:14]=1
の代わりにtest[30,9:14]=1
を設定すると、何らかの理由で楕円の配置とサイズがずっと良くなります(十分ではありませんが)。しかし、代わりにtest[30,9:14]=1
を使用すると、楕円はもう一度大きくなり、左上隅に配置されます。実際にフォームの形状を変更しない1つのピクセルに対するそのような感度は顕著である。
提案がありますか?私は本当にこれを解決する必要があります。 – dcoppin