Opencv C++ Tutorial Mat resize

Resize the Mat or Image in the Opencv C++ tutorial. It is obviously a simple task and important to learn. This tutorial is visualized step by step and well-described each of them. The main trick is in that simple code.

Mat Input;
Mat Resized;
int ColumnOfNewImage = 60;
int RowsOfNewImage = 60;
resize(Input, Resized, Size(ColumnOfNewImage,RowsOfNewImage));

This code just takes an Input image and resized save to output Mat. How big is the resized image is based on the Size? Size just contains two parameters. Simple numbers of how the result should be big. The simple number of columns (width) and rows (height). That is basically it. Enjoy

                                                Boring same face again and again. 
Opencv Mat Tutorial

Load Image, resize, and save image Opencv C++

Let's go through the commented code. 

#include <Windows.h>
#include "opencv2\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include "opencv2\video\background_segm.hpp"
#include "opencv2\video\tracking.hpp"

using namespace cv;
using namespace std;

int main(int argc, const char** argv)
  //  Load the image from file
 Mat LoadedImage;
  // Just loaded image Lenna.png from project dir to LoadedImage Mat
 LoadedImage = imread("Lenna.png", IMREAD_COLOR);
  //I would like to visualize Mat step by step to see the result immediately.
  // Show what is in the Mat after load
 namedWindow("Step 1 image loaded", WINDOW_AUTOSIZE);
 imshow("Step 1 image loaded", LoadedImage);
  // Same the result from LoadedImage to Step1.JPG
 imwrite("Step1.JPG", LoadedImage);
  // Saved Image looks like original :)
Opencv Mat tutorial

 // You can load colored images directly as grayscale
 LoadedImage = imread("Lenna.png", CV_LOAD_IMAGE_GRAYSCALE);
 // Show what is in the Mat after load
 namedWindow("Step 2 gray image loaded", WINDOW_AUTOSIZE);
 imshow("Step 2 gray image loaded", LoadedImage);
 // Show the result for a longer time.
 // If you want to see video frames in high rates in the loop jist put here waitKey(20).
Opencv Mat tutorial

 // Same the result from LoadedImage to Step2.JPG
 imwrite("Step2.JPG", LoadedImage);
  //  Basic resize and rescale
 // Resize LoadedImage and save the result to same Mat loaded Image.
 // You can also resize( loadedImage, Result, ..... )

 // Load again source images
 LoadedImage = imread("Lenna.png", IMREAD_COLOR);
 //You can resize to any size you want Size(width,heigth)
 resize(LoadedImage, LoadedImage, Size(100, 100));
 // Vizualization
 namedWindow("Step 3 image resize", WINDOW_AUTOSIZE);
 imshow("Step 3 image resize", LoadedImage);

 // Yes it is smaller than source. 100x100 image
Opencv Mat Resize
 //Save above image to Step3.jpg
 imwrite("Step3.JPG", LoadedImage);
 LoadedImage = imread("Lenna.png", IMREAD_COLOR);

 // Better is resize based on ratio of width and heigth
 // Width and heigth are 2 times smaller than original source image
 // result will be saved into same mat. If you are confused by this.
 // You can try to modify the code and add MAT outputImage and dysplay it.
 //!! cols number of collumn of the image mat. and rows are rows
// cols and rows are same ase width and heigth
 resize(LoadedImage, LoadedImage, Size(LoadedImage.cols/2, LoadedImage.rows/2));
 // Vizualization
 namedWindow("Step 4 image resize better", WINDOW_AUTOSIZE);
 imshow("Step 4 image resize better", LoadedImage);
Opencv Mat Resize
//Yes it is 2 times smaller then source
 // Save
 imwrite("Step4.JPG", LoadedImage);
 //All the steps are saved in Step1 Step
See you soon

Next Post Previous Post
  • Unknown
    Unknown June 29, 2016 at 12:03 AM

    Thank you very much for post..

  • Varuna
    Varuna April 21, 2017 at 4:45 AM

    Thank you for the info

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

    not working

    • Vl
      Vl May 12, 2017 at 10:35 PM

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

  • Unknown
    Unknown August 29, 2017 at 5:20 PM

    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 2: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 28, 2018 at 8:18 PM

    Every word of this blog helps me to give detail to me.

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

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

  • supersonic100g9
    supersonic100g9 May 1, 2019 at 7:15 PM

    หลายๆท่านมักเจอะเจออย่างงี้แน่ๆจ้ะ ด้วยเหตุว่าข้าราชการรับรถยนต์พบบ๊อยบ่อยครั้งเช่นเดียวกัน จะต้องใช้รถยนต์ในวันสองภายหน้านี้แล้ว แม้กระนั้นยังไม่จองรถยนต์เลย ค้ำประกันเลยแพงมั่นใจๆรวมทั้งปัญหาที่ลูกค้าจะพบตามมามันก็คือ รถเช่าเต็ม แต่ว่าก็ยังมีทางออกนะคะ ทำให้คุณจำต้องใช้รถเช่ารุ่นใหญ่ขึ้น โน่นก็แปลว่าชำระค่าเช่าแพงที่กว่า และก็อีกปัญหา เป็นการส่งคูปองตัวจริงด้วยไปรษณีย์ EMS จะส่งมาไม่ทัน จำต้องเดินทางมารับคูปองด้วยตัวเอง

  • murakami
    murakami November 17, 2019 at 5:28 AM

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

  • nira
    nira November 24, 2019 at 11:55 PM

    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