2016-11-08 99 views
1

如何將cv2.boundingRect應用於np.array點?
以下代碼會產生錯誤。在np.array上應用cv2.boundingRect

points = np.array([[1, 2], [3, 4]], dtype=np.float32) 
import cv2 
cv2.boundingRect(points) 

錯誤:

OpenCV Error: Unsupported format or combination of formats (The image/matrix format is not supported by the function) in cvBoundingRect, file /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/shapedescr.cpp, line 97 

File "<ipython-input-23-42e84e11f1a7>", line 1, in <module> 
    cv2.boundingRect(points) 
error: /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/shapedescr.cpp:970: error: (-210) The image/matrix format is not supported by the function in function cvBoundingRect 
+0

我要建議'輸出cv2.boundingRect(cv2.cv.fromarray(點))'但是這似乎並沒有工作。返回'TypeError:點不是一個numpy數組,也不是一個標量' – jmunsch

+0

我試過你的代碼,它給了我這個邊界框沒有錯誤(1,2,3,3),這似乎是真的。但是,我的簡歷版本是'3.1.0-dev'。 – cagatayodabasi

回答

1
的OpenCV的2.X版本的

Python綁定使用一些數據比3.x中的那些略有不同表現

從現有的代碼採樣(例如上SO this answer),我們可以看到,我們可以稱之爲cv2.boundingRect通過cv2.findContours返回輪廓的列表的連接元件。讓我們看看是什麼樣子:

>>> a = cv2.copyMakeBorder(np.ones((3,3), np.uint8),1,1,1,1,0) 
>>> b,c = cv2.findContours(a, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 
>>> b[0] 
array([[[1, 1]], 

     [[1, 3]], 

     [[3, 3]], 

     [[3, 1]]]) 

我們可以看到,在輪廓的每個點表示爲[[x, y]]和我們人的名單。

因此

import numpy as np 
import cv2 

point1 = [[1,2]] 
point2 = [[3,4]] 

points = np.array([point1, point2], np.float32) 

print cv2.boundingRect(points) 

而我們得到

(1, 2, 3, 3) 
相關問題