Image Processing in C++: CImg, OpenCV, and Magick++ Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Images in C++: Library Overview and Practical Examples



Digital image processing is one of the key tasks in modern programming. In C++, there are several powerful libraries that allow you to load, modify, analyze, and save images. In this article, we will look at three of the most popular tools: CImg (lightweight and simple), OpenCV (the industry standard for computer vision), and GraphicsMagick (for high-performance conversion).



1. CImg — A Lightweight Library for Basic Operations



CImg (The C++ Template Image Processing Library) is a header-only library that does not require installing additional DLLs. It is ideal for educational projects and rapid prototyping. It supports BMP, PNG, JPEG, TIFF formats (via external codecs).



Installation

Simply download the CImg.h file and include it in your project. For JPEG/PNG support, you will also need libjpeg, libpng, and zlib.



Example: Loading, Color Inversion, and Saving

#include "CImg.h"using namespace cimg_library;

int main() { // Load the image CImg<unsigned char> img("input.jpg"); // Invert colors (pixel = 255 - pixel) CImg<unsigned char> inverted = 255 - img; // Apply Gaussian blur (radius 3) CImg<unsigned char> blurred = img.get_blur(3); // Save the results inverted.save("inverted.jpg"); blurred.save("blurred.jpg"); return 0;}


2. OpenCV — A Powerful Framework for Computer Vision



OpenCV (Open Source Computer Vision Library) is the de facto standard for recognition, object detection, and complex image processing tasks. It supports hundreds of algorithms, video processing, and neural networks. It is cross-platform.



Installation

On Ubuntu: sudo apt install libopencv-dev. On Windows — via vcpkg or the official installer.



Example: Edge Detection (Canny) and Resizing

#include <opencv2/opencv.hpp>using namespace cv;

int main() { // Read the image Mat img = imread("photo.jpg"); if (img.empty()) return -1; // Convert to grayscale Mat gray; cvtColor(img, gray, COLOR_BGR2GRAY); // Edge detection using the Canny algorithm Mat edges; Canny(gray, edges, 50, 150); // Resize to 300x300 Mat resized; resize(img, resized, Size(300, 300)); // Save and display imwrite("edges.jpg", edges); imshow("Original", img); waitKey(0); return 0;}


3. GraphicsMagick — Fast Conversion and Batch Processing



GraphicsMagick is a fork of ImageMagick focused on stability and performance. It is used for mass conversion, resizing, and watermarks. It works with over 200 formats.



Installation

Via package manager: sudo apt install graphicsmagick. For C++, the libgraphicsmagick++-dev library is required.



Example: Image Reduction and Conversion to PNG

#include <Magick++.h>using namespace Magick;

int main() { InitializeMagick(nullptr); // Load the image Image image("input.tiff"); // Scale to 800 pixels wide image.scale(Geometry("800x")); // Rotate 90 degrees image.rotate(90); // Set JPEG quality (if output format is JPEG) image.quality(85); // Save as PNG image.write("output.png"); return 0;}


Library Comparison

  • CImg — easy start, minimal

Recommendations