目录
一、GCC生成静态库.a与动态库.so
(1)准备过程
(2)静态库使用
(3)使用动态库
二、使用作业一代码做静态库,动态库比较
三、opencv的编程
(1)图片
(2)播放视频
(3)录制视频
四、总结
一、GCC生成静态库.a与动态库.so
(1)准备过程
1.创建目录
2.创建代码
hello.h
#ifndef HELLO_H #define HELLO_H void hello(const char *name); #endif//HELLO_H
hello.c
#includevoid hello(const char *name) { printf("Hello %sn",name); }
main.c
#include"hello.h"
int main()
{
hello("everyone");
return 0;
}
3.得到.o文件
(2)静态库使用
1.创建与使用静态库
创建静态库的工具:ar
命名规范:以lib作为前缀,是.a文件
ar -crv libmyhello.a hello.o
gcc -o hello main.c -L. -lmyhello
gcc main.c libmyhello.a -o hello
main.o gcc -c main.c
gcc -o hello main.c libmyhello.a
(3)验证静态库的特点
在删掉静态库的情况下,运行可执行文件,发现程序仍旧正常运行,表明静态库跟程序执行没有联系。同时,也表明静态库是在程序编译的时候被连接到代码中的。
(3)使用动态库
注意:
创建动态库的工具:gcc
动态文件命名规范:以lib作为前缀,是.so文件
gcc编译得到.o文件 gcc -c hello.c
创建静态库 ar -crv libmyhello.a hello.o
创建动态库 gcc -shared -fPIC -o libmyhello.so hello.o
使用库生成可执行文件 gcc -o hello main.c -L. -lmyhello
执行可执行文件 ./hello
二、使用作业一代码做静态库,动态库比较
1.代码
sub1.c
float x2x(int a,int b)
{
float c=0;
c=a+b;
return c;
}
sub2.c
float x2y(int a,int b)
{
float c=0;
c=a/b;
return c;
}
sub.h
#ifndef SUB_H #define SUB_H float x2x(int a,int b); float x2y(int a,int b); #endif
main.c
#include#include"sub.h" void main() { int a,b; printf("Please input the value of a:"); scanf("%d",&a); printf("Please input the value of b:"); scanf("%d",&b); printf("a+b=%.2fn",x2x(a,b)); printf("a/b=%.2fn",x2y(a,b)); }
静态库
动态库
生成文件之间的对比
静态库
动态库
静态库与动态库相比较小
三、opencv的编程
安装教程可参考:https://blog.csdn.net/ssj925319/article/details/109231145https://blog.csdn.net/ssj925319/article/details/109231145https://blog.csdn.net/ssj925319/article/details/109231145
(1)图片
首先创建一个代码存放在文件夹open
test.cpp
#include#include 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 height;i++) for (int j = 0;j 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; }
注意:图片名要与cvLoadImage()中一样
运行该程序后会生成 test.png
(2)播放视频
生成test2.cpp
#include#include using namespace cv; int main() { //从摄像头读取视频 VideoCapture capture("man.mp4"); //循环显示每一帧 while(1){ Mat frame;//定义一个Mat变量,用于存储每一帧的图像 capture >> frame;//读取当前帧 if(frame.empty())//播放完毕,退出 break; imshow("读取视频帧",frame);//显示当前帧 waitKey(30);//掩饰30ms } pause(); return 0; }
视频可以自己下载,我准备了自己命名的xxx.cpp
编译 test2.cpp 文件,并运行
g++ test2.cpp -o test2 `pkg-config --cflags --libs opencv`
./test2
(3)录制视频
test3.cpp
#include#include #include #include 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 (cap.get(CV_CAP_PROP_frame_WIDTH)); int h = static_cast (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; }
四、总结
1.通过三个程序用gcc生成静态库和动态库的练习过程,基本上能够熟练的生成静态库和动态库。在两种库的比较中,能够明显看出两者的差别。虽然,过程中,遇到一些小问题,但是很快就解决了。
2.在opencv的作业过程中需要调整系统设置:
使用快捷键 Win + R ,输入 services.msc ,并回车。
点击 “ 虚拟机 ” ,然后点击 “ 设置(S)… ”。选择 “ USB控制器 ” ,将 “ USB兼容性 ” 设置为 “ USB 3.0 ” ,并点击确定
选择 “ 虚拟机 ” ,再选择 “ 可移动设备 ” ,再选择 “ Quanta USB2.0 VGA UVC WebCam ” ,最后点击 “ 连接 ” ,再弹出的窗口内点击 “ 确定 ” 。



