Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

For compiling using g++ see this answer

The tutorial you are working through is pretty old. Some of tutorial code will no longer work with the latest versions of OpenCV. Here is edited tutorial code which will work with OpenCV 3.4.0 (the latest version)

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"

#include <iostream>

using namespace cv;
using namespace std;

//will need a path to an image as a command line argument!
int main(int argc, char *argv[])
{
    //switched variablename C to Q to avoid redefinition !!!
    Mat A, Q;                                 // creates just the header parts
    A = imread(argv[1], CV_LOAD_IMAGE_COLOR); // here we'll know the method used (allocate matrix)

    Mat B(A);                                 // Use the copy constructor

    Q = A;                                    // Assignment operator

    Mat D(A, Rect(10, 10, 100, 100)); // using a rectangle
    //switched to variable name from E to X to avoid redefinition !!!
    Mat X = A(Range::all(), Range(1, 3)); // using row and column boundaries

    Mat F = A.clone();
    Mat G;
    A.copyTo(G);

    Mat M(2, 2, CV_8UC3, Scalar(0, 0, 255));
    cout << "M = " << endl << " " << M << endl << endl;

    int sz[3] = { 2,2,2 };
    Mat L(3, sz, CV_8UC(1), Scalar::all(0));

    //need path to an image here!!
    IplImage* img = cvLoadImage("put\\path\\here\\picture.png", 1);
    //conversion has to be performed using a cv::cvarrToMat function !!!
    Mat mtx(cvarrToMat(img)); // convert IplImage* -> Mat

    M.create(4, 4, CV_8UC(2));
    cout << "M = " << endl << " " << M << endl << endl;

    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;

    Mat C = (Mat_<double>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    cout << "C = " << endl << " " << C << endl << endl;

    Mat RowClone = C.row(1).clone();
    cout << "RowClone = " << endl << " " << RowClone << endl << endl;

    Mat R = Mat(3, 2, CV_8UC3);
    randu(R, Scalar::all(0), Scalar::all(255));

    cout << "R (default) = " << endl << R << endl << endl;

    //Formatter::FMT_PYTHON needed here !!!
    cout << "R (python)  = " << endl << cv::format(R, Formatter::FMT_PYTHON) << endl << endl;

    //Formatter::FMT_CSV needed here !!!
    cout << "R (csv)     = " << endl << format(R, Formatter::FMT_CSV) << endl << endl;

    //Formatter::FMT_NUMPY needed here !!!
    cout << "R (numpy)   = " << endl << format(R, Formatter::FMT_NUMPY) << endl << endl;

    //Formatter::FMT_C needed here !!!
    cout << "R (c)       = " << endl << format(R, Formatter::FMT_C) << endl << endl;

    Point2f P(5, 1);
    cout << "Point (2D) = " << P << 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;
}

For compiling using g++ see this answer

The You will need #include "opencv2/core.hpp" and #include "opencv2/highgui.hpp". Please note that this tutorial you are working through is pretty old. for OpenCV 2.4. Some of the tutorial code will no longer not work with the latest 3.X versions of OpenCV. Here is edited tutorial code which will work with OpenCV 3.4.0 (the (the latest version)version)

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"

#include <iostream>

using namespace cv;
using namespace std;

//will need a path to an image as a command line argument!
int main(int argc, char *argv[])
{
    //switched variablename C to Q to avoid redefinition !!!
    Mat A, Q;                                 // creates just the header parts
    A = imread(argv[1], CV_LOAD_IMAGE_COLOR); // here we'll know the method used (allocate matrix)

    Mat B(A);                                 // Use the copy constructor

    Q = A;                                    // Assignment operator

    Mat D(A, Rect(10, 10, 100, 100)); // using a rectangle
    //switched to variable name from E to X to avoid redefinition !!!
    Mat X = A(Range::all(), Range(1, 3)); // using row and column boundaries

    Mat F = A.clone();
    Mat G;
    A.copyTo(G);

    Mat M(2, 2, CV_8UC3, Scalar(0, 0, 255));
    cout << "M = " << endl << " " << M << endl << endl;

    int sz[3] = { 2,2,2 };
    Mat L(3, sz, CV_8UC(1), Scalar::all(0));

    //need path to an image here!!
    IplImage* img = cvLoadImage("put\\path\\here\\picture.png", 1);
    //conversion has to be performed using a cv::cvarrToMat function !!!
    Mat mtx(cvarrToMat(img)); // convert IplImage* -> Mat

    M.create(4, 4, CV_8UC(2));
    cout << "M = " << endl << " " << M << endl << endl;

    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;

    Mat C = (Mat_<double>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    cout << "C = " << endl << " " << C << endl << endl;

    Mat RowClone = C.row(1).clone();
    cout << "RowClone = " << endl << " " << RowClone << endl << endl;

    Mat R = Mat(3, 2, CV_8UC3);
    randu(R, Scalar::all(0), Scalar::all(255));

    cout << "R (default) = " << endl << R << endl << endl;

    //Formatter::FMT_PYTHON needed here !!!
    cout << "R (python)  = " << endl << cv::format(R, Formatter::FMT_PYTHON) << endl << endl;

    //Formatter::FMT_CSV needed here !!!
    cout << "R (csv)     = " << endl << format(R, Formatter::FMT_CSV) << endl << endl;

    //Formatter::FMT_NUMPY needed here !!!
    cout << "R (numpy)   = " << endl << format(R, Formatter::FMT_NUMPY) << endl << endl;

    //Formatter::FMT_C needed here !!!
    cout << "R (c)       = " << endl << format(R, Formatter::FMT_C) << endl << endl;

    Point2f P(5, 1);
    cout << "Point (2D) = " << P << 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;
}

For compiling using g++ see this answer

You will need #include "opencv2/core.hpp" and #include "opencv2/highgui.hpp". Please note that this tutorial is for OpenCV 2.4. Some of the tutorial code will not work with the 3.X versions OpenCV. Here is edited tutorial code which will work with OpenCV 3.4.0 (the latest version)). If you are using a 3.X version of OpenCV you should work through the updated tutorials here

#include "opencv2/core.hpp"
#include "opencv2/highgui.hpp"

#include <iostream>

using namespace cv;
using namespace std;

//will need a path to an image as a command line argument!
int main(int argc, char *argv[])
{
    //switched variablename C to Q to avoid redefinition !!!
    Mat A, Q;                                 // creates just the header parts
    A = imread(argv[1], CV_LOAD_IMAGE_COLOR); // here we'll know the method used (allocate matrix)

    Mat B(A);                                 // Use the copy constructor

    Q = A;                                    // Assignment operator

    Mat D(A, Rect(10, 10, 100, 100)); // using a rectangle
    //switched to variable name from E to X to avoid redefinition !!!
    Mat X = A(Range::all(), Range(1, 3)); // using row and column boundaries

    Mat F = A.clone();
    Mat G;
    A.copyTo(G);

    Mat M(2, 2, CV_8UC3, Scalar(0, 0, 255));
    cout << "M = " << endl << " " << M << endl << endl;

    int sz[3] = { 2,2,2 };
    Mat L(3, sz, CV_8UC(1), Scalar::all(0));

    //need path to an image here!!
    IplImage* img = cvLoadImage("put\\path\\here\\picture.png", 1);
    //conversion has to be performed using a cv::cvarrToMat function !!!
    Mat mtx(cvarrToMat(img)); // convert IplImage* -> Mat

    M.create(4, 4, CV_8UC(2));
    cout << "M = " << endl << " " << M << endl << endl;

    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;

    Mat C = (Mat_<double>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
    cout << "C = " << endl << " " << C << endl << endl;

    Mat RowClone = C.row(1).clone();
    cout << "RowClone = " << endl << " " << RowClone << endl << endl;

    Mat R = Mat(3, 2, CV_8UC3);
    randu(R, Scalar::all(0), Scalar::all(255));

    cout << "R (default) = " << endl << R << endl << endl;

    //Formatter::FMT_PYTHON needed here !!!
    cout << "R (python)  = " << endl << cv::format(R, Formatter::FMT_PYTHON) << endl << endl;

    //Formatter::FMT_CSV needed here !!!
    cout << "R (csv)     = " << endl << format(R, Formatter::FMT_CSV) << endl << endl;

    //Formatter::FMT_NUMPY needed here !!!
    cout << "R (numpy)   = " << endl << format(R, Formatter::FMT_NUMPY) << endl << endl;

    //Formatter::FMT_C needed here !!!
    cout << "R (c)       = " << endl << format(R, Formatter::FMT_C) << endl << endl;

    Point2f P(5, 1);
    cout << "Point (2D) = " << P << 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;
}