2016-10-20 3 views
5

이 dtype 문제를 발견하여 도움이되기를 바랍니다.opencv cvtColor dtype 문제 (오류 : (-215))

img = cv2.imread("img.jpg"), 0) 
imgColor=cv2.cvtColor(img , cv2.COLOR_GRAY2BGR) 

을하지만 때로는 먼저 이미지를 정상화 할 수 있습니다 :

img = cv2.imread("img.jpg"), 0)/255. 
imgColor=cv2.cvtColor(img , cv2.COLOR_GRAY2BGR) 

그것은이 오류가 발생합니다 :

error: (-215) depth == CV_8U || depth == CV_16U || depth == CV_32F in function >cv::cvtColor

작동이 같은

일반적으로 우리가 변환 할 색상,

요점은 앞의 예에서 dtype은 uint8이고 후자는 float64입니다. 는,이 문제를 해결 한 줄 추가하려면

img = cv2.imread("img.jpg"), 0)/255. 
img=img.astype(numpy.float32) 
imgColor=cv2.cvtColor(img , cv2.COLOR_GRAY2BGR) 
+0

이것은 OpenCV가 아닌 division을 사용하는 numpy 동작과 관련된 질문입니다. – Miki

답변

4

그래서이 해결되지만 다른 함수와 관련된 유사한 문제가 될 것를 cv2.drawKeypoints().

이 작동합니다 : 그러나

img = cv2.imread("img.jpg"), 1) 
img_out = numpy.copy(img) 
image_out = cv2.drawKeypoints(img,keypointList,img_out,(255,0,0),4) 

, 이것은 컴파일되지 않습니다 : 255 또는 기타 다시

error: (-5) Incorrect type of input image.

, 부문 : 여기

img = cv2.imread("img.jpg"), 1)/255.0 
img_out = numpy.copy(img) 
image_out = cv2.drawKeypoints(img,keypointList,img_out,(255,0,0),4) 

우리가이 오류가 "img"로 처리하여 float 숫자로 변환하면 "img"는 drawKeypoints의 올바른 유형이 아닙니다. 여기에 img = img.astype(numpy.float32)을 추가하는 것이 도움이되지 않습니다. 입력 이미지 img의 경우 uint8이 작동하지만 float32는 작동하지 않습니다. 나는 문서화에서 그러한 요구 사항을 찾을 수 없었다. 위의 cvtColor와 관련된 문제와 다른 점은 "type"에 대해 불평합니다.

그래서 그것이 작동되도록합니다 : 마지막 줄에 들어

img = cv2.imread("img.jpg"), 1)/255.0 
img_out = numpy.copy(img) 
img=img.astype(numpy.uint8) 
image_out = cv2.drawKeypoints(img,keypointList,img_out,(255,0,0),4) 

, 나는 cv2.DRAW_RICH_KEYPOINTS이 플래그합니다 (drawKeyPoints 함수의 마지막 인자)로 일 거라 생각 했어요. 그러나 내가 번호 4를 사용할 때만 작동합니다. 모든 설명을 부탁드립니다.