You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.9 KiB
C++

#include "opencv2/core.hpp"
#include <QDebug>
using namespace cv;
void core__mat_the_basic_image_container()
{
QTextStream cout(stdout);
Mat M(2,2, CV_8UC3, Scalar(0,0,255));
// qDebug() << "M = " << endl << " " << M << endl << endl;
//! [constructor]
// create by using the create function()
//! [create]
M.create(4,4, CV_8UC(2));
// qDebug() << "M = "<< endl << " " << M << endl << endl;
//! [create]
// create multidimensional matrices
//! [init]
int sz[3] = {2,2,2};
Mat L(3,sz, CV_8UC(1), Scalar::all(0));
//! [init]
// Cannot print via operator <<
// Create using MATLAB style eye, ones or zero matrix
//! [matlab]
Mat E = Mat::eye(4, 4, CV_64F);
// qDebug() << "E = " << endl << " " << E << endl << endl;
Mat O = Mat::ones(2, 2, CV_32F);
// qDebug() << "O = " << endl << " " << O << endl << endl;
Mat Z = Mat::zeros(3,3, CV_8UC1);
// qDebug() << "Z = " << endl << " " << Z << endl << endl;
//! [matlab]
// create a 3x3 double-precision identity matrix
//! [comma]
Mat C = (Mat_<double>(3,3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
// qDebug() << "C = " << endl << " " << C << endl << endl;
//! [comma]
// do the same with initializer_list
#ifdef CV_CXX11
//! [list]
C = (Mat_<double>({0, -1, 0, -1, 5, -1, 0, -1, 0})).reshape(3);
// qDebug() << "C = " << endl << " " << C << endl << endl;
//! [list]
#endif
//! [clone]
Mat RowClone = C.row(1).clone();
// qDebug() << "RowClone = " << endl << " " << RowClone << endl << endl;
//! [clone]
// Fill a matrix with random values
//! [random]
Mat R = Mat(3, 2, CV_8UC3);
randu(R, Scalar::all(0), Scalar::all(255));
//! [random]
// Demonstrate the output formatting options
//! [out-default]
// qDebug() << "R (default) = " << endl << R << endl << endl;
//! [out-default]
//! [out-python]
qDebug() << "R (python) = " << endl << format(R, Formatter::FMT_PYTHON) << endl << endl;
//! [out-python]
//! [out-numpy]
qDebug() << "R (numpy) = " << endl << format(R, Formatter::FMT_NUMPY ) << endl << endl;
//! [out-numpy]
//! [out-csv]
qDebug() << "R (csv) = " << endl << format(R, Formatter::FMT_CSV ) << endl << endl;
//! [out-csv]
//! [out-c]
qDebug() << "R (c) = " << endl << format(R, Formatter::FMT_C ) << endl << endl;
//! [out-c]
//! [out-point2]
Point2f P(5, 1);
// qDebug() << "Point (2D) = " << P << endl << endl;
//! [out-point2]
//! [out-point3]
Point3f P3f(2, 6, 7);
// qDebug() << "Point (3D) = " << P3f << endl << endl;
//! [out-point3]
//! [out-vector]
std::vector<float> v;
v.push_back( (float)CV_PI); v.push_back(2); v.push_back(3.01f);
// qDebug() << "Vector of floats via Mat = " << Mat(v) << endl << endl;
//! [out-vector]
//! [out-vector-points]
std::vector<Point2f> vPoints(20);
for (size_t i = 0; i < vPoints.size(); ++i)
vPoints[i] = Point2f((float)(i * 5), (float)(i % 7));
// qDebug() << "A vector of 2D Points = " << vPoints << endl << endl;
//! [out-vector-points]
}