GDB debugging on Ubuntu and image processing and recording using opencv

Tip: This article is a coursework, and there are many shortcomings.



Tip: The following is the content of this article, the following cases are for reference

One, GDB debugging

1. Introduction to
GDB•GDB: GNU Debugger is a debugger developed by the GNU Project for the GNU operating system, but its use is not limited to the GNU operating system. GDB can run on UNIX, Linux or even Microsoft Windows.
• GDB can debug programs written in C, C++, Objective-C, Pascal, Ada and other languages; the debugged program can run on the same computer as GDB or on different computers.

• Using GDB we can:
-Set breakpoints to stop the program
-Monitor or modify the value of variables in the program
-Track the code execution process

2. The use of gdb
• The program must contain debugging information if it can be debugged.
• When compiling the program, add debugging information to the program through the -g option of
gcc,for example: gcc –g –o helloworld helloworld.c

3.gdb command

GDB commands effect
file Load the executable file you want to debug
kill Terminate the program being debugged
list List part of the source code that generates the executable file
next Execute a line of source code but do not enter the function
step Execute a line of source code and enter the function
run Execute the program currently being debugged.
c Continue running the program
quit Terminate gdb
watch Allows you to monitor the value of a variable regardless of when it is changed
backtrace Stack trace to find out who called the code
print View the value of a variable
make Allows you to regenerate executable files without exiting gdb
shell Enables you to execute UNIX shell commands without leaving gdb
whatis Display variable or function type
break Set a breakpoint in the code, which will cause the program to be suspended when it reaches this point
info break Display the current breakpoint list, including the number of times to reach the breakpoint, etc.
info files Display the detailed information of the debugged file
info func Show all function names
info local Display the local variable information in the current function.
info prog Display the execution status of the debugged program.
delete [n] Delete the nth breakpoint.
disable[n] Close the nth breakpoint
enable[n] Open the nth breakpoint.
ptype Display structure definition
set variable Set the value of the variable
call name(args) Call and execute the function named name and parameter args
Finish Terminate the current function and output the return value
return value Stop the current function and return value to the caller

Use of the break command
-set breakpoints based on the line number:
• (gdb) break linenum
– set breakpoints based on the function name:
• (gdb) break funcname
– stop execution when executing a line or a function that is not the current source file:
• ( gdb) break filename:linenum
• (gdb) break filename:funcname
– stop program execution based on conditions:
• (gdb) break linenum if expr
• (gdb) break funcname if expr

GDB debugging exercise

In Linux, when a program crashes, a core file is generally generated to record the state before the process exits, and debug segfault problems. 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 a file

mkdir gdb
cd gdb
touch test1.c
gedit test1.c

Insert picture description here

2. Write documents

#include<stdio.h>
int main()
{
    
    
  int *p=0;
  *p=1;
  return 0;
}

3. Execution

Insert picture description here

Find the problem:

Insert picture description here

Two, compile Opencv3.4.1 under Ubuntu system

**

1. Install Opencv3.4.1

**

Click here to install and compile the tutorial==> Windows10&Ubantu16.04&Raspberry Pi 3B+ install opencv tutorial

Insert picture description here
There is a problem with this step, click the link below

The problem that the OpenCv dependency package libjasper-dev cannot be installed under Ubuntu 18.04

Insert picture description here
Insert picture description here
A warning (setting of attributes, etc. is not supported...) is a normal phenomenon and does not need to be changed after performing these two steps.

2. Compile Opencv3.4.1

Insert picture description here

gedit test.cppYou can not modify the content of the document
into
sudo gedit /test.cpp

#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;
}

> Problems encountered in the process and solutions

Put the photo in the mytest file directory and change the name to lena.jpg

Insert picture description here

Or modify the code lena below to be the name of the picture (line 9 of the original code)

Insert picture description here

Undefined for main
Insert picture description here

The modification instruction here is

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

Encounter new problems
Insert picture description here
Enter the following code (it will be installed automatically, just click y to execute during the process)

sudo apt-get install libcanberra-gtk-module

Insert picture description here
> Successfully compiled

Insert picture description here
Insert picture description here
Insert picture description here

Third, use the opencv library to write a program to open the camera's compressed video

1. Normally turn on the camera and summary of problems

First execute the command:

cd opencv-3.4.1
cd 123
touch test2.cpp
sudo gedit /test2.cpp

Insert picture description here

Write 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;
}

Continue to execute the command

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

Insert picture description here
You can see that the camera is turned on normally

> Problems encountered in the process and solutions:

1. Cannot find the camera option in removable devices
Insert picture description here

Solution: Set the usb option in the virtual machine settings => select usb3.0 => click below to confirm and restart the system
Insert picture description here
Insert picture description here

2. The camera still cannot appear after running the program

1. Program error
2. The camera is not connected before executing the command

3. Connect the camera to display the driver error (this is my own reason and spent nearly two hours looking for the problem)
we need to check if the Vmware usb device is missing,
right click on my computer => click on properties => find the upper left Device Manager=>Check Universal Serial Bus Controller=>Check if there is a Vmware usb device
Insert picture description here
Insert picture description here
Insert picture description here
If there is no Vmware usb device, we need to reinstall Vmware Tools (click when the virtual machine is powered on)
Insert picture description here
Install the Vmware Tools portal

After installation, restart the system and i can use the camera

PS: Various problem solving portals

VMware prompts USB connection failure and driver error

The camera driver cannot be displayed in the virtual machine

The USB device of the camera cannot be recognized in the virtual machine

The computer camera and usb camera cannot be recognized in the virtual machine

2. Turn on the camera to record

First execute the command:

cd opencv-3.4.1
cd 123
touch test3.cpp
sudo gedit /test3.cpp

Programming:

#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;
	}

	//获得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();
}

Excuting an order

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

Click esc to exit the recording.
Screenshot of the recording. The video is generated
Insert picture description here
Insert picture description here
in the corresponding folder
Insert picture description here

3. Question answering

1) If you are required to open a video file on your hard disk for playback, how do you modify the 7th line of the sample code 1?

Change the 0 in brackets to path + video name

VideoCapture capture(0);
VideoCapture capture(路径+视频名);

2) In the while loop on line 9 of sample code 1, what data structure is Mat? Why is it necessary to add a waitKey delay code and delete it?

Mat consists of two parts.
Header: storage method, image size and other information. The size of the header is a constant
data block: the value of the image pixel (stored in row order).
Detailed introduction of the data structure of the Mat

The waitKey function is used to display the delay. For example, waitKey(0) will display the window indefinitely until any key is pressed to exit the delayed event (suitable for displaying images). If delay is greater than 0, for example, waitKey(25) will display a frame of video every at least 25ms (suitable for displaying video frames). If you want to exit by pressing the key, you need to combine waitKey(25) with a key value (ASCII code) Comparison.
OpenCV: a detailed introduction to the delay function waitKey()

Can't delete

3) The sample code 1 code will always run in the while loop. If you try to close the image display window with the mouse, you will find that it can't be closed all the time. You need to use the keyboard Ctrl+C to forcibly interrupt the program, which is very unfriendly. How to improve?

The improvement method has not been researched yet ( the frustration of learning scum ), you can check the related methods of the big guys.


to sum up

There were a lot of problems during this experiment, and the problems I encountered were more difficult to solve than other copper whiskers. It took a lot of time to solve these problems, but correspondingly Yes, I also learned a lot from it.

Guess you like

Origin blog.csdn.net/aiwr_/article/details/109339662