了解由memcpy()完成的副本 [英] Understanding the copy done by memcpy()

查看:90
本文介绍了了解由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.

尽管这是一个非常简单的任务,并且我已经完成了,但是我想知道使用memcpy() 完成此任务所获得的奇怪结果.

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().

我的代码:

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;
}

原始图片:

结果

预期结果:

问题:当生成的图像仅是原始图像的两倍时,如何将4个原始图像复制到新图像?其次,memcpy()以行方式复制连续的内存位置,因此,根据我应该得到的图像显示在预期结果"中.

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.

新图片的布局如下:

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

这篇关于了解由memcpy()完成的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