Share
Visit

Monday, December 7, 2015

Opencv C++ Tutorial Mat resize

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);
waitKey(1000);
// 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). 
waitKey(1000);
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);
waitKey(1000);

 // 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);
 waitKey(1000);
   
                    
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


9 comments:

  1. Thank you very much for post..


    www.cute-it.com

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

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

    ReplyDelete
  4. 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,


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

    ReplyDelete

ad