Opencv学習-load image and display

3011 ワード

OpenCV2.xバージョンには、Mat imreadやIplImage cvLoadImageなどのC++インタフェースがあります.
They are the two different interfaces (Mat/imread for C++ and Ipl... and Cv.. for C interface). The C++ interface is nicer, safer and easier to use. It automatically handles memory for you, and allows you to write less code for the same task. The OpenCV guys advocate for the usage of C++, unless some very specific project requirements force you to C.
今日からOpenCVをしっかり勉強します.
次はsampleの最初の例で、最も基本的なのは、画像を読み出して表示します.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);	// Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.

    waitKey(0);											 // Wait for a keystroke in the window
    return 0;
}

ここにいるよhppで宣言されたCV_EXPORTS_W Mat imread( const string& filename, int flags=1 ); CMakeListsを見てみました.txt
set(highgui_hdrs
    src/precomp.hpp
    src/utils.hpp
    src/cap_ffmpeg_impl.hpp
    )

set(highgui_srcs
    src/cap.cpp
    src/cap_images.cpp
    src/cap_ffmpeg.cpp
    src/loadsave.cpp
    src/utils.cpp
    src/window.cpp
    )

imreadはloadsaveにあるはずだ.cppで定義されているのは、やはり.
loadsave.cppで定義
Mat imread( const string& filename, int flags )
{
    Mat img;
    imread_( filename, flags, LOAD_MAT, &img );
    return img;
}

flags:読み込み画像の色と深さを指定します.
指定された色は、入力されたピクチャを3チャネルカラー(CV_LOAD_IMAGE_COLOR)、シングルチャネル階調(CV_LOAD_IMAGE_GRAYSCALE)、またはそのまま(CV_LOAD_IMAGE_ANYCOLOR)に変換することができる.
cvLoadImageも呼び出されました_imread.
についてimread, http://www.linuxidc.com/Linux/2013-09/90258.htm説明の詳細
次はMatの一部の声明です
class CV_EXPORTS Mat
{
public:
// ... a lot of methods ...
...

/*! includes several bit-fields:
- the magic signature
- continuity flag
- depth
- number of channels
*/
int flags;
//! the array dimensionality, >= 2
int dims;
//! the number of rows and columns or (-1, -1) when the array has more than 2 dimensions
int rows, cols;
//! pointer to the data
uchar* data;

//! pointer to the reference counter;
// when array points to user-allocated data, the pointer is NULL
int* refcount;

// other members
...
};

これらの基本的なメンバーはよく理解しています
私はlinuxの下で、次は私のshellスクリプトgoです.sh,1行
g++ $1 -o Display `pkg-config opencv --cflags --libs` 
そしてコンパイルして使えます!