【问题标题】:Compressing images on opencv (imwrite). How to explicitly set the compression factor?在 opencv (imwrite) 上压缩图像。如何显式设置压缩因子?
【发布时间】:2011-11-06 10:04:02
【问题描述】:

我想知道是否有一种方法可以在 opencv 上压缩图像时轻松指定压缩因子,而无需声明虚拟向量。如果我声明一个向量 p(类似于 this discussion),但只包含 2 个项目,这是 imwrite 需要的,我可以拨打电话:

vector<int> p(2);
p[0] = CV_IMWRITE_JPEG_QUALITY;
p[1] = 50; // compression factor

imwrite("compressed.jpg", img, p);

以上工作正常。但是,我想在一个循环中使用多个压缩因子压缩相同的图像。有没有办法将参数显式传递给 imwrite?比如:

imwrite("compressed.jpg", img, {CV_IMWRITE_JPEG_QUALITY, factor}); // this doesn't work

顺便说一句,函数头是:

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

谢谢!

更新: 激活 C++0x 后,我可以将内联显式定义的向量传递给函数。

【问题讨论】:

  • 这是在 gcc 版本 4.5.2 的 VM 上运行
  • 如果启用 C++0x 支持,可以说std::vector&lt;int&gt;({1,2}) inline。
  • 是的,就是这样;)谢谢
  • 将您的更新放入回答并标记为“已回答”。

标签: c++ image graphics vector opencv


【解决方案1】:

正如建议的那样,激活 C++0x 允许我将明确定义的内联向量传递给函数。这解决了问题。

【讨论】:

    【解决方案2】:
    vector<int> compression_params;
    compression_params.push_back(IMWRITE_JPEG_QUALITY);
    compression_params.push_back(30);
    compression_params.push_back(IMWRITE_JPEG_PROGRESSIVE);
    compression_params.push_back(1);
    compression_params.push_back(IMWRITE_JPEG_OPTIMIZE);
    compression_params.push_back(1);
    compression_params.push_back(IMWRITE_JPEG_LUMA_QUALITY);
    compression_params.push_back(30);
    imwrite('sample.jpg', img, compression_params);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      • 2016-03-09
      相关资源
      最近更新 更多