opencv 3.1 drawKeypoints抛出错误

时间:2016-02-13 12:11:31

标签: opencv sift

我使用以下代码在opencv3.1中运行基本的SIFT代码:

import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionFilter implements Filter {

    private ArrayList<String> urlList;

    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        String url = request.getServletPath();
        boolean allowedRequest = false;

        if (urlList.contains(url)) {
            allowedRequest = true;
        }

        if (!allowedRequest) {
            HttpSession session = request.getSession(false);
            if (null == session) {
                response.sendRedirect("Index.jsp");
            }
        }
        chain.doFilter(req, res);
    }

    public void init(FilterConfig config) throws ServletException {
        String urls = config.getInitParameter("avoid-urls");
        StringTokenizer token = new StringTokenizer(urls, ",");
        urlList = new ArrayList<String>();
        while (token.hasMoreTokens()) {
            urlList.add(token.nextToken());
        }
    }
}

但代码#include <opencv2/highgui/highgui.hpp> #include <opencv2/xfeatures2d/nonfree.hpp> #include <opencv2/xfeatures2d.hpp> #include <vector> using namespace std; using namespace cv; int main(int argc, char *argv[]) { //cv::initModule_nonfree(); //initModule_features2d(); Mat img_1 = imread("11.bmp", 1); Mat img_2 = imread("22.bmp", 1); cv::Ptr<Feature2D> f2d = xfeatures2d::SIFT::create(); //-- Step 1: Detect the keypoints: std::vector<KeyPoint> keypoints_1, keypoints_2; f2d->detect( img_1, keypoints_1 ); f2d->detect( img_2, keypoints_2 ); //-- Step 2: Calculate descriptors (feature vectors) Mat descriptors_1, descriptors_2; f2d->compute( img_1, keypoints_1, descriptors_1 ); f2d->compute( img_2, keypoints_2, descriptors_2 ); Mat out0; drawKeypoints(img_1, keypoints_1, out0); imshow("KeyPoint0.jpg", out0); imwrite("KeyPoint0", out0); //-- Step 3: Matching descriptor vectors using BFMatcher : BFMatcher matcher; std::vector< DMatch > matches; matcher.match( descriptors_1, descriptors_2, matches ); /* Mat img_matches; drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_matches); imshow("matches",img_matches); imwrite("matches.jpg",img_matches); */ char c = ' '; while ((c = waitKey(0)) != 'q'); // Keep window there until user presses 'q' to quit. return 0; } drawKeypoints会引发错误:

drawMatches

1 个答案:

答案 0 :(得分:0)

我还没有使用SIFT,但似乎你没有在本节中实例化img_matches:

Mat img_matches;
drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_matches); 

错误消息暗示它不期望空()图像。 您可以尝试首先使用黑色像素初始化图像:

img_matches = Mat::zeros( img_1.size(), CV_8UC3 );

注意我正在使用第一个图像尺寸/尺寸,假设两个图像具有相同的尺寸。请务必仔细检查您的设置并使用合适的尺寸。 此外,您可以将块放在try...catch块中以查看详细信息:

try { 
//your drawKeyPoints code here
}catch (cv::Exception &e) {
cerr << e.msg << endl; 
}