简体   繁体   中英

OpenCV read 16 bits tiff image in c++

Recently, I have a problem to read 16 bits gray tiff image. Each pixel of this kind image has 2 samples and each sample has 16 bits. However, when I read it in OpenCV, it always in 8 bits and I'm not sure how opencv arranges the 2 samples in .data . I've tried every combination of flags in imread() including CV_LOAD_IMAGE_ANYDEPTH. It still read the image in 8 bits. The image's information is as below (read with MatLab):

                     FileSize: 34375411
                       Format: 'tif'
                FormatVersion: []
                        Width: 5597
                       Height: 4051
                     BitDepth: 32
                    ColorType: 'grayscale'
              FormatSignature: [73 73 42 0]
                    ByteOrder: 'little-endian'
               NewSubFileType: 0
                BitsPerSample: [16 16]
                  Compression: 'LZW'
    PhotometricInterpretation: 'BlackIsZero'
                 StripOffsets: [1×4051 double]
              SamplesPerPixel: 2
                 RowsPerStrip: 1
              StripByteCounts: [1×4051 double]
                  XResolution: []
                  YResolution: []
               ResolutionUnit: 'Inch'
                     Colormap: []
          PlanarConfiguration: 'Chunky'
                    TileWidth: []
                   TileLength: []
                  TileOffsets: []
               TileByteCounts: []
                  Orientation: 1
                    FillOrder: 1
             GrayResponseUnit: 0.0100
               MaxSampleValue: [65535 65535]
               MinSampleValue: [0 0]
                 Thresholding: 1
                       Offset: 8
                     Software: 'pix4dmapper'
                    Predictor: 'Horizontal differencing'
                 ExtraSamples: 0
                 SampleFormat: {'Unsigned integer'  'Unsigned integer'}
           ModelPixelScaleTag: [0.0385 0.0385 0]
             ModelTiepointTag: [0 0 0 3.0791e+05 5.9588e+06 0]
           GeoKeyDirectoryTag: [1×32 double]
            GeoAsciiParamsTag: 'WGS84 / UTM zone 55S|WGS 84|'
                  GDAL_NODATA: '-10000'

Can some tell me how to handle this situation? Thanks a lot.

It will auto-convert unless you specify CV_LOAD_IMAGE_ANYDEPTH or similar. See the exact same question (which I will shamlessly copy and paste into this field) on their official help site.

Mat test1(1000, 1000, CV_16U, Scalar(400));
imwrite("test.tiff", test1);
Mat test2 = imread("stam.tiff", CV_LOAD_IMAGE_ANYDEPTH);
cout << test1.depth() << " " << test2.depth() << endl;
cout << test2.at<unsigned short>(0,0) << endl;

http://answers.opencv.org/question/19272/unable-to-properly-read-16bit-tiff-image/?answer=19278#post-id-19278

If that doesn't work, try using libtiff directly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM