我一直在尝试使用Opencv Camera为增强现实项目对Irrlicht 3d模型进行纹理映射,我使用irrlicht 1.8.1和opencv 2.4.9进行了贴图,我几乎完成了textureMapping,但问题是在其中出现了奇怪的线条相机供稿及其低于4fps的帧,输出如以下屏幕截图所示(如下):
我有以下代码可以做到这一点:
#include <irrlicht.h>
#include "driverChoice.h"
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
#include <time.h>
#include <sys/timeb.h>
#include <opencv2\highgui\highgui.hpp>
#include <IGUIEnvironment.h>
using namespace cv;
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
int main()
{
IrrlichtDevice *device = createDevice(video::EDT_OPENGL, dimension2d<s32> (640 , 480), 16, false, true /* shadow */, false);
if (!device)
return 1;
device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();
ITexture* frame_tex = driver->addTexture(vector2d<s32>(640, 480), "video_stream");
guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
rect<s32>(10,10,260,22), true);
IAnimatedMesh* mesh = smgr->getMesh("E:/Akshay/VS2012Projects/IrrlichtDemoApp/IrrlichtDemoApp/ratamahatta.md2");
if (!mesh)
{
device->drop();
return 1;
}
IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );
if (node)
{
node->setMaterialFlag(EMF_LIGHTING, false);
node->setMD2Animation(scene::EMAT_STAND);
node->setMaterialTexture( 0, driver->getTexture("E:/Akshay/VS2012Projects/IrrlichtDemoApp/IrrlichtDemoApp/ctf_r.png") );
}
smgr->addCameraSceneNode(0, vector3df(20,30,-50), vector3df(0,5,0));
while(device->run())
{
VideoCapture capture(0);
Mat camera_frame;
if( cv::waitKey(50) >= 0 ) break;
if( !capture.grab() )
{
std::cout << "Unable to grab camera frame\n";
}
capture >> camera_frame;
if( ! camera_frame.empty() )
{
//exception here
unsigned char *tex_buf = (unsigned char*)frame_tex->lock();
unsigned char *frame_buf = camera_frame.data;
// Convert from RGB to RGBA
for(int y=0; y < camera_frame.rows; y++) {
for(int x=0; x < camera_frame.cols; x++) {
*(tex_buf++) = *(frame_buf++);
*(tex_buf++) = *(frame_buf++);
*(tex_buf++) = *(frame_buf++);
*(tex_buf++) = 255;
}
}
frame_tex->unlock();
driver->beginScene(true, true, SColor(255,100,101,140));
driver->draw2DImage(frame_tex, core::rect<s32>(0,0,640,480),
core::rect<s32>(0,0,480,640));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
}
device->drop();
}
任何帮助将不胜感激!
提前致谢!
最佳答案
我解决了问题,问题出在我的笔记本电脑上,我尝试在另一台PC上进行此操作,并且工作正常。由于是s32,我将其更改为u32,现在可以正常工作了。感谢你的帮助!