在opencv中实现基于GPU_SURF的实时摄像头时出错



由于opencv中基于CPU的SURF对于实时应用程序来说非常慢,我们决定使用GPU_SURF,在设置了opencv_gpu之后,我们做了以下代码:

#include <iostream>
#include <iomanip>
#include <windows.h>
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/gpu/gpu.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/core/types_c.h"
using namespace std;
using namespace cv;
using namespace cv::gpu;
void help()
{
cout << "nThis program demonstrates using SURF_GPU features detector, descriptor extractor and BruteForceMatcher_GPU" << endl;
cout << "nUsage:ntmatcher_simple_gpu <image1> <image2>" << endl;
}
int main(int argc, char* argv[])
{
GpuMat img1(imread("C:\OpenCV2.3\opencv2.3\bin\Debug\tsucuba_left.png", CV_LOAD_IMAGE_GRAYSCALE));
SURF_GPU surf;
// detecting keypoints & computing descriptors
GpuMat keypoints1GPU, keypoints2GPU;
GpuMat descriptors1GPU, descriptors2GPU;
surf(img1, GpuMat(), keypoints1GPU, descriptors1GPU);
cout << "FOUND " << keypoints1GPU.cols << " keypoints on first image" << endl;
//cout << "FOUND " << keypoints2GPU.cols << " keypoints on second image" << endl;
CvCapture* capture = cvCreateCameraCapture(0);
int frame_width = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
int frame_height = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
cout<<"frames donen";
cv::gpu::GpuMat frame_gpu = cv::gpu::GpuMat(frame_width, frame_height, CV_8UC3);
cv::gpu::GpuMat frame_gpu_cvt = cv::gpu::GpuMat(frame_width, frame_height, CV_8UC1);
cout<<"gpu frmes loadedn";
//Sleep(200);
while(cvGrabFrame(capture))
{
IplImage* frame;
frame =cvQueryFrame(capture);
CvMat* image=0;
image = cvCreateMat(frame->height, frame->width, CV_8UC1);
frame_gpu.upload(image);
cout<<"frame uploadedn";
cvtColor(frame_gpu,frame_gpu_cvt,CV_RGB2GRAY);
cout<<"color donen";
surf(frame_gpu_cvt, GpuMat(), keypoints2GPU, descriptors2GPU);
// matching descriptors
BruteForceMatcher_GPU< L2<float> > matcher;
GpuMat trainIdx, distance;
matcher.matchSingle(descriptors1GPU, descriptors2GPU, trainIdx, distance);
// downloading results
vector<KeyPoint> keypoints1, keypoints2;
vector<float> descriptors1, descriptors2;
vector<DMatch> matches;
surf.downloadKeypoints(keypoints1GPU, keypoints1);
surf.downloadKeypoints(keypoints2GPU, keypoints2);
surf.downloadDescriptors(descriptors1GPU, descriptors1);
surf.downloadDescriptors(descriptors2GPU, descriptors2);
BruteForceMatcher_GPU< L2<float> >::matchDownload(trainIdx, distance, matches);
// drawing the results
Mat img_matches;
drawMatches(img1, keypoints1, frame_gpu, keypoints2, matches, img_matches);
namedWindow("matches", 0);
imshow("matches", img_matches);
//waitKey(0);
}
return 0;
}

执行时出现的错误是:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in unknown function, file
......opencv_2.3opencvmodulesgpusrccolor.cpp, line 186

这是由于这一行:

cvtColor(frame_gpu,frame_gpu_cvt,CV_RGB2GRAY);

可能还有其他错误,谁能帮我们解决这个问题

scncvtColor的第一个参数中的通道数。从RGB转换到GRAY需要第一个参数有三个或四个通道。线路frame_gpu.upload(image);frame_gpu转换为一个通道,因为image有一个通道。看起来你可以跳过对cvtColor的调用,直接在frame_gpu上调用SURF

最新更新