반응형



유튜브 영상의 댓글로 코드 실행시 다음과 같은 에러가 난다고 알려주신 분이 있어서 확인해보았습니다. 


The convex hull indices are not monotonous, which can be in the case when the input contour contains self-intersections in function 'cv::convexityDefects'




유튜브 영상에서 설명하고 있는 다음 포스트의 코드에서 발생한 에러입니다. 


OpenCV를 사용하여 손 검출 및 인식하기(Hand Detection and Recognition using OpenCV)

https://webnautes.tistory.com/1378




코드 작성시 테스트한 버전에서는 문제 없던 코드가 OpenCV 4.3.0에서 실행해보니 알려주신대로 에러가 발생하네요. 

테스트하지 않은 다른 버전에서도 같은 문제가 발생할 수 있습니다. 


cv2.error: OpenCV(4.3.0) C:\opencv_sources\opencv-4.3.0\modules\imgproc\src\convhull.cpp:360: error: (-5:Bad argument) The convex hull indices are not monotonous, which can be in the case when the input contour contains self-intersections in function 'cv::convexityDefects'




convexityDefects 함수의 입력으로 사용하는 컨투어에 self-intersections가 포함되어 있어 문제가 되는 듯했습니다. 




구글링해서 발견한 다음 해결방법을 적용해보니 문제가 사라진 듯합니다.

https://stackoverflow.com/questions/62392240/opencv-cv2-moments-returns-all-moments-to-zero




명령 프롬프트에서 shapely를 설치합니다.


pip install shapely




코드에  Polygon 클래스를 사용하도록 임포트합니다. 

from shapely.geometry import Polygon



넘파이의 squeeze 함수를 사용하여 컨투어의 차원을 줄여준 후

max_contour2 = np.squeeze(max_contour) 




Polygon 객체로 변환합니다.

polygon = Polygon(max_contour2)




Polygon 객체의 is_simple의 리턴값이 False라면 self-intersections가 컨투어에 포함된 것입니다. 

convexityDefects 함수 처리를 하지 못하도록 처리합니다. 


if polygon.is_simple == False: 

      return -1,None


hull = cv.convexHull(max_contour, returnPoints=False)

defects = cv.convexityDefects(max_contour, hull)





코드에 적용한 후.. 테스트 해보니 문제가 해결되었습니다. 



반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts