Mat input = imread("src.tif");
if (input.empty())
printf("Failed!\n");
More information 1
The same situation occurs in Python. I got the error message below
Python 3.8.0 (default, Nov 6 2019, 21:49:08)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> input = cv2.imread("src.tif")
[ WARN:0] global /io/opencv/modules/imgcodecs/src/grfmt_tiff.cpp (519) readData OpenCV TIFF(line 519): failed TIFFReadRGBAStrip(tif, y, (uint32*)buffer)
imread_('src.tif'): can't read data: OpenCV(4.1.2) /io/opencv/modules/imgcodecs/src/grfmt_tiff.cpp:519: error: (-2:Unspecified error) OpenCV TIFF: failed TIFFReadRGBAStrip(tif, y, (uint32*)buffer) in function 'readData'
More information 2
When I changed the version back to OpenCV 3.3.0, the tif file was read SUCCESSFULLY.
Looks like your input file have some problems:
tiffinfo.exe -D src.tif
src.tif: Warning, incorrect count for field "StripOffsets" (3, expecting 1); tag trimmed.
src.tif: Warning, incorrect count for field "StripByteCounts" (3, expecting 1); tag trimmed.
TIFF Directory at offset 0x3483788 (55064456)
Image Width: 3384 Image Length: 2712
Resolution: 72, 72
Bits/Sample: 16
Compression Scheme: None
Photometric Interpretation: RGB color
Samples/Pixel: 3
Planar Configuration: single image plane
Perhaps OpenCV does not handle these inconsistencies like tiffinfo.exe does. There have been made several changes since 3.3.0 which added stricter checks to TIFF reading module (e.g. #14249), so I guess the code which worked for you in 3.3.0 could work incorrectly.
I tried to "fix" your file using tiffcp
utility:
tiffcp.exe src.tif dst.tif
src.tif: Warning, incorrect count for field "StripOffsets" (3, expecting 1); tag trimmed.
src.tif: Warning, incorrect count for field "StripByteCounts" (3, expecting 1); tag trimmed.
And it started working:
Mat img = imread("dst.tif", IMREAD_UNCHANGED);
cout << img.size() << ", cn: " << img.channels() << ", depth: " << img.depth() << endl;
Output:
[3384 x 2712], cn: 3, depth: 2
BTW, if you want to get 16UC3 image as a result, you should use imread
with IMREAD_UNCHANGED
option.
Hi mshabunin, thanks for your explanation of tiff tools.
I tried to reproduce the operations but got this
D:\ProgramFiles\Anaconda\Library\bin> .\tiffinfo.exe -D E:\repository\screen\bin\data\src.tif
TIFFReadDirectory: Warning, Bogus "StripByteCounts" field, ignoring and calculating from imagelength.
TIFF Directory at offset 0x3483788 (55064456)
Image Width: 3384 Image Length: 2712
Resolution: 72, 72
Bits/Sample: 16
Compression Scheme: None
Photometric Interpretation: RGB color
Samples/Pixel: 3
Planar Configuration: single image plane
TIFFReadEncodedStrip: Read error at scanline 4294967295; got 40 bytes, expected 55064448.
And copy
didn't work, either.
D:\ProgramFiles\Anaconda\Library\bin> .\tiffcp.exe E:\repository\screen\bin\data\src.tif E:\repository\screen\bin\data\dst.tif
TIFFReadDirectory: Warning, Bogus "StripByteCounts" field, ignoring and calculating from imagelength.
TIFFFillStrip: Read error at scanline 4294967295; got 40 bytes, expected 55064448.
E:\repository\screen\bin\data\src.tif: Error, can't read scanline 0.
more information 1
Then I realized that this tool is provided by anaconda, which version is 4.0.10.
D:\ProgramFiles\Anaconda\Library\bin> .\tiffcp.exe
LIBTIFF, Version 4.0.10
Thus I went to http://www.libtiff.org/
and found out the latest version for windows was 3.8.2, unbelievable.
When I downloaded 3.8.2 and used this version instead, I got the same output as yours.
more information 2
I searched for the ERROR message Bogus "StripByteCounts" field, ignoring and calculating from imagelength
given by 4.0.10 and got an explanation in http://www.libtiff.org/man/TIFFReadDirectory.3t.html
Bogus "StripByteCounts" field, ignoring and calculating from imagelength.
Certain vendors violate the specification by writing zero for the StripByteCounts tag when they want to leave the value unspecified.
If the image has a single strip, the library will estimate the missing value based on the file size.
Seems that it is the vendors who intentionally write zero. The TIFF file was generated by the CCD device CX-2B/3B Imaging Luminance Meter
,
summary
I am not sure why the 4.0.10 version of tiffinfo,exe rejects the TIFF file which has zero for the StripByteCounts, while the 3.8.2 version can recognize and even fix it.
Since OpenCV 3.3.0 works but OpenCV 3.4.5 not, does OpenCV follow the same "rule"?
Media I/O:
ZLib: build (ver 1.2.8)
JPEG: build (ver 90)
WEBP: build (ver encoder: 0x020e)
PNG: build (ver 1.6.24)
TIFF: build (ver 42 - 4.0.2)
JPEG 2000: build (ver 1.900.1)
OpenEXR: build (ver 1.7.1)
GDAL: NO
GDCM: NO
OpenCV 3.4.5 BuildInformation
Media I/O:
ZLib: build (ver 1.2.11)
JPEG: build-libjpeg-turbo (ver 1.5.3-62)
WEBP: build (ver encoder: 0x020e)
PNG: build (ver 1.6.35)
TIFF: build (ver 42 - 4.0.9)
JPEG 2000: build (ver 1.900.1)
OpenEXR: build (ver 1.7.1)
HDR: YES
SUNRASTER: YES
PXM: YES
Yes, I used utilities from libtiff.org too.
TIFFReadEncodedStrip: Read error at scanline 4294967295; got 40 bytes, expected 55064448.
The same message is produced by libtiff in OpenCV, but we we don't see it due to some reasons.
Perhaps there is a difference in libtiff (4.0.2 vs 4.0.9), not in its usage. In this case, if you are able to reproduce the issue with latest version of libtiff utils, you can report it or propose a fix in the upstream (https://gitlab.com/libtiff/libtiff) and we will eventually upgrade our version.
Also you can try to build your version of OpenCV with your custom version of libtiff.
Thanks for your response.
I replaced v4.0.9 with 4.0.2 and rebuilded OpenCV 3.4.5, it works.
Meantime, there are too many commits between v4.0.9 and 4.0.2, I will try to find the key difference and report a issue if necessary.
[ WARN:[email protected]] global /io/opencv/modules/imgcodecs/src/grfmt_tiff.cpp (1176) writeLibTiff OpenCV TIFF(line 1176): failed TIFFWriteScanline(tif, buffer, y, 0) == 1 imwrite_('FILENAME.tif'): can't write data: OpenCV(4.6.0) /io/opencv/modules/imgcodecs/src/grfmt_tiff.cpp:1176: error: (-2:Unspecified error) OpenCV TIFF: failed TIFFWriteScanline(tif, buffer, y, 0) == 1 in function 'writeLibTiff'
I am also facing a similar problem, in my case while writing, not reading. I have large satellite images that I am trying to split into smaller patches to use in a DL model. The whole image is read correctly, when the smaller patches are created, most of them are created without any issue. The tiff files don't have any problem as well. I can open them on QGIS and work on different functions without any problem. Only while creating specific one or two patches I am getting this error every time. The base image is 10k * 10k. My patches are of size 500 * 500px. What does it mean by error: (-2:Unspecified error)
?
OpenCV 4.6.0
Python 3.8.13