使用gdal对图像建立金字塔

来源:互联网 发布:黑道圣徒4人物数据 编辑:程序博客网 时间:2024/04/30 09:13

对图像建立金字塔,参考了李民录的博客,给出了一个可以直接粘贴就能运行的代码

// CreatePyramids.cpp : 定义控制台应用程序的入口点。#define _AFXDLL#pragma#include "stdafx.h"#include "gdal.h"#include "gdal_priv.h"#include "afx.h"#pragma comment(lib,"gdal_i.lib")using namespace std;#pragma warning(disable: 4996)/** @brief 进度条基类** 提供进度条基类接口,来反映当前算法的进度值*/class  CProcessBase{public: /** * @brief 构造函数 */ CProcessBase()  {  m_dPosition = 0.0;  m_iStepCount = 100;  m_iCurStep = 0;  m_bIsContinue = true; } /** * @brief 析构函数 */ virtual ~CProcessBase() {} /** * @brief 设置进度信息 * @param pszMsg   进度信息 */ virtual void SetMessage(const char* pszMsg) = 0; /** * @brief 设置进度值 * @param dPosition  进度值 * @return 返回是否取消的状态,true为不取消,false为取消 */ virtual bool SetPosition(double dPosition) = 0; /** * @brief 进度条前进一步,返回true表示继续,false表示取消 * @return 返回是否取消的状态,true为不取消,false为取消 */ virtual bool StepIt() = 0; /** * @brief 设置进度个数 * @param iStepCount  进度个数 */ virtual void SetStepCount(int iStepCount) {  ReSetProcess();   m_iStepCount = iStepCount; } /** * @brief 获取进度信息 * @return 返回当前进度信息 */ string GetMessage() {  return m_strMessage; } /** * @brief 获取进度值 * @return 返回当前进度值 */ double GetPosition() {  return m_dPosition; } /** * @brief 重置进度条 */ void ReSetProcess() {  m_dPosition = 0.0;  m_iStepCount = 100;  m_iCurStep = 0;  m_bIsContinue = true; } /*! 进度信息 */ string m_strMessage; /*! 进度值 */ double m_dPosition;   /*! 进度个数 */ int m_iStepCount;   /*! 进度当前个数 */ int m_iCurStep;    /*! 是否取消,值为false时表示计算取消 */ bool m_bIsContinue;   };  /*** @brief 控制台进度条类** 提供控制台程序的进度条类接口,来反映当前算法的进度值*/class CConsoleProcess : public CProcessBase{public: /** * @brief 构造函数 */ CConsoleProcess()  {  m_dPosition = 0.0;  m_iStepCount = 100;  m_iCurStep = 0; }; /** * @brief 析构函数 */ ~CConsoleProcess()  {  //remove(m_pszFile); }; /** * @brief 设置进度信息 * @param pszMsg   进度信息 */ void SetMessage(const char* pszMsg) {  m_strMessage = pszMsg;  printf("%s\n", pszMsg); } /** * @brief 设置进度值 * @param dPosition  进度值 * @return 返回是否取消的状态,true为不取消,false为取消 */ bool SetPosition(double dPosition) {  m_dPosition = dPosition;  TermProgress(m_dPosition);  m_bIsContinue = true;  return true; } /** * @brief 进度条前进一步 * @return 返回是否取消的状态,true为不取消,false为取消 */ bool StepIt() {  m_iCurStep ++;  m_dPosition = m_iCurStep*1.0 / m_iStepCount;  TermProgress(m_dPosition);  m_bIsContinue = true;  return true; }private: void TermProgress(double dfComplete) {  static int nLastTick = -1;  int nThisTick = (int) (dfComplete * 40.0);  nThisTick = MIN(40,MAX(0,nThisTick));  // Have we started a new progress run?    if( nThisTick < nLastTick && nLastTick >= 39 )   nLastTick = -1;  if( nThisTick <= nLastTick )   return ;  while( nThisTick > nLastTick )  {   nLastTick++;   if( nLastTick % 4 == 0 )    fprintf( stdout, "%d", (nLastTick / 4) * 10 );   else    fprintf( stdout, "." );  }  if( nThisTick == 40 )   fprintf( stdout, " - done.\n" );  else   fflush( stdout ); }};bool CreatePyramids(const char* pszFileName, CProcessBase *pProgress)  {      if (pProgress != NULL)      {    pProgress->SetMessage("创建金字塔");       }        GDALAllRegister();      CPLSetConfigOption("USE_RRD","YES");    //创建Erdas格式的字塔文件        /* -------------------------------------------------------------------- */      /*      Open data file.                                                 */    /* -------------------------------------------------------------------- */        GDALDatasetH     hDataset;      hDataset = GDALOpen( pszFileName, GA_ReadOnly );        GDALDriverH hDriver = GDALGetDatasetDriver(hDataset);      const char* pszDriver = GDALGetDriverShortName(hDriver);      if (EQUAL(pszDriver, "HFA") || EQUAL(pszDriver, "PCIDSK"))      {          GDALClose(hDataset);    //如果文件是Erdas的img或者PCI的pix格式,创建内金字塔,其他的创建外金字塔          hDataset = GDALOpen( pszFileName, GA_Update );      }        if( hDataset == NULL )      {          if (pProgress != NULL)     pProgress->SetMessage("打开图像失败,请检查图像是否存在或文件是否是图像文件!");                    return false ;      }        /* -------------------------------------------------------------------- */      /*      Get File basic infomation                                       */      /* -------------------------------------------------------------------- */      int iWidth = GDALGetRasterXSize(hDataset);      int iHeigh = GDALGetRasterYSize(hDataset);       int iPixelNum = iWidth * iHeigh;    //图像中的总像元个数      int iTopNum = 4096;                 //顶层金字塔大小,64*64      int iCurNum = iPixelNum / 4;        int anLevels[1024] = { 0 };      int nLevelCount = 0;                //金字塔级数        do    //计算金字塔级数,从第二级到顶层      {          anLevels[nLevelCount] = static_cast<int>(pow(2.0, nLevelCount+2));          nLevelCount ++;          iCurNum /= 4;      } while (iCurNum > iTopNum);        const char      *pszResampling = "nearest"; //采样方式      GDALProgressFunc pfnProgress = GDALTermProgress;//进度条        /* -------------------------------------------------------------------- */      /*      Generate overviews.                                             */      /* -------------------------------------------------------------------- */      if (nLevelCount > 0 &&          GDALBuildOverviews( hDataset,pszResampling, nLevelCount, anLevels,          0, NULL, pfnProgress, pProgress ) != CE_None )      {          if (pProgress != NULL)           return true ;      }        /* -------------------------------------------------------------------- */      /*      Cleanup                                                         */      /* -------------------------------------------------------------------- */      GDALClose(hDataset);      GDALDestroyDriverManager();       if (pProgress != NULL)    pProgress->SetMessage("创建金字塔完成!");        return true;  }  int _tmain(int argc, _TCHAR* argv[]){ CConsoleProcess *pProgress = new CConsoleProcess();        bool f = CreatePyramids("C:\\tiff\\111.tif", pProgress);        if (f == true)         printf("计算成功/n");      else          printf("计算失败/n");      delete pProgress;    return 0;} 


 

原创粉丝点击