Working with Images: resize and crop in C++ with OpenCV
In modern application development, especially in the fields of computer vision, web scraping, or media file processing, there is often a need to programmatically resize and crop images. The C++ language combined with the powerful OpenCV library provides all the necessary tools for this. In this article, we will detail how to perform these operations, what parameters affect the quality of the result, and provide ready-made code examples.
The OpenCV Library — the de facto standard for image processing in C++
OpenCV (Open Source Computer Vision Library) is a cross-platform open-source library containing hundreds of optimized algorithms for image and video processing. It is written in C++ and has bindings for Python, Java, and other languages. For C++, OpenCV is the most performant and functional solution.
Key advantages of OpenCV for resize and crop tasks:
- High speed — all functions use SIMD instructions and multithreading.
- Flexibility — support for various interpolation methods (bilinear, bicubic, Lanczos, etc.).
- Convenience — working with images via the
cv::Matclass, which automatically manages memory.
Installing OpenCV for C++
To get started, you need to install the library. The most common method is via the vcpkg package manager or building from source. Example installation via vcpkg:
git clone https://github.com/Microsoft/vcpkg.gitcd vcpkg./bootstrap-vcpkg.sh./vcpkg install opencv4After installation, link the library in your CMakeLists.txt:
find_package(OpenCV REQUIRED)target_link_libraries(your_project ${OpenCV_LIBS})Resizing an Image (resize)
The cv::resize() function allows you to change the dimensions of the source image to a specified width and height. Syntax:
cv::resize( InputArray src, // source image OutputArray dst, // destination image Size dsize, // new size (width, height) double fx = 0, // scale factor along X (if dsize is not specified) double fy = 0, // scale factor along Y int interpolation = INTER_LINEAR // interpolation method);The interpolation parameter determines the algorithm used to compute new pixels. Main options:
INTER_NEAREST— nearest neighbor method (fast, but low quality).INTER_LINEAR— bilinear interpolation (good balance of speed and quality).INTER_CUBIC— bicubic interpolation (higher quality, but slower).INTER_LANCZOS4— Lanczos interpolation (best quality for upscaling).
Example of upscaling an image by 2x with bicubic interpolation:
#include <opencv2/opencv.hpp>#include <iostream>
int main() { cv::Mat src = cv::imread("input.jpg"); if (src.empty()) { std::cerr << "Failed to load image" << std::endl; return -1; }
cv::Mat dst; cv::resize(src, dst, cv::Size(), 2.0, 2.0, cv::INTER_CUBIC);
cv::imwrite("output_resized.jpg", dst); std::cout << "Image upscaled and saved" << std::endl; return 0;}Cropping an Image (crop)
OpenCV does not have a separate function for cropping. Instead, the Region of Interest (ROI) mechanism is used — selecting a rectangular area within the image. This is done using the () operator or the cv::Mat::operator() method.
Cropping syntax