Using OpenCV with MFC - Loading a tif-file

  | Back | Next |

Here I will add code that will load a tif file into an IplImage.

To load a tiff-file a whole lot of code must be written and this is out of scope of this little tutorial. So I will use a library found on the internet to do this in a easy way.
This library is called libtiff and the easies way to find them is to use google, search for "libtiff binaries windows".
I found them here named "libtiff_bin_354.zip" but they might have moved -so use google to locate them.

Extract the zip file to a temporary folder, then move only the subfolder "libtiff" to "C:\Program Files" so that the dll, lib and h-files is located in the folder "C:\Program Files\libtiff" -Add this folder to the search path or move the file "libtiff.dll" into e.g. "C:\Windows\system32". (To keep the documentation for the library the html folder can be a subfolder of libtiff).

Now press Alt-F7 to get to the "Project settings" and choose "All Confiurations" in the "Settings For:" with "MyApplication" highlighted. In the "Link" pane choose "Input" in the roll down menu of the "Category:" field, and add " libtiff.lib" to the "Object/library modules:" field, the filenames in the field must be space separated e.g "OpenCV.lib libtiff.lib".

Add the path of the library "C:\Program Files\libtiff" to the "Additional library path:" the path names must be comma separated e.g "C:\Program Files\Intel\opencv\lib,C:\Program Files\libtiff".

Now go to the "C/C++" pane and with "Preprocessor" chosen on the "Category:" roll down menu add the path to the library in the "Additional include directories:" e.g "C:\Program Files\Intel\opencv\cv\include,C:\Program Files\libtiff"

Use the FVB and double click on "MyIplClass.h" and include the library after the other include directives with:

- - - Start cut'n paste - - -
#include "tiff.h"
#include "tiffio.h"

- - - End cut'n paste - - -

In the CVB right click on "MyIplClass" and add the void member function "LoadTiff(CString FileName)" and add the following code to the function body:

- - - Start cut'n paste - - -
     int npixels;
     unsigned int w,h,Width,Height;
     unsigned long* TIFFImageData;
     TIFF* tif= TIFFOpen(FileName, "r"); // Open filename

     if (tif!=NULL)
     {
          TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &Width); // get size
          TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &Height);

          if ( (Width == IMAGE_WIDTH) ||
               (Height == IMAGE_HEIGHT) )
          {
               npixels = Width * Height;

               TIFFImageData = (uint32*) _TIFFmalloc(npixels * sizeof (uint32)); // allocate temp memory
               if (TIFFImageData == NULL)
                    m_Ipl = NULL;

               if ( !TIFFReadRGBAImage(tif, Width, Height, TIFFImageData, 0) )
                    m_Ipl = NULL;

               for ( w = 0; w < Width; w++ )
                    for (h = 0; h < Height; h++)
                         m_Ipl->imageData[Height*w + h]=(char)TIFFImageData[Height*w + h]; // Copy data to ipl

               _TIFFfree(TIFFImageData); // Release temp memory
          }
          else
          {
               AfxMessageBox("Wrong size of tiff image");
          }
          TIFFClose(tif);
     }

- - - End cut'n paste - - -

Now find a program to convert ring.bmp to a tiff file e.g. Paint Shop Pro and save it as "ring.tif" in the same folder, go to "OnProcess()" with the CVB and change the lines with the file name and load function to:

- - - Start cut'n paste - - -
     filename = "C:\\Program Files\\Intel\\opencv\\tests\\cv\\testdata\\snakes\\ring.tif";
     Img->LoadTiff(filename);

- - - End cut'n paste - - -

Thats it hit F5 to build and run, when the "Process" button is pressed the ring appears with a superimposed "snake".
The interesting fact is that the final snake have another appearance compared with the result from the bmp-file.

Press on the link to go back to the start go the tutorial.
Press on the link to use a Matrox frame grabber.

^ back to top ^

 

Last updated:
May 3 - 2004: My counter from freewebcounter.com created popups, so a new counter is used.
June 21 2003: Removed links to the IPL library.
May 5 2003: Created.