opencv保存16bit图片

来源:互联网 发布:linux find 子目录 编辑:程序博客网 时间:2024/05/23 10:49

原文:http://blog.163.com/gz_ricky/blog/static/18204911820115203045235/

这几天找了一下怎么存取OpenCV图像的东西,发现网上挺多人在讨论怎么保存其他格式的图像呢!毕竟8UC3格式的范围太小了,in particular,对于Kinect捕获的原始深度信息(mm为单位),存成8位的精度会降低。不过网上不知道是因为这东西太简单还是怎么的,都找不到解决方法……于是就找了一点资料了。


http://opencv.willowgarage.com/documentation/cpp/reading_and_writing_images_and_video.html
Mat imread(const string& filename, int flags=1)

Loads an image from a file.

Parameters:
  • filename – Name of file to be loaded.
  • flags –

    Specifies color type of the loaded image:

    • >0 the loaded image is forced to be a 3-channel color image
    • =0 the loaded image is forced to be grayscale
    • <0 the loaded image will be loaded as-is (note that in the current implementation the alpha channel, if any, is stripped from the output image, e.g. 4-channel RGBA image will be loaded as RGB if flags\ge0 ).

The function imread loads an image from the specified file and returns it. If the image can not be read (because of missing file, improper permissions, unsupported or invalid format), the function returns empty matrix ( Mat::data==NULL ).Currently, the following file formats are supported:

  • Windows bitmaps - *.bmp, *.dib (always supported)
  • JPEG files - *.jpeg, *.jpg, *.jpe (see Note2 )
  • JPEG 2000 files - *.jp2 (see Note2 )
  • Portable Network Graphics - *.png (see Note2 )
  • Portable image format - *.pbm, *.pgm, *.ppm (always supported)
  • Sun rasters - *.sr, *.ras (always supported)
  • TIFF files - *.tiff, *.tif (see Note2 )

Note1 : The function determines type of the image by the content, not by the file extension.

Note2 : On Windows and MacOSX the shipped with OpenCV image codecs (libjpeg, libpng, libtiff and libjasper) are used by default; so OpenCV can always read JPEGs, PNGs and TIFFs. On MacOSX there is also the option to use native MacOSX image readers. But beware that currently these native image loaders give images with somewhat different pixel values, because of the embedded into MacOSX color management.

On Linux, BSD flavors and other Unix-like open-source operating systems OpenCV looks for the supplied with OS image codecs. Please, install the relevant packages (do not forget the development files, e.g. “libjpeg-dev” etc. in Debian and Ubuntu) in order to get the codec support, or turn on OPENCV_BUILD_3RDPARTY_LIBS flag in CMake.

cv::imwrite

Comments from the Wiki

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

Saves an image to a specified file.

Parameters:
  • filename – Name of the file.
  • img – The image to be saved.
  • params –

    The format-specific save parameters, encoded as pairs paramId_1,paramValue_1, paramId_2, paramValue_2, ... . The following parameters are currently supported:

    • In the case of JPEG it can be a quality ( CV_IMWRITE_JPEG_QUALITY), from 0 to 100 (the higher is the better), 95 by default.
    • In the case of PNG it can be the compression level (CV_IMWRITE_PNG_COMPRESSION ), from 0 to 9 (the higher value means smaller size and longer compression time), 3 by default.
    • In the case of PPM, PGM or PBM it can a binary format flag (CV_IMWRITE_PXM_BINARY ), 0 or 1, 1 by default.

The function imwrite saves the image to the specified file. The image format is chosen based on the filename extension, see imread for the list of extensions. Only 8-bit (or 16-bit in the case of PNG, JPEG 2000 and TIFF) single-channel or 3-channel (with ‘BGR’ channel order) images can be saved using this function. If the format, depth or channel order is different, use Mat::convertTo , andcvtColor to convert it before saving, or use the universal XML I/O functions to save the image to XML or YAML format.


主要是这两个函数后面的参数了,对于jpg格式保存下来的图片默认会变成8位BGR三通道格式的,不过对于PNG可保存16位的,而且PNG无损压缩,很适合啊~ 所以直接用png就好了。至于TIF格式的据说很严格很规范,也没有细去研究了。

使用imsave就直接定义png为后缀就能保存16bit了,当然后面可以加上参数。

读取的时候要注意,imread要定义为mat=imread("xxx.png",CV_LOAD_IMAGE_ANYCOLOR|CV_LOAD_IMAGE_ANYDEPTH);

这样读出来才是原来的16bit,不然会转化成默认的8uc3的。

0 0