Working with Images in C++: Library Overview and Practical Examples
Image processing is one of the key tasks in modern programming, from computer vision to graphic editors. In C++, there are several powerful libraries that allow you to load, process, and save images. In this article, we will look at three popular tools: CImg, OpenCV, and stb_image. Each of them has its own features, advantages, and areas of application.
1. CImg Library
CImg (C++ Image Processing Library) is a lightweight, cross-platform library for image processing. It is distributed as a single header file (CImg.h), making it ideal for rapid prototyping and small projects. CImg supports BMP, JPEG, PNG, TIFF, and other formats, but requires additional libraries (e.g., libpng or libjpeg) to work with them.
Key Features:
- Loading, displaying, and saving images.
- Filters (blur, sharpening, median filter).
- Geometric transformations (rotation, scaling, flipping).
- Working with color spaces (RGB, HSV, Lab).
- Drawing primitives (lines, circles, text).
Example: Loading an image and applying blur
#include "CImg.h"using namespace cimg_library;
int main() { // Load the image CImg<unsigned char> image("input.jpg");
// Apply Gaussian blur (radius 3) CImg<unsigned char> blurred = image.get_blur(3);
// Save the result blurred.save("output_blurred.jpg");
return 0;}This code demonstrates CImg's minimalist approach: one line for loading, one for processing, and one for saving. The library automatically manages memory.
2. OpenCV Library
OpenCV (Open Source Computer Vision Library) is the de facto standard for computer vision and image processing tasks in C++. It includes over 2500 optimized algorithms, supports GPU acceleration, and is actively used in industrial solutions, robotics, and scientific research. OpenCV works with JPEG, PNG, TIFF, WebP, and many other formats.
Key Features:
- Loading, displaying, and recording video/images.
- Filtering, morphology, thresholding.
- Object detection (faces, cars) using Haar cascades or deep learning.
- Camera calibration, stereo vision.
- Machine learning (SVM, KNN, neural networks).
Example: Resizing and converting to grayscale
#include <opencv2/opencv.hpp>using namespace cv;
int main() { // Load a color image Mat image = imread("input.jpg", IMREAD_COLOR); if (image.empty()) { return -1; }
// Convert to grayscale Mat gray; cvtColor(image, gray, COLOR_BGR2GRAY);
// Reduce size by half Mat resized; resize(gray, resized, Size(), 0.5, 0.5, INTER_LINEAR);
// Save the result imwrite("output_gray_resized.jpg", resized);
return 0;}OpenCV uses the Mat class to represent images. All operations are performed through static functions or methods. The library requires prior installation but provides maximum flexibility and performance.
3. stb_image Library
stb_image is part of a set of single-header libraries by Sean Barrett. It is designed exclusively for loading and saving images, without processing functions. It supports JPEG, PNG, BMP, GIF, PS
Also in library
Image Processing in Java: Resizing and Cropping (resize, crop)
HTTP Client for Swift (iOS/macOS): Top Libraries for Networking
Generating PDF Reports in Rust: Libraries and Practical Examples
Working with ZIP/archives in TypeScript: a complete guide to JSZip and adm-zip