Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 come 4 original images are getting copied to 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".

like image 272
skm Avatar asked Jul 28 '15 10:07

skm


1 Answers

The left cat consists of the odd numbered lines and the right cat consists of the even numbered lines of the original picture. This is then doubled, so that there are two more cats underneath. The new cats have half the number of lines of the original cat.

The new picture is laid out like this:

line 1  line 2
line 3  line 4
line 5  line 6
...     
line n-1 line n
line 1  line 2
line 3  line 4
line 5  line 6
...     
line n-1 line n
like image 112
Klas Lindbäck Avatar answered Oct 11 '22 06:10

Klas Lindbäck