title: How to read a .tif floating point gray scale image in openCV
body: <p>I am trying to read a <code>.tif</code> or <code>.tiff</code> floating point gray scale image in <code>OpenCV</code>. </p>
  
  <p>I can read and write routine file format such as <code>png</code>, <code>jpg</code> etc but I am not able to read from my Desktop a format I never used before which is <code>.tif</code> or <code>.tiff</code> format.</p>
  
  <p><strong>The image</strong>: the image I am trying to read has the following parameters:
  Size:</p>
  
  <p><img src="https://i.imgur.com/knPUdAF.png" alt="size image"></p>
  
  <p>And width and height:</p>
  
  <p><img src="https://i.imgur.com/d4Vmlzq.png" alt="widthHeight image"></p>
  
  <p>After some documentation and various sources I was able to understand that it is possible to use a <code>convertTo</code> function to convert between available data types, the source can be found <a href="https://stackoverflow.com/questions/16741791/converting-to-floating-point-image-from-tif">here</a>. However this didn't work well and I actually had a compilation error saying:</p>
  
  <blockquote>
    <p>OpenCV(3.4.1) Error: Assertion failed (size.width>0 &amp;&amp; size.height>0) in imshow, file /home/to/opencv/modules/highgui/src/window.cpp, line 356
    terminate called after throwing an instance of cv::Exception
    what():  OpenCV(3.4.1) /home/to/opencv/modules/highgui/src/window.cpp:356: error: (-215) size.width>0 &amp;&amp; size.height>0 in function imshow</p>
  </blockquote>
  
  <p>The code I am using is the following:</p>
  
  <pre><code>#include &lt;opencv2/core.hpp&gt;
  #include &lt;opencv2/imgcodecs.hpp&gt;
  #include &lt;opencv2/highgui.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace cv;
  using namespace std;
  
  int main( int argc, char** argv )
  {
      Mat img = imread("/home/to/Desktop/example.tif");
      cv::imshow("source",img);
      Mat dst;  // destination image
  
      // check if we have RGB or grayscale image
      if (img.channels() == 3) {
          // convert 3-channel (RGB) 8-bit uchar image to 32 bit float
          img.convertTo(dst, CV_32FC3);
      }
      else if (img.channels() == 1) {
          // convert 1-chanel (grayscale) 8-bit uchar image to 32 bit float
          img.convertTo(dst, CV_32FC1);
      }
  
      // display output, note that to display dst image correctly
      // we have to divide each element of dst by 255 to keep
      // the pixel values in the range [0,1].
      cv::imshow("output",dst/255);
      waitKey();
  }
  </code></pre>
  
  <p>Additional example I tried to make it work is directly from the <code>OpenCV</code> documentation which can be found <a href="https://docs.opencv.org/2.4/doc/tutorials/introduction/display_image/display_image.html" rel="nofollow noreferrer">here</a>, with a small modification though. I read from <a href="https://answers.opencv.org/question/19272/unable-to-properly-read-16bit-tiff-image/?answer=19278#post-id-19278" rel="nofollow noreferrer">official documentation</a> that the options <code>IMREAD_ANYCOLOR | IMREAD_ANYDEPTH</code> should also be activated and in fact is what I did in the second additional trial below:</p>
  
  <pre><code>#include &lt;opencv2/core.hpp&gt;
  #include &lt;opencv2/imgcodecs.hpp&gt;
  #include &lt;opencv2/highgui.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace cv;
  using namespace std;
  
  int main( int argc, char** argv )
  {
      String imageName( "/home/to/Desktop/example.tif" ); // by default
      if( argc &gt; 1)
      {
          imageName = argv[1];
      }
  Mat image;
  Mat outImage;
  image = imread( imageName, IMREAD_ANYCOLOR | IMREAD_ANYDEPTH ); // Read the file
  if( image.empty() )                      // Check for invalid input
  {
      cout &lt;&lt;  "Could not open or find the image" &lt;&lt; std::endl ;
      return -1;
  }
  namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
  
  resize(image, outImage, cv::Size(500,500));
  
  imshow("orig", image);
  imshow("resized", outImage);
  
  
  
  // Show our image inside it.
  waitKey(0); // Wait for a keystroke in the window
  return 0;
  </code></pre>
  
  <p>This time the compiler runs without any error but no image is shown as it is possible to see from the print screen below:</p>
  
  <p><img src="https://i.imgur.com/6DSFBwv.png" alt="image"></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>This is the result after the <code>cv::resize</code> </p>
  
  <p><img src="https://i.imgur.com/M0DpqHA.png" alt="resized"></p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>This the result after applying <code>imshow("Display window", image*10);</code></p>
  
  <p><img src="https://i.imgur.com/ZPp9X7W.png" alt="5"></p>
  
  <p>Is there something that I am missing from the official documentation or something else I am forgetting to do?
  Thanks for shedding light on this issue.</p>