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:
Result
Expected Results:
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".
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With