Opencv Visual Studio

1. Install Python3

Apr 19, 2020 OpenCV-4.2.0 for Visual Studio 2019 All the required steps for setting up OpenCV for a Visual C development. Step1: Install the C Desktop development Workload Open your Visual Studio Installer tool and add C for desktop development as a workload to your current Visual Studio IDE version.

Opencv Visual Studio

1.1 In Windows

Scientific programming in Python requires multiple third-party packages (like numpy), whose installing procedure needs complicated compiling tools. Those compiling tools are tricky in Windows. Anaconda is the most popular Python data science platform, which includes essential pre-compiled third-package packages. Thus, in Windows platform, we suggest installing Python by Anaconda.

  • Hi people, In this post I will show you how to add emgucv (A.net wrapper for opencv i.e. Basically opencv for c#) to visual studio. I am using emgucv version 2.4.10 and visual studio professional.
  • 4 hours ago¬† If I open python either separately from visual studio code or inside the vsc terminal and write import cv2 I don't get such errors. I have tried uninstalling.
  • How to use OpenCV FreeType module with Visual Studio - CMakeLists.txt.

Download and install Anaconda Python 3.7 from official website. Plase check adding to system path while installing.

In order to test the Anaconda. Open command line PowerShell. If the installed Anaconda is the only Python version in your operating system. Type python. In If it shows:

Then type exit() and enter. Type pip -V. If the screen shows:

Opencv Visual Studio Code

Congratulations! Your Anaconda environment is successfully installed in your Windows.

If you have another version of Python, try python3 / py3 and pip3 instead.

1.2 In Linux (like Ubuntu)

In general, Python is pre-installed in Ubuntu. For Ubuntu 18.04, in Terminal, you can type python3 to enter python console mode.

Opencv Visual Studio

If your Ubuntu does not have Python3, please try:

to install Python3.

We also need pip to maintain Python third-party packages. Use

to install pip3.

You can also install Anaconda in Linux if you want.

2. Install OpenCV3

OpenCV is the most popular open source computer vision libraries. We will use pip to install OpenCV for Python. Please type pip -V in command line to check the current pip is matched to your target Python version. You may use pip3 instead (In Ubuntu 18.04).

In command line, type pip install opencv-python.

Opencv Visual Studio Python

To test OpenCV, in python consle mode, type the following codes in order.

You will see an empty window shon then disappeared.

3. Install Visual Studio Code

Download and install Visual Studio Code from official website.

Now, you can write your code in VS Code and run the code in command line. You can also open command line in VS Code by Ctrl + `.

If you want to set up a debug environment for Python, please follow this official tutorial. There are two tips:

  1. Check the Python intepretor in the left bottom of VS Code is correct or not.
  2. Add attribute 'args': ['--arg1', '--arg2'] in launch.json file to set up program arguments.
-->

In this sample, we will build the OpenCV library for Windows and add it to a UWP C++ app, which will run facial and body recognition on a photo.

Create a new UWP C++ project

The sample code is available to download, but as an exercise, we will create this app from scratch.

Even if you download the sample, code, you'll need to follow the steps in Compile the OpenCV Libraries and Add the Libraries to your Project below.

  • Make sure your device is running and set up and you have Visual Studio installed.

  • You will need your device's IP address when connecting to it remotely.

  • Make sure you have the C++ components for Visual Studio. To check, open the Start Menu, type in Visual Studio Installer and hit enter. Once the Visual Studio Installer opens, click Modify under your installed version of Visual Studio. Check to make sure you have the below components selected. If not, select them and update your installation.

  1. Start Visual Studio 2017.

  2. Create a new project with (File New Project..).

    In the New Project dialog, navigate to Universal as shown below (in the left pane in the dialog: Templates Visual C++ Windows Universal).

  3. Select the template Blank App (Windows Universal).

    Note that we call the app OpenCVExample. You can name it something different, but you will have to adjust sample code that references OpenCVExample as well.

Note: If this is the first project you create, Visual Studio will likely prompt you to enable developer mode for Windows 10.

Compile the OpenCV Libraries

  1. Download the Microsoft fork of OpenCV from GitHub

    Download Microsoft's fork of the OpenCV library from GitHub. Unzip it to a place you can remember it, and copy the folder's path (i.e. C:/path/to/opencv/) for the next step.

  2. Create the OpenCV environment variable

    Open the Start Menu and enter Edit the system environment variables and hit Enter. On the next screen, press Environment Variables, then New. Create a new variable called OCV2015_ROOT with a value of the path you copied, i.e. C:/path/to/opencv/

  3. Build the solution

    This step depends on your device's architecture. Follow one of the options below:

    • x86 (i.e. Your machine or Minnowboard Max)

      Open the file explorer and navigate to the following path: '%OCV2015_ROOT%vs2015WS10.0x86', then open OpenCV.sln in Visual Studio.

      On the top of the screen, next to the green Run button, select Release instead of Debug and Win32 instead of ARM or x64.

    • ARM (i.e. Raspberry Pi)

      Open the file explorer and navigate to the following path: '%OCV2015_ROOT%vs2015WS10.0ARM', then open OpenCV.sln in Visual Studio.

      On the top of the screen, next to the green Run button, select Release instead of Debug and ARMPlex app on vizio smart tv. instead of x86 or x64.

    • x64

      Open the file explorer and navigate to the following path: '%OCV2015_ROOT%vs2015WS10.0x64', then open OpenCV.sln in Visual Studio.

      On the top of the screen, next to the green Run button, select Release instead of Debug and x64 instead of x86 or ARM.

    Press Ctrl+Shift+B to Build the solution. Alternatively, press Build Build Solution. These results should compile without errors. You will have to re-build this project for each platform (x86, x64, or ARM) and release type (Debug or Release) that you want to use.

Add the Libraries to your Project

  1. Add the DLLs directly

    Return to your project's Visual Studio window. Right click on the project in the window and navigate to Add Existing Items

    • x86: In the new window, navigate to %OCV2015_ROOT%vs2015WS10.0x86binRelease

    • ARM: In the new window, navigate to %OCV2015_ROOT%vs2015WS10.0ARMbinRelease

    • x64: In the new window, navigate to %OCV2015_ROOT%vs2015WS10.0x64binRelease

    Add the following files: opencv_core300.dll, open_imgcodecs300.dll, opencv_imgproc300.dll, opencv_ml300.dll, opencv_objdetect300.dll

    After adding the files, click on each one in the Solution Explorer and make Content = 'True'

  2. Modify you General Properties

    Right click on the OpenCVExample (Universal Windows) line of the Solution Explorer and click on Properties.

    In the dialog that pops up, navigate to Configuration Properties C / C++ General. From there, add the following to the Additional Include Directories field before the already-present contents:

    %OCV2015_ROOT%modulescoreinclude;%OCV2015_ROOT%modulesimgprocinclude;%OCV2015_ROOT%modulesimgcodecsinclude;%OCV2015_ROOT%modulesobjdetectinclude;%OCV2015_ROOT%moduleshalinclude;%OCV2015_ROOT%moduleshighguiinclude;%OCV2015_ROOT%modulesmlinclude;

    For each new DLL you want to add, you need to add the appropriate 'include' directory to this field.

  3. Modify your Linker Properties

    In the Properties window still, navigate to Linker General, then add the following to the Additional Library Directories field:

    • x86: %OCV2015_ROOT%vs2015WS10.0x86librelease;%(AdditionalLibraryDirectories)

    • ARM: %OCV2015_ROOT%vs2015WS10.0ARMlibrelease;%(AdditionalLibraryDirectories)

    • x64: %OCV2015_ROOT%vs2015WS10.0x64librelease;%(AdditionalLibraryDirectories)

    Switch the menu to Input (still within Linker) and add the following to the Additional Dependencies field:

    opencv_core300.lib;opencv_imgproc300.lib;opencv_imgcodecs300.lib;opencv_objdetect300.lib;opencv_ml300.lib;

    For each new DLL you want to add, add the appropriate LIB in this field as well.

Set up the User Interface

Open MainPage.xaml and replace the existing code with the following code to create the window UI:

To view the entire UI, change the dropdown in the top left corner from '5' Phone' to '12' Tablet'.

Modify the actual C++ Files

Modify the Header File

Open MainPage.xaml.h. Replace the contents with the following code:

The header file stablishes the functions we're going to declar in the main .cpp file, as well as a private variable (_stored_image) which stores the content of the storedImage XAML Image element once we upload it.

Add the Includes and Namespaces to the .cpp file

Add the following header files to the top of your code, right after the #include 'MainPage.xaml.h' line:

These lines allow us to use OpenCV library functions, along with some necessary default classes as well. We also define the locations of the features classifiers we'll use later.

Add the UpdateImage function

Add the following function right after the MainPage Constructor

This function changes the image contained in the 'storedImage' XAML Image element to the contents of the 'image' argument.

Add the Upload Button (loadImageButton) handler

Add the following function right after the UpdateImage function

This function opens an image and uploads a default image to the StoredImage UI element.

Add the Canny Edge Button (cannyEdgesButton) handler

Add the following function right after the last handler:

This function applies Canny Edge detection to the image and updates the image container with the results.

Visual

Add the face and body classifiers

Add the following lines and function right after the last handler:

This function uses cascade classification to classify and detect bodies and faces in a video stream (or image) using two Haar classifiers, face_cascade and body_cascade, stored in the xml files we provided for you. It's a method of classification involving machine learning, as explained on OpenCV's website.

Add the 'Detect Faces and Bodies' button (detectFeaturesButton) event handler

Add the following lines and function right after the last helper function:

This function loads the classifiers, re-reads the image (the classification doesn't work on a Canny image in case the user clicked that button first), finds the faces and bodies using the helper function from the last step, and draws rectangles around the results: red for the faces, black for the bodies. It then pushes the updated image to the container.

Add in the Resources

Download the picture, face classifier, and body classifier and add them to your Assets folder within your project.

Optional: Build and test your app locally

  1. If you've built the x86 version of OpenCV, you can test the program on your local machine. Make sure the app builds correctly by invoking the Build Build Solution menu command.

  2. Press F5 to run the program on your machine.

    Press the 'Test Image' button to see the test image.

    Press the 'Canny' button to see the Canny edges on the test image.

    Press the 'Detect' button to see the detected faces and bodies in the image indicated by rectangles.

    Close the app once you're done validating its correct operation.

Deploy the app to your Windows 10 IoT Core Device

  1. Click on the dropdown next to the Local Machine label and click on Remote Machine. Make sure the dropdown just to the left says your device's architecture, either x86, x64, or ARM. The Remote Machine click should open a dialog. Enter your device's IP address (or unique name) into the field labeled Address, then press Select.

  2. If the dialog does not appear, right click on your project and go to the Properties menu. From there, click on Debugging. Clicking on Machine Name and then Locate should provide the same dialog to enter the device's IP address.

    Make sure the device is powered on, then click the 'Run' button. The app should run on your device.

  3. Congratulations! Your app should now be working!

Notes:

  • Make sure if you have Latest or Required Windows 10 SDK mentioned in the Project. Right click on Project -> Properties -> General ( under Configuration Properties ) -> Target Platform Version
  • Make sure to following the Instructions provided in above link
  • Compile OpenCV.sln as per link Instructions
  • Then Try to Compile this current solution as there are some dependency libraries thats get used in here.

Troubleshoot:

  • error LNK1104: cannot open file 'opencv_core300d.lib'Can be 2 reasons
    • a. You might not have followed OpenCV documentation and did not configure Environment Variable
    • b. You might have not compiled OpenCV.sln for respective platform for which you are trying to compile OpenCVExample.sln