mardi 28 juillet 2015

Understanding the copy done by memcpy()

I have to create an image which has two times the number of columns to that of the original image. Therefore, I have kept the width of the new image two times to that of the original image.

Although this is a very simple task and I have already done it but I am wondering about the strange results obtained by doing this task using memcpy().

My code:

int main()
{

    Mat image = imread("pikachu.png", 1);
    int columns = image.cols;
    int rows = image.rows;

    Mat twoTimesImage(image.rows, 2 * image.cols, CV_8UC3, Scalar(0));

    unsigned char *pSingleImg = image.data;
    unsigned char *ptwoTimes = twoTimesImage.data;

    size_t memsize = 3 * image.rows*image.cols;

    memcpy(ptwoTimes , pSingleImg, memsize);
    memcpy(ptwoTimes + memsize, pSingleImg, memsize);

    cv::imshow("two_times_image.jpg", twoTimesImage);

    return 0;
}

Original Image:

image1

Result

image2

Expected Results:

image3

Question: When the resulting image is just two times to that of the original image then, how can I get 4 original images inside the new image? Secondly, the memcpy() copies the continous memory location in a row-wise fashion so, according to that I should get an image which is shown in the "Expected results" (it is created in Paint so, please don't mind about the white background).

Aucun commentaire:

Enregistrer un commentaire