OpenCV C++ Tutorial: Resizing an Image
Resizing an image is a fundamental operation in image processing and computer vision. In this tutorial, we'll explore how to resize an image in OpenCV using C++. The process is straightforward, and by the end, you'll have a clear understanding of how to achieve this step-by-step.
Why Resize Images?
Resizing is often necessary to standardize image dimensions for machine learning models, reduce storage requirements, or adjust the image to fit a specific display size. Understanding how to resize an image is essential for both beginners and advanced users.
Step-by-Step Guide to Resizing an Image
Here is a simple code snippet to resize an image in OpenCV:
// Define the size of the resized image
int ColumnsOfNewImage = 60; // Width of the new image
int RowsOfNewImage = 60; // Height of the new image
// Create a Mat to store the resized image
Mat Resized;
// Perform the resizing operation
resize(Input, Resized, Size(ColumnsOfNewImage, RowsOfNewImage));
The resize
function takes three parameters:
Input
: The original image.Resized
: The output Mat where the resized image is stored.Size
: The new size of the image, specified as Size(width, height)
Bonus Opencv resize tip to keep aspect ratio
To resize an image while preserving its aspect ratio, you can calculate the new dimensions based on the original size:
int newWidth = 100;
int newHeight = Input.rows * newWidth / Input.cols;
resize(Input, Resized, Size(newWidth, newHeight));
Exercise Opencv resize with Lenna
Boring same face again and again in image processing.
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);
waitKey(1000);
// Same the result from LoadedImage to Step1.JPG
imwrite("Step1.JPG", LoadedImage);
// Saved Image looks like original :)
// 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).
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.
//!! 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);
// Save
imwrite("Step4.JPG", LoadedImage);
//All the steps are saved in Step1 Step
}
Conclusion
Resizing an image in OpenCV with C++ is a simple yet useful and common operation. By mastering this basic technique, you can manipulate images for various applications. Experiment with different sizes and explore additional features of OpenCV.
Thank you very much for post..
www.cute-it.com
Thank you for the info
not working
Works, Ahoj Jaroslave, funguje;). S čím mas přesně problém a co ti to hází ? To nejak vyresime.
Excellent tutorial, I lerned to much with Mat ROI tutorial and with this too.
I hope to see a book for those practices...
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,
Every word of this blog helps me to give detail to me.
คาสิโนออนไลน์
c++ programming snippets
cplusplus code - Program for Priority Queue
หลายๆท่านมักเจอะเจออย่างงี้แน่ๆจ้ะ ด้วยเหตุว่าข้าราชการรับรถยนต์พบบ๊อยบ่อยครั้งเช่นเดียวกัน จะต้องใช้รถยนต์ในวันสองภายหน้านี้แล้ว แม้กระนั้นยังไม่จองรถยนต์เลย ค้ำประกันเลยแพงมั่นใจๆรวมทั้งปัญหาที่ลูกค้าจะพบตามมามันก็คือ รถเช่าเต็ม แต่ว่าก็ยังมีทางออกนะคะ ทำให้คุณจำต้องใช้รถเช่ารุ่นใหญ่ขึ้น โน่นก็แปลว่าชำระค่าเช่าแพงที่กว่า และก็อีกปัญหา เป็นการส่งคูปองตัวจริงด้วยไปรษณีย์ EMS จะส่งมาไม่ทัน จำต้องเดินทางมารับคูปองด้วยตัวเอง
เช่ารถ
เช่ารถกรุงเทพ
เช่ารถราคาถูก
เช่ารถรายเดือน
เช่ารถขับเอง
You are making we r proud! Keep up the great work! Thanks for inspiring us.
joker123
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