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 Lenna

Original Mat

Resized 100x100

100x100 Fixed

Better Resize Ratio

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.

Explore More Tutorials

Next Post Previous Post
11 Comments
  • Unknown
    Unknown June 29, 2016 at 9:03 AM

    Thank you very much for post..


    www.cute-it.com

  • Varuna
    Varuna April 21, 2017 at 1:45 PM

    Thank you for the info

  • Unknown
    Unknown May 11, 2017 at 9:23 PM

    not working

    • Vl
      Vl May 13, 2017 at 7:35 AM

      Works, Ahoj Jaroslave, funguje;). S čím mas přesně problÃĐm a co ti to hÃĄzí ? To nejak vyresime.

  • Unknown
    Unknown August 30, 2017 at 2:20 AM

    Excellent tutorial, I lerned to much with Mat ROI tutorial and with this too.
    I hope to see a book for those practices...

  • Unknown
    Unknown March 6, 2018 at 11:33 AM

    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,


  • thanida
    thanida November 29, 2018 at 5:18 AM

    Every word of this blog helps me to give detail to me.
    āļ„āļēāļŠิāđ‚āļ™āļ­āļ­āļ™āđ„āļĨāļ™์

  • trustno1
    trustno1 January 31, 2019 at 11:55 AM

    c++ programming snippets
    cplusplus code - Program for Priority Queue

  • supersonic100g9
    supersonic100g9 May 2, 2019 at 4:15 AM

    āļŦāļĨāļēāļĒāđ†āļ—่āļēāļ™āļĄัāļāđ€āļˆāļ­āļ°āđ€āļˆāļ­āļ­āļĒ่āļēāļ‡āļ‡ี้āđāļ™่āđ†āļˆ้āļ° āļ”้āļ§āļĒāđ€āļŦāļ•ุāļ§่āļēāļ‚้āļēāļĢāļēāļŠāļāļēāļĢāļĢัāļšāļĢāļ–āļĒāļ™āļ•์āļžāļšāļš๊āļ­āļĒāļš่āļ­āļĒāļ„āļĢั้āļ‡āđ€āļŠ่āļ™āđ€āļ”ีāļĒāļ§āļัāļ™ āļˆāļ°āļ•้āļ­āļ‡āđƒāļŠ้āļĢāļ–āļĒāļ™āļ•์āđƒāļ™āļ§ัāļ™āļŠāļ­āļ‡āļ āļēāļĒāļŦāļ™้āļēāļ™ี้āđāļĨ้āļ§ āđāļĄ้āļāļĢāļ°āļ™ั้āļ™āļĒัāļ‡āđ„āļĄ่āļˆāļ­āļ‡āļĢāļ–āļĒāļ™āļ•์āđ€āļĨāļĒ āļ„้āļģāļ›āļĢāļ°āļัāļ™āđ€āļĨāļĒāđāļžāļ‡āļĄั่āļ™āđƒāļˆāđ†āļĢāļ§āļĄāļ—ั้āļ‡āļ›ัāļāļŦāļēāļ—ี่āļĨูāļāļ„้āļēāļˆāļ°āļžāļšāļ•āļēāļĄāļĄāļēāļĄัāļ™āļ็āļ„ืāļ­ āļĢāļ–āđ€āļŠ่āļēāđ€āļ•็āļĄ āđāļ•่āļ§่āļēāļ็āļĒัāļ‡āļĄีāļ—āļēāļ‡āļ­āļ­āļāļ™āļ°āļ„āļ° āļ—āļģāđƒāļŦ้āļ„ุāļ“āļˆāļģāļ•้āļ­āļ‡āđƒāļŠ้āļĢāļ–āđ€āļŠ่āļēāļĢุ่āļ™āđƒāļŦāļ่āļ‚ึ้āļ™ āđ‚āļ™่āļ™āļ็āđāļ›āļĨāļ§่āļēāļŠāļģāļĢāļ°āļ„่āļēāđ€āļŠ่āļēāđāļžāļ‡āļ—ี่āļāļ§่āļē āđāļĨāļ°āļ็āļ­ีāļāļ›ัāļāļŦāļē āđ€āļ›็āļ™āļāļēāļĢāļŠ่āļ‡āļ„ูāļ›āļ­āļ‡āļ•ัāļ§āļˆāļĢิāļ‡āļ”้āļ§āļĒāđ„āļ›āļĢāļĐāļ“ีāļĒ์ EMS āļˆāļ°āļŠ่āļ‡āļĄāļēāđ„āļĄ่āļ—ัāļ™ āļˆāļģāļ•้āļ­āļ‡āđ€āļ”ิāļ™āļ—āļēāļ‡āļĄāļēāļĢัāļšāļ„ูāļ›āļ­āļ‡āļ”้āļ§āļĒāļ•ัāļ§āđ€āļ­āļ‡
    āđ€āļŠ่āļēāļĢāļ–
    āđ€āļŠ่āļēāļĢāļ–āļāļĢุāļ‡āđ€āļ—āļž
    āđ€āļŠ่āļēāļĢāļ–āļĢāļēāļ„āļēāļ–ูāļ
    āđ€āļŠ่āļēāļĢāļ–āļĢāļēāļĒāđ€āļ”ืāļ­āļ™
    āđ€āļŠ่āļēāļĢāļ–āļ‚ัāļšāđ€āļ­āļ‡

  • murakami
    murakami November 17, 2019 at 2:28 PM

    You are making we r proud! Keep up the great work! Thanks for inspiring us.
    joker123

  • nira
    nira November 25, 2019 at 8:55 AM

    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

Add Comment
comment url