将 16 位单通道图像写入 Tiff
Writing 16 bit single channel image into Tiff
>我正在尝试以tiff文件格式保存图像。我使用 libraw 从相机读取原始数据,它给了我无符号的短数据。我已经对数据进行了一些操作,我想将结果保存为 16 位灰度(1 通道)图像,具有 Tiff 文件格式。但结果只是一个空白的图像。即使我使用保留原始拜耳图像的缓冲区,它也不会正确保存。这是我用于保存的代码:
// Open the TIFF file
if((output_image = TIFFOpen("image.tiff", "w")) == NULL){
std::cerr << "Unable to write tif file: " << "image.tiff" << std::endl;
}
TIFFSetField(output_image, TIFFTAG_IMAGEWIDTH, width());
TIFFSetField(output_image, TIFFTAG_IMAGELENGTH, height());
TIFFSetField(output_image, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(output_image, TIFFTAG_BITSPERSAMPLE, 16);
TIFFSetField(output_image, TIFFTAG_ROWSPERSTRIP, 1);
TIFFSetField(output_image, TIFFTAG_ORIENTATION, (int)ORIENTATION_TOPLEFT);
TIFFSetField(output_image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(output_image, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
TIFFSetField(output_image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
// Write the information to the file
tsize_t image_s;
if( (image_s = TIFFWriteEncodedStrip(output_image, 0, &m_data_cropped[0], width()*height())) == -1)
{
std::cerr << "Unable to write tif file: " << "image.tif" << std::endl;
}
else
{
std::cout << "Image is saved! size is : " << image_s << std::endl;
}
TIFFWriteDirectory(output_image);
TIFFClose(output_image);
看起来您的代码中有两个问题。
-
您正在尝试通过一次调用
TIFFWriteEncodedStrip
来写入整个映像,但同时将TIFFTAG_ROWSPERSTRIP
设置为1
(在这种情况下应将其设置为height()
)。 -
您正在向
TIFFWriteEncodedStrip
传递错误的值。最后一个参数是以字节为单位的条带长度,您显然是以像素为单位传递长度的。
我不确定 &m_data_cropped[0]
参数是否指向整个图像的第一个字节,因此您可能也想检查此参数的正确性。
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 平均图像时图像损坏
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 如何在 OpenCV c++ 中压缩 TIFF 格式的图像?
- 是否可以通过每次在内存中仅保存一个平铺来处理完整的平铺 tiff 图像?
- 将 16 位单通道图像写入 Tiff
- 用c++读取tiff图像
- 带有GUI的C++程序,可使用远程放置的相机进行拍摄并读取未压缩的TIFF图像
- 将 tiff 图像转换为字符串,以作为二进制应用程序/八位字节流 (C++) 发布
- c++ 32位tiff图像保存和加载
- 我如何将TIFF图像数据传递给JUCE(不支持TIFF)
- 用于将JPEG图像转换为TIFF格式的C, c++或Objective-C代码
- 处理大型TIFF图像C++/Magick/libtiff
- 访问TIFF图像
- 读取tiff图像tesseract和leptonica
- 在内存中计算TIFF图像大小[C/ c++]