目录
一.? 学习并掌握可执行程序的编译、组装过程。
1.先前准备
2.静态库链接
?3.动态库链接
?二.opencv下载
?1.图片
2.视频
一.? 学习并掌握可执行程序的编译、组装过程。
1.先前准备
main.c
?x2x.c
.x2y.c
2.静态库链接
.先gcc -c main.c
gcc -c x2x.c
gcc -c x2y.c
然后ar -crv libmyx.a x2x.o x2y.o
然后gcc -o xx main.c libmyx.a(xx为重命名名字)
最后./xx
输出结果如下
通过静态库链接生成的可执行程序的大小
为8.4kb(8416字节)
?3.动态库链接
通过命令
gcc -shared -fPIC -o libmyx.so x2x.o x2y.o
生成动态库文件,命令
gcc main.c libmyx.so -o xx2
结果如下
通过动态库链接生成的可执行程序的大小
为8.4kb(8360字节)
可以看出相对与静态库链接少了56字节
?二.opencv下载
详情见教程链接:?opencv安装教程
?1.图片
#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("flower.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;
}
text代码?
#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("flower.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;
}
再在code文件夹中准备flower.jpg文件
?
2.视频
再次在code文件夹下打开命令行,新建test2.cpp文件 test2.cpp
/*********************************************************************
打开电脑摄像头,空格控制视频录制,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;
}
//获得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;
}
g++ test2.cpp -o test2 `pkg-config --cflags --libs opencv`
?
视频保存成功
?参考: 1.OpenCV使用示例 2.Ubuntu18.04下OpenCV3.4.11的安装及使用示例 3.gcc生成静态库.a和动态库.so
|