다른 클러스터 주위에 타원을 맞추고 싶습니다. 지금까지 cv2.fit Ellipse를 사용했는데 정상적으로 작동했습니다. 그러나 하나 이상의 타원에 문제가 있음을 발견했습니다 (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
을 사용하면 타원이 다시 커지고 왼쪽 상단에 위치합니다. 양식의 모양을 실제로 변경하지 않는 한 픽셀에 대한 이러한 민감도는 뛰어납니다.
의견이 있으십니까? 정말이 문제를 해결해야합니다. – dcoppin