GDAL图像数据格式转化为openCV图像数据格式

来源:互联网 发布:供应商品质数据库建立 编辑:程序博客网 时间:2024/05/22 03:53

openCV库提供了大量的图像处理函数,给图像处理算法工程师提供了很多的便利,但是在处理卫星影像等大图像时,opencv中的imread()函数无法装载。但是利用gdal中的函数将影像加载到程序中,再利用opencv中的函数去处理,这样就能充分利用openCV中大量的函数了。废话不多,直接上代码:

GDALAllRegister();    SourceTifPath = m_strimagedir + "\\" + m_imagename + "***.TIF";    const char * pszFile = SourceTifPath.c_str();    GDALDataset *poDataset;    //使用只读方式打开图像      poDataset = (GDALDataset*)GDALOpen(pszFile, GA_ReadOnly);    if (poDataset == NULL)    {        printf("File: %s不能打开!\n", pszFile);    }    GDALRasterBand *pBand = poDataset->GetRasterBand(1);    int iwidth = pBand->GetXSize();    int iheigth = pBand->GetYSize();    unsigned short *pBuf = new unsigned short[iwidth*iheigth];    pBand->RasterIO(GF_Read, 0, 0, iwidth, iheigth, pBuf, iwidth, iheigth, GDT_UInt16, 0, 0);    Mat image16 = Mat(iheigth, iwidth, CV_16UC1, pBuf);///构造openCV Mat类的数据格式,16位无符号单通道,openCV中无法显示,一般要转化为8位的    unsigned char *pBuf_8 = new unsigned char[iwidth*iheigth];    Mat image8 = Mat(iheigth, iwidth, CV_8UC1, *pBuf_8);///构造openCV Mat类的数据格式,8位无符号单通道
0 0
原创粉丝点击