Open Source C++ Library for Image Processing
Apply Image Filters, Create, Manipulate & Render Popular Images file formats using Free C++ API.
CImg Library is an open source library that provides useful features for loading, saving, displaying, and processing various types of images inside C++ applications. The CImg is a very lightweight and user-friendly library. The good thing is that it avoids handling complex dependencies and library compatibility issues. It is made of a single header file CImg.h that must be included in your C++ source. It helps developers by performing complex image processing activities in just a few lines of code.
The API supports advanced features like handling 3D images, Transforming images, image filtering, image animation, Image Binarization, and more. CImg library is very portable & self-contained. It is can be easily used on different operating systems with ease. Moreover, it’s also very compatible with numerous C++ compilers such as Visual C++, ICC, G++, etc.
Getting Started with CImg
The CImg Library is available as .zip package which is platform-independent. It contains all the required files, along with various examples, which show how to use the library functions and classes.
You need to add these two lines in your C++ source code, in order to be able to work with CImg.
Add these lines for CImg working
#include "CImg.h"
using namespace cimg_library
Get the Latest Version of CImg via Git
git clone --depth=1 https://github.com/GreycLab/CImg.git
C++ API to Create & Modify Images
CImg open-source library allows C++ developers to create and manipulate different types of images inside their own applications. It also supports how to handle image display and mouse events. First of all, you need to include the main and only header files of the CImg library. The good thing is the library reduces the developer's load by allowing them to write a very small amount of code. Please also note that the source will perfectly work on UNIX and Windows systems.
Create Image via C++ library
#include "CImg.h"
using namespace cimg_library;
int main() {
CImg 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;
}
Image Filtering Support
CImg library provides support for the image filtering process. Sometimes we need to retrieve information about images and that’s where Image filtering is commonly used. The image filtering process is one of the most common methods to apply to images to retrieve information. Mostly, filters are used in image noise removal, computer image derivatives, image edge enhancement, Shape analysis, and more.
Apply Fourier Filtering in C++ Apps
void* item_fourier_filtering() {
const CImg img = CImg(data_milla,211,242,1,3).RGBtoYCbCr().channel(0).resize(256,256);
CImgList F = img.get_FFT();
cimglist_apply(F,shift)(img.width()/2,img.height()/2,0,0,2);
const CImg mag = ((F[0].get_pow(2) + F[1].get_pow(2)).sqrt() + 1).log().normalize(0,255);
CImgList visu(img,mag);
CImgDisplay disp(visu,"[#16] - Fourier Filtering (Click to set filter)");
CImg mask(img.width(),img.height(),1,1,1);
const unsigned char one[] = { 1 }, zero[] = { 0 }, white[] = { 255 };
int rmin = 0, rmax = 256;
while (!disp.is_closed() && !disp.is_keyQ() && !disp.is_keyESC()) {
disp.wait();
const int
xm = disp.mouse_x()*2*img.width()/disp.width() - img.width(),
ym = disp.mouse_y()*img.height()/disp.height(),
x = xm - img.width()/2,
y = ym - img.height()/2;
if (disp.button() && xm>=0 && ym>=0) {
const int r = (int)std::max(0.0f,(float)std::sqrt((float)x*x + y*y) - 3);
if (disp.button()&1) rmax = r;
if (disp.button()&2) rmin = r;
if (rmin>=rmax) rmin = std::max(rmax - 1,0);
mask.fill(0).draw_circle(mag.width()/2,mag.height()/2,rmax,one).
draw_circle(mag.width()/2,mag.height()/2,rmin,zero);
CImgList nF(F);
cimglist_for(F,l) nF[l].mul(mask).shift(-img.width()/2,-img.height()/2,0,0,2);
visu[0] = nF.FFT(true)[0].normalize(0,255);
}
if (disp.is_resized()) disp.resize(disp.window_width(),disp.window_width()/2).display(visu);
visu[1] = mag.get_mul(mask).draw_text(5,5,"Freq Min/Max = %d / %d",white,zero,0.6f,13,(int)rmin,(int)rmax);
visu.display(disp);
}
return 0;
}