Opencv C++ Tutorial Mat resize
A comprehensive guide to scaling images, maintaining aspect ratios, and understanding interpolation.
Resizing an image is more than just changing its dimensions; it is a fundamental building block of Computer Vision. Whether you are preparing datasets for Deep Learning, optimizing web performance, or building a UI, mastering the cv::resize function in OpenCV is essential.
In this tutorial, we will move beyond the basics to explore interpolation algorithms and how to scale images without losing quality.
What You'll Learn:
✅ The
cv::resize()Function Syntax✅ Scaling by Fixed Dimensions vs. Scale Factors
✅ Maintaining Perfect Aspect Ratio
✅ Choosing the Right Interpolation (Area, Linear, Cubic)
✅ Full Implementation Code
The cv::resize Function
In OpenCV, resizing is handled by a single, powerful function. Here is the modern C++ signature:
void resize(InputArray src, OutputArray dst, Size dsize,
double fx = 0, double fy = 0, int interpolation = INTER_LINEAR);src / dst: The source (input) and destination (output) image matrices.
dsize: The target size (width, height). If this is zero, fx and fy are used.
fx / fy: Scale factors (e.g., 0.5 for half size).
ð ️ Interactive Aspect Ratio Tool
Input your original image size to find the new dimensions while keeping the ratio.
x ➔ Target Width: New Height: -
Pro Tip: Choosing the Right Interpolation
Not all "resizes" are created equal. Depending on whether you are shrinking or enlarging, you should change the interpolation parameter:
Method Best For... INTER_NEAREST Fastest, but pixelated. Good for masks. INTER_LINEAR Default. Good balance of speed/quality. INTER_AREA Best for Downsampling (shrinking). Avoids moirÃĐ patterns. INTER_CUBIC Best for Upsampling (enlarging). Higher quality than linear.
Full C++ Implementation
This modernized code demonstrates how to load an image, display it, and perform different types of resizing.
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main() {
// 1. Load the original image
Mat img = imread("Lenna.png", IMREAD_COLOR);
if(img.empty()) {
cout << "Could not read the image" << endl;
return 1;
}
// 2. Simple Resize (Fixed Size: 100x100)
Mat imgFixed;
resize(img, imgFixed, Size(100, 100), 0, 0, INTER_LINEAR);
// 3. Scale by Ratio (e.g., 50% of original)
Mat imgScaled;
resize(img, imgScaled, Size(), 0.5, 0.5, INTER_AREA);
// 4. Maintain Aspect Ratio (Fixed Width of 300px)
int newWidth = 300;
double scale = (double)newWidth / img.cols;
int newHeight = cvRound(img.rows * scale);
Mat imgAspect;
resize(img, imgAspect, Size(newWidth, newHeight), 0, 0, INTER_CUBIC);
// Visualization
imshow("Original", img);
imshow("Fixed 100x100", imgFixed);
imshow("Scaled 0.5x", imgScaled);
imshow("Maintained Aspect", imgAspect);
waitKey(0);
return 0;
}Visual Comparison: Lenna Resize

Original Mat
100x100 Fixed
0.5x Scale Ratio
Ready to level up your Computer Vision skills?
Resizing is just the beginning. Check out our other tutorials on Image Thresholding and Edge Detection to start building your own AI applications.
Thank you very much for post..
www.cute-it.com
Thank you for the info
not working
Works, Ahoj Jaroslave, funguje;). S ÄÃm mas pÅesnÄ problÃĐm a co ti to hÃĄzà ? To nejak vyresime.
Excellent tutorial, I lerned to much with Mat ROI tutorial and with this too.
I hope to see a book for those practices...
Nice tutorial,I appreciate you for sharing this knowledge.Thank you so much for the examples.Its very helpful for me and newbies.I learned much .Have a look on yii2 development company,
Every word of this blog helps me to give detail to me.
āļāļēāļŠิāđāļāļāļāļāđāļĨāļ์
c++ programming snippets
cplusplus code - Program for Priority Queue
āļŦāļĨāļēāļĒāđāļ่āļēāļāļĄัāļāđāļāļāļ°āđāļāļāļāļĒ่āļēāļāļี้āđāļ่āđāļ้āļ° āļ้āļ§āļĒāđāļŦāļุāļ§่āļēāļ้āļēāļĢāļēāļāļāļēāļĢāļĢัāļāļĢāļāļĒāļāļ์āļāļāļ๊āļāļĒāļ่āļāļĒāļāļĢั้āļāđāļ่āļāđāļีāļĒāļ§āļัāļ āļāļ°āļ้āļāļāđāļ้āļĢāļāļĒāļāļ์āđāļāļ§ัāļāļŠāļāļāļ āļēāļĒāļŦāļ้āļēāļี้āđāļĨ้āļ§ āđāļĄ้āļāļĢāļ°āļั้āļāļĒัāļāđāļĄ่āļāļāļāļĢāļāļĒāļāļ์āđāļĨāļĒ āļ้āļģāļāļĢāļ°āļัāļāđāļĨāļĒāđāļāļāļĄั่āļāđāļāđāļĢāļ§āļĄāļั้āļāļัāļāļŦāļēāļี่āļĨูāļāļ้āļēāļāļ°āļāļāļāļēāļĄāļĄāļēāļĄัāļāļ็āļืāļ āļĢāļāđāļ่āļēāđāļ็āļĄ āđāļ่āļ§่āļēāļ็āļĒัāļāļĄีāļāļēāļāļāļāļāļāļ°āļāļ° āļāļģāđāļŦ้āļุāļāļāļģāļ้āļāļāđāļ้āļĢāļāđāļ่āļēāļĢุ่āļāđāļŦāļ่āļึ้āļ āđāļ่āļāļ็āđāļāļĨāļ§่āļēāļāļģāļĢāļ°āļ่āļēāđāļ่āļēāđāļāļāļี่āļāļ§่āļē āđāļĨāļ°āļ็āļีāļāļัāļāļŦāļē āđāļ็āļāļāļēāļĢāļŠ่āļāļูāļāļāļāļัāļ§āļāļĢิāļāļ้āļ§āļĒāđāļāļĢāļĐāļีāļĒ์ EMS āļāļ°āļŠ่āļāļĄāļēāđāļĄ่āļัāļ āļāļģāļ้āļāļāđāļิāļāļāļēāļāļĄāļēāļĢัāļāļูāļāļāļāļ้āļ§āļĒāļัāļ§āđāļāļ
āđāļ่āļēāļĢāļ
āđāļ่āļēāļĢāļāļāļĢุāļāđāļāļ
āđāļ่āļēāļĢāļāļĢāļēāļāļēāļูāļ
āđāļ่āļēāļĢāļāļĢāļēāļĒāđāļืāļāļ
āđāļ่āļēāļĢāļāļัāļāđāļāļ
You are making we r proud! Keep up the great work! Thanks for inspiring us.
joker123
I very like reading through a post that can make people thinks. Also, many thanks for permitting me to comment!Thanks for sharing these tips,bk8