The Core Functionality1(Mat - The Basic Image Container)

Mat基本上是一个包含两个数据部分的类:矩阵头(包含矩阵大小,用于存储的方法,存储矩阵的地址等信息)以及包含像素值(维度取决于选择用于存储的方法)。矩阵头部大小是恒定的,但是矩阵本身的大小可能随图像而变化,并且通常比数量级大。


为了避免一系列的图像副本降低程序的速度,引用了reference counting system。通过令矩阵头中的指针指向相同的地址,从而分享矩阵数据,从而进一步的减小了内存消耗。因此普通的复制,赋值等操作只是令矩阵头中的指针指向了同一个矩阵,它们分享了数据。


根据官网改写的一个程序,用于测试!这个我这里是使用kDevelop编写的,所以稍微有点问题,需要修改下CMakeLists.txt文件了,同时编译成功后,需要把“lena.jpg”放入build文件中,要么修改“lena.jpg”文件的路径,我是懒的改!

#include <opencv2/opencv.hpp>

#include <iostream>
#include <string>


using namespace cv;
using namespace std;


int main( int argc, char** argv )
{
    String imageName( "../build/lena.jpg" ); // by default,稍加修改
    if( argc > 1)
    {
        imageName = argv[1];
    }
    Mat A,C;


    A = imread( imageName, IMREAD_COLOR );//三通道彩色图
    
    Mat B(A);                                 // Use the copy constructor
    C = A;                                    // Assignment operator
    
    
    Mat D (A, Rect(10, 10, 100, 100) ); // using a rectangle
    Mat E = A(Range::all(), Range(1,3)); // using row and column boundaries
    
    Mat F = A.clone(); //深度复制
    Mat G;
    A.copyTo(G); //深度复制
    
    namedWindow( "A", WINDOW_AUTOSIZE);
    namedWindow( "B", WINDOW_AUTOSIZE);
    namedWindow( "C", WINDOW_AUTOSIZE);
    namedWindow( "D", WINDOW_AUTOSIZE);
    namedWindow( "E", WINDOW_AUTOSIZE);
    namedWindow( "F", WINDOW_AUTOSIZE);
    namedWindow( "G", WINDOW_AUTOSIZE);


    //imshow( "A", A );
    //imshow( "B", B );
    //imshow( "C", C );
    //imshow( "D", D );
    //imshow( "E", E );
    //imshow( "F", F );
    //imshow( "G", G );
    
    cout << "Test Good!" << endl;
    waitKey(0);
    return 0;

}

测试代码的方式很多种,不一定要用这个使用vim也行,然后用cmake命令。CMakeLists.txt文件如下:

cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "-std=c++11")
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )   

install(TARGETS DisplayImage RUNTIME DESTINATION bin) #这句话什么意思,懒的查,目前也用不到,不使用kDevelop进行编译的话,使用也不会影响!

测试结果如下:好多大美女的照片!

啰嗦两句:kDevelop的使用,昨天刚会用,想要了解可以参考分类IDE:kevelop的内容。

图像的存储方法:

对于色彩方式,我们有更多的方法可供选择。它们中的每一个都将其分解为三个或四个基本组件,我们可以使用这些组合来创建其他组件。

最流行的是RGB,主要是因为这也是我们的眼睛如何构建颜色。它的基本颜色是红色,绿色和蓝色。要编码颜色的透明度,有时需要添加第四个元素:alpha(A)。

然而,还有许多其他颜色系统各有其优点:

  • RGB是最常见的,因为我们的眼睛使用类似的东西,但请记住,OpenCV标准显示系统使用BGR色彩空间(红色和蓝色通道的开关)组合颜色。
  • HSV和HLS将颜色分解为色调,饱和度和值/亮度分量,这是我们描述颜色的更自然的方式。例如,您可能会忽略最后一个组件,使您的算法对输入图像的光照条件不那么敏感。
  • YCrCb由流行的JPEG图像格式使用。
  • CIE L * a * b *是一个感知上均匀的颜色空间,如果您需要测量给定颜色与另一种颜色距离,则该颜色空间很方便

上述内容没啥意思!其他的所有内容均包含在下面的这个程序里:

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
    //1 
    Mat M(2,2, CV_8UC3, Scalar(0,0,255));
    cout << "M = " << endl << " " << M << endl << endl;
    
    //2
    int sz[3] = {2,2,2};
    Mat P(3,sz, CV_8UC(1), Scalar::all(0)); //cout useless
    
    //3
    Mat L;
    L.create(4,4, CV_8UC(2)); //the same as CV_8UC2
    cout << "L = "<< endl << " "  << L << endl << endl;
    cout << CV_8UC(2) << endl;  
    
    //4
    Mat E = Mat::eye(4, 4, CV_64F);
    cout << "E = " << endl << " " << E << endl << endl;
    Mat O = Mat::ones(2, 2, CV_32F);
    cout << "O = " << endl << " " << O << endl << endl;
    Mat Z = Mat::zeros(3,3, CV_8UC1);
    cout << "Z = " << endl << " " << Z << endl << endl;
    
    //5
    Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    cout << "C = " << endl << " " << C << endl << endl;
    //Mat C = (Mat_<double>({0, -1, 0, -1, 5, -1, 0, -1, 0})).reshape(3);
    //cout << "C = " << endl << " " << C << endl << endl;
    
    //6

    Mat RowClone = C.row(1).clone();
    cout << "RowClone = " << endl << " " << RowClone << endl << endl;
    Mat ColClone = C.colRange(0,1).clone();
    cout << "ColClone = " << endl << " " << ColClone << endl << endl;
    
    //7
    Mat R = Mat(3, 2, CV_8UC3);
    randu(R, Scalar::all(0), Scalar::all(255));
    
    //the way of display
    //1

    cout << "R (default) = " << endl << R << endl << endl;
    
    //2
    cout << "R (python)  = " << endl << format(R, Formatter::FMT_PYTHON) << endl << endl;
    
    //3
    cout << "R (csv)     = " << endl << format(R, Formatter::FMT_CSV ) << endl << endl;
    
    //4
    cout << "R (numpy)   = " << endl << format(R, Formatter::FMT_NUMPY ) << endl << endl;
    
    //5
    cout << "R (c)       = " << endl << format(R, Formatter::FMT_C ) << endl << endl;
    
    //Output of other common items
    Point2f P2(5, 1);
    cout << "Point (2D) = " << P2 << endl << endl;
    
    
    Point3f P3f(2, 6, 7);
    cout << "Point (3D) = " << P3f << endl << endl;
    
    vector<float> v;
    v.push_back( (float)CV_PI);   v.push_back(2);    v.push_back(3.01f);
    cout << "Vector of floats via Mat = " << Mat(v) << endl << endl;
    
    
    vector<Point2f> vPoints(20);
    for (size_t i = 0; i < vPoints.size(); ++i)
        vPoints[i] = Point2f((float)(i * 5), (float)(i % 7));
    cout << "A vector of 2D Points = " << vPoints << endl << endl;
    
    return 0;
}

此外,这个程序的CMakeLists.txt文件如下所示:

cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "-std=c++11")
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage main.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

install(TARGETS DisplayImage RUNTIME DESTINATION bin)

运行结果较多,就不放图了!

猜你喜欢

转载自blog.csdn.net/qq_27806947/article/details/80102937
今日推荐