OPENCV3.3---introduction

3665 ワード

Opencv 2.* および1.*の違い:2.*主にc++のインタフェース、1.*主にcインタフェースです.
API概念
1.cvネーミングスペース
using namespace cv;

2.自動メモリ管理
Matなどの大きなデータ型は,いずれも参照カウントによりメモリを管理する.Mat::cloneを利用して本当にコピーすることができます.
//     8M   
Mat A(1000, 1000, CV_64F);
//             ,      
Mat B = A;
//    A      ,       
Mat C = B.row(3);
//     
Mat D = B.clone();
//   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      
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();

自動破棄を考慮していない他のデータ型については,Ptrテンプレートを利用することができる.
//    :T* ptr = new T(...);
Ptr ptr(new T(...));
Ptr ptr = makePtr(...);

これで参照カウントができます.
3.出力の自動配分
#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は自動的に割り当てられた出力マトリクスである.タイプ、サイズが変化しない限り、一度だけ割り当てます.cv::mixChannels,cv::RNG::fillなどの自動割り当てメカニズムに合致しない方法があります.自分で事前に割り当てる必要があります.
4.飽和演算
一般的な画素タイプは、ucharであり、飽和演算:I(x,y)=min(max(round(r),0),255)をサポートする.OpencvコードはI.at(y, x) = saturate_cast(r)です.8 s,16 s,uタイプについても同様に適用される.32ビット整数は適用されません.
5.固定画素タイプ、限られたテンプレート
単純なテンプレートは少数しかありません.generic Ptr<> implementation , saturate_cast<>()のように、画素接触動作もいくつかあります.基礎データ型:
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

複雑なアルゴリズムほどサポートされるデータ型は少なくなり、例えば、顔認識は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.