osg結合opencv顯示影象
osg編譯的時候,似乎只要沒有勾png,就無法讀取png影象麼?
所以就只好用opencv先讀進來,然後傳給osg。程式碼如下:
#include<osgViewer/Viewer>
#include<osgDB/ReadFile>
#include<osgDB/WriteFile>
#include<osg/Geode>
#include<iostream>
#include<osg/DrawPixels>
#include<osg/Texture2D>
#include"FunctionHead.h"
#include"OSGInclude.h"
//opencv
//利用opencv讀取影象
#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
osg::ref_ptr<osg::Image> CVMat_2_OSGImage(cv::Mat& cvimg)
{
osg::Image osgframe2();
osg::Image* 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);
return osgframe;
}
int NinOsg_DrawPicture()
{
osg::ref_ptr<osg::DrawPixels> bitmap1 = new osg::DrawPixels;
osg::ref_ptr<osg::DrawPixels> bitmap2 = new osg::DrawPixels;
osg::ref_ptr<osg::DrawPixels> bitmap3 = new osg::DrawPixels;
bitmap1->setPosition(osg::Vec3(0.0, 0.0, 0.0));
bitmap2->setPosition(osg::Vec3(80.0, 0.0, 0.0));
bitmap3->setPosition(osg::Vec3(200.0, 0.0, 0.0));
//bitmap3->setUseSubImage(true);
//bitmap1->setImage(osgDB::readImageFile("..\\OpenSceneGraph-Data\\Images\\osg64.png"));
cv::Mat cvMat1 = cv::imread("..\\OpenSceneGraph-Data\\Images\\osg64.png");
cv::Mat cvMat2 = cv::imread("..\\OpenSceneGraph-Data\\Images\\osg128.png");
cv::Mat cvMat3 = cv::imread("..\\OpenSceneGraph-Data\\66.png");
osg::ref_ptr<osg::Image> image = CVMat_2_OSGImage(cvMat1);
osg::ref_ptr<osg::Image> image2 = CVMat_2_OSGImage(cvMat2);
osg::ref_ptr<osg::Image> image3 = CVMat_2_OSGImage(cvMat3);
osgDB::writeImageFile(*image3, "osgframe.bmp");//寫檔案
bitmap1->setImage(image);
bitmap2->setImage(image2);
bitmap3->setImage(image3);
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
//geode->addDrawable(bitmap1.get());
//geode->addDrawable(bitmap2.get());
geode->addDrawable(bitmap3.get());
osgViewer::Viewer viewer;
viewer.setSceneData(geode.get());
return viewer.run();
}
似乎是顯示出來了,首先放效果。效果如下:
問題:1。當載入多張圖片的時候,就會報錯,似乎是記憶體訪問錯誤
問題:2。顯示的圖片是反過來的,應該是opencv和opengl預設的圖片原點位置不同導致的。
目前,記憶體報錯還不知道是由什麼引起的。繼續學習。