catalogue
1, Static library generated by gcc and its application
2, Dynamic library generated by gcc and its application
1. Creation of dynamic library:
2.1 create four files: (sub1.c, sub2.c, sub.h, main.c)
2.2 using static libraries in programs
2.3 running dynamic library in program
2.4 comparison of files generated by static library and dynamic library
4, Installation and use of opencv
1, Static library generated by gcc and its application1. Preparation process
Create three documents: hello.c, hello.h, and main.c:
hello.c
#include<stdio.h> void hello(const char *name) { printf("Hello %s\n",name); }
hello.h
#ifndef HELLO_H #define HELLO_H void hello(const char *name); #endif//HELLO_H
main.c
#include"hello.h" int main() { hello("everyone"); return 0; }
Then compile with gcc to get the hello.o file: gcc -c hello.c
You can enter ls again to view the file directory to see if there is hello.o:
2. Creation of static libraryar -crv libmyhello.a hello.o
Then you can ls check to see if a static library has been generated
3. Use of static libraries
gcc -o hello main.c -L. -lmyhello
Then execute. / hello, and the result is as follows:
gcc main.c libmyhello.a -o hello
Similarly, execute. / hello, and the results are as follows:
gcc -o hello main.c libmyhello.a
Similarly, execute. / hello, and the results are as follows:
Then try to delete the static library rm libmyhello.a, run ls to see if there is still a static library in the file directory, and finally run the executable file to see if hello everyone:
2, Dynamic library generated by gcc and its application1. Creation of dynamic library:
gcc -shared -fPIC -o libmyhello.so hello.o
Then you can ls check the directory:
2. Use of dynamic library
gcc -o hello main.c -L. -lmyhello
Then, you can ls check the file directory to see if a dynamic directory is generated. Finally, execute. / hello, and an error occurs:
Solution: copy libmyhello.so to the directory / usr/lib
mv libmyhello.so /usr/lib
Note: when static library and dynamic library exist at the same time, the program will give priority to the use of dynamic library.
3, Instance operation1. Example 1:
Create four files: (A1.c, A2.c, A.h, test.c)
#include<stdio.h> void print1(int arg) { printf("A1 print arg:%d\n",arg); }
#include<stdio.h> void print2(char *arg) { printf("A2 printf arg:%s\n",arg); }
#ifndef A_H #define A_H void print1(int); void print2(char *); #endif
#include<stdio.h> #include"A.h" int main() { print1(1); print2("test"); return 0; }
Using static libraries in programs:
gcc -c A2.c gcc -c A1.c ar crv libfile.a A1.o A2.o gcc -o test test.c libfile.a
The results are as follows:
Using dynamic libraries in programs:
rm libfile.a gcc -shared -fPIC -o libfile.so A1.o A2.o gcc -o test test.c libfile.so sudo mv libfile.so /usr/lib ./test
ls can be interspersed in the to see whether the file directory master statement is executed correctly:
2. Example 2
2.1 create four files: (sub1.c, sub2.c, sub.h, main.c)
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<stdio.h> #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=%.2f\n",x2x(a,b)); printf("a/b=%.2f\n",x2y(a,b)); }
2.2 using static libraries in programs
ar crv libsub.a sub1.c sub2.c ar crv libsub.a sub1.o sub2.o//Run the previous statement before running this statement, otherwise an error will be reported and the reason is unknown gcc -o main main.c libsub.a
As shown in the figure:
2.3 running dynamic library in program
gcc -shared -fPIC-o libsub.so sub1.o sub2.o gcc -o main main.c libsub.so
As shown in the figure:
2.4 comparison of files generated by static library and dynamic library
Use the "ls -l" or "ls -al" or "ll" command under linux to view the file and directory details
Finally, it is found that the static library is much smaller than the dynamic library, and there are small differences in the generated execution files
4, Installation and use of opencv4.1 installation of opencv
Referring to this blog, the installation tutorial is very detailed. If you set a password, you will encounter [sudo] password for task: You need to enter your own password, but the password will not be displayed on the page. You can enter it correctly
4.2 use of pencv
4.2.1 using pictures
First create a code folder, then enter the folder and create a test1.cpp in the code folder
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; }
Add a photo in the code folder and name it lena.jpg (you can add it directly by dragging the one in the computer file to unbuntu)
Then execute the following command:
g++ test1.cpp -o test1 `pkg-config --cflags --libs opencv` ./test1
It can be seen that different effects are presented:
4.2.2 video examples
After shutting down the virtual machine, use the shortcut key Win+R, enter services.msc, and click OK:
Then find the VMware USB Arbitration S... Service to see if it is started, click the settings in the virtual machine, select the USB controller, change the USB compatibility to USB3.1, and click OK
Then operate as shown in the figure:
After completing the above steps, check whether the chart at the bottom right of the virtual machine lights up a small green dot. If so, the connection is successful:
Then create a test2.cpp file:
test2.cpp
#include <opencv2/opencv.hpp> using namespace cv; int main() { //Read video from camera VideoCapture capture("man.mp4"); //Cycle through each frame while(1){ Mat frame;//Define a Mat variable to store the image of each frame capture >> frame;//Read current frame if(frame.empty())//Play finished, exit break; imshow("Read video frame",frame);//Displays the current frame waitKey(30);//Cover up 30ms } system("pause"); return 0; }
Prepare a small video named man.mp4 in the code folder: (some MP4 will not play, you can choose to decode or convert MP4 to avi)
Just compile the file:
g++ test2.cpp -o test2 `pkg-config --cflags --libs opencv` ./test2
4.2.3 video recording examples
Similarly, create a test3.cpp in the code folder
test3.cpp
/********************************************************************* Turn on the computer camera, control the video recording with a blank space, and ESC exits and saves the video 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() { //Turn on the computer camera VideoCapture cap(0); if (!cap.isOpened()) { cout << "error" << endl; waitKey(0); return 0; } //Get the resolution of 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;//Record keyboard keys char startOrStop = 1;//0 starts recording video; 1 end recording video char flag = 0;//Recording flag 0 - not recording; 1 - recording while (1) { cap >> frame; key = waitKey(100); if (key == 32)//Press the space to start recording and pause recording to switch back and forth { startOrStop = 1 - startOrStop; if (startOrStop == 0) { flag = 1; } } if (key == 27)//Press ESC to exit the whole program and save the video file to disk { 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; }
Save delete! Then compile:
g++ test3.cpp -o test3 `pkg-config --cflags --libs opencv` ./test3
The results are as follows: (an. avi file is generated and frames are generated continuously.)
5, SummaryThrough the operation of static library and dynamic library, we realized the difference between static and dynamic. We also encountered many problems in installing opencv. We solved them through our own online query and inquiry, and exercised our self-learning ability. Then we also had problems when editing pictures, playing videos and recording videos, such as how to convert videos into avi, But when you explore patiently, the problem will always be solved.