GDB调试
1.先创建一个RevertNum.c文件
jzddyy@ubuntu:~/opencv-3.4.11/my_build_dir$ nano RevertNum.c
代码如下(功能为对一个整型数进行反转输出)
#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); }
编译.c文件
jzddyy@ubuntu:~/opencv-3.4.11/my_build_dir$ gcc -o RevertNum -g RevertNum.c
启动GDB
jzddyy@ubuntu:~/opencv-3.4.11/my_build_dir$ gdb RevertNum
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from RevertNum...done.
(gdb)
利用l显示代码及行号
(gdb) l
1 #include <stdio.h>
2 void ShowRevertNum(int iNum)
3 { while (iNum > 10)
4 { printf("%d", iNum % 10);
5 iNum = iNum / 10; }
6 printf("%d\n", iNum);
7 }
8 int main(void)
9 { int iNum;
10 printf("Please input a number :");
(gdb)
执行代码发现输入123时能正确输出,输入100时却输出了010
(gdb) run
Starting program: /home/jzddyy/opencv-3.4.11/my_build_dir/RevertNum
Please input a number :123
After revert : 321
[Inferior 1 (process 19339) exited normally]
(gdb) run
Starting program: /home/jzddyy/opencv-3.4.11/my_build_dir/RevertNum
Please input a number :100
After revert : 010
[Inferior 1 (process 19383) exited normally]
故执行以下调试,找到问题:
分别用行号和函数名设置断点,并显示出所有断点信息
(gdb) b 12
Breakpoint 1 at 0x5555555547d7: file RevertNum.c, line 12.
(gdb) b ShowRevertNum
Breakpoint 2 at 0x555555554725: file RevertNum.c, line 3.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00005555555547d7 in main at RevertNum.c:12
2 breakpoint keep y 0x0000555555554725 in ShowRevertNum
at RevertNum.c:3
运行,并查看iNum类型
(gdb) run
Starting program: /home/jzddyy/opencv-3.4.11/my_build_dir/RevertNum
Please input a number :100
Breakpoint 1, main () at RevertNum.c:12
12 printf("After revert : ");
(gdb) whatis iNum
type = int
(gdb) print iNum
$1 = 100
(gdb)
继续执行并打印iNum的值
(gdb) c
Continuing.
Breakpoint 2, ShowRevertNum (iNum=100) at RevertNum.c:3
3 { while (iNum > 10)
(gdb) p iNum
$2 = 100
单步调试
(gdb) s
4 { printf("%d", iNum % 10);
(gdb) s
__printf (format=0x555555554894 "%d") at printf.c:28
可知这一步出现错误,通过分析发现,应该在代码的while条件中将>改为>=,消除10这一数字得到的不正确结果,修改后的代码如下:
#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); }
编译后执行的结果如下:
(gdb) run
Starting program: /home/jzddyy/opencv-3.4.11/my_build_dir/RevertNum
Please input a number :100
After revert : 001
[Inferior 1 (process 19455) exited normally]
输入100得到了正确的输出001
GDB段错误调试
创建segDemo.c文件
jzddyy@ubuntu:~$ nano segDemo.c
#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);
}
运行段错误
jzddyy@ubuntu:~$ nano segDemo.c
jzddyy@ubuntu:~$ gcc -o segDemo -g segDemo.c
zddyy@ubuntu:~$ ./segDemo
Please input a string:Neuedu
Segmentation fault (core dumped)
gdb) print pStr
$2 = 0x0
(gdb) b 11
Breakpoint 1 at 0x6d3: file segDemo.c, line 11.
(gdb) r
Starting program: /home/jzddyy/segDemo
Please input a string:Neuedu
Program received signal SIGSEGV, Segmentation fault.
_IO_gets (buf=0x0) at iogets.c:53
53 iogets.c: No such file or directory.
(gdb) set variable pStr=&acBuf
(gdb) n
opencv的使用
参考学姐博客安装opencv
https://cungudafa.blog.youkuaiyun.com/article/details/84451066
1.编写一个test1.cpp实现打开图片进行特效显示的代码
jzddyy@ubuntu:~$ nano test1.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;
}
编译过程中遇到以下提示,进行安装
jzddyy@ubuntu:~$ gcc test1.cpp -o test1 `pkg-config --cflags --libs opencv`
Command 'pkg-config' not found, but can be installed with:
sudo apt install pkg-config
sudo apt install pkgconf
2.编写一个打开摄像头显示处理视频的程序代码
新建一个camera.cpp文件
jzddyy@ubuntu:~$ touch camera.cpp
jzddyy@ubuntu:~$ sudo gedit /camera.cpp
代码如下所示
#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;
}
然后根据上述编译方法编译为可执行文件
打开如下所示
问题一:如果要求打开你硬盘上一个视频文件来播放,请问第7行代码如何修改?
改为:VideoCapture
capture(“test.mp4”);
问题二:在第9行的while循环中,Mat是一个什么数据结构? 为什么一定要加一句waitKey延时代码,删除它行不行?
Mat是一个类。由两部分数据组成:矩阵头(包括矩阵尺寸、存储方法、存储地址等信息)和一个指向所有像素值的矩阵(根据所选存储方法不同,矩阵可以是不同的维数)的指针
使用OpenCV的imshow函数显示图片,必须配合waitKey 函数使用,才能将图片显示在windows窗体上。否则,imshow 函数单独使用只能弹出空白窗体,而无法显示图片
总结
1.在opencv的操作中,将所编译的文件放在opencv文件夹中执行。
2.若gcc出现问题,编译不成功可将gcc改为g++。
3.摄像头打开若出现问题,可右键点击ubuntu打开设备,在设置中将USB调成3.0.。