Autonomous Tech./Installation
[Windows] OpenCV 3 설치 방법 (Visual Studio 2019)
Engineeus
2021. 7. 9. 18:25
728x90
OpenCV 다운로드
- https://opencv.org/releases/ 접속
- 3.x 버전 중 제일 최신 말고 그 이전 것 다운로드 (제일 최신은 오류가 있을 수도 있습니다.)
- 폴더 생성 후 압축 풀기
- 파일 생성 확인
Visual Studio 2019 환경 구축
- 새 프로젝트 만들기
- 빈프로젝트
- 프로젝트 이름 설정
- 소스파일 폴더 우클릭 후 새파일 생성
- debug mode, 64bit로 설정
- 오른쪽 솔루션탐색기의 opencv_test1의 프로젝트 파일 클릭
- 위 상단의 프로젝트 - 속성
- 속성 페이지가 열리면 모든구성 - 활성(x64)
※OpenCV 버전마다 위 3번은 다릅니다. [C:\opencv\opencv\build\x64\vc14\lib]에 들어가 보면 'opencv_world***.lib'와 'opencv_world***d.lib'가 있으니 확인 해보시고 알맞게 적으시면 됩니다.
시스템 환경 변수 설정
- 시스템 환경 변수 편집
샘플 코드 돌려보기
/**
@file videocapture_basic.cpp
@brief A very basic sample for using VideoCapture and VideoWriter
@author PkLab.net
@date Aug 24, 2016
*/
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
int main(int, char**)
{
Mat frame;
//--- INITIALIZE VIDEOCAPTURE
VideoCapture cap;
// open the default camera using default API
// cap.open(0);
// OR advance usage: select any API backend
int deviceID = 0; // 0 = open default camera
int apiID = cv::CAP_ANY; // 0 = autodetect default API
// open selected camera using selected API
cap.open(deviceID, apiID);
// check if we succeeded
if (!cap.isOpened()) {
cerr << "ERROR! Unable to open camera\n";
return -1;
}
//--- GRAB AND WRITE LOOP
cout << "Start grabbing" << endl
<< "Press any key to terminate" << endl;
for (;;)
{
// wait for a new frame from camera and store it into 'frame'
cap.read(frame);
// check if we succeeded
if (frame.empty()) {
cerr << "ERROR! blank frame grabbed\n";
break;
}
// show live and wait for a key with timeout long enough to show images
imshow("Live", frame);
if (waitKey(5) >= 0)
break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}