2017-08-05 127 views
1

我想使用选择性搜索算法将图像分割成可能的对象位置。我发现我已经用于计算机视觉的库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) 
+0

试试'help(gs.process)',看看它告诉你什么。具有'CV_OUT'参数的IIRC'CV_WRAP'意味着在Python中它将是一个参数,也是一个返回值。通常参数是可选的,但我想这是一个并非总是如此的证明。因此,我希望你想'rects = gs.process(rects)'。 –

+1

@DanMašek进程(...)cv2.ximgproc_segmentation_SelectiveSearchSegmentation实例的方法 进程(rects) - >无。 'rects = gs.process(rects)'只是使rects为None,并导致程序终止并产生异常。 – CaptainObvious

+0

如果您尝试传递一个numpy数组而不是Python列表,该怎么办?这是什么版本的OpenCV? –

回答

0

很显然,我的Python编译OpenCV的3.2.0版本在本地缺少此fix。我继续使用OpenCV 3.3.0的latest stable release重新编译我的python绑定,并在OpenCV contrib存储库和示例脚本之后按照预期工作。

相关问题