Reading tiff file

Hi, I need help with importing tiff file, but it is not an ordinary tiff file, instead of integers stored in the pixels there are double values stored there. The double values represent distance between the object and the camera and I need to load the file and extract specific pixel values.

welcome. MRE required.

try IMREAD_UNCHANGED flag

I tiried this code:

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    cv::Mat image = cv::imread("C:/Users/phill/OneDrive/Dokumenty/prace/Dave/vyvoj_vez/hloubkove_mapy/Aplikace/build-pokus_tiff-Desktop_Qt_6_4_2_MinGW_64_bit-Debug/pokus.tif", cv::IMREAD_UNCHANGED);

    if (image.empty()) {
        std::cout << "Could not read the image." << std::endl;
        return -1;
    }

    int pixelValue = image.at<uchar>(0, 4);

    std::cout << "Pixel value at (0, 0): " << pixelValue << std::endl;

    cv::waitKey(0);

    return 0;
}

But it gives, me this response:

imread_('C:/Users/phill/OneDrive/Dokumenty/prace/Dave/vyvoj_vez/hloubkove_mapy/Aplikace/build-pokus_tiff-Desktop_Qt_6_4_2_MinGW_64_bit-Debug/pokus.tif'): can't read header: OpenCV(4.7.0-dev) C:\GHA-OCV-2\_work\ci-gha-workflow\ci-gha-workflow\opencv\modules\imgcodecs\src\grfmt_tiff.cpp:344: error: (-2:Unspecified error) in function 'bool __cdecl cv::TiffDecoder::readHeader(void)':
    'sample_format == SAMPLEFORMAT_UINT || sample_format == SAMPLEFORMAT_INT'
where
    '(int)sample_format' is 3

Could not read the image.

C:\Users\phill\OneDrive\Dokumenty\prace\Dave\vyvoj_vez\hloubkove_mapy\Aplikace\ConsoleApplication1\x64\Release\ConsoleApplication1.exe (process 4172) exited with code -1.
Press any key to close this window . . .

I know that the file is ok because i load it with no problem in matlab.

I have, similar problem and solution would be super Handy, I am in dead end and despered.

I can even share the picture from which i want to extract the values

Hi, is there any news about this issue ? I cannot read my tiff files.
The identify command on a single image returns

TIFF 4096x4096 4096x4096+0+0 64-bit Grayscale Gray".

When trying this

cv::Mat inputMat = cv::imread(imagePath, cv::IMREAD_UNCHANGED);

I get

imread_('/path/to/file/467370-5243100_32720_PHISAT2-BANDS-GRID_0_2020-09-23T14-14-39_MOSAIC.tiff'): can't read header: OpenCV(4.3.0-dev) /opt/opencv/modules/imgcodecs/src/grfmt_tiff.cpp:148: error: (-215:Assertion failed) channels <= 4 in function 'normalizeChannelsNumber'

I played with many options but cannot read my tiff files without error. Any idea?

why are you waiting for fixes when you run an incredibly old version of OpenCV?

if that still doesn’t work with the current release, just file an issue on opencv’s github. include a small example file so anyone can reproduce the issue and test whether changes to the code fix the issue.

You’re right. I gonna test with a more recent version.

Hi! Tested with OpenCV 4.8.0. Still the same error

[ERROR:0@4.849] global loadsave.cpp:440 imread_ imread_('/path/to/file/467370-5243100_32720_PHISAT2-BANDS-GRID_0_2020-09-23T14-14-39_MOSAIC.tiff'): can't read header: OpenCV(4.8.0-dev) /opt/opencv/modules/imgcodecs/src/grfmt_tiff.cpp:155: error: (-2:Unspecified error) in function 'int cv::TiffDecoder::normalizeChannelsNumber(int) const'
> Unsupported number of channels:
> 'channels >= 1 && channels <= 4'
> where
> 'channels' is 8

is the error message accurate in that your file has eight channels or planes or layers? or maybe it has fewer, but an unusual data type?

I would have expected IMREAD_UNCHANGED to return the data as is… might indeed be a bug.

if this hasn’t been reported by anyone yet, you should submit an issue on the github.

I browsed the code a little. It’s an assertion without any immediate reason.

Maybe this can be raised with no ill effect.

Cannot read multi spectral tiff images · Issue #24258 · opencv/opencv (github.com) including a tif file attached.

I continue the discussion on github due the reply number restrictions.

Thxx a lot

you ought to provide a sample file to reproduce the problem. if possible, make it small. don’t use the entire file you encountered this on.