Opencv 2.* 和 1. * 的區(qū)別:2.* 主要是c++的接口顾复,1.* 主要是c接口。
API概念
1. cv命名空間
using namespace cv;
2. 自動(dòng)內(nèi)存管理
Mat等大的數(shù)據(jù)類型都是通過(guò)引用計(jì)數(shù)管理內(nèi)存衷模。你可以利用Mat::clone真的進(jìn)行復(fù)制。
// 創(chuàng)建一個(gè)8M的矩陣
Mat A(1000, 1000, CV_64F);
// 創(chuàng)建同一個(gè)矩陣的一個(gè)引用蒲赂,是即時(shí)生效的
Mat B = A;
// 創(chuàng)建對(duì)A的第三行引用阱冶,同樣不進(jìn)行復(fù)制
Mat C = B.row(3);
// 真的復(fù)制
Mat D = B.clone();
// 復(fù)制A的第五行到第3行
B.row(5).copyTo(C);
// now let A and D share the data; after that the modified version
// of A is still referenced by B and C.
A = D;
// now make B an empty matrix (which references no memory buffers),
// but the modified version of A will still be referenced by C,
// despite that C is just a single row of the original A 即A仍然不會(huì)釋放
B.release();
// finally, make a full copy of C. As a result, the big modified
// matrix will be deallocated, since it is not referenced by anyone 即 A 被釋放掉
C = C.clone();
對(duì)于其他沒(méi)有把自動(dòng)銷毀考慮在內(nèi)的數(shù)據(jù)類型,可以利用Ptr模板滥嘴。
//不是這樣:T* ptr = new T(...);
Ptr<T> ptr(new T(...));
Ptr<T> ptr = makePtr<T>(...);
這樣就可以進(jìn)行引用計(jì)數(shù)了木蹬。
3. 輸出的自動(dòng)分配
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0);
if(!cap.isOpened()) return -1;
Mat frame, edges;
namedWindow("edges",1);
for(;;)
{
cap >> frame;
cvtColor(frame, edges, COLOR_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
return 0;
}
frame,edges是自動(dòng)分配的輸出矩陣若皱。除非類型镊叁、大小變化,否則只分配一次走触。
有個(gè)別方法不符合自動(dòng)分配機(jī)制晦譬,比如 cv::mixChannels, cv::RNG::fill 。你需要自己事先分配互广。
4. 飽和運(yùn)算
常用的像素類型是uchar
就支持飽和運(yùn)算:I(x,y)=min(max(round(r),0),255)
敛腌。opencv代碼是I.at<uchar>(y, x) = saturate_cast<uchar>(r)
卧土。對(duì)于8s,16s和u類型同樣適用像樊。不適用32位整數(shù)尤莺。
5. 固定的像素類型,有限的模板
只有少數(shù)的簡(jiǎn)單模板生棍。像generic Ptr<> implementation , saturate_cast<>()
還有一些像素接觸操作炼幔。
基礎(chǔ)數(shù)據(jù)類型:
8-bit unsigned integer (uchar)
8-bit signed integer (schar)
16-bit unsigned integer (ushort)
16-bit signed integer (short)
32-bit signed integer (int)
32-bit floating-point number (float)
64-bit floating-point number (double)
枚舉:enum { CV_8U=0, CV_8S=1, CV_16U=2, CV_16S=3, CV_32S=4, CV_32F=5, CV_64F=6 };
Mat mtx(3, 3, CV_32F); // make a 3x3 floating-point matrix
Mat cmtx(10, 1, CV_64FC2); // make a 10x1 2-channel floating-point
// matrix (10-element complex vector)
Mat img(Size(1920, 1080), CV_8UC3); // make a 3-channel (color) image
// of 1920 columns and 1080 rows.
Mat grayscale(image.size(), CV_MAKETYPE(image.depth(), 1)); // make a 1-channel image of
// the same size and same
// channel type as img
越復(fù)雜的算法支持的數(shù)據(jù)類型越少音五,如人臉識(shí)別只支持8位的灰度或彩色圖像。
6. InputArray and OutputArray
Many OpenCV functions process dense 2-dimensional or multi-dimensional numerical arrays. Usually, such functions take cppMat as parameters, but in some cases it's more convenient to use std::vector<> (for a point set, for example) or Matx<> (for 3x3 homography matrix and such).To avoid many duplicates in the API, special "proxy" classes have been introduced. The base "proxy" class is InputArray.