1

CImg: hello world

I implemented CImg hello world that loads an image, display on a CImg window, and save it. The complete code is available on my github.

#include <iostream>
#include <string>
#include <CImg.h>
int main()
{
std::cout << "run CImg hello world..." << std::endl;
cimg_library::CImg<unsigned char> img;
std::string strFileInput = "lena.pgm";
std::string strFileOutput = "lena.bmp";
img = cimg_library::CImg<unsigned char>(strFileInput.c_str());
std::cout << "load " << strFileInput << "..." << std::endl;
std::cout << "width = " << img.width() << std::endl;
std::cout << "height = " << img.height() << std::endl;
std::cout << "color = " << img.spectrum() << std::endl;
img.display("Input image");
img.save_bmp(strFileOutput.c_str());
return 0;
}
Advertisements
0

CImg: display image(s)

This code loads an image lena.bmp and shows the original image and its grayscaled one in a window, which shows text “Original image | Grayscale image” on its bar.

std::string strFile = "lena.bmp";
  
cimg_library::CImg<unsigned char> img1(strFile.c_str());
cimg_library::CImg<unsigned char> img2;
int color = _imgInput.spectrum(); // to check the number of color channels
switch(color)
{
    case 1:
        img2 = img1;
        break;
    case 3: // get 0-th color slice (Y color component) of YCbCr image format
        img2 = img1.get_RGBtoYCbCr().get_channel(0);
        break;
}
std::string strWindow = "Original image | Grayscale image";
(img1,img2).display(strWindow);
0

CImg: convert an RGB image to a grayscale image

This code loads a image lena.bmp and converts the image to grayscale one.

std::string strFile = "lena.bmp";
 
cimg_library::CImg<unsigned char> img1(strFile.c_str());
cimg_library::CImg<unsigned char> img2;
int color = _imgInput.spectrum(); // to check the number of color channels
switch(color)
{
    case 1:
        img2 = img1;
        break;
    case 3: // get 0-th color slice (Y color component) of YCbCr image format
        img2 = img1.get_RGBtoYCbCr().get_channel(0);
        break;
}