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

[C++ ๋กœ OpenCV ๊ตฌํ˜„ํ•˜๊ธฐ] (1) Read Images, Videos and Webcam ๋ณธ๋ฌธ

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

[C++ ๋กœ OpenCV ๊ตฌํ˜„ํ•˜๊ธฐ] (1) Read Images, Videos and Webcam

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

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

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

 

๐ŸŒ€ Read Images, Videos and Webcam

๐Ÿ’ง Importing Images

#include <opencv2/imgcodecs.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;

// #1. Read Images Videos and Webcams
// 1) Importing Images
void main() {
	// Mat์€ ์ด๋ฏธ์ง€๋ฅผ ๋‹ด์„ ๊ฐ์ฒด์ด๋‹ค. ํ–‰๋ ฌ ๊ตฌ์„ฑ
	string path = "Resources/alpaca.jpg";
	Mat img = imread(path);
	imshow("img", img);
	waitKey(0);
}

 

๐Ÿ’ง Importing Videos 

#include <opencv2/imgcodecs.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;

// 2) Importing Video
void main() {
	string path = "Resources/eddy.mp4";
	// VideoCapture '๋ณ€์ˆ˜ ์ด๋ฆ„'( "๋™์˜์ƒ ์ด๋ฆ„" ) : ๋™์˜์ƒ ํ™”๋ฉด(frame)์„ ์ฝ์Œ
	VideoCapture cap(path);
	Mat video;

	while (true) {
		cap.read(video);
		imshow("Video", video);
		// ํ‚ค ์ž…๋ ฅ์„ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋Œ€๊ธฐ ํ•จ์ˆ˜
		// 0 : ๋ฌดํ•œ ๋Œ€๊ธฐ
		// ms(๋ฐ€๋ฆฌ์„ธ์ปจ) ๋‹จ์œ„์˜ ์‹œ๊ฐ„ : ํ•ด๋‹น ์‹œ๊ฐ„๋งŒํผ ๋Œ€๊ธฐ (1000ms = 1์ดˆ)
		waitKey(20);
	}
}

 

๐Ÿ’ง Importing Webcam

#include <opencv2/imgcodecs.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) Importing Webcam
int main(int argv, char** argc) {
	// ๋‚ด์žฅ ์›น์บ ์ด ์žˆ๊ณ , ํ™œ์„ฑํ™”๊ฐ€ ๋˜์–ด์žˆ๋Š” ๊ฒฝ์šฐ ๋‚ด์žฅ ์›น์บ ์˜ ์นด๋ฉ”๋ผ ๋ฒˆํ˜ธ๋Š” '0' 
	// ์›น์บ ์„ ์ถ”๊ฐ€ํ•  ๊ฒฝ์šฐ ์นด๋ฉ”๋ผ ๋ฒˆํ˜ธ๋Š” '1' 
	VideoCapture cap(0);
	
	// ์นด๋ฉ”๋ผ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ฝ์–ด์„œ ๋ชจ๋‘ Matํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๋Š”๋ฐ, RGB๊ฐ€ ์•„๋‹Œ BGR๋กœ ์ฝ์–ด์˜ด
	Mat webcam;

	while (1) {
		// ์žฌ์ƒ๋˜๋Š” ๋น„๋””์˜ค์˜ ํ•œ frame์”ฉ ์ฝ์–ด์„œ true ๋ฐ˜ํ™˜
		cap.read(webcam);
		// Read๋ฅผ ํ†ตํ•ด ์ฝ์–ด์˜จ ํ”„๋ ˆ์ž„์„ ํ™”๋ฉด์— Displayํ•˜๋Š” ํ•จ์ˆ˜.
		imshow("Webcam", webcam);
		// ํ‚ค ์ž…๋ ฅ์„ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋Œ€๊ธฐ ํ•จ์ˆ˜
		// 0 : ๋ฌดํ•œ ๋Œ€๊ธฐ
		// ms(๋ฐ€๋ฆฌ์„ธ์ปจ) ๋‹จ์œ„์˜ ์‹œ๊ฐ„ : ํ•ด๋‹น ์‹œ๊ฐ„๋งŒํผ ๋Œ€๊ธฐ (1000ms = 1์ดˆ)
		waitKey(1);
	}
	return 0;
}
#include <opencv2/imgcodecs.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) Importing Webcam
int main(int, char**)
{
	Mat frame; // OpenCV์—์„œ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ด ๋˜๋Š” Matrix ๊ตฌ์กฐ์ฒด(์ด๋ฏธ์ง€๋ฅผ ์ฝ์–ด ํ•ด๋‹น ์ •๋ณด๋ฅผ Matํ˜•ํƒœ๋กœ ๋ณ€ํ™˜)
	VideoCapture cap; // ๋™์˜์ƒ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
	cap.open(0); // ๋™์˜์ƒ ์—ด๊ธฐ(Camera ์—ด๊ธฐ) + ์นด๋ฉ”๋ผ๋ฒˆํ˜ธ(0(๋‚ด์žฅ ์šฐ์„ ))
	if (!cap.isOpened())
	{
		cout << "Error! Cannot open the camera" << endl;
		return -1;
	}
	while (1)
	{
		cap.read(frame); // ๋น„๋””์˜ค์˜ ํ•œ ํ”„๋ ˆ์ž„์”ฉ read
		imshow("LIVE", frame); // ํ”„๋ ˆ์ž„์„ ํ™”๋ฉด์— display
		if (waitKey(5) >= 0) // 5๋งŒํผ ํ‚ค์ž…๋ ฅ์„ ๋Œ€๊ธฐํ•˜๊ณ , ๋ฐœ์ƒ์‹œ ๋ฐ˜ํ™˜
			break;
	}
	return 0;
}

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