问题
#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;}
When i compile this code the error: CImg::load(): Failed to recognize format of the file "lena.jpg" shows up.Any suggestion?
I installed the imageMagick but the error still happens.
回答1:
To enable native JPG file support in CImg, put this before including CImg.h
:
#define cimg_use_jpeg
#include "CImg.h"
....
and link your code with the libjpeg
library. It works flawlessly for me.
If you don't use this, CImg will try to do an external call to ImageMagick's convert tool to load the file, which is not the cleanest solution. Using libjpeg inside CImg is definitely better.
That works the same for other image formats (tiff, png, ...).
回答2:
Have you tried any other image files other then "lena.jpg"? Is "lena.jpg" in the same directory than the current program? What compiler you using?
Does this example work (wouldn't really make sense if it did though)?
#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;
}
Source: http://cimg.eu/reference/group__cimg__tutorial.html
I noticed the documentation says it only supports jpg's if imageMagick is installed, perhaps you did something wrong there and it isn't properly installed?
EDIT:
Does this work?
#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;
}
回答3:
What worked for me was using the absolute path to the file, rather than just the file name:
Ex. Change this: CImg image("lena.jpg"), visu(500,400,1,3,0);
to this: CImg image("C:\Users\youruser\Desktop\lena.jpg"), visu(500,400,1,3,0);
Of course, this path would differ depending on where you have your file.
来源:https://stackoverflow.com/questions/32390318/cimg-failed-to-recognize-the-jpg-format