[原][osg][osgearth]简单的通过osgDB,读取高程tif,修改高程tif
1 ReadResult result; 2 osg::ref_ptr<osgDB::ReaderWriter> reader = osgDB::Registry::instance()->getReaderWriterForExtension("tif"); 3 std::string name("D:\\gd.tif"); 4 osgDB::ReaderWriter::Options* opt= NULL; 5 osgDB::ReaderWriter::ReadResult rr = reader->readImage(name, opt); 6 7 if (rr.validImage()) 8 { 9 result = ReadResult(rr.takeImage()); 10 result.getImage()->setName("nameNoUse.tif"); 11 } 12 13 if (result.succeeded()) 14 { 15 result.getObject(); 16 result.metadata(); 17 osg::ref_ptr<osg::Image> image = result.getImage(); 18 19 osgEarth::ImageToHeightFieldConverter conv; 20 osg::HeightField* hf = conv.convert(image.get()); 21 22 23 24 for (unsigned col = 0; col < hf->getNumColumns(); ++col) 25 { 26 for (unsigned row = 0; row < hf->getNumRows(); ++row) 27 { 28 float height = hf->getHeight(col, row); 29 if (height < 1.0) 30 { 31 float newh = cos(height*3.141593f); 32 //float rf = rand()% 500; 33 hf->setHeight(col, row, -1000* newh); 34 } 35 else// if(height > 1) 36 { 37 //height = 100;//下断点看看 38 } 39 } 40 } 41 42 osg::Image* newimage = conv.convert(hf); 43 std::string nameofnew("D:\\gd2.tif"); 44 reader->writeImage(*newimage, nameofnew); 45 46 }
如题
加SB的“平滑”功能
float fBegin = 0.1; //float fEnd = 0.000001; float fLowestValue = 1000.0; int fWide = 100.0; ReadResult result; osg::ref_ptr<osgDB::ReaderWriter> reader = osgDB::Registry::instance()->getReaderWriterForExtension("tif"); std::string name("D:\\gd.tif"); osgDB::ReaderWriter::Options* opt= NULL; osgDB::ReaderWriter::ReadResult rr = reader->readImage(name, opt); if (rr.validImage()) { result = ReadResult(rr.takeImage()); result.getImage()->setName("guandao.tif"); } if (result.succeeded()) { result.getObject(); result.metadata(); osg::ref_ptr<osg::Image> image = result.getImage(); osgEarth::ImageToHeightFieldConverter conv; osg::HeightField* hf = conv.convert(image.get()); int *fFlag = new int[hf->getNumColumns()*hf->getNumRows()]; for (unsigned col = 0; col < hf->getNumColumns(); ++col) { for (unsigned row = 0; row < hf->getNumRows(); ++row) { fFlag[col*hf->getNumRows() + row] = 0; float height = hf->getHeight(col, row); if (height < fBegin) { fFlag[col*hf->getNumRows() + row] = 1; hf->setHeight(col, row, -fLowestValue); /* float newh = -1000.0; if(height > 0.00001) newh = 0.1 - (0.1 - height)/ (0.1-0.00001)*1000.0; hf->setHeight(col, row, newh);*/ } } } for (int i = 0; i < hf->getNumColumns()*hf->getNumRows(); i++) { if (fFlag[i] == 1)//如果这值在海面以下 { bool isNearSide = false; int nowX = i/hf->getNumRows(); int nowY = i%hf->getNumRows(); for (int j = 0; j <= fWide; j++) { //从离此值最近的值开始找附近的岸边,往外延伸 //向东南西北四个方向找,没层都遍历一圈 for ( int x = 0;x <= j;x++ ) { //如果找到有岸边 int fDifValueX = x; int fDifValueY = j - x; int realX = nowX - fDifValueX; if (realX > 0) { int realY = nowY - fDifValueY; if (realY > 0) { if (fFlag[realX*hf->getNumRows() + realY] == 0)//如果是岸边 isNearSide = true; } realY = nowY + fDifValueY; if (realY < hf->getNumRows()) { if (fFlag[realX*hf->getNumRows() + realY] == 0)//如果是岸边 isNearSide = true; } } realX = nowX + fDifValueX; if (realX < hf->getNumColumns()) { int realY = nowY - fDifValueY; if (realY > 0) { if (fFlag[realX*hf->getNumRows() + realY] == 0)//如果是岸边 isNearSide = true; } realY = nowY + fDifValueY; if (realY < hf->getNumRows()) { if (fFlag[realX*hf->getNumRows() + realY] == 0)//如果是岸边 isNearSide = true; } } } //查找这个范围内是否有值,如果有值则用此值 if (isNearSide) { float fRealHeight = fBegin - j * fLowestValue / fWide; hf->setHeight((i / hf->getNumRows()), (i % hf->getNumRows()), fRealHeight); break;//退出当前寻找的延伸 } } } } osg::Image* newimage = conv.convert(hf); std::string nameofnew("D:\\gd2.tif"); reader->writeImage(*newimage, nameofnew); delete[]fFlag; }