【问题标题】:Convert IplImage to CvMat将 IplImage 转换为 CvMat
【发布时间】:2011-10-16 23:46:59
【问题描述】:

这是gpu冲浪代码:

#include <iostream>
#include <iomanip>
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/imgproc/imgproc_c.h>
#include "opencv2/gpu/gpu.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"

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 = cvCaptureFromCAM(0);
int frame_width = (int) cvGetCaptureProperty(capture, 320);
int frame_height = (int) cvGetCaptureProperty(capture, 240);
cout<<"frames done\n";
GpuMat frame_gpu = GpuMat(frame_width, frame_height, CV_8UC3);
GpuMat frame_gpu_cvt = GpuMat(frame_width, frame_height, CV_8UC1);
cout<<"gpu frmes loaded\n";

IplImage* frame;
while(1)
{
frame =cvQueryFrame(capture);
CvMat* image = cvCreateMat(frame->height, frame->width, CV_8UC1);
/*CvMat* image = cvCreateMatHeader(frame->height, frame->width, CV_8UC1);
image->step = 4 * (image->cols * CV_ELEM_SIZE1(image->type) * CV_MAT_CN(image->type) / 4 + 1);//critical
cvCreateData(image);*/
cvInitMatHeader( image, frame->width, frame->height, CV_8UC1,frame->imageData);
// cvConvert( frame, image );
//cvCvtColor( frame, image, CV_RGB2GRAY );
cvConvertImage( frame, image, CV_RGB2GRAY);
namedWindow("aa", 1);
cvShowImage("aa", frame);
frame_gpu.upload(image);
cout<<"frame uploaded\n";
surf(frame_gpu, GpuMat(), keypoints2GPU, descriptors2GPU);
cout<<"surf done\n";
// matching descriptors
BruteForceMatcher_GPU< L2<float> > matcher;
GpuMat trainIdx, distance;
matcher.matchSingle(descriptors1GPU, descriptors2GPU, trainIdx, distance);
cout<<"match done\n";
// 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);
cout<<"match  done\n";
namedWindow("matches", 1);
imshow("matches", img_matches);
cvReleaseMat(&image);
frame_gpu.release();
cvReleaseImage(&frame);
img_matches.release();
cout<<"deallocation  done\n";
waitKey(0);
}
cvReleaseCapture(&capture);
cout<<"work done";
return 0;
}

我们在frame_gpu 中没有得到正确的图像,因此从frame 获取图像存在问题,我们使用cvShowImage("aa", frame); 打印frame,但如果我们尝试image 而不是frame只是空白屏幕

【问题讨论】:

    标签: opencv cuda gpu surf iplimage


    【解决方案1】:

    使用您的 IplImage 进行计数,然后使用此构造函数将其转换为 Mat(在 C++ 中):

    Mat(const IplImage* img, bool copyData=false);

    所以你会这样做:

    Mat myMat(img);

    这将使您的矩阵。您可以在程序的跟踪部分使用它!

    注意:数据不会被复制。不过,如果要复制数据,可以将 copyData 参数设置为 true

    【讨论】:

    • 仍然帮助我...干杯。 ;)
    【解决方案2】:

    试试这样的:

    IplImage* frame;
    
    // here load your frame with some image
    
    // convert to cv::Mat and show the converted image
    cv::Mat image(frame);
    cv::imshow("Image Window", image)
    

    这就是如何将 IplImage 转换为 cv::Mat 并显示出来的。

    关于与 cvMat 的转换,您可以在此处找到所有详细信息:

    http://wangpengnorman.blogspot.com/2009/06/transform-between-cvmat-and-iplimage.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多