Embedded program debugging and opencv image library

1. Practice GDB debugging of program code on Ubuntu system

Enter the terminal under ubuntu18.04, and create a folder to store GDB code, enter the folder, create a GDB code file, and write the following code for debugging.
Insert picture description here

#include <stdio.h> 
void ShowRevertNum(int iNum) 
{         
	while (iNum > 10)         
		{  
			printf("%d", iNum % 10);                 
			iNum = iNum / 10;         
		}         
		printf("%d\n", iNum); 
} 
int main(void) 
{         
	int iNum;         
	printf("Please input a number :");         
	scanf("%d", &iNum);         
	printf("After revert : ");        
	ShowRevertNum(iNum);
} 

Compile the code in the terminal (note that you need to add -g here) and enter the GDB debugging interface, enter l in the interface to display the code and line number
Insert picture description here

b 16   //通过行号设置断点
b ShowRevertNum   //通过函数名设置断点
info b   //查看所有断点信息
run   //执行程序
whatis iNum   //查看iNum的类型
c   //继续调试
p iNum   //打印iNum的值
s   //单步调试

Insert picture description here
Insert picture description here
Segment error debugging
This code is

#include<stdio.h>
#include<string.h>
#define BUFSIZE 256
static char acBuf[BUFSIZE];
static char *pStr;
int main(void)
{
	printf("please input a string:");
	gets(pStr);
	printf("\nyour string is:%s\n",pStr);
}

Insert picture description here
Compile and find a segfault, and then we enter GDB debugging, make the wrong modification
Insert picture description here

print pStr   //打印pStr的位置
b 9   //设置断点
r   //重新运行程序
set variable pStr=&acBuf   //修改变量的值
n   //单步执行

2. Installation and simple application of OpenCV

1. The installation process of OpenCV

It is recommended to download OpenCV here (compared to the official website download will be much faster), version 3.x is recommended, copy Insert picture description here
the downloaded file to the main directory for decompression configuration

Insert picture description here
Enter the command line after decompression

cd opencv-3.4.11

Insert picture description here
Install dependent libraries and cmake. If you are reminded that you need apt-get update, first sudo su to enter root privileges, then sudo apt-get update, and then execute the following command

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

After the installation is complete, execute the command to create a compilation folder and enter the folder for configuration

mkdir my_build_dir
cd my_build_dir

Insert picture description here

cmake

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

Execute the command and wait for the compilation to complete

sudo make

Insert picture description here

Excuting an order

sudo make install

After this command is executed, the OpenCV compilation process is over. After that, some OpenCV compilation environments need to be configured. First, add the OpenCV library to the path so that it can be found by the system

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

After executing this command, a blank file may be opened, don’t worry, just add at the end of the file

/usr/local/lib  

Insert picture description here

Save back to the command line interface and
execute the following command to make the configuration path effective

sudo ldconfig  

Configure bath

sudo gedit /etc/bash.bashrc  

Add the following at the end

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

Insert picture description here

Save, execute the following command to make the configuration effective

source /etc/bash.bashrc  

So far complete the configuration

Programming under Linux

Create a test folder under opencv-3.4.11, cd test into the folder, create test1.cpp file, and enter programming. The source code of test1.cpp is as follows

#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
Save and compile

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

Here, if you use gcc to compile, this error may occur.
Insert picture description here
Here, replace gcc with g++, you can compile and run normally,
Insert picture description here
Insert picture description here
put the original picture into the folder, and then run

./test1

Insert picture description here
Insert picture description here

Three, use the OpenCV library to write a program to open the camera

Configure before starting, open win+r on the physical machine and enter services.msc, find VMware USB Arbitration Service, enable this option to
Insert picture description here
Insert picture description here
enter the virtual machine, click Virtual Machine-Settings, select the USB controller on this interface, and select USB 3.0 for compatibility
Insert picture description here
Then the operation is as follows
Insert picture description here

After that, you can open the camera normally to
create the test2.cpp file and put the following code into it, a
Insert picture description here
simplified version

#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;

int main()
{
	VideoCapture capture(0);
	while (1)
	{
		Mat frame;
		capture >> frame;
		imshow("读取视频帧", frame);
		waitKey(30);
	}

	system("pause");
	return 0;
}

The specific effects are as follows: an
Insert picture description here
improved version

#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();
	return 0;
}

Use this version to generate .AVI files after recording
Insert picture description here

Question:
If you are required to open a video file on your hard drive to play, how do you modify the 7th line of the code in the simplified version?
Answer: VideoCapture capture(0); The 0 in the statement is changed to the path of the hard disk video

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?
Answer: The Mat category has two parts of data. One is the matrix header, the size of this part is fixed, including the size of the matrix, the storage method, the address of the matrix storage, and so on. The other part is a pointer to the pixel values ​​contained in the matrix.
The function of the waitKey() function is to constantly refresh the image, the frequency is delay, the unit is ms, the return value is the value currently pressed by the keyboard, and when there is no key, it returns -1. For
details, please refer to the OpenCV data structure Mat and waitKey in Opencv ()

This code will always run in the while loop. If you try to close the image display window with the mouse, you will find that you can't close it all the time. You need to use the keyboard Ctrl+C to forcibly interrupt the program, which is very unfriendly. How to improve?
Answer: Write an if statement, judge that a certain key is pressed, and exit the while loop

Reference article

Windows10&Ubantu16.04&Raspberry Pi 3B+ install opencv tutorial .

Guess you like

Origin blog.csdn.net/java_creater/article/details/109373159