Recording 16-bit uncompressed images using OpenCV

How to save a 16-bit image (PNG or TIFF) without compression? What is the syntax?

+5
source share
5 answers

After 2 years, OpenCV can save 16-bit TIFFs (version 2.4.x) with compression (LZW). But it also supports the undocumented parameter TIFFTAG_COMPRESSION in the function

bool imwrite(const string& filename, InputArray img, const vector<int>& params=vector<int>() )

It can be set as COMPRESSION_LZMA for example (and it works correctly).

You can use COMPRESSION_NONE as the value for this parameter (you must also set the TIFFTAG_ROWSPERSTRIP parameter for the image height).

But OpenCV still generates the wrong TIFF in this case (trying to set TIFFTAG_COMPRESSION to COMPRESSION_NONE) even in version 2.4.9.

OpenCV 2 :

  • OpenCV "opencv/modules/imgcodecs/src/grfmt_tiff.cpp" - OpenCV , , libtiff TIFF, .

    if ( !TIFFSetField(pTiffHandle, TIFFTAG_IMAGEWIDTH, width) || !TIFFSetField(pTiffHandle, TIFFTAG_IMAGELENGTH, height) || !TIFFSetField(pTiffHandle, TIFFTAG_BITSPERSAMPLE, bitsPerChannel) || !TIFFSetField(pTiffHandle, TIFFTAG_COMPRESSION, compression) || !TIFFSetField(pTiffHandle, TIFFTAG_PHOTOMETRIC, colorspace) || !TIFFSetField(pTiffHandle, TIFFTAG_SAMPLESPERPIXEL, channels) || !TIFFSetField(pTiffHandle, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG) || !TIFFSetField(pTiffHandle, TIFFTAG_ROWSPERSTRIP, rowsPerStrip) || !TIFFSetField(pTiffHandle, TIFFTAG_PREDICTOR, predictor) ) { TIFFClose(pTiffHandle); return false; }

    if ( !TIFFSetField(pTiffHandle, TIFFTAG_IMAGEWIDTH, width) || !TIFFSetField(pTiffHandle, TIFFTAG_IMAGELENGTH, height) || !TIFFSetField(pTiffHandle, TIFFTAG_BITSPERSAMPLE, bitsPerChannel) || !TIFFSetField(pTiffHandle, TIFFTAG_COMPRESSION, compression) || !TIFFSetField(pTiffHandle, TIFFTAG_PHOTOMETRIC, colorspace) || !TIFFSetField(pTiffHandle, TIFFTAG_SAMPLESPERPIXEL, channels) || !TIFFSetField(pTiffHandle, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG) || !TIFFSetField(pTiffHandle, TIFFTAG_ROWSPERSTRIP, rowsPerStrip) ) { TIFFClose(pTiffHandle); return false; } if (compression != COMPRESSION_NONE && !TIFFSetField(pTiffHandle, TIFFTAG_PREDICTOR, predictor) ) { TIFFClose(pTiffHandle); return false; }

    OpenCV TIFF ( imwrite).

  • libtiff TIFF. , . libtiff, (libtiff make MinGW MSVC, 32 64 ).

+4

OpenCV, cvSaveImage 8- BGR-. . 2.0 docs.

libpng, libMagick ++ .

libpng PNG. RGB 16 , bit_depth = 16 color_type = PNG_COLOR_TYPE_RGB.

() , png++.

: OpenCV, , 16bpc- . . p.streef. .

+2

, 2.1 ( ) cvSaveImage 16b. .png .

16- "somedata":

IplImage* image = cvCreateImage(cvSize(100,100),16,1);
memset(image->imageData,someData,image->width*image->height*2);
cvSaveImage("image.png",image);
+2

, OpenCV 16 ( 3- ). pgm , ( ).

0

, - , , pull https://github.com/opencv/opencv/pull/3744,

- ( 3.2):

cv::Mat shorts(1024*cv::Mat::ones(100, 100, CV_16UC1));
vector<int> tags = {TIFFTAG_COMPRESSION, COMPRESSION_NONE};

bool success = cv::imwrite("uncompressed.tif", shorts, tags);
0

All Articles