Linux under OpenCV programming
Two methods of compiling and running OPENCV program in Linux environment
First method: Command line (methods using command-line arguments)
XX. C:GCC ' pkg-config--cflags--libs opencv '-o img_1 img_1.c xx. cpp:g++ ' pkg-config--cflags--libs opencv '-O imgshow imgshow.cpp
(where ' is not a single quotation mark in the English state, but a "'" symbol on a key on the keyboard "~")
The second method: using the CMake tool recommended by OPENCV
Step1: Create a new directory to store the relevant pictures in our code and program.
Step2: Add the file CMakeLists.txt required when the CMake tool is compiled.
STEP3: Let's Enter the command "CMake." Compile the current project.
1, Example 1
The code for IMG_1.C is as follows:
#include
OpenCV C-language compilation command:
#gcc ' pkg-config--cflags--libs opencv '-O img_1 img_1.c
Program execution:
#./img_1 lena.jpg
Example 2
The code for Imgshow.cpp is as follows:
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace CV;
using namespace std;
int main (int argc, char**argv)
{
Mat src=imread ("Lena.jpg", Cv_load_image_color); Namedwindow ("Hello"); Imshow ("Hello", SRC); cout<< "Press ' Q ' to quit ..." <<endl; while (1) {if (char (Waitkey (1)) = = ' Q ') break;} destroyallwindows (); return 0; }
OpenCV C-language compilation command:
#g + + ' pkg-config--cflags--libs opencv '-O imgshow imgshow.cpp
Program execution:
#./imgshow
Example 3
Grayscale of color image
The Test_1.cpp code is as follows:
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace CV;
using namespace std;
int main (int argc,char **argv)
{
Mat src=imread (argv[1));
if (!src.data)
{
cout<< "image to load failure!" <<endl;
return-1;
}
Namedwindow ("src");
Imshow ("src", src);
Mat DST;
Cvtcolor (Src,dst,cv_bgr2gray);
Namedwindow ("Gray");
Imshow ("Gray", DST);
cout<< "Press ' Q ' to quit ..." <<endl;
while (1)
{
if (char (Waitkey (1)) = = ' Q ') break
;
Destroyallwindows ();
return 0;
}
Perform:
#g + + ' pkg-config--cflags--libs opencv '-O test_1 test_1.cpp
#./test_1 lena.jpg
Example 4
The binary value of color image
The code for Test2.cpp is as follows:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace CV;
int main (int argc,char **argv)
{
Mat src=imread (argv[1));
if (!src.data)
{
cout<< "image to load failure!" <<endl;
return-1;
}
Namedwindow ("src");
Imshow ("src", src);
Mat DST;
Cvtcolor (Src,dst,cv_bgr2gray);
Namedwindow ("Gray");
Imshow ("Gray", DST);
Mat bin;
Threshold (dst,bin,110,250,thresh_binary);
Namedwindow ("bin");
Imshow ("Bin", bin);
cout<< "Press ' Q ' to quit ..." <<endl;
while (1)
{
if (char (Waitkey (1)) = = ' Q ') break
;
Destroyallwindows ();
return 0;
}
Implementation: (1) g++ ' pkg-config--cflags--libs opencv '-O test2 test2.cpp
(2)./test2 lena.jpg
The effect is shown in the following illustration