4

我是 C++ 编程新手,今天我试图使用 CImg 保存图像。CImg 是 C++ 模板图像处理库。

我写的基本代码是(请原谅任何语法错误,作为我代码的复制部分):

#include "CImg.h"// Include CImg library header.
#include <iostream>

using namespace cimg_library;
using namespace std;

const int screen_size = 800;

//-------------------------------------------------------------------------------
//  Main procedure
//-------------------------------------------------------------------------------
int main()
{

CImg<unsigned char> img(screen_size,screen_size,1,3,20);
CImgDisplay disp(img, "CImg Tutorial");   
//Some drawing using img.draw_circle( 10, 10, 60, RED);
img.save("result.jpg"); // save the image    
return 0;   
}

但是我无法运行我的程序,因为它说:

Invalid Parameter - 100%
'gm.exe' is not recognized as an internal or external command,
operable program or batch file.

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

[CImg] *** CImgIOException *** [instance(800,800,1,3,02150020,non-shared)] CImg<unsigned char>::save_other() : Failed to save file 'result.jpg'. Format is not natively supported, and no external commands succeeded.
terminate called after throwing an instance of 'cimg_library::CImgIOException'
  what():  [instance(800,800,1,3,02150020,non-shared)] CImg<unsigned char>::save_other() : Failed to save file 'result.jpg'. Format is not natively supported, and no external commands succeeded.

虽然我可以看到图像,但我无法保存它。谷歌搜索了一下后,我发现有人说要安装 ImageMagick,我已经安装了它但没有帮助。一些论坛说要针对 libjpeg、libpng、libmagick++ 进行编译。但我不知道如何针对这些库进行编译。我正在使用 Eclipse CDT 插件编写 C++ 项目。请帮我 。

4

3 回答 3

6

我遇到了同样的错误,安装 GraphicsMagick(不是 ImageMagick)对我有帮助。

我已经从ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/windows/下载并安装了 GraphicsMagick-1.3.26-Q8-win64-dll.exe 。如果需要,您可以选择另一个:

请注意,提供行业标准 24/32 位像素的 QuantumDepth=8 版本 (Q8) 比为高分辨率颜色提供 48/64 位像素的 QuantumDepth=16 版本 (Q16) 消耗一半的内存和大约 30% 的 CPU . Q8 版本非常适合处理打算在计算机屏幕上查看的典型照片。如果您正在处理电影、科学或医学图像,使用 ICC 颜色配置文件,或者处理对比度有限的图像,则建议使用 Q16 版本。

重要提示:在安装过程中,不要删除复选框“更新可执行搜索路径”,它会更新环境变量 %PATH%,使其gm.exe在任何地方都可用。

就我而言,还需要安装 Ghostscript - GraphicsMagick 强烈建议安装它。有一个 x64 Ghostscript 的链接:https ://sourceforge.net/projects/ghostscript/files/GPL%20Ghostscript/9.09/gs909w64.exe/download (我把它放在这里,因为来自 GraphicMagick 网站的链接会引导你到仅限 32 位)。

在那之后,它对我来说很好。

于 2017-08-20T15:39:38.037 回答
3

对于某些图像格式(如.jpg.png.tif基本上所有需要数据压缩的格式),CImg 将尝试使用外部工具来保存它们(例如convert来自 ImageMagick 或gm来自 GraphicsMagick)。如果您没有安装任何文件,那么您将无法保存.jpg文件而无需将您的代码与 libjpeg 库链接,以获得对 JPEG 读/写的本机支持(然后,您需要#define cimg_use_jpegbefore #include "CImg.h",告诉库您要使用 libjpeg 功能)。

如果您想让事情变得更简单,我建议您使用另一种(非压缩)图像格式保存您的图像,如.bmp.ppm. 这些格式由 CImg 本地处理,不需要与外部库链接。

于 2013-09-11T07:44:03.873 回答
0

我知道这个问题很老了,但是我在一个项目上一直遇到同样的错误,而在另一个项目上却没有,这是谷歌唯一的问题。

要摆脱它,你必须做两件事:

  1. 为您的适当操作系统和架构(32/64)安装动态 ImageMagick 库。关联

  2. 我使用的是 VisualStudio,字符集必须设置为“Unicode”。当我恢复为多字节字符集时,该错误会再次出现。我想这与 CImg 处理字符串和错误比较它们的方式有关。

于 2016-07-05T04:00:00.357 回答