1. 程式人生 > >使用libtiff讀取tif/tiff影象

使用libtiff讀取tif/tiff影象

這幾天在寫tif影象的程式,需要用libtiff讀取tif影象(當然用OpenCV的imread和GDAL會更加方便),
點選下載libtiff
Demo程式如下:

int TestTIFFDemo()
{
    //開啟影象
    char* fileName = "D:/Image/Color/Beauty.tif";
    //char* fileName = "D:/Image/Projects/ShipImage/01001.tif";
    //char *fileName = "D:/Image/Color/Example400.tif";
    TIFF* tiff =TIFFOpen( fileName, "r"
);//開啟Tiff檔案,得到指標,以後所有的操作都通過指標進行 //獲取影象引數 int width, height; TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width); TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height); //讀取影象 //注意:TIFFReadRGBAImage讀取的通道順序為:ABGR uint32* image; int pixelCount = width*height; image = (uint32
*)malloc(pixelCount * sizeof (uint32)); TIFFReadRGBAImage(tiff, width, height, image, 1); //讀取R通道 //由於tiff格式的影象資料與bmp圖儲存方式一致,是從下到上,所以讀的時候,需要從下往上讀 BYTE* RImage = new BYTE[pixelCount]; //為存放資料分配記憶體空間 uint32 *rowPointerToSrc = image + (height - 1)*width; BYTE *rowPointerToR = RImage; for
(int y = height - 1; y >= 0; --y) { uint32 *colPointerToSrc = rowPointerToSrc; BYTE *colPointerToR = rowPointerToR; for (int x = 0; x <= width - 1; ++x) { colPointerToR[0] = (BYTE)TIFFGetR(colPointerToSrc[0]);//獲取R通道 //TIFFGetB(colPointerToSrc[0]);//獲取B通道 //TIFFGetG(colPointerToSrc[0]);//獲取G通道 colPointerToR++; colPointerToSrc++; } rowPointerToSrc -= width; rowPointerToR += width; } //除錯 //這裡使用了OpenCV Mat RImage_Mat(height, width, CV_8UC1, RImage, width); imwrite("D:/111.bmp", RImage_Mat); //釋放空間 _TIFFfree(image); _TIFFfree(RImage); TIFFClose(tiff); return 0; }

但是程式執行的時候出現了下面的警告提示
這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

到網上找了下解決方案,都沒有解決,最後,在OpenCV原始碼中找到了解決方案
修改後的程式如下:

//警告處理
static int grfmt_tiff_err_handler_init = 0;
static void GrFmtSilentTIFFErrorHandler(const char*, const char*, va_list) {}
int TestTIFFDemo()
{
    //警告處理:防止出現unknown field with tag  33500 encountered警告
    if (!grfmt_tiff_err_handler_init)
    {
        grfmt_tiff_err_handler_init = 1;

        TIFFSetErrorHandler(GrFmtSilentTIFFErrorHandler);
        TIFFSetWarningHandler(GrFmtSilentTIFFErrorHandler);
    }

    //開啟影象
    char* fileName = "D:/Image/Color/Beauty.tif";
    //char* fileName = "D:/Image/Projects/ShipImage/01001.tif";
    //char *fileName = "D:/Image/Color/Example400.tif";
    TIFF* tiff =TIFFOpen( fileName, "r");//開啟Tiff檔案,得到指標,以後所有的操作都通過指標進行

    //獲取影象引數
    int width, height;
    TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width);
    TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height);

    //讀取影象
    //注意:TIFFReadRGBAImage讀取的通道順序為:ABGR
    uint32* image;
    int pixelCount = width*height;
    image = (uint32*)malloc(pixelCount * sizeof (uint32));
    TIFFReadRGBAImage(tiff, width, height, image, 1);

    //讀取R通道
    //由於tiff格式的影象資料與bmp圖儲存方式一致,是從下到上,所以讀的時候,需要從下往上讀
    BYTE* RImage = new BYTE[pixelCount];    //為存放資料分配記憶體空間
    uint32 *rowPointerToSrc = image + (height - 1)*width;
    BYTE *rowPointerToR = RImage;
    for (int y = height - 1; y >= 0; --y)
    {
        uint32 *colPointerToSrc = rowPointerToSrc;
        BYTE *colPointerToR = rowPointerToR;
        for (int x = 0; x <= width - 1; ++x)
        {
            colPointerToR[0] = (BYTE)TIFFGetR(colPointerToSrc[0]);//獲取R通道
            //TIFFGetB(colPointerToSrc[0]);//獲取B通道
            //TIFFGetG(colPointerToSrc[0]);//獲取G通道

            colPointerToR++;
            colPointerToSrc++;
        }
        rowPointerToSrc -= width;
        rowPointerToR += width;
    }

    //除錯
    //這裡使用了OpenCV
    Mat RImage_Mat(height, width, CV_8UC1, RImage, width);
    imwrite("D:/111.bmp", RImage_Mat);

    //釋放空間
    _TIFFfree(image);
    _TIFFfree(RImage);
    TIFFClose(tiff);
    return 0;
}

