Opencv C++ Tutorial Mat resize


My favourite

  • Opencv tutorial people detection
  • Head people cascade download
  • Opencv tutorial optical flow
  • Opencv Video stabilization
  • Opencv car dataset download
  • Opencv tutorial Transparent mask
  • Opencv videowriter
  • Opencv FFMPEG
  • Opencv Canny edge and hough lines
  • Opencv Mat Resize 

     Resize the Mat or Image in Opencv C++ tutorial. It is obviously simple tast and important to learn. This tutorial is visualize 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 take a Input image and resized save to output Mat. How big is the resized image is based on the Size. Size just contain two parameters. Simple numbers how the result should be big. Simple number of column (width) and rows (height). That is basically it. Enjoy

                                                    Boring same face again and again. 
    Opencv Mat Tutorial

    Load Image, resize and save Opencv C++

    #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 image directly as gray scale
    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 the 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


    1. Thank you very much for post..

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



    Follow by Email

    Powered by Blogger.