如何将numpy数组视图转换为opencv矩阵?

时间:2021-08-12 21:27:48

I'm using opencv v2.2 to do some template matching on ndarrays, and I had great trouble with memory leaks when using their wrapped method cv.fromarray(). Rather than plug the memory leaks I avoided the fromarray() function and used cv.SetData directly, like this:

我正在使用opencv v2.2在ndarrays上进行一些模板匹配,当使用它们的包装方法cv.fromarray()时,我遇到了很大的内存泄漏问题。我没有插入内存泄漏,而是避免了fromarray()函数并直接使用了cv.SetData,如下所示:

assert foo_numpy.dtype == 'uint8'
assert foo_numpy.ndim == 3
h, w = foo_numpy.shape[:2]
foo_cv = cv.CreateMat(h, w, cv.CV_8UC3)
cv.SetData(foo_cv, foo_numpy.data, foo_numpy.strides[0])

This seems to solve the memory leaks and foo_cv seems to be deallocated properly when it goes out of scope. However, now I have the issue where if foo_numpy is just a slice/view on a bigger array, I'm not permitted foo_numpy.data (cannot get single-segment buffer for discontiguous array). At the moment I'm working around this by making foo_numpy.copy() if foo_numpy.base != None, which permits getting the buffer on the new copy. But I have the feeling this is unnecessary, the slice has the __array_struct__ and __array_interface__ so I should be able to just stride it with the appropriate stepsizes somehow? I'm not sure how to do it in a nice way, because the base of this one can also be a view on another larger array ad infinitum.

这似乎解决了内存泄漏问题,当foo_cv超出范围时,foo_cv似乎已正确解除分配。但是,现在我遇到的问题是,如果foo_numpy只是一个更大的数组上的切片/视图,我不允许foo_numpy.data(不能为不连续的数组获取单段缓冲区)。目前我正在通过制作foo_numpy.copy()来解决这个问题,如果foo_numpy.base!= None,它允许在新副本上获取缓冲区。但我觉得这是不必要的,切片有__array_struct__和__array_interface__所以我应该能够以某种方式用适当的步长来大步走?我不确定如何以一种很好的方式做到这一点,因为这个的基础也可以是另一个更大的阵列无限的视图。

1 个解决方案

#1


2  

I think the problem with what you were trying to do is that the array data you're interested in (ie. foo_np_view) is actually only stored in one place i.e. foo_np.data, and the OpenCV SetData method doesn't provide any way to specify stride settings that would allow you to skip the bytes that are not part of foo_np_view.

我认为你试图做的问题是你感兴趣的数组数据(即foo_np_view)实际上只存储在一个地方,即foo_np.data,而OpenCV SetData方法没有提供任何方法指定步幅设置,允许您跳过不属于foo_np_view的字节。

You can, however, get around this problem using Numpy’s tostring() method, which turns an array (or views therein) into a byte string:

但是,您可以使用Numpy的tostring()方法解决此问题,该方法将数组(或其中的视图)转换为字节字符串:

>>> import numpy as np
>>> import cv
>>> foo_np = np.array( 255 * np.random.rand( 200 , 300 , 3 ), dtype = 'uint8' )
>>> foo_np_view = foo_np [ 50:150:2 , 10:290:5 , : ]
>>> h,w,d = foo_np_view.shape
>>> foo_cv = cv.CreateMat( h , w , cv.CV_8UC3 )

Recreating the original problem:

重新创建原始问题:

>>> cv.SetData( foo_cv , foo_np_view.data, foo_np_view.strides[0] )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: cannot get single-segment buffer for discontiguous array

Using the tostring() method (see below for explanation of the stride setting):

使用tostring()方法(参见下面的步幅设置说明):

>>> cv.SetData( foo_cv , foo_np_view.tostring() , w * d * foo_np_view.dtype.itemsize )
>>> np.array_equal( np.asarray( foo_cv ) , foo_np_view )
True

The value w * d * foo_np_view.dtype.itemsize gives us a stride value identical to that of foo_np_view.copy(), which is necessary as the string representations of the view and its copy are identical:

值w * d * foo_np_view.dtype.itemsize给出了一个与foo_np_view.copy()相同的步幅值,这是必要的,因为视图的字符串表示形式和它的副本是相同的:

>>> foo_np_view.copy().tostring() == foo_np_view.tostring()
True
>>> foo_np_view.copy().strides[0] == w * d * foo_np_view.dtype.itemsize
True

#1


2  

I think the problem with what you were trying to do is that the array data you're interested in (ie. foo_np_view) is actually only stored in one place i.e. foo_np.data, and the OpenCV SetData method doesn't provide any way to specify stride settings that would allow you to skip the bytes that are not part of foo_np_view.

我认为你试图做的问题是你感兴趣的数组数据(即foo_np_view)实际上只存储在一个地方,即foo_np.data,而OpenCV SetData方法没有提供任何方法指定步幅设置,允许您跳过不属于foo_np_view的字节。

You can, however, get around this problem using Numpy’s tostring() method, which turns an array (or views therein) into a byte string:

但是,您可以使用Numpy的tostring()方法解决此问题,该方法将数组(或其中的视图)转换为字节字符串:

>>> import numpy as np
>>> import cv
>>> foo_np = np.array( 255 * np.random.rand( 200 , 300 , 3 ), dtype = 'uint8' )
>>> foo_np_view = foo_np [ 50:150:2 , 10:290:5 , : ]
>>> h,w,d = foo_np_view.shape
>>> foo_cv = cv.CreateMat( h , w , cv.CV_8UC3 )

Recreating the original problem:

重新创建原始问题:

>>> cv.SetData( foo_cv , foo_np_view.data, foo_np_view.strides[0] )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: cannot get single-segment buffer for discontiguous array

Using the tostring() method (see below for explanation of the stride setting):

使用tostring()方法(参见下面的步幅设置说明):

>>> cv.SetData( foo_cv , foo_np_view.tostring() , w * d * foo_np_view.dtype.itemsize )
>>> np.array_equal( np.asarray( foo_cv ) , foo_np_view )
True

The value w * d * foo_np_view.dtype.itemsize gives us a stride value identical to that of foo_np_view.copy(), which is necessary as the string representations of the view and its copy are identical:

值w * d * foo_np_view.dtype.itemsize给出了一个与foo_np_view.copy()相同的步幅值,这是必要的,因为视图的字符串表示形式和它的副本是相同的:

>>> foo_np_view.copy().tostring() == foo_np_view.tostring()
True
>>> foo_np_view.copy().strides[0] == w * d * foo_np_view.dtype.itemsize
True