5

我下载 OpenCV (OpenCV-2.3.1-win-superpack.exe) 并在 python 2.6 上使用它。

我使用find_obj.py目录中的OpenCV\samples\python

一切都很神奇,但即使我使用 try except 块来包装代码,如果代码引发错误,OpenCV 也会打印错误。

OpenCV 错误:未知函数中的错误标志(参数或结构字段)(无法识别或不支持的数组类型),文件......\OpenCV-2.3.1\modules\core\src\array.cpp,第 2482 行

那么如何禁用 OpenCV 警告消息。

编辑:

这是代码:

import numpy as np
import cv2
from functools import partial

help_message = '''SURF image match 

USAGE: findobj.py [ <image1> <image2> ]
'''

FLANN_INDEX_KDTREE = 1  # bug: flann enums are missing

flann_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 4)

def anorm(a):
    return np.sqrt( anorm2(a) )

def match_bruteforce(desc1, desc2, r_threshold = 0.75):
    res = []
    for i in xrange(len(desc1)):
        dist = anorm( desc2 - desc1[i] )
        n1, n2 = dist.argsort()[:2]
        r = dist[n1] / dist[n2]
        if r < r_threshold:
            res.append((i, n1))
    return np.array(res)


def match_flann(desc1, desc2, r_threshold = 0.6):
    flann = cv2.flann_Index(desc2, flann_params)
    idx2, dist = flann.knnSearch(desc1, 2, params = {}) # bug: need to provide empty dict
    mask = dist[:,0] / dist[:,1] < r_threshold
    idx1 = np.arange(len(desc1))
    pairs = np.int32( zip(idx1, idx2[:,0]) )
    return pairs[mask]


def draw_match(img1, img2, p1, p2, status = None, H = None):
    h1, w1 = img1.shape[:2]
    h2, w2 = img2.shape[:2]
    vis = np.zeros((max(h1, h2), w1+w2), np.uint8)
    vis[:h1, :w1] = img1
    vis[:h2, w1:w1+w2] = img2
    vis = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)

    if H is not None:
        corners = np.float32([[0, 0], [w1, 0], [w1, h1], [0, h1]])
        corners = np.int32( cv2.perspectiveTransform(corners.reshape(1, -1, 2), H).reshape(-1, 2) + (w1, 0) )
        cv2.polylines(vis, [corners], True, (255, 255, 255))

    if status is None:
        status = np.ones(len(p1), np.bool_)
    green = (0, 255, 0)
    red = (0, 0, 255)
    for (x1, y1), (x2, y2), inlier in zip(np.int32(p1), np.int32(p2), status):
        col = [red, green][inlier]
        if inlier:
            cv2.line(vis, (x1, y1), (x2+w1, y2), col)
            cv2.circle(vis, (x1, y1), 2, col, -1)
            cv2.circle(vis, (x2+w1, y2), 2, col, -1)
        else:
            r = 2
            thickness = 3
            cv2.line(vis, (x1-r, y1-r), (x1+r, y1+r), col, thickness)
            cv2.line(vis, (x1-r, y1+r), (x1+r, y1-r), col, thickness)
            cv2.line(vis, (x2+w1-r, y2-r), (x2+w1+r, y2+r), col, thickness)
            cv2.line(vis, (x2+w1-r, y2+r), (x2+w1+r, y2-r), col, thickness)
    return vis


def search(fn1, fn2):
    try:        
        img1 = cv2.imread(fn1, 0)
        img2 = cv2.imread(fn2, 0)

        surf = cv2.SURF(1000)
        kp1, desc1 = surf.detect(img1, None, False)
        kp2, desc2 = surf.detect(img2, None, False)
        desc1.shape = (-1, surf.descriptorSize())
        desc2.shape = (-1, surf.descriptorSize())

        def match_and_draw(match, r_threshold):
            try:
                m = match(desc1, desc2, r_threshold)
                matched_p1 = np.array([kp1[i].pt for i, j in m])
                matched_p2 = np.array([kp2[j].pt for i, j in m])
                H, status = cv2.findHomography(matched_p1, matched_p2, cv2.RANSAC, 5.0)
                vis = draw_match(img1, img2, matched_p1, matched_p2, status, H)
                return vis
            except:
                pass

        vis_brute = match_and_draw( match_bruteforce, .9 )
        #vis_flann = match_and_draw( match_flann, 0.6 ) # flann tends to find more distant second # neighbours, so r_threshold is decreased
        if vis_brute != None:
            cv2.imshow('find_obj SURF', vis_brute)
            #cv2.imshow('find_obj SURF flann', vis_flann)
            return True
        else:
            return False
    except:
        return False


print search('obj.png', 'pass.png')
print search('obj.png', 'fail.png')

它在更大的图像中搜索图像(obj.png),如果搜索成功一切正常,但如果失败,它总是打印错误。

编辑2

这是dll列表

C:\Python26\Lib\site-packages>dir opencv*
 Volume in drive C has no label.
 Volume Serial Number is BE63-8A7C

 Directory of C:\Python26\Lib\site-packages

08/17/2011  08:27 AM           743,936 opencv_calib3d231.dll
08/17/2011  08:27 AM           547,328 opencv_contrib231.dll
08/17/2011  08:26 AM         6,595,072 opencv_core231.dll
08/17/2011  08:27 AM           878,080 opencv_features2d231.dll
08/17/2011  08:28 AM         7,417,192 opencv_ffmpeg.dll
08/17/2011  08:26 AM           395,264 opencv_flann231.dll
08/17/2011  08:27 AM           221,184 opencv_gpu231.dll
08/17/2011  08:27 AM           891,904 opencv_highgui231.dll
08/17/2011  08:26 AM         1,648,128 opencv_imgproc231.dll
08/17/2011  08:27 AM           761,856 opencv_legacy231.dll
08/17/2011  08:26 AM           451,584 opencv_ml231.dll
08/17/2011  08:27 AM           903,168 opencv_objdetect231.dll
08/17/2011  08:51 AM               335 opencv_python-2.3.1-py2.6.egg-info
08/17/2011  08:26 AM           566,272 opencv_ts231.dll
08/17/2011  08:27 AM           288,768 opencv_video231.dll
              15 File(s)     22,310,071 bytes
               0 Dir(s)  37,611,061,248 bytes free
4

0 回答 0