osg与OpenCV图像互转

本文介绍如何将OpenCV获取的视频帧转化为OSG中的纹理背景输出,重点讲解了OpenCV图像数据与OSG之间的转换过程及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因研究需要,这几天想把Opecv获取的视频帧转到OSG中作为纹理背景输出,关键的一步就是Opencv帧图像的传递。

那么问题来了:Opencv存放图像数据的格式和OSG的差异大吗?

因为自己也是个新手,首先找到了

osg抓帧放到openCV里

http://wenku.baidu.com/link?url=PXeAHmmV7vyA7s2fEK30_gEQvJzpHA9uDl9y1YIUHstCXCj_HKOTGjs_5PMnesuFDocCapTlBOyp5UjzWDu0LE7VayoDq98X3jzpPpyiQCm

原作者无从可考,但是留下了一句重要的代码:

memcpy((unsigned char*)pIplImg->imageData,pCImg->data(),pCImg->getImageSizeInBytes());

给出的信息就是两者数据转换,只要知道长和宽,以及像素数据的指针位置就可以了。完整的代码如下:

IplImage* CImage2IplImage(osg::ref_ptr<osg::Image> pCImg,IplImage *pIplImg)
{
//输出图像设置
pIplImg->origin=IPL_ORIGIN_BL;
//输入图像信息
memcpy((unsigned char*)pIplImg->imageData,pCImg->data(),pCImg->getImageSizeInBytes());
//将RGB存储为BGR
unsigned int temp=0;
for (int y=0;y<pIplImg->height;y++)
{
uchar* ptr=(uchar*)(pIplImg->imageData+y*pIplImg->widthStep);
for (int x=0;x<pIplImg->width;x++)
{
temp=ptr[3*x];//保存原始的R值
ptr[3*x]=ptr[3*x+2];
ptr[3*x+2]=temp;
}
}
return pIplImg;
}

顺藤摸瓜,再重申下我的目的是Opencv的图转成OSG的,因为配置了Opencv2.0,所以使用了最新的图像格式Mat(同时包括了老版本的IplImage和cvMat)

于是任务变成:Opencv Mat->OSG::Image

去Image看构造函数,只有setImage中要求设定长款和char* data,数据格式相符。OSG源码:

/** Set the image dimensions, format and data. */
        virtual void setImage(int s,int t,int r,
                      GLint internalTextureformat,
                      GLenum pixelFormat,GLenum type,
                      unsigned char* data,
                      AllocationMode mode,
                      int packing=1, int rowLength=0);
之后试了一下居然成了。我的代码:

osg::ref_ptr<osg::Image> mat2img::mat2image(Mat cvimg)
{
osgframe=new osg::Image;
osgframe->setImage(cvimg.cols,cvimg.rows,3,
GL_BGR,GL_BGR,GL_UNSIGNED_BYTE,cvimg.data,
osg::Image::NO_DELETE,1);
if(osgframe)
printf("convert done!");
osgDB::writeImageFile(*osgframe,"D:\\OV2OV\\OV2OV\\osgframe.bmp");
return osgframe;
}

调试输出的图片和文字信息全部正确。

之后就是重新加工视频流了,用osg纹理贴图即可。


以下是使用OpenCV C++进行图像拼接的示例代码: ```cpp #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main() { // 读取需要拼接的两张图片 Mat img1 = imread("image1.jpg"); Mat img2 = imread("image2.jpg"); // 转换为灰度图 Mat gray1, gray2; cvtColor(img1, gray1, COLOR_BGR2GRAY); cvtColor(img2, gray2, COLOR_BGR2GRAY); // 提取特征点 vector<KeyPoint> keypoints1, keypoints2; Ptr<FeatureDetector> detector = ORB::create(); detector->detect(gray1, keypoints1); detector->detect(gray2, keypoints2); // 计算特征描述子 Mat descriptors1, descriptors2; Ptr<DescriptorExtractor> descriptor = ORB::create(); descriptor->compute(gray1, keypoints1, descriptors1); descriptor->compute(gray2, keypoints2, descriptors2); // 特征点匹配 vector<DMatch> matches; Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("BruteForce-Hamming"); matcher->match(descriptors1, descriptors2, matches); // 筛选匹配点 vector<Point2f> points1, points2; for (int i = 0; i < matches.size(); i++) { points1.push_back(keypoints1[matches[i].queryIdx].pt); points2.push_back(keypoints2[matches[i].trainIdx].pt); } // 计算单应性矩阵 Mat H = findHomography(points2, points1, RANSAC); // 图像拼接 Mat result; warpPerspective(img2, result, H, Size(img1.cols + img2.cols, img1.rows)); Mat half(result, Rect(0, 0, img1.cols, img1.rows)); img1.copyTo(half); // 显示拼接结果 imshow("Result", result); waitKey(0); return 0; } ```
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值