OpenCV4 learning record (5) cross-platform compilation and development using C++ and CMake

I believe that every Taoist who has used the Raspberry Pi for image detection has had such troubles when he was a beginner: sometimes the real-time effect of the code is not good when installing the precompiled package of opencv-python, if combined with some external control actions Sometimes python's syntax is not very good for writing code. After being familiar with embedded C programming, I still prefer C or C++ to write control-related codes, so why can't I combine the advantages of C++ and use CMake to develop Opencv's C++ cross-platform programs?

For using cmake development on the computer side, you can refer to another blog VS2019+CMake

Using C++ to develop related programs, as far as my current experience is concerned, the running speed of the code is not too obvious, but compared with python, it has the following advantages: 1. Multi-thread and multi-process development is more efficient;
2
. The data processing is more refined, and it is easier to consider the relevant control part code writing
3. It is more convenient to process and refer to multiple program source files
4. You can use CMake to develop cross-platform
Of course, the disadvantages are also obvious:
1. The program is in Grammatical requirements are higher, and error-prone
2. External package management is not very convenient, and some third-party libraries for data processing are not as rich as python
. I believe in the truth that "a black cat is a white cat, the one that can catch mice is a good cat", as long as it can better solve the needs and problems, of course, the more convenient the development method, the better. *

install opencv

First of all, we need to install opencv on the target platform. It should be noted here that it needs to be installed through source code compilation. Instead of using python's precompiled packages.
For the installation method of the jetson nano platform, you can refer to my other blog: If you compile and install opencv from the source code
of jetson nano for the Raspberry Pi, there are too many tutorials on how to install opencv from the source code on the Internet, so I won’t repeat it.

After compiling, we need to ensure that CMake is installed on the device, and then we start to create our own project template.

template

Directory Structure

First, create a directory structure as shown below. main.cppThe file is used to write our main function, and \srcthe directory is used to store our own header files and library programs.

├── build
├── CMakeLists.txt
├── main.cpp
└── src
    ├── CMakeLists.txt
    ├── Test1.cpp
    └── Test1.h

In addition, you can also add a readme file, a doc folder for storing relevant instructions, and a .sh file for quick program startup. These are not necessary here, you can add them yourself if necessary.

Outer CMakeLists.txt

This file defines the main content of the modified project, including those external libraries to be introduced, the definition of the project name, the compilation search path, etc.
The content is as follows:

CMAKE_MINIMUM_REQUIRED(VERSION 3.1)

PROJECT( main )

#INCLUDE_DIRECTORIES(/usr/local/include/opencv4/opencv2)	#添加头文件搜索路径

FIND_PACKAGE(OpenCV REQUIRED)
MESSAGE(STATUS	"OpenCV library status:")
MESSAGE(STATUS	"	version: 	${OpenCV_VERSION}")
MESSAGE(STATUS	"	libraries: 	${OpenCV_LIBS}")
MESSAGE(STATUS	"	include path: 	${OpenCV_INCLUDE_DIRS}")

ADD_SUBDIRECTORY( src bin )

AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS})

TARGET_LINK_LIBRARIES(main Test)
TARGET_LINK_LIBRARIES(main ${OpenCV_LIBS})

CMAKE_MINIMUM_REQUIRED(VERSION 3.1): Specify the minimum version requirement of CMake when compiling, if it is lower than this version, an error will be reported
PROJECT( main ): The project name of the definition project is "main"
FIND_PACKAGE(OpenCV REQUIRED): Find the package installation path of opencv, and the following four MESSAGEs will print out the relevant information of opencv: version, installation Library and installation path
ADD_SUBDIRECTORY( src ): Add the src path to the file search path. This command is used to add a subdirectory for storing source files to the current project, and you can specify the location where the intermediate binary and target binary are stored as the bin path: add the current
AUX_SOURCE_DIRECTORY(. DIR_SRCS)directory Assign main.cpp to the variable DIR_SRCS
ADD_EXECUTABLE(main ${DIR_SRCS}): compile the file represented by the DIR_SRCS variable to generate an executable file named 'main', note that 'main' here has nothing to do with the project name above: link the Test library
TARGET_LINK_LIBRARIES(main Test)to 'main' to Executable
TARGET_LINK_LIBRARIES(main ${OpenCV_LIBS}): link the library in OpenCV_LIBS to the mian executable

Main function file main.cpp

First, we quote the opencv library and the header files we want to write:

#include "src/Test1.h"
#include <opencv2/opencv.hpp>

We can try to read a picture in the main function:

	cv::Mat image = cv::imread("/home/CV2/opencvlogo.jpg");	//j图片路径和图片名修改为自己的
	if(image.empty())
	{
		std::cout << "conld not load image..." << std::endl ;
		return -1;
	}
	cv::imshow("test_opencv_setup", image);

At the same time, we defined a class A in the library we wrote, and A defined a method f. We reference it in the main function, so the complete main.cpp file is as follows:

#include "src/Test1.h"
#include <opencv2/opencv.hpp>

int main()
{
	A a;
	a.f();
	
	std::cout << "Hello World" << std::endl;
	
	cv::Mat image = cv::imread("/home/wuito/CVProjece/CV2/opencvlogo.jpg");
	if(image.empty())
	{
		std::cout << "conld not load image..." << std::endl ;
		return -1;
	}

	cv::imshow("test_opencv_setup", image);
	cv::waitKey(0);
	return 0;
}

Inner CMakeLists.txt

The main function of CMakeLists.txt in the src folder is to explain that the programs in this folder are to be compiled into libraries for the main program to call.

AUX_SOURCE_DIRECTORY(. DIR_TEST_SRC)

ADD_LIBRARY(Test STATIC ${DIR_TEST_SRC})

Because there are few files in the src folder, we only use two lines of definition
AUX_SOURCE_DIRECTORY(. DIR_TEST_SRC): add the .cpp and .h files in this directory to the variable DIR_TEST_SRC
ADD_LIBRARY(Test STATIC ${DIR_TEST_SRC}): this command compiles the file pointed to by DIR_TEST_SRC into a file named Test The static library file.

Inner library file

The first is to define the header file Test1.h, which declares method A, as well as its private function f() and variable i;

#ifndef TEST1_H_
#define TEST1_H_

#include<iostream>

class A
{
public:
	int i;
	void f();
};

#endif

Then there is our corresponding Test1.cpp file, which defines the function f.

#include"Test1.h"

void A::f()
{
	i = 10;
	std::cout << i << std::endl;
}

buildThen we open the terminal under the created question price and enter CMake ..to initialize the project and create the makefile. Then execute make to compile and link. Finally, find the executable file main under the output folder bin, and execute it ./mainto run the program just now.

Guess you like

Origin blog.csdn.net/weixin_47407066/article/details/126284851