一、opencv 常用库(https://docs.opencv.org/3.4.0/modules.html)
opencv_core
opencv_improc
opencv_highgui
opencv_features2d
opencv_calib3d(calibration+3d)
opencv_video
opencv_objdetect
opencv_stitching
opencv_ml
opencv_contrib
opencv_flann
opencv_nonfree
二、基本数据结构
(1)Point
成员函数x,y,z
typedef Point2i Point;typedef Point_<float> Point2f;typedef Point_<double> Point2d;
typedef Point3_<int> Point3i;typedef Point3_<float> Point3f;typedef Point3_<double> Point3d;
Point A(20,30);
Point B;
B.x=20;
B.y=30;
(2)Mat
浅拷贝:指向同一内存
Mat A;
Mat img;
A=img;
Mat B(img);
深拷贝:单独开辟内存空间
Mat img;
Mat A,B;
A=img.clone();//不管什么都重新申请空间
img.copyTo(B);//没指定空间,则重新申请
Mat C=img.col(1);
img.col(0).copyTo(C)//前面已经指定空间,则不必申请
(3)Size、Rect
Size成员函数:width,height
Rect成员函数:x,y,width,height
Size A(20,30);
Size B;
B.width=20;
B.height=30;
(4)Scalar
emplate class for a 4-element vector derived from Vec.
Scalar bgr(B,G,R);//省略透明通道
三、linux下基本运行
linux终端运行
g++ test_opencv.cpp -o test_opencv `pkg-config --cflags --libs opencv`
./test_opencv
使用makefile文件
//makefile-opencv
INCLUDE = $(shell pkg-config --cflags opencv)
LIBS = $(shell pkg-config --libs opencv)
SOURCES = test_opencv.cpp
OBJECTS = $(SOURCES:.cpp=.o) #$:申明变量,OBJECTS为中间类型,.cpp=.o意思是替换
TARGET = TEST_OPENCV #可执行文件
$(TARGET):$(OBJECTS) #:意思是依赖于,并且可以自动推导(由.o文件推导出.c,即可以不写.c文件)
g++ -o $(TARGET) $(OBJECTS) -I $(INCLUDE) $(LIBS)
$(OBJECTS):$(SOURCES)
.PHONY:clean #伪目标,防止执行make clean 文件名与clean重名
clean:
-rm $(OBJECTS) $(TARGET) #-表示即使有部分错误也执行
#%.o:%.cpp # %意思是匹配0或若干字符
# g++ -I $(INCLUDE) -O $@ -c $< #@表示目前规则中所有目标的集合 <表示所有依赖项
四:example
/*
*
* cvout_sample just demonstrates the serial out capabilities of cv::Mat
* That is, cv::Mat M(...); cout << M; Now works.
*
*/
#include "opencv2/core.hpp"
#include <iostream>
using namespace std;
using namespace cv;
static void help()
{
cout
<< "\n------------------------------------------------------------------\n"
<< " This program shows the serial out capabilities of cv::Mat\n"
<< "That is, cv::Mat M(...); cout << M; Now works.\n"
<< "Output can be formated to OpenCV, matlab, python, numpy, csv and \n"
<< "C styles Usage:\n"
<< "./cvout_sample\n"
<< "------------------------------------------------------------------\n\n"
<< endl;
}
int main(int argc, char** argv)
{
cv::CommandLineParser parser(argc, argv, "{help h||}");//命令行解析,名称及简称|默认值|帮助信息
if (parser.has("help"))
{
help();
return 0;
}
Mat I = Mat::eye(4, 4, CV_64F);
I.at<double>(1,1) = CV_PI;
cout << "I = \n" << I << ";" << endl << endl;
Mat r = Mat(10, 3, CV_8UC3);
randu(r, Scalar::all(0), Scalar::all(255));
cout << "r (default) = \n" << r << ";" << endl << endl;
cout << "r (matlab) = \n" << format(r, Formatter::FMT_MATLAB) << ";" << endl << endl;
cout << "r (python) = \n" << format(r, Formatter::FMT_PYTHON) << ";" << endl << endl;
cout << "r (numpy) = \n" << format(r, Formatter::FMT_NUMPY) << ";" << endl << endl;
cout << "r (csv) = \n" << format(r, Formatter::FMT_CSV) << ";" << endl << endl;
cout << "r (c) = \n" << format(r, Formatter::FMT_C) << ";" << endl << endl;
Point2f p(5, 1);
cout << "p = " << p << ";" << endl;
Point3f p3f(2, 6, 7);
cout << "p3f = " << p3f << ";" << endl;
vector<float> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
cout << "shortvec = " << Mat(v) << endl;
vector<Point2f> points(20);
for (size_t i = 0; i < points.size(); ++i)
points[i] = Point2f((float)(i * 5), (float)(i % 7));
cout << "points = " << points << ";" << endl;
return 0;
}