CImg:无法识别jpg格式

时间:2015-09-04 04:51:56

标签: c++ windows codeblocks cimg

#include <iostream>
#include <stdlib.h>
#include "CImg.h"

using namespace cimg_library;
using namespace std;

int main(){
CImg<unsigned char> image("lena.jpg"), visu(500,400,1,3,0);

const unsigned char red[] = { 255,0,0 }, green[] = { 0,255,0 }, blue[] = { 0,0,255 };



return 0;}

当我编译此代码时出现错误:CImg :: load():无法识别文件的格式&#34; lena.jpg&#34;出现了。有什么建议吗?

我安装了imageMagick,但错误仍然存​​在。

3 个答案:

答案 0 :(得分:6)

要在CImg中启用本机JPG文件支持,请在包含CImg.h

之前添加此文件
#define cimg_use_jpeg
#include "CImg.h"
....

并将您的代码与libjpeg库相关联。它对我来说完美无瑕。 如果你不使用它,CImg将尝试外部调用ImageMagick的转换工具来加载文件,这不是最干净的解决方案。在CImg中使用libjpeg肯定更好。 对于其他图像格式(tiff,png,...)也是如此。

答案 1 :(得分:0)

您是否尝试过“lena.jpg”以外的任何其他图像文件? “lena.jpg”与当前程序在同一目录中吗?你使用什么编译器?

这个例子是否有效(如果它确实有效,那么它真的没有意义吗?)

#include "CImg.h"
using namespace cimg_library;
int main() {
  CImg<unsigned char> image("lena.jpg"), visu(500,400,1,3,0);
  const unsigned char red[] = { 255,0,0 }, green[] = { 0,255,0 }, blue[] = { 0,0,255 };
  image.blur(2.5);
  CImgDisplay main_disp(image,"Click a point"), draw_disp(visu,"Intensity profile");
  while (!main_disp.is_closed() && !draw_disp.is_closed()) {
    main_disp.wait();
    if (main_disp.button() && main_disp.mouse_y()>=0) {
      const int y = main_disp.mouse_y();
      visu.fill(0).draw_graph(image.get_crop(0,y,0,0,image.width()-1,y,0,0),red,1,1,0,255,0);
      visu.draw_graph(image.get_crop(0,y,0,1,image.width()-1,y,0,1),green,1,1,0,255,0);
      visu.draw_graph(image.get_crop(0,y,0,2,image.width()-1,y,0,2),blue,1,1,0,255,0).display(draw_disp);
      }
    }
  return 0;
}

来源:http://cimg.eu/reference/group__cimg__tutorial.html

我注意到文档说如果安装了imageMagick它只支持jpg,也许你在那里做错了而且没有正确安装?

编辑:

这有用吗?

#include "CImg.h"
using namespace cimg_library;
int main() {
  CImg<unsigned char> img(640,400,1,3);  // Define a 640x400 color image with 8 bits per color component.
  img.fill(0);                           // Set pixel values to 0 (color : black)
  unsigned char purple[] = { 255,0,255 };        // Define a purple color
  img.draw_text(100,100,"Hello World",purple); // Draw a purple "Hello world" at coordinates (100,100).
  img.display("My first CImg code");             // Display the image in a display window.
  return 0;
}

答案 2 :(得分:0)

对我有用的是使用文件的绝对路径,而不仅仅是文件名:

例如更改此: CImg image(“ lena.jpg”),visu(500,400,1,3,0);

对此: CImg image(“ C:\ Users \ youruser \ Desktop \ lena.jpg ”),visu(500,400,1,3,0);

当然,此路径会根据文件的存储位置而有所不同。

相关问题