Ask Your Question

Revision history [back]

mat assignment/copy not apply to me code

Hi,

im expecting the mat assignment method only copies the mat headers, which lead to any operation of the child matrix would lead to change of the root mat. However, this is not happening for me code. Could someone please take a look where my bug is? thanks.

My code end up only Image B displayed as grayscale picture, A and C are colored.

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out <Image_Path>\n");
        return -1;
    }

    Mat A;

    A = imread( argv[1], IMREAD_COLOR );
    Mat B(A);
    Mat C = A;

    if ( !A.data )
    {
        printf("No image data \n");
        return -1;
    }

    cvtColor(B, B, COLOR_BGR2GRAY);
    //A = A(Range::all(), Range(1,8));
    cout<<"MAT A: \n"<<A(Range(1,5), Range(1,5))<<"\n \n";
    cout<<"MAT B: \n"<<B(Range(1,5), Range(1,5))<<"\n \n";
    cout<<"MAT C: \n"<<C(Range(1,5), Range(1,5))<<"\n \n";

    namedWindow("Display A", WINDOW_AUTOSIZE );
    imshow("Display A", A);

    namedWindow("Display B", WINDOW_AUTOSIZE );
    imshow("Display B", B);

    namedWindow("Display C", WINDOW_AUTOSIZE );
    imshow("Display C", C);
    waitKey(0);

    return 0;
}