新程式可以正常運行了。
原圖
這裡寫圖片描述

結果圖

這裡寫圖片描述

原來是需要加入一個警告處理。

注意:
1、由於tiff格式的影象資料與bmp圖儲存方式一致,是從下到上,所以讀的時候,需要從下往上讀,否則影象會出錯
2、 image = (uint32*)malloc(pixelCount * sizeof (uint32)); 如果需要申請的影象記憶體比較大,可以通過修改VS屬性的辦法申請大記憶體:properties->Linker->System->Heap Reserve Size
這裡寫圖片描述

這裡順便貼出tiff的OpenCV的原始碼:
原始碼在sources\modules\imgcodecs\src\中的grfmt_tiff.hpp和grfmt_tiff.cpp中
相關原始碼如下:

//tif影象解碼器(grfmt_tiff.hpp)
class TiffDecoder : public BaseImageDecoder
{
public:
    TiffDecoder();
    virtual ~TiffDecoder();

    bool  readHeader();
    bool  readData( Mat& img );
    void  close();
    bool  nextPage();

    size_t signatureLength() const;
    bool checkSignature( const String& signature ) const;
    ImageDecoder newDecoder() const;

protected:
    void* m_tif;
    int normalizeChannelsNumber(int channels) const;
    bool readHdrData(Mat& img);
    bool m_hdr;
};

其部分實現(grfmt_tiff.cpp)

#include "tiff.h"
#include "tiffio.h"

static int grfmt_tiff_err_handler_init = 0;
static void GrFmtSilentTIFFErrorHandler( const char*, const char*, va_list ) {}

TiffDecoder::TiffDecoder()
{
    m_tif = 0;

    //警告處理:防止出現unknown field with tag  33500 encountered警告
    if( !grfmt_tiff_err_handler_init )
    {
        grfmt_tiff_err_handler_init = 1;

        TIFFSetErrorHandler( GrFmtSilentTIFFErrorHandler );
        TIFFSetWarningHandler( GrFmtSilentTIFFErrorHandler );
    }
    m_hdr = false;
}


void TiffDecoder::close()
{
    if( m_tif )
    {
        TIFF* tif = (TIFF*)m_tif;
        TIFFClose( tif );
        m_tif = 0;
    }
}

TiffDecoder::~TiffDecoder()
{
    close();
}

size_t TiffDecoder::signatureLength() const
{
    return 4;
}

bool TiffDecoder::checkSignature( const String& signature ) const
{
    return signature.size() >= 4 &&
        (memcmp(signature.c_str(), fmtSignTiffII, 4) == 0 ||
        memcmp(signature.c_str(), fmtSignTiffMM, 4) == 0);
}

int TiffDecoder::normalizeChannelsNumber(int channels) const
{
    return channels > 4 ? 4 : channels;
}

ImageDecoder TiffDecoder::newDecoder() const
{
    return makePtr<TiffDecoder>();
}

//讀取檔案頭
bool TiffDecoder::readHeader()
{
    bool result = false;

    TIFF* tif = static_cast<TIFF*>(m_tif);
    if (!m_tif)
    {
        // TIFFOpen() mode flags are different to fopen().  A 'b' in mode "rb" has no effect when reading.
        // http://www.remotesensing.org/libtiff/man/TIFFOpen.3tiff.html
        //開啟tif檔案
        tif = TIFFOpen(m_filename.c_str(), "r");
    }

    if( tif )
    {
        uint32 wdth = 0, hght = 0;
        uint16 photometric = 0;
        m_tif = tif;

        //獲取屬性
        if( TIFFGetField( tif, TIFFTAG_IMAGEWIDTH, &wdth ) &&
            TIFFGetField( tif, TIFFTAG_IMAGELENGTH, &hght ) &&
            TIFFGetField( tif, TIFFTAG_PHOTOMETRIC, &photometric ))
        {
            uint16 bpp=8, ncn = photometric > 1 ? 3 : 1;
            TIFFGetField( tif, TIFFTAG_BITSPERSAMPLE, &bpp );
            TIFFGetField( tif, TIFFTAG_SAMPLESPERPIXEL, &ncn );

            m_width = wdth;
            m_height = hght;
            if((bpp == 32 && ncn == 3) || photometric == PHOTOMETRIC_LOGLUV)
            {
                m_type = CV_32FC3;
                m_hdr = true;
                return true;
            }
            m_hdr = false;

            if( bpp > 8 &&
               ((photometric != 2 && photometric != 1) ||
                (ncn != 1 && ncn != 3 && ncn != 4)))
                bpp = 8;

            int wanted_channels = normalizeChannelsNumber(ncn);
            switch(bpp)
            {
                case 8:
                    m_type = CV_MAKETYPE(CV_8U, photometric > 1 ? wanted_channels : 1);
                    break;
                case 16:
                    m_type = CV_MAKETYPE(CV_16U, photometric > 1 ? wanted_channels : 1);
                    break;

                case 32:
                    m_type = CV_MAKETYPE(CV_32F, photometric > 1 ? 3 : 1);
                    break;
                case 64:
                    m_type = CV_MAKETYPE(CV_64F, photometric > 1 ? 3 : 1);
                    break;

                default:
                    result = false;
            }
            result = true;
        }
    }

    if( !result )
        close();

    return result;
}

bool TiffDecoder::nextPage()
{
    // Prepare the next page, if any.
    return m_tif &&
           TIFFReadDirectory(static_cast<TIFF*>(m_tif)) &&
           readHeader();
}

//讀取影象資料
bool  TiffDecoder::readData( Mat& img )
{
    if(m_hdr && img.type() == CV_32FC3)
    {
        return readHdrData(img);
    }
    bool result = false;
    bool color = img.channels() > 1;
    uchar* data = img.ptr();

    if( img.depth() != CV_8U && img.depth() != CV_16U && img.depth() != CV_32F && img.depth() != CV_64F )
        return false;

    //讀影象資料
    if( m_tif && m_width && m_height )
    {
        TIFF* tif = (TIFF*)m_tif;
        uint32 tile_width0 = m_width, tile_height0 = 0;
        int x, y, i;
        int is_tiled = TIFFIsTiled(tif);
        uint16 photometric;
        TIFFGetField( tif, TIFFTAG_PHOTOMETRIC, &photometric );
        uint16 bpp = 8, ncn = photometric > 1 ? 3 : 1;
        TIFFGetField( tif, TIFFTAG_BITSPERSAMPLE, &bpp );
        TIFFGetField( tif, TIFFTAG_SAMPLESPERPIXEL, &ncn );
        const int bitsPerByte = 8;
        int dst_bpp = (int)(img.elemSize1() * bitsPerByte);
        int wanted_channels = normalizeChannelsNumber(img.channels());

        if(dst_bpp == 8)
        {
            char errmsg[1024];
            if(!TIFFRGBAImageOK( tif, errmsg ))
            {
                close();
                return false;
            }
        }

        if( (!is_tiled) ||
            (is_tiled &&
            TIFFGetField( tif, TIFFTAG_TILEWIDTH, &tile_width0 ) &&
            TIFFGetField( tif, TIFFTAG_TILELENGTH, &tile_height0 )))
        {
            if(!is_tiled)
                TIFFGetField( tif, TIFFTAG_ROWSPERSTRIP, &tile_height0 );

            if( tile_width0 <= 0 )
                tile_width0 = m_width;

            if( tile_height0 <= 0 )
                tile_height0 = m_height;

            AutoBuffer<uchar> _buffer( size_t(8) * tile_height0*tile_width0);
            uchar* buffer = _buffer;
            ushort* buffer16 = (ushort*)buffer;
            float* buffer32 = (float*)buffer;
            double* buffer64 = (double*)buffer;
            int tileidx = 0;

            for( y = 0; y < m_height; y += tile_height0, data += img.step*tile_height0 )
            {
                int tile_height = tile_height0;

                if( y + tile_height > m_height )
                    tile_height = m_height - y;

                for( x = 0; x < m_width; x += tile_width0, tileidx++ )
                {
                    int tile_width = tile_width0, ok;

                    if( x + tile_width > m_width )
                        tile_width = m_width - x;

                    switch(dst_bpp)
                    {
                        case 8:
                        {
                            uchar * bstart = buffer;
                            if( !is_tiled )
                                ok = TIFFReadRGBAStrip( tif, y, (uint32*)buffer );
                            else
                            {
                                ok = TIFFReadRGBATile( tif, x, y, (uint32*)buffer );
                                //Tiles fill the buffer from the bottom up
                                bstart += (tile_height0 - tile_height) * tile_width0 * 4;
                            }
                            if( !ok )
                            {
                                close();
                                return false;
                            }

                            for( i = 0; i < tile_height; i++ )
                                if( color )
                                {
                                    if (wanted_channels == 4)
                                    {
                                        icvCvt_BGRA2RGBA_8u_C4R( bstart + i*tile_width0*4, 0,
                                                             data + x*4 + img.step*(tile_height - i - 1), 0,
                                                             cvSize(tile_width,1) );
                                    }
                                    else
                                    {
                                        icvCvt_BGRA2BGR_8u_C4C3R( bstart + i*tile_width0*4, 0,
                                                             data + x*3 + img.step*(tile_height - i - 1), 0,
                                                             cvSize(tile_width,1), 2 );
                                    }
                                }
                                else
                                    icvCvt_BGRA2Gray_8u_C4C1R( bstart + i*tile_width0*4, 0,
                                                              data + x + img.step*(tile_height - i - 1), 0,
                                                              cvSize(tile_width,1), 2 );
                            break;
                        }

                        case 16:
                        {
                            if( !is_tiled )
                                ok = (int)TIFFReadEncodedStrip( tif, tileidx, (uint32*)buffer, (tsize_t)-1 ) >= 0;
                            else
                                ok = (int)TIFFReadEncodedTile( tif, tileidx, (uint32*)buffer, (tsize_t)-1 ) >= 0;

                            if( !ok )
                            {
                                close();
                                return false;
                            }

                            for( i = 0; i < tile_height; i++ )
                            {
                                if( color )
                                {
                                    if( ncn == 1 )
                                    {
                                        icvCvt_Gray2BGR_16u_C1C3R(buffer16 + i*tile_width0*ncn, 0,
                                                                  (ushort*)(data + img.step*i) + x*3, 0,
                                                                  cvSize(tile_width,1) );
                                    }
                                    else if( ncn == 3 )
                                    {
                                        icvCvt_RGB2BGR_16u_C3R(buffer16 + i*tile_width0*ncn, 0,
                                                               (ushort*)(data + img.step*i) + x*3, 0,
                                                               cvSize(tile_width,1) );
                                    }
                                    else if (ncn == 4)
                                    {
                                        if (wanted_channels == 4)
                                        {
                                            icvCvt_BGRA2RGBA_16u_C4R(buffer16 + i*tile_width0*ncn, 0,
                                                (ushort*)(data + img.step*i) + x * 4, 0,
                                                cvSize(tile_width, 1));
                                        }
                                        else
                                        {
                                            icvCvt_BGRA2BGR_16u_C4C3R(buffer16 + i*tile_width0*ncn, 0,
                                                (ushort*)(data + img.step*i) + x * 3, 0,
                                                cvSize(tile_width, 1), 2);
                                        }
                                    }
                                    else
                                    {
                                        icvCvt_BGRA2BGR_16u_C4C3R(buffer16 + i*tile_width0*ncn, 0,
                                                               (ushort*)(data + img.step*i) + x*3, 0,
                                                               cvSize(tile_width,1), 2 );
                                    }
                                }
                                else
                                {
                                    if( ncn == 1 )
                                    {
                                        memcpy((ushort*)(data + img.step*i)+x,
                                               buffer16 + i*tile_width0*ncn,
                                               tile_width*sizeof(buffer16[0]));
                                    }
                                    else
                                    {
                                        icvCvt_BGRA2Gray_16u_CnC1R(buffer16 + i*tile_width0*ncn, 0,
                                                               (ushort*)(data + img.step*i) + x, 0,
                                                               cvSize(tile_width,1), ncn, 2 );
                                    }
                                }
                            }
                            break;
                        }

                        case 32:
                        case 64:
                        {
                            if( !is_tiled )
                                ok = (int)TIFFReadEncodedStrip( tif, tileidx, buffer, (tsize_t)-1 ) >= 0;
                            else
                                ok = (int)TIFFReadEncodedTile( tif, tileidx, buffer, (tsize_t)-1 ) >= 0;

                            if( !ok || ncn != 1 )
                            {
                                close();
                                return false;
                            }

                            for( i = 0; i < tile_height; i++ )
                            {
                                if(dst_bpp == 32)
                                {
                                    memcpy((float*)(data + img.step*i)+x,
                                           buffer32 + i*tile_width0*ncn,
                                           tile_width*sizeof(buffer32[0]));
                                }
                                else
                                {
                                    memcpy((double*)(data + img.step*i)+x,
                                         buffer64 + i*tile_width0*ncn,
                                         tile_width*sizeof(buffer64[0]));
                                }
                            }

                            break;
                        }
                        default:
                        {
                            close();
                            return false;
                        }
                    }
                }
            }

            result = true;
        }
    }

    return result;
}

OpenCV原始碼真是個好東西,能夠從中學習到很多優秀的程式設計技巧,能夠幫助你解決很多問題。

相關推薦

使用libtiff讀取tif/tiff影象

這幾天在寫tif影象的程式,需要用libtiff讀取tif影象(當然用OpenCV的imread和GDAL會更加方便), 點選下載libtiff Demo程式如下: int TestTIFFDemo() { //開啟影象 char* f

用python讀取tif格式影象

用python讀取tif格式影象 import cv2 img = cv2.imread("11.tif",1) #第二個引數是通道數和位深的引數, #IMREAD_UNCHANGED = -1#不進行轉化,比如儲存為了16位的圖片,讀取出來仍然為16位。 #IMREAD_GRAYSC

tif格式影像(TIFF影象)轉DEM

(一)資料下載:  一般沒有直接提供的DEM資料,但是可以通過下載TIF資料轉換,這裡以SRTM為例,常用下載站點有: ①原始官方網站http://srtm.csi.cgiar.org/SELECTION/inputCoord.asp, ②國際科學資料服務平臺(中國的),地址為:http://datamirr

macOS python3 opencv 讀取和寫入影象

python opencv 影象 讀取 寫入 1,讀取和寫入影象 1,讀取和寫入影象 #! /usr/local/bin/python3 # coding:utf-8 import cv2 #讀取影象 img = cv2.imread("/U

Halcon一日一練:讀取檔案目錄影象的三種方法

第一種方法: 讀了一個單一影象: read_image(Image,'fabrik') 這種方式可以快速的讀取軟體自身攜帶的庫影象檔案,系統設定了庫影象映像檔案的快速讀取方式,我們也可以通過絕對地址的方式來讀取指定的目錄檔案: read_image(Image1,'E:/Halcon一日一練/參考資料

利用Python PIL cPickle讀取和儲存影象資料庫

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

Keras處理TIFF影象

Keras是一個高層神經網路API,Keras由純Python編寫而成並基 Tensorflow、Theano以及CNTK後端。Keras 為支援快速實驗而生,能夠把你的idea迅速轉換為結果,如果你有如下需求,請選擇Keras: 簡易和快速的原型設計(keras具有高度模組化,

C語言讀取BMP數字影象

// 資料型別說明: // WORD:16位無符號短整形,佔2個位元組 // DWORD:32位無符號短整形,佔4個位元組 // LONG:有符號32位整形,佔4個位元組 // RGBQUAD:用於定義調色盤陣列元素的型別 // LPBITMAPINFOHEADER:點陣圖資

讀取tif檔案 並且分別儲存

import os import numpy as np import glob import cv2 from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array

QT讀取本地txt影象資料並在介面上顯示該影象

int pixel[180*1500]; int i=0; char filenametxt[150] = "inputimg.txt"; ifstream infile(filenametxt,ios::in); //測試檔案是否成功開啟 if(

減少OpenCV讀取高解析度影象的時間

意義 目前無論是工業上還是生活中相機的解析度也會越來越高,無論是學術上還是工業上使用OpenCV進行影象處理,特別是大批量處理的時候,讀取一張高解析度影象到記憶體中的時間減少的話對大批量的影象處理的效率有大大的幫助,特別現在全景圖越來越普遍,好了,廢話不少說。

TIFF影象檔案格式詳解(3)

基本TIFF TAGS 本頁主要摘自 http://www.awaresystems.be/imaging/tiff/tifftags/baseline.html。每一個TAG均有原始連結,可以點選檢視詳細的描述。 這些基本Tag是所有TIFF編解碼器必須支援的Tag

Matlab讀取和顯示影象

Matlab影象讀取: f=imread(‘imgName.extend’) imgName——影象名 extend———影象格式字尾名 如何檢視f對應引數: whos f; 影象顯示: imshow(f);——–預設灰度級數256 imshow

【檔案格式-TIFFTIFF影象格式結構

1、 TIF 影象格式概覽 tif格式標籤(tag)檢視工具:AsTiffTagViewer TIF 圖由四個部分組成: 1.1、影象檔案頭(Image File Header 簡稱 IFH) 圖一、IFH 結構描述 IFH 資料結構包含3個成員共計 8 個位元組

TIFF影象檔案格式詳解

1 什麼是TIFF? TIFF是Tagged Image File Format的縮寫。在現在的標準中,只有TIFF存在, 其他的提法已經捨棄不用了。做為一種標記語言,TIFF與其他檔案格式最大的不同在於除了影象資料,它還可以記錄很多影象的其他資訊。它記錄影象資料的方式也

OpenCV入門學習之讀取usb攝像頭影象,實時顯示

首先,新建一個.cpp的源程式檔案,例如,gedit test.cpp 在該檔案中新增以下程式: #include <opencv2/core/core.hpp> #include

ArcGIS教程:TIFF影象拼接及幾何校正

  對於掃描好的兩幅TIF影象,應該是先進行拼接,再進行幾何校正比較合理,都是應用這個工具來的。   1)影象拼接   應用Georeference 工具,先將Auto Adjust勾去。   同名地物點的新增,可以先確定A圖的座標,然後將B圖的座標輸入為A,再 Up

手把手教你用MFC和OpenCV,製作mfc讀取並顯示影象(兩種方式)

MFC OpenCV 讀取並顯示影象 *************************************************完成的效果如圖******************************************** 前言:我用的是VS2013 + O

VTK學習筆記-2-TIFF影象資料的重切片

玩了半天,終於可以實現對於TIFF影象的三維重切片。首先需要注意的是vtkTIFFReader並不是支援所有的TIFF壓縮格式,比如LZ壓縮的就不支援。個人覺得,VTK在DICOM格式上具有更強的操作性,貌似其存在便是針對了DICOM格式而進行的,至於其他格式的資料,有點

用Java輸出解析度300dpi以上的TIFF影象

http://blog.csdn.net/casularm/article/details/4054484 國外期刊發表文章需要提供解析度在300~600dpi的影象,而使用javax.imageio.ImageIO只能夠輸出72dpi的JPG、PNG或GIF影象,無法