Linux calls OpenCV programming (i) __HTML5

Source: Internet
Author: User

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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.