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.

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);

waitKey(1000);

// Same the result from LoadedImage to Step1.JPG

imwrite("Step1.JPG", LoadedImage);

// Saved Image looks like original :)

// 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).

waitKey(1000);

// 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);

waitKey(1000);

// Yes it is smaller than source. 100x100 image

//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.