Embedded program application debugging

One, GDB simple debugging

1. Install gdb

sudo apt-get install gdb

2. GDB debugging of memory errors

• In Linux, when a program crashes, a core file is generally generated to record the state before the process exits, and debug segmentation faults. With the help of this file, the problem can be quickly located.
• The core file can be generated and used according to the following steps
-Step 1: Let the system generate a core file
• ulimit -c num #Set the core file capacity (num is a number, 0 means no core file is generated)
-Step 2: Run the program, Let the program crash and generate the core file
– Step 3: gdb cooperates with the core file to locate the problem
• gdb program name core file name
• For example: gdb segDemo core
(1) Create and compile the
Insert picture description here
sample code:

#include<stdio.h>
int main(void)
{
    
    
int *p=0;
*p=1;
return 0;
}
gcc -o tao -g tao.c

(2) Let the system generate a core file and run it to crash the program, thereby generating a core file

ulimit -c 100

Insert picture description here
(3) Enter the gdb file name core to find the file problem

gdb tao core

Two, install opencv-3.4.12

1. Drag opencv-3.4.12 downloaded from the Internet into unbuntu and copy it to the main directory

Insert picture description here

2. Open the terminal and unzip the package

unzip opencv-3.4.12.zip

3. Enter into the decompressed file package

cd opencv-3.4.12

4. Install dependent libraries and cmake

sudo apt-get install cmake  
 sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff5.dev libswscale-dev libjasper-dev

5. Create a compilation folder

mkdir my_build_dir

Enter the folder to configure

cd my_build_dir

6. Perform cmake

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

Excuting an order

sudo make

Insert picture description here

sudo make install

7. Configure OpenCV compilation environment

Add the OpenCV library to the path

sudo gedit /etc/ld.so.conf.d/opencv.conf 

Enter after opening

/usr/local/lib

Insert picture description here

8. Execute the following command to make the configuration path effective

sudo ldconfig  

Configure bash

sudo gedit /etc/bash.bashrc  

Add at the end

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig  
export PKG_CONFIG_PATH  

Insert picture description here

9. Execute the following commands to make the configuration effective and update

source /etc/bash.bashrc  
sudo updatedb  

Two, Linux programming

1. Create a new folder mytest under opencv-3.4.1

cd opencv-3.4.1
mkdir mytest

2. Create test.cpp

touch test.cpp

3. Enter programming

sudo gedit  /test.cpp

The image path is placed directly in the home directory;

#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
    
    
	CvPoint center;
    double scale = -3; 

	IplImage* image = cvLoadImage("lena.jpg");
	argc == 2? cvLoadImage(argv[1]) : 0;
	
	cvShowImage("Image", image);
	
	
	if (!image) return -1; 	center = cvPoint(image->width / 2, image->height / 2);
	for (int i = 0;i<image->height;i++)
		for (int j = 0;j<image->width;j++) {
    
    
			double dx = (double)(j - center.x) / center.x;
			double dy = (double)(i - center.y) / center.y;
			double weight = exp((dx*dx + dy*dy)*scale);
			uchar* ptr = &CV_IMAGE_ELEM(image, uchar, i, j * 3);
			ptr[0] = cvRound(ptr[0] * weight);
			ptr[1] = cvRound(ptr[1] * weight);
			ptr[2] = cvRound(ptr[2] * weight);
		}

	Mat src;Mat dst;
	src = cvarrToMat(image);
	cv::imwrite("test.png", src);

    cvNamedWindow("test",1);  	imshow("test", src);
	 cvWaitKey();
	 return 0;
}

Insert picture description here

4. Save and compile

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

5. Running results

Run./test will appear and
Insert picture description here
enter the command

 sudo apt-get install libcanberra-gtk-module

result
Insert picture description here

Three, use the opencv library to write a program to open the camera compressed video

In the above configuration environment

1. First change the usn compatibility under the usb controller of the virtual machine to 3.0 and connect the camera

Insert picture description here
Click the last connection in the removable device
Insert picture description here

2. Create file and compile

touch a.cpp
gedit a.cpp

Code:

 /*********************************************************************
打开电脑摄像头,空格控制视频录制,ESC退出并保存视频RecordVideo.avi
*********************************************************************/
#include<iostream>
#include <opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;

int main()
{
    
    
	//打开电脑摄像头
	VideoCapture cap(0);
	if (!cap.isOpened())
	{
    
    
		cout << "error" << endl;
		waitKey(0);
		return 0;
	}
```c
	//获得cap的分辨率
	int w = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_WIDTH));
	int h = static_cast<int>(cap.get(CV_CAP_PROP_FRAME_HEIGHT));
	Size videoSize(w, h);
	VideoWriter writer("RecordVideo.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25, videoSize);
	
	Mat frame;
	int key;//记录键盘按键
	char startOrStop = 1;//0  开始录制视频; 1 结束录制视频
	char flag = 0;//正在录制标志 0-不在录制; 1-正在录制

	while (1)
	{
    
    
		cap >> frame;
		key = waitKey(100);
		if (key == 32)//按下空格开始录制、暂停录制   可以来回切换
		{
    
    
			startOrStop = 1 - startOrStop;
			if (startOrStop == 0)
			{
    
    
				flag = 1;
			}
		}
		if (key == 27)//按下ESC退出整个程序,保存视频文件到磁盘
		{
    
    
			break;
		}

		if (startOrStop == 0 && flag==1)
		{
    
    
			writer << frame;
			cout << "recording" << endl;
		}
		else if (startOrStop == 1)
		{
    
    
			flag = 0;
			cout << "end recording" << endl;
			
		}
		imshow("picture", frame);
	}
	cap.release();
	writer.release();
	destroyAllWindows();
	return 0;
}

3. Run results after compilation

g++ a.cpp -o a `pkg-config --cflags --libs opencv`
./a

Turn on the camera,
Insert picture description here
space to control video recording, ESC to exit and save the video

to sum up

1) If it is required to open a video file on your hard disk to play, how to modify the 5th line of code?
Code:

#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
    
    
  VideoCapture capture(0);
  while(1)
  {
    
    
     Mat frame;
     capture >> frame; 
     imshow("读取视频帧",frame);
     waitKey(30);
   }
     system("pause");
     return 0;
}

Change the 5th line of code to

 VideoCapture capture("你想播放的视频的名字");

2) In the while loop on line 9, what data structure is Mat? Why is it necessary to add a waitKey delay code and delete it?
MAT is a class consisting of two parts of data: a matrix header (including matrix size, storage method, storage address and other information) and a matrix pointing to all pixel values ​​(depending on the selected storage method, the matrix can be of different dimensions) pointer. The wajitKey cannot be deleted or the video cannot be displayed if the time is too short.

Guess you like

Origin blog.csdn.net/m0_51039112/article/details/109366685