# Tutorial: Create OpenCV Project

## Prepare Project Workspace

Create the lecture workspace as **C:\Users\yourID\source\repos\DLIP**

* e.g. `C:\Users\ykkim\source\repos\DLIP`

Then, create sub-directories as :

* `C:\Users\yourID\source\repos\DLIP\Tutorial`
* `C:\Users\yourID\source\repos\DLIP\Include`



## Save Property Sheet

Copy or move the OpenCV setup property sheets you have created in the **Include** folder.

* `C:\Users\yourID\source\repos\DLIP\Include`

**Property Sheets**

* opencv-4.9.0\_debug\_x64.props
* opencv-4.9.0\_release\_x64.props&#x20;

If you don't have property sheets, then  follow the instruction:

{% content-ref url="../../installation-guide/opencv/opencv-install" %}
[opencv-install](../../installation-guide/opencv/opencv-install)
{% endcontent-ref %}



## Create  a New Project in VS

Create a new C++ project in Visual Studio Community

* 새 프로젝트 만들기 > 빈 프로젝트(C++)

Project Name and Location

* Project Name: `DLIP_OpenCV_Simple_Demo`
* Project Folder: `~\DLIP\Tutorial\`

<figure><img src="https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2F2EAeBkz4RBWC0wh2HaLd%2Fimage.png?alt=media&#x26;token=b1b27e3f-23f6-4356-a3ea-7ba994ec8c11" alt=""><figcaption></figcaption></figure>

## Setup of Project Property &#x20;

Include the already created OpenCV property sheets

**(Debug x64)**

* **VS 메뉴바**:  `보기>다른 창>속성 관리자`  선택
* **속성 관리자 탭**:  `프로젝트명 > Debugx64`  RightClick.
* `기존 속성 시트 추가` 선택 후 저장된 " **opencv-4.9.0\_debug\_x64.props "**  추가
  * It should be located in "`~\DLIP\Include\"`

<figure><img src="https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2FsJPwYEw8ps4gX6iXY2rh%2Fimage.png?alt=media&#x26;token=6b20cb5f-1bb3-400d-a2e7-85219e53521b" alt=""><figcaption></figcaption></figure>

**(Release x64)**

* **속성 관리자 탭**:  `프로젝트명 > Releasex64`  RightClick.
* `기존 속성 시트 추가` 선택 후 저장된 " **opencv-4.9.0\_release\_x64.props "**  추가

![](https://github.com/ykkimhgu/DLIP_doc/assets/84508106/cebf8418-4f40-4f23-b9a8-1c94df38de0a)

### Create Demo  Program

Create a new C+ source file

* File Name: `DLIP_OpenCV_Simple_Demo.cpp`
* 솔루션탐색기 탭:  \[프로젝트] > 소스 파일 > 추가 > 새항목 > C++파일(cpp) 선택&#x20;

<figure><img src="https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2FfLau4Lvwue1qNa9uQnX1%2Fimage.png?alt=media&#x26;token=59d34426-31b5-4400-a0ff-b7f8f4b384b5" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2FnahGhpxVgU9GCzlhuB53%2Fimage.png?alt=media&#x26;token=4fed60c2-5022-44b1-bd45-826bc9f5cd6f" alt=""><figcaption></figcaption></figure>

구성 관리자를 **Debug x64**로 설정&#x20;

<figure><img src="https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2FbDswT2M5LSm1EHhWayAF%2Fimage.png?alt=media&#x26;token=43ce4d49-9454-4ba7-989d-4b13a44ee9ac" alt=""><figcaption></figcaption></figure>

Run the following demo program. You can run the program by pressing  (CTRL+F5)



**Demo 1**: Image File Read

* 이미지 파일 다운로드: [Click here](https://github.com/ykkimhgu/DLIP-src/blob/main/tutorial-install/testImage.JPG)

> 이미지 파일과 소스코드가 동일 폴더에 있어야 함!!

```cpp
#include <opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

//* @function main
int main()
{
Mat src;

src = imread("testImage.jpg", 1);/// Load an image

if (src.empty())/// Load image check
{
cout << "File Read Failed : src is empty" << endl;
waitKey(0);
}

/// Create a window to display results
namedWindow("DemoWIndow", WINDOW_AUTOSIZE); //WINDOW_AUTOSIZE(1) :Fixed Window, 0: Unfixed window

if (!src.empty())imshow("DemoWIndow", src); // Show image

waitKey(0);//Pause the program
return 0;
}
```

Expected Output

![](https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2Fgit-blob-f37b41c3f5e4de871c778a0299ec7be7884bee80%2Fimage.png?alt=media)

***

**Demo  2:**  Video Cam capture

```cpp
#include "opencv.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    VideoCapture cap(0); // open the video camera no. 0

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video cam" << endl;
        return -1;
    }
   namedWindow("MyVideo",WINDOW_AUTOSIZE); //create a window called "MyVideo"

    while (1)
    {
        Mat frame;
        bool bSuccess = cap.read(frame); // read a new frame from video
         if (!bSuccess) //if not success, break loop
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }
        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
       {
            cout << "esc key is pressed by user" << endl;
            break; 
       }
    }
    return 0;
}
```

Expected Output

<figure><img src="https://3883264845-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MR8tEAjhiC8uN1kHR2J%2Fuploads%2F2QqCcOarv0xIorZmmGNu%2Fimage.png?alt=media&#x26;token=9da6f71b-4469-4532-a3bb-653ea3ae950c" alt=""><figcaption></figcaption></figure>
