opencv4, Raspberry Pi compiles opencv by itself

Background: I built a Raspberry Pi 3b+ and wanted to use it for opencv image processing. I have already written the cpp code in Windows and configured the environment for migration.

Running account: The opencv in the computer is 2413 (I’m really old), and the latest version on the official website is 4.1.0.

0th time:

apt-get install opencv2

I thought it was simple and rude, but it turns out that python is easy to use, but cpp is not.

 

The first time: I thought about my own exploration (I compiled 2413 before) and downloaded the source code and cmake. The dependent library felt that the previous apt-get was not installed.

cmake
make
make install

One day later, I finished and ran a routine (there are still some twists and turns, I will talk about it later), and the gtk library is missing.

The second time: I checked the necessary libraries, and found that make -j4 can speed up by the way.

make -j4, the running light of the Raspberry Pi is always on, the power light is always off, the keyboard and mouse cannot be operated, and the screen cannot be woken up.

Three days later, the deadlock ends. In fact, opencv make can continue to be interrupted, just run make again, and another day, I still don’t see the result, and I can’t wake up the screen.

give up

I also skipped pits before. It may be that the latest version has bugs and there are no tutorials. I'd better go back and work on 2413.

The third time, upload 2413 source code (use ftp, I am very familiar with linux), still, cmake, make, error, all kinds of error

After checking the information, it is said that the gcc version is too high, and everyone's approach is to reduce the gcc version. If I refuse to accept it, the old things have to be eliminated.

The fourth time, this time I took it, check the tutorial, and follow the steps.

The tutorial is as follows, there are still discrepancies in practice, I will write it down. https://blog.csdn.net/leaves_joe/article/details/67656340

1. Brush the system and remove the pits left before.

2. Install various tools and libraries. The tutorials are installed one by one. You can also install all of them in one sentence.

sudo apt-get install build-essential cmake git pkg-config 
sudo apt-get install libjpeg8-dev 
sudo apt-get install libtiff5-dev 
sudo apt-get install libjasper-dev 
sudo apt-get install libpng12-dev 
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libgtk2.0-dev
sudo apt-get install libatlas-base-dev gfortran
sudo apt-get install python-dev python3-dev

Everyone operates flexibly, and must understand the function of each library. I am too lazy to copy the notes, and everyone can Baidu by themselves. When the deadlock ended before, it was stuck in the process of compiling the python library. In addition, I plan to use cpp (I am superstitious about the performance of cpp, and I am not very familiar with python. Unfortunately, most of the raspberry pie is python tutorials, and cpp can only be explored by myself), so the python library is not installed.

I also installed more cmake-gui, function, hehe

3. cmake-gui, configure, and then check the library that needs to be generated, and cancel the library that does not need to be generated, such as python. Although I have not installed it, it may come with the system, and it must be canceled manually. (Actually, I was stuck at this step later, and a pkg-config was unchecked, which caused some problems). Then generate, forgot to mention uploading code and creating a new build folder.

4. Enter the build folder, make or make -j4, still prompting make -j4 will cause the keyboard and mouse to become unresponsive.

5. This time it was relatively smooth, and the mistakes that occurred later can be solved easily. If there are not many make left, if there is an interruption, you can change it to make -j3 

6.make install ,sudo ldconfig

7.finish, start to run the routine. I am a piece of code that calls the camera.

#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/contrib/contrib.hpp>
#include <iostream>
#include <string> 
#include<windows.h>

using namespace std;
using namespace cv;

Mat src ;
Mat dst, gray, gray1, output_mask, output_mask1, output_mask2, gray2, output_mask3, output_mask4;

int main()
{
	VideoCapture cam(0);
	while (!cam.isOpened())
		waitKey(10);
	//cam.set(CV_CAP_PROP_FRAME_WIDTH, 1920);
	//cam.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
	//cam.set(CV_CAP_PROP_FPS, 30);
	//cam.set(CV_CAP_PROP_EXPOSURE, 0); // where 0.25 means "manual exposure, manual iris"
	//cam.set(CV_CAP_PROP_SETTINGS, 1);//各种相机设置,windows专用
	while (waitKey(10)!= 27)
	{
		cam >> src;
		if (src.empty()) { //判断图像是否载入
			cout << "can not load the frame" << endl;
		}
		else {
			imshow("src", src);
		}
	}
	waitKey();
	return 0;
}

This is the code on windows, Linux needs to remove stdafx.h and windows.h 

8 g++ camera.cpp reports an error, cannot find opencv2. The tutorial says to run like this

gcc test.cpp -o test `pkg-config --cflags --libs opencv`

Then I was prompted that opencv could not be found, so I asked to find opencv.pc, which made it easy for me to find. did not find. Fortunately, I know the above code, which is to run pkg-config --cflags --libs opencv first, and then add the running result to gcc test.cpp -o test to run.

9 I found the result of running pkg-config --cflags --libs opencv https://www.jianshu.com/p/7eee92d8ad7b

Spelled a super long compilation statement

gcc test.cpp -o test -I/usr/local/Cellar/opencv3/3.1.0_4/include/opencv -I/usr/local/Cellar/opencv3/3.1.0_4/include -L/usr/local/Cellar/opencv3/3.1.0_4/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lippicv -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core

I searched for the addresses in the statement one by one, and replaced them with similar ones in my folder, that is

g++ edge.cpp -o test -I/usr/local/include/opencv4 -I/usr/local/include  -L/usr/local/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core

Pay special attention to the fact that opencv3 has changed to opencv4. If there is no path and only the name, it will not be checked. Run it, and it will report an error, linking two libraries that do not exist. Delete, the final statement is as follows

g++ edge.cpp -o test -I/usr/local/include/opencv4 -I/usr/local/include  -L/usr/local/lib -lopencv_stitching -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core

(Actually, according to the code, I only need to link the used libraries, and there is no need to link all of them.) I have already stepped on this pit in the first run, otherwise I will not see the prompt of missing gtk library. Unfortunately, the statement is not saved. I Checked again.

10 run again, success

11 I have obsessive-compulsive disorder, and I must solve the problem of pkg-config. I wonder if it was dropped during configure. After careful inspection, sure enough, it still has a beginning, and it is all capitalized, which is troublesome. Re-check, configure, generate, make, make install, finish, because it has been compiled before, this time it is fast.

12 Run pkg-config --cflags --libs opencv again and still prompt an error, don't worry, pkg-config --cflags --libs opencv4 will have content

But the previous short statement still can't run, let's put it first.

13 I came back and said that the program I want to migrate, the opencv2413 version, has many differences, the following is listed for your reference

CV_BGR2GRAY becomes COLOR_BGR2GRAY

CV_8UC3 has not changed. Some people say that all CVs have changed to COLOR, so I replaced them all, but the error is still wrong.

CV_RETR_CCOMP becomes RETR_CCOMP

CV_CHAIN_APPROX_SIMPLE becomes CHAIN_APPROX_SIMPLE

CV_AA becomes LINE_AA

There should be others, but I haven't encountered them. It is recommended that you directly find the routines that come with the source code. If you can’t run it, you can also look at the definition above. Otherwise, you can only check the manual, and the online tutorials are too slow.

Guess you like

Origin blog.csdn.net/u010752777/article/details/96474350