2016-10-20 4 views
5

Ich stieß auf dieses dtype Problem und hoffe, es wird für einige hilfreich sein.opencv cvtColor dtype Problem (Fehler: (-215))

Normalerweise würden wir konvertieren Farben wie dieses, das funktioniert:

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

aber manchmal können Sie das Bild normalisieren zuerst:

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

Es ist in diesem Fehler führen:

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

Der Punkt ist, im ersten Beispiel ist dtype Uint8, während in der letzteren ist float64. Um dies zu korrigieren, fügen Sie eine Zeile:

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

Dies ist eine Frage, die für das "numpy" Verhalten mit Division und nicht für OpenCV spezifisch ist. – Miki

Antwort

4

So wäre dies ein ähnliches Problem, das gelöst wird, sondern auf eine andere Funktion im Zusammenhang, cv2.drawKeypoints().

Dies funktioniert:

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

Dies ist jedoch nicht kompiliert:

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) 

Hier haben wir diesen Fehler haben:

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

Wieder Division durch 255 oder jede andere Die Verarbeitung mit "img", die zu einer Umwandlung in Gleitkommazahlen führt, macht "img" nicht zum korrekten Typ für drawKeepoints. Hier hilft das Hinzufügen von img = img.astype(numpy.float32) nicht. Für Input Image img stellt sich heraus, dass uint8 funktioniert, float32 jedoch nicht. Ich konnte eine solche Anforderung in Dokumentationen nicht finden. Es ist verwirrend, dass es anders als in cvtColor über "type" klagt.

So, damit es funktioniert:

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) 

Für die letzte Zeile, dachte ich cv2.DRAW_RICH_KEYPOINTS wie die Flagge funktionieren würde (das letzte Argument in der drawKeyPoints-Funktion). Allerdings nur, wenn ich die Nummer 4 benutze, dass es funktioniert. Jede Erklärung wird geschätzt.

Verwandte Themen