Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the problem with what you were trying to do is that the array data you're interested in (ie. <code>foo_np_view</code>) is actually only stored in one place i.e. <code>foo_np.data</code>, and the OpenCV <code>SetData</code> method doesn't provide any way to specify stride settings that would allow you to skip the bytes that are not part of <code>foo_np_view</code>.</p> <p>You can, however, get around this problem using Numpy’s <code>tostring()</code> method, which turns an array (or views therein) into a byte string:</p> <pre><code>&gt;&gt;&gt; import numpy as np &gt;&gt;&gt; import cv &gt;&gt;&gt; foo_np = np.array( 255 * np.random.rand( 200 , 300 , 3 ), dtype = 'uint8' ) &gt;&gt;&gt; foo_np_view = foo_np [ 50:150:2 , 10:290:5 , : ] &gt;&gt;&gt; h,w,d = foo_np_view.shape &gt;&gt;&gt; foo_cv = cv.CreateMat( h , w , cv.CV_8UC3 ) </code></pre> <p>Recreating the original problem:</p> <pre><code>&gt;&gt;&gt; cv.SetData( foo_cv , foo_np_view.data, foo_np_view.strides[0] ) Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; AttributeError: cannot get single-segment buffer for discontiguous array </code></pre> <p>Using the <code>tostring()</code> method (see below for explanation of the stride setting):</p> <pre><code>&gt;&gt;&gt; cv.SetData( foo_cv , foo_np_view.tostring() , w * d * foo_np_view.dtype.itemsize ) &gt;&gt;&gt; np.array_equal( np.asarray( foo_cv ) , foo_np_view ) True </code></pre> <p>The value <code>w * d * foo_np_view.dtype.itemsize</code> gives us a stride value identical to that of <code>foo_np_view.copy()</code>, which is necessary as the string representations of the view and its copy are identical:</p> <pre><code>&gt;&gt;&gt; foo_np_view.copy().tostring() == foo_np_view.tostring() True &gt;&gt;&gt; foo_np_view.copy().strides[0] == w * d * foo_np_view.dtype.itemsize True </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload