프로그래밍/영상처리

[별첨]tif파일 처리

산을좋아한라쯔 2017. 10. 9. 12:32
반응형

tif파일을 처리하기 위해서 libtiff 라이브러리를 사용했다.


libtiff는 오픈소스 프로젝트로 http://www.libtiff.org/ 에서 내용확인할 수 있다.

여기서는, libtiff에 있는 것중에서, tiff파일을 로드해서 읽어들이는 것만 수행할 것이기에, libtiff에 있는 것중에서 필요한 라이브러리만 사용할 것이다.

필요한 라이브러리를 받기 위해서 다음과 같은 과정을 거쳤다.


1. libtiff 설치

2. 필요 파일

  - C:\Program Files (x86)\GnuWin32 에 프로그램이 설치되게 되는데, 여기서 필요한 파일은 다음과 같다.

     libtiff.lib   tiff.h   tiffconf.h   tiffio.h   tiffvers.h

     jpeg62.dll  libtiff3.dll  zlib1.dll

  - 이 파일들만 zip파일을 만들어 뒀다.


libtiff.zip



Visual Studio에서, tif파일을 CByteImage로 로드해서 화면에 출력하는 것을 예로 들어보겠다.

1. Visual Studio에서 프로젝트 생성

- 프로젝트 이름: TiffTest

- MFC 응용프로그램 - Dialog 


2. imageSrc 폴더 생성 및 프로젝트에 추가

    

imageSrc.zip


- 프로젝트 폴더내에 imageSrc폴더 생성하고, 다음 파일들 paste

    ImageFrameWnd.cpp

    ImageFrameWnd.h

    ImageFrameWndManager.cpp

    ImageFrameWndManager.h

    ImageView.cpp

    ImageView.h

    LoadImageFromFileDialog.cpp

    LoadImageFromFileDialog.h

    MyImage.h

    MyImageFunc.cpp

    MyImageFunc.h

- Visual Studio의 솔루션 탐색기에서 프로젝트 선택하고 마우스 우클릭 - 추가 - 기존항목

  : imageSrc내 모든 파일 선택 후 추가


3. libtiff 파일 추가

- 프로젝트 폴더내에 libtiff폴더 생성 후, 다음 파일들 paste

   libtiff.lib

   tiff.h

   tiffconf.h

   tiffio.h

   tiffvers.h

Visual Studio의 솔루션 탐색기에서 프로젝트 선택하고 마우스 우클릭 - 추가 - 기존항목

  : libtiff내 모든 파일 선택 후 추가


4. libtiff용 DLL파일들 추가

- 다음 파일들을 프로젝트의 exe파일이 생성되는 곳에 paste

  (Debug 모드면 debug폴더 내. 프로젝트를 한번이라도 build해야 해당 폴더가 생성된다.)

  jpeg62.dll

  libtiff3.dll

  zlib1.dll


5. TiffTestDlg.cpp에 헤더파일 include

#include "libtiff\tiff.h"

#include "libtiff\\tiffio.h"

 

#include "imageSrc\MyImage.h"

#include "imageSrc\ImageFrameWndManager.h"


6. tif파일 로드용 버튼 생성하고 처리함수 작성

- 다이얼로그 윈도우에 버튼 하나 생성

. caption: TIFF파일 보기

- 버튼을 더블클릭해서, 버튼클랙에 대한 함수 생성 후, 아래내용처럼 코딩

void CTiffTestDlg::OnBnClickedButton1()

{

         TIFF* tif;

         tif = TIFFOpen("c:\\tmp\\test.tif", "r"); 

        

         if (tif) {

                  uint32 w, h;

                  TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);

                  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);         

 

                  size_t npixels = w * h;

                  uint32* raster = (uint32*)_TIFFmalloc(npixels * sizeof(uint32));

                  if (raster != NULL) {

                           if (TIFFReadRGBAImage(tif, w, h, raster, 0)) {

                                   CByteImage image(w, h, 3);

                                   BYTE* p;

                                   int offset = 0;                            

                                   for (int r = h-1; r >= 0; r--) {

                                            p = image.GetPtr(r);

                                            for (int c = 0; c < w; c++) {

                                                     int rgb = raster[offset++];

                                                     p[c * 3] = (BYTE)((rgb >> 16) & 0xff);

                                                     p[c * 3+1] = (BYTE)((rgb >> 8) & 0xff);

                                                     p[c * 3+2] = (BYTE)(rgb & 0xff);

                                            }

                                   }

                                   if (!image.IsEmpty()) {

                                            ShowImage(image, "변환된 이미지");

                                   }

                           }

                           _TIFFfree(raster);

                           TIFFClose(tif);

                  }                         

         }       

}



7. 실행해 보기

- 위 코드에서 보듯히, c:\tmt폴더에 적당한 tiff파일을 위치시키고(test.tif), 프로젝트를 빌드해서 실행해본다.

- 흑백의 지문 영상이 나오면 ok


----------------------------------------------------------------------------

위 코드에서, Tiff영상을 CByteImage로 변환하는 함수를 따로 만든다면, 모듈화해서 유용하게 사용할 수 있겠다.

따로 만든 함수 및 호출되는 코드는 다음과 같다.


CByteImage CTiffTestDlg::Tiff2Image(TIFFtif) {            

         uint32 w, h;     

         TIFFGetField(&tif, TIFFTAG_IMAGEWIDTH, &w);

         TIFFGetField(&tif, TIFFTAG_IMAGELENGTH, &h);

 

         CByteImage image(w, h, 3);

         size_t npixels = w * h;

         uint32* raster = (uint32*)_TIFFmalloc(npixels * sizeof(uint32));

         if (raster != NULL) {

                  if (TIFFReadRGBAImage(&tif, w, h, raster, 0)) {

                           BYTE* p;

                           int offset = 0;

                           for (int r = h - 1; r >= 0; r--) {

                                   p = image.GetPtr(r);

                                   for (int c = 0; c < w; c++) {

                                            int rgb = raster[offset++];

                                            p[c * 3] = (BYTE)((rgb >> 16) & 0xff);

                                            p[c * 3 + 1] = (BYTE)((rgb >> 8) & 0xff);

                                            p[c * 3 + 2] = (BYTE)(rgb & 0xff);

                                   }

                           }

                  }

                  _TIFFfree(raster);

         }

         return image;

}

 

void CTiffTestDlg::OnBnClickedButton1()

{

         TIFF* tif;

         tif = TIFFOpen("c:\\dlls\\test.tif", "r");

 

         if (tif) {

                  CByteImage image = Tiff2Image(*tif);

 

                  if (!image.IsEmpty()) {

                           ShowImage(image, "변환된 이미지");

                  }

                  TIFFClose(tif);

         }       

}


-끝-  

libtiff.zip
0.25MB
imageSrc.zip
0.01MB
반응형