我想使用選擇性搜索算法將圖像分割成可能的對象位置。我發現我已經用於計算機視覺的庫OpenCV實現了這個功能,如文檔here所示。但是,我使用的是Python而不是C++,因此我查看了OpenCV的github存儲庫,直到找到我在下面轉載的example。從Python中調用這個OpenCV函數的正確方法是什麼?
#!/usr/bin/env python
'''
A program demonstrating the use and capabilities of a particular image segmentation algorithm described
in Jasper R. R. Uijlings, Koen E. A. van de Sande, Theo Gevers, Arnold W. M. Smeulders:
"Selective Search for Object Recognition"
International Journal of Computer Vision, Volume 104 (2), page 154-171, 2013
Usage:
./selectivesearchsegmentation_demo.py input_image (single|fast|quality)
Use "a" to display less rects, 'd' to display more rects, "q" to quit.
'''
import cv2
import sys
if __name__ == '__main__':
img = cv2.imread(sys.argv[1])
cv2.setUseOptimized(True)
cv2.setNumThreads(8)
gs = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
gs.setBaseImage(img)
if (sys.argv[2][0] == 's'):
gs.switchToSingleStrategy()
elif (sys.argv[2][0] == 'f'):
gs.switchToSelectiveSearchFast()
elif (sys.argv[2][0] == 'q'):
gs.switchToSelectiveSearchQuality()
else:
print(__doc__)
sys.exit(1)
rects = gs.process()
nb_rects = 10
while True:
wimg = img.copy()
for i in range(len(rects)):
if (i < nb_rects):
x, y, w, h = rects[i]
cv2.rectangle(wimg, (x, y), (x+w, y+h), (0, 255, 0), 1, cv2.LINE_AA)
cv2.imshow("Output", wimg);
c = cv2.waitKey()
if (c == 100):
nb_rects += 10
elif (c == 97 and nb_rects > 10):
nb_rects -= 10
elif (c == 113):
break
cv2.destroyAllWindows()
不幸的是,該命令python selective_search.py "/home/christopher/DroneKit/Vision/Face Detection/Annotated Faces in the Wild/originalPics/2002/07/19/big/img_135.jpg" f
運行此程序給了我以下錯誤:
Traceback (most recent call last):
File "selective_search.py", line 37, in <module>
rects = gs.process()
TypeError: Required argument 'rects' (pos 1) not found
基於該錯誤信息,我想也許我可以將它傳遞一個Python列表,然後底層C++函數會用算法的輸出填充它。然而,當我excecuted下面的代碼:
rects = []
gs.process(rects)
print(rects)
的輸出是一個空的列表,並在拍攝時沒有上繪製矩形顯示。因此,我對如何致電gs.process()
感到不知所措。如果有幫助,函數的C++聲明
CV_WRAP virtual void process(CV_OUT std::vector<Rect>& rects) = 0;
(編輯)從註釋複製的附加信息:
輸出help(gs.process)
:到None
process(...) method of cv2.ximgproc_segmentation_SelectiveSearchSegmentation instance process(rects) -> None. rects = gs.process(rects) just makes rects None and causes the program to terminate with an exception
使用rects = gs.process(rects)
套rects和導致程序以異常終止。
OpenCV版本是3.2.0。
使用numpy的陣列,而不是一個Python列表的崩潰我的程序以下消息:
OpenCV Error: Assertion failed (channels() == CV_MAT_CN(dtype)) in copyTo, file /home/christopher/opencv/modules/core/src/copy.cpp, line 259
terminate called after throwing an instance of 'cv::Exception'
what(): /home/christopher/opencv/modules/core/src/copy.cpp:259: error: (-215) channels() == CV_MAT_CN(dtype) in function copyTo
Aborted (core dumped)
試試'help(gs.process)',看看它告訴你什麼。具有'CV_OUT'參數的IIRC'CV_WRAP'意味着在Python中它將是一個參數,也是一個返回值。通常參數是可選的,但我想這是一個並非總是如此的證明。因此,我希望你想'rects = gs.process(rects)'。 –
@DanMašek進程(...)cv2.ximgproc_segmentation_SelectiveSearchSegmentation實例的方法 進程(rects) - >無。 'rects = gs.process(rects)'只是使rects爲None,並導致程序終止併產生異常。 – CaptainObvious
如果您嘗試傳遞一個numpy數組而不是Python列表,該怎麼辦?這是什麼版本的OpenCV? –