在 cv2 python 中克隆图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16533078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:56:45  来源:igfitidea点击:

Clone an image in cv2 python

pythonopencv

提问by tintin

I'm new to opencv, here is a question, what is the python function which act the same as cv::clone() in cpp? I just try to get a rect by

我是 opencv 的新手,这里有一个问题,与 cpp 中的 cv::clone() 作用相同的 python 函数是什么?我只是想得到一个 rect

    rectImg = img[10:20, 10:20]

but when I draw a line on it ,I find the line appear both on img and the rectImage,so , how can I get this done?

但是当我在上面画一条线时,我发现这条线同时出现在 img 和 rectImage 上,那么,我该如何完成呢?

采纳答案by Abid Rahman K

If you use cv2, correct method is to use .copy()method in Numpy. It will create a copy of the array you need. Otherwise it will produce only a view of that object.

如果使用cv2,正确的方法是使用.copy()Numpy 中的方法。它将创建您需要的数组的副本。否则,它只会生成该对象的视图。

eg:

例如:

In [1]: import numpy as np

In [2]: x = np.arange(10*10).reshape((10,10))

In [4]: y = x[3:7,3:7].copy()

In [6]: y[2,2] = 1000

In [8]: 1000 in x
Out[8]: False     # see, 1000 in y doesn't change values in x, parent array.

回答by yildirim

You can simply use Python standard library. Make a shallow copy of the original image as follows:

您可以简单地使用 Python 标准库。制作原始图像的浅拷贝,如下所示:

import copy

original_img = cv2.imread("foo.jpg")
clone_img = copy.copy(original_img)

回答by Hyman Guy

My favorite method uses cv2.copyMakeBorder with no border, like so.

我最喜欢的方法使用没有边框的 cv2.copyMakeBorder,就像这样。

copy = cv2.copyMakeBorder(original,0,0,0,0,cv2.BORDER_REPLICATE)

回答by Ash Ketchum

The first answer is correct but you say that you are using cv2 which inherently uses numpy arrays. So, to make a complete different copy of say "myImage":

第一个答案是正确的,但您说您使用的是 cv2,它本质上使用 numpy 数组。因此,要制作一个完全不同的“myImage”副本:

newImage = myImage.copy()

The above is enough. No need to import numpy.

以上就够了。无需导入 numpy。