๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[C++ ๋กœ OpenCV ๊ตฌํ˜„ํ•˜๊ธฐ] (3) Resize and Crop ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป IoT (Embedded)/Image Processing

[C++ ๋กœ OpenCV ๊ตฌํ˜„ํ•˜๊ธฐ] (3) Resize and Crop

์ง•์ง•์•ŒํŒŒ์นด 2023. 6. 12. 01:23
728x90
๋ฐ˜์‘ํ˜•

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” Murtaza's Workshop ์˜ ์œ ํŠœ๋ธŒ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :-)>

=> LEARN OPENCV C++ in 4 HOURS | Including 3x Projects | Computer Vision

 

๐ŸŒ€ Resize and Crop

๐Ÿ’ง Resize

์ด๋ฏธ์ง€๋ฅผ ํ™•๋Œ€ ๋˜๋Š” ์ถ•์†Œ

cv2.resize ( img , dsize, fx, fy, interpolation )

 

img  : image file 

dsize : ๊ฐ€๋กœ, ์„ธ๋กœ ํ˜•ํƒœ์˜ ํŠœํ”Œ์„ ์ง€์ •ํ•˜์—ฌ, ํ™•๋Œ€ ๋ฐ ์ถ•์†Œ ๊ฐ€๋Šฅ ex) (100, 200)

fx : ๊ฐ€๋กœ ์‚ฌ์ด์ฆˆ์˜ ๋ฐฐ์ˆ˜ , 2๋ฐฐ ํ™•๋Œ€๋Š” 2.0 , 1/2 ์ถ•์†Œ๋Š” 0.5     ex

fy : ์„ธ๋กœ ์‚ฌ์ด์ฆˆ์˜ ๋ฐฐ์ˆ˜

interpolation : ๋ณด๊ฐ„๋ฒ• ( ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ด๋ฏธ์ง€ ๊ฐ’์„ ์ฑ„์šธ๊ฒƒ์ธ๊ฐ€ )

   INTER_NEAREST

   INTER_LINEAR

   INTER_AREAINTER_CUBIC

   INTER_LANCZOS4

#include <opencv2/opencv.hpp>		// OpenCV์—์„œ ์ง€์›ํ•˜๋Š” ๋ชจ๋“  ๊ธฐ๋Šฅ
#include <opencv2/videoio.hpp>		// ๋น„๋””์˜ค ์ถ”์  ๋ฐ ๋ฐฐ๊ฒฝ segmentation๊ณผ ๊ด€๋ จ๋œ ๋ฃจํ‹ด
#include <opencv2/imgcodecs.hpp>	// ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ์„ ์–ธ (Mat ์ด๋‚˜ Point๊ฐ€ ์„ ์–ธ, ํ–‰๋ ฌ ์—ฐ์‚ฐ ํ˜น์€ ๋ฒกํ„ฐ ์—ฐ์‚ฐ)
#include <opencv2/highgui.hpp>		// ์œˆ๋„์šฐ ํ™”๋ฉด, UI์ฒ˜๋ฆฌ(์Šฌ๋ผ์ด๋”, ๋ฒ„ํŠผ ๋“ฑ) ๋ฐ ๋งˆ์šฐ์Šค ์ œ์–ด ๊ฐ€๋Šฅ
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

// #3. Resize and Crop
// 1) Importing Images
void main() {
	// Mat์€ ์ด๋ฏธ์ง€๋ฅผ ๋‹ด์„ ๊ฐ์ฒด์ด๋‹ค. ํ–‰๋ ฌ ๊ตฌ์„ฑ
	string path = "Resources/alpaca.jpg";
	Mat img = imread(path);
	Mat imgResize;

	cout << img.size() << endl;		// [612 X 536]
	resize(img, imgResize, Size(300, 300));

	imshow("Image", img);
	imshow("Image Resize", imgResize);
	waitKey(0);
}

 

๐Ÿ’ง Rect roi

์›๋ณธ ์ด๋ฏธ์ง€์—์„œ ์›ํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ์ž˜๋ผ์„œ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ

์›ํ•˜๋Š” ํ”ผ์‚ฌ์ฒด(๋Œ€์ƒ)๋ฅผ ๋”ฐ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์˜์ƒ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด '๊ด€์‹ฌ์˜์—ญ ์„ค์ •' => RoI(Region of Interest)

 

src( Rect( x, y, width, height)) )

src : ๊ด€์‹ฌ์˜์—ญ์„ ์„ค์ •ํ•  ์›๋ณธ ์ด๋ฏธ์ง€

Rect( x, y, width, height) ) : ๊ด€์‹ฌ์˜์—ญ์„ ์„ค์ •ํ•  ์‚ฌ๊ฐํ˜•์˜ ์ขŒํ‘œ( x, y), ์‚ฌ๊ฐํ˜• ๋ฒ”์œ„ (width, height)

#include <opencv2/opencv.hpp>		// OpenCV์—์„œ ์ง€์›ํ•˜๋Š” ๋ชจ๋“  ๊ธฐ๋Šฅ
#include <opencv2/videoio.hpp>		// ๋น„๋””์˜ค ์ถ”์  ๋ฐ ๋ฐฐ๊ฒฝ segmentation๊ณผ ๊ด€๋ จ๋œ ๋ฃจํ‹ด
#include <opencv2/imgcodecs.hpp>	// ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ์„ ์–ธ (Mat ์ด๋‚˜ Point๊ฐ€ ์„ ์–ธ, ํ–‰๋ ฌ ์—ฐ์‚ฐ ํ˜น์€ ๋ฒกํ„ฐ ์—ฐ์‚ฐ)
#include <opencv2/highgui.hpp>		// ์œˆ๋„์šฐ ํ™”๋ฉด, UI์ฒ˜๋ฆฌ(์Šฌ๋ผ์ด๋”, ๋ฒ„ํŠผ ๋“ฑ) ๋ฐ ๋งˆ์šฐ์Šค ์ œ์–ด ๊ฐ€๋Šฅ
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

// #3. Resize and Crop
// 1) Importing Images
void main() {
	// Mat์€ ์ด๋ฏธ์ง€๋ฅผ ๋‹ด์„ ๊ฐ์ฒด์ด๋‹ค. ํ–‰๋ ฌ ๊ตฌ์„ฑ
	string path = "Resources/alpaca.jpg";
	Mat img = imread(path);
	Mat imgResize;
	Mat imgCrop;

	cout << img.size() << endl;		// [612 X 536]
	// Resize
	// : ์ด๋ฏธ์ง€๋ฅผ ํ™•๋Œ€ ๋˜๋Š” ์ถ•์†Œ
	resize(img, imgResize, Size(300, 300));
	
	// ROI (crop)
	// : ์›๋ณธ ์ด๋ฏธ์ง€์—์„œ ์›ํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ์ž˜๋ผ์„œ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ
	Rect roi(100, 100, 300, 250);
	imgCrop = img(roi);

	imshow("Image", img);
	imshow("Image Resize", imgResize);
	imshow("Image Crop", imgCrop);
	waitKey(0);
}

 

 

728x90
๋ฐ˜์‘ํ˜•
Comments