Using OpenCV with C++ or Python

In this section, we'll demonstrate how you can use OpenCV in your C++ or Python projects with a very simple example that we'll call HelloOpenCV. You might already know that the purpose of such a project is either one of the following:

  • To get started with a new library, such as OpenCV, that you've never used before
  • To make sure your OpenCV installation is functional and works fine

So, even if you are not an OpenCV beginner, it's still worth going through the following instructions and running the simple example in this section to test your OpenCV build or installation.

We'll start with the required steps for using OpenCV in a C++ project:

  1. Create a new folder named HelloOpenCV
  2. Create two new text files inside this folder and name them CMakeLists.txt and main.cpp
  3. Make sure the CMakeLists.txt file contains the following:
cmake_minimum_required(VERSION 3.1) 
 
project(HelloOpenCV) 
 
set(OpenCV_DIR "path_to_opencv") 
find_package(OpenCV REQUIRED) 
include_directories(${OpenCV_INCLUDE_DIRS}) 
 
add_executable(${PROJECT_NAME} "main.cpp") 
 
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 
In the preceding code, you need to replace "path_to_opencv" with the path to the folder containing the OpenCVConfig.cmake and OpenCVConfig-version.cmake files, which is the same folder where you have installed your OpenCV libraries. If you are using the Linux operating system and the prebuilt OpenCV libraries, you might not need an exact path to the OpenCV folder.
  1. As for the main.cpp file, make sure it contains the following, which is the actual C++ code we'll be running:
#include <iostream> 
#include <opencv2/opencv.hpp> 
 
using namespace std; 
using namespace cv; 
 
int main() 
{ 
    Mat image = imread("MyImage.png"); 
    if(!image.empty()) 
    { 
        imshow("image", image); 
        waitKey(); 
    } 
    else 
    { 
        cout << "Empty image!" << endl; 
    } 
 
    return 0; 
} 

We'll be covering the functions used in the preceding code individually later on, in this and the upcoming chapters, however, for now it's worth noting that this program is trying to open and display an image saved on disk. If it succeeds, the image will be displayed until any key is pressed, otherwise the Empty image! message will be displayed. Note that this program, under normal circumstances, should not crash at all and it should build successfully. So, if the opposite happens to you, then you'll need to go through the topics discussed previously in this chapter.

  1. Our C++ project is ready. Now, we can use CMake to generate Visual Studio or any other type of project that we want (depending on the platform, compiler, and IDE we're going to use) and then build and run it. Note that CMake is simply used to make sure a cross-platform and IDE-independent C++ project is created.

By running this example project, your input image (in this case MyImage.png) will be read and displayed until any key on the keyboard is pressed. If any problems occur during the reading of the image, then the Empty image! message will be displayed.

We can create and run the same project in Python by using the following code:

import cv2 
 
image = cv2.imread("MyImage.png") 
if image is not None : 
    cv2.imshow("image", image) 
    cv2.waitKey() 
 
else: 
    print("Empty image!") 

The resemblance is quite unmistakable here. The exact same imshow and waitKey functions are also used in the Python version of the same code. As was mentioned before, for now don't bother with the exact way of using any of the functions and just focus on making sure that you are able to run these programs, either in C++ or Python, or both, and that you are able to see the image displayed.

If you were able to successfully run the HelloOpenCV example project in this section, then you are ready to take on the next sections of this chapter and the next chapters of this book without any problems. If you still face problems with the topics discussed so far, or you feel that you need a stronger understanding of those topics, you can go through them once again from the start of the chapter, or even better, you can refer to the additional books mentioned in the Further reading section at the end of this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.123.73