Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

It turns out for cvtColor the destination matrix will always be allocated new memory so it will never point to the same data as the source. I was curious so I wrote a short test code:

void DoStuff(const Mat& img)
{
    Mat gray = img;

    cvtColor(img, gray, COLOR_BGR2GRAY);

    cout << "    gray: " << gray << endl;
    cout << "    img.data: " << (void*)img.data << endl;
    cout << "    gray.data: " << (void*)gray.data << endl;
}

int main()
{
    Mat img = Mat(1,1,CV_8UC3);

    img.at<Vec3b>(0)[0] = 0;
    img.at<Vec3b>(0)[1] = 100;
    img.at<Vec3b>(0)[2] = 200;

    cout << "img before: " << img << endl;

    DoStuff(img);

    cout << "img after: " << img << endl;
}

results in

img before: [0, 100, 200]
    gray: [118]
    img.data: 0x14eeb10
    gray.data: 0x14eeb40
img after: [0, 100, 200]