OPENCV输出单色TIFF组4压缩

发布于 2025-01-22 04:05:01 字数 1065 浏览 1 评论 0原文

有没有办法在第4组压缩中输出OpenCV中的单色TIFF文件?

这是使用ImageMagick/GraphicsMagick

'gm convert '.$file.' -type bilevel -monochrome -compress group4 '.$output

OpenCV版本4.5.1

Update> Update

# g++ -Wall -O3 -std=c++17 main.cpp -o main `pkg-config opencv4 --cflags --libs` -ltiff

查找不返回任何

root@x:/usr/include/opencv4/opencv2# find . -name "*tif*"

代码

#include <tiffio.h>
#include <opencv4/opencv2/opencv.hpp>

std::vector<int> params = {cv::IMWRITE_TIFF_COMPRESSION, COMPRESSION_CCITTFAX4};
cv::imwrite(_output, src_bin, params);

错误

的命令

[WARN:0] Global ../modules/imgcodecs/src/grfmt_tiff.cpp(914) Writelibtiff Opencv Tiff(第914行):失败的Tiffsetfield(tif, tifftag_predictor,preditiveor)imwrite _('out.tif'):无法写入数据: OPENCV(4.5.1)../modules/imgcodecs/src/grfmt_tiff.cpp:914:错误: (-2:未指定的错误)OpenCV TIFF:失败TiffsetField(Tif, tifftag_predictor,预测指标)在函数'Writelibtiff'

Is there a way to output monochrome TIFF files in OpenCV with group 4 compression?

This is the command to do it with imagemagick/graphicsmagick

'gm convert '.$file.' -type bilevel -monochrome -compress group4 '.$output

OpenCV version 4.5.1

update

# g++ -Wall -O3 -std=c++17 main.cpp -o main `pkg-config opencv4 --cflags --libs` -ltiff

find doesn't return anything

root@x:/usr/include/opencv4/opencv2# find . -name "*tif*"

code

#include <tiffio.h>
#include <opencv4/opencv2/opencv.hpp>

std::vector<int> params = {cv::IMWRITE_TIFF_COMPRESSION, COMPRESSION_CCITTFAX4};
cv::imwrite(_output, src_bin, params);

error

[ WARN:0] global ../modules/imgcodecs/src/grfmt_tiff.cpp (914)
writeLibTiff OpenCV TIFF(line 914): failed TIFFSetField(tif,
TIFFTAG_PREDICTOR, predictor) imwrite_('out.tif'): can't write data:
OpenCV(4.5.1) ../modules/imgcodecs/src/grfmt_tiff.cpp:914: error:
(-2:Unspecified error) OpenCV TIFF: failed TIFFSetField(tif,
TIFFTAG_PREDICTOR, predictor) in function 'writeLibTiff'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走就滚别墨迹 2025-01-29 04:05:01

OPENCV使用libtiff link )。如果您调用cv.imwrite)您可以传递其他参数。

对于第4组,压缩文档说您必须通过imwrite_tiff_compression flag(link) with value COMPRESSION_CCITTFAX4 (

示例:

#include <tiffio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

vector<int> params = {IMWRITE_TIFF_COMPRESSION, COMPRESSION_CCITTFAX4};

bool success = imwrite("image.tif", img, params);

更新

好,这就是应该工作的方式。但是,OpenCV4目前似乎在编写4组TIFF方面存在一个或多个问题。顺便说一句,当您尝试Python-lib时,情况是一样的。 这个已知的问题 处理一个预测标记,该标签被OpenCV错误地添加。我将修复程序应用于我的本地构建,但是在此期间又出现了一个问题,缺少将8位垫子转换为Group4的1bit/示例。因此,我的建议是:

解决方法

相反,您可以使用graphicsMagick api magick ++

#include <opencv2/opencv.hpp>
#include <Magick++.h>

using namespace cv;
using namespace Magick;

int main(int argc, char* argv[]) {
        InitializeMagick("");

        Mat img = imread("img.png");
        Image image(img.cols, img.rows, "BGR", CharPixel, (char *)img.data);
        image.compressType(CompressionType::Group4Compression);
        image.write("img.tif");
        return(0);
}

OpenCV uses libtiff (link). If you call cv.imwrite (link) you can pass additional parameters.

For group 4 compression the docs say that you have to pass the IMWRITE_TIFF_COMPRESSION flag (link) with value COMPRESSION_CCITTFAX4 (link).

Example:

#include <tiffio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

vector<int> params = {IMWRITE_TIFF_COMPRESSION, COMPRESSION_CCITTFAX4};

bool success = imwrite("image.tif", img, params);

Update

Well, this is how it should work. But it seems that opencv4 currently has one or more issues in writing Group 4 TIFF. Btw it's the same when you try the python-lib. This known issue deals with a predictor tag that is wrongly added by opencv. I applied the fix to my local build but after that one more issue came up missing a conversion of the 8bit Mat to 1bit/sample for group4. So my recommendation is:

Workaround

Instead you can use the GraphicsMagick API Magick++.

#include <opencv2/opencv.hpp>
#include <Magick++.h>

using namespace cv;
using namespace Magick;

int main(int argc, char* argv[]) {
        InitializeMagick("");

        Mat img = imread("img.png");
        Image image(img.cols, img.rows, "BGR", CharPixel, (char *)img.data);
        image.compressType(CompressionType::Group4Compression);
        image.write("img.tif");
        return(0);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文