๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[C++ ๋ก OpenCV ๊ตฌํํ๊ธฐ] (1) Read Images, Videos and Webcam ๋ณธ๋ฌธ
๐ฉ๐ป IoT (Embedded)/Image Processing
[C++ ๋ก OpenCV ๊ตฌํํ๊ธฐ] (1) Read Images, Videos and Webcam
์ง์ง์ํ์นด 2023. 6. 11. 23:45728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ 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
๋ฐ์ํ
'๐ฉโ๐ป IoT (Embedded) > Image Processing' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Comments