2011-07-30 35 views
1

作爲OpenCV中的CPU基於SURF是爲實時應用程序非常緩慢,我們決定使用GPU_SURF,建立opencv_gpu後,我們做了如下代碼:錯誤實施實時相機基於GPU_SURF OpenCV中

#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:\n\tmatcher_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 done\n"; 
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 loaded\n"; 
//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 uploaded\n"; 
cvtColor(frame_gpu,frame_gpu_cvt,CV_RGB2GRAY); 
cout<<"color done\n"; 
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.3\opencv\modules\gpu\src\color.cpp, line 186 

這是由於線路:

cvtColor(frame_gpu,frame_gpu_cvt,CV_RGB2GRAY); 

也許有其他錯誤的,有人可以請幫助我們在這一個。

回答

1

scn是在第一個參數cvtColor信道的數目。從RGB轉換爲灰色要求第一個參數有三個或四個通道。行frame_gpu.upload(image);因爲image被轉換frame_gpu到一個信道具有一個信道。它看起來像你可以跳過調用cvtColor並調用SURF直接frame_gpu

+0

謝謝你的回答,那個錯誤消失了,我們把代碼改了一點點 現在問題在於從iplimage獲取cvmat從攝像頭捕獲 'frame = cvQueryFrame(capture); ' 'CvMat * image = cvCreateMat(frame-> height,frame-> width,CV_8UC1);' 'cvInitMatHeader(image-> width,frame-> height,CV_8UC1,frame-> imageData);' ' frame_gpu.upload(圖像); ' 我們不frame_gpu得到正確的圖像,所以有問題的框架越來越像,我們用印刷框架: 'cvShowImage(「AA」,幀);' 但不是框架,如果我們試圖像存在只是黑屏 –

+0

你應該張貼此作爲一個單獨的問題 – SSteve