<div dir="ltr">Hi there, <div><br></div><div>In my programmable filter I want to use numpy to perform some operations on the pixels (in this case a simple thresholding). I've come up with this solution which works. I'm wondering if there are other ways to do the conversion from numpy array to vtkImageData.</div><div class="cye-lm-tag"><br></div><div class="cye-lm-tag">in the following code I get a numpy array out of the input vtkImageData as B. Then I do something on the content an store the result in a numpy array, A. </div><div class="cye-lm-tag"><br></div><div class="cye-lm-tag">Then I convert the numpy array to a vtkImageData with vtkImageImportFromArray importer class. </div><div class="cye-lm-tag">Finally I copy the importer output to the output of the programmable filter.</div><div class="cye-lm-tag"><br></div><div class="cye-lm-tag">Thanks for any suggestions</div><div class="cye-lm-tag"><br></div><div class="cye-lm-tag">Edo</div><div class="cye-lm-tag"><pre style="margin-top:12px;margin-bottom:0px"><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">import</span><span style="font-family:"Courier New"" class="cye-lm-tag"> </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,0,255)" class="cye-lm-tag">numpy</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">from</span><span style="font-family:"Courier New"" class="cye-lm-tag"> </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,0,255)" class="cye-lm-tag">vtk.util</span><span style="font-family:"Courier New"" class="cye-lm-tag"> </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">import</span><span style="font-family:"Courier New"" class="cye-lm-tag"> numpy_support, vtkImageImportFromArray</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">import</span><span style="font-family:"Courier New"" class="cye-lm-tag"> </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,0,255)" class="cye-lm-tag">vtk.numpy_interface.dataset_adapter</span><span style="font-family:"Courier New"" class="cye-lm-tag"> </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">as</span><span style="font-family:"Courier New"" class="cye-lm-tag"> </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,0,255)" class="cye-lm-tag">dsa</span><span style="font-family:"Courier New"" class="cye-lm-tag"> </span></pre>
<pre style="margin-top:0px;margin-bottom:0px;font-family:"Courier New""><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">B </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">=</span><span style="font-family:"Courier New"" class="cye-lm-tag"> numpy_support</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">vtk_to_numpy(</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag"> inputs[</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">0</span><span style="font-family:"Courier New"" class="cye-lm-tag">]</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">GetPointData()</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">GetScalars())</span></pre>
<pre style="margin-top:0px;margin-bottom:0px;font-family:"Courier New""><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">ifLarger </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">=</span><span style="font-family:"Courier New"" class="cye-lm-tag"> </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">lambda</span><span style="font-family:"Courier New"" class="cye-lm-tag"> x,M: x </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">if</span><span style="font-family:"Courier New"" class="cye-lm-tag"> x</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag"><=</span><span style="font-family:"Courier New"" class="cye-lm-tag">M </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">else</span><span style="font-family:"Courier New"" class="cye-lm-tag"> </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">0</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">fgt </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">=</span><span style="font-family:"Courier New"" class="cye-lm-tag"> numpy</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">frompyfunc(ifLarger,</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">2</span><span style="font-family:"Courier New"" class="cye-lm-tag">,</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">1</span><span style="font-family:"Courier New"" class="cye-lm-tag">)</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New";font-style:italic;color:rgb(64,128,128)" class="cye-lm-tag">#A = fgt(B,int(2**16*0.8))</span></pre>
<pre style="margin-top:0px;margin-bottom:0px;font-family:"Courier New";font-style:italic;color:rgb(64,128,128)"><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">ifSmaller </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">=</span><span style="font-family:"Courier New"" class="cye-lm-tag"> </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">lambda</span><span style="font-family:"Courier New"" class="cye-lm-tag"> x,M: x </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">if</span><span style="font-family:"Courier New"" class="cye-lm-tag"> x</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">></span><span style="font-family:"Courier New"" class="cye-lm-tag">M </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">else</span><span style="font-family:"Courier New"" class="cye-lm-tag"> </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">0</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">flt </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">=</span><span style="font-family:"Courier New"" class="cye-lm-tag"> numpy</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">frompyfunc(ifSmaller,</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">2</span><span style="font-family:"Courier New"" class="cye-lm-tag">,</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">1</span><span style="font-family:"Courier New"" class="cye-lm-tag">)</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New";font-style:italic;color:rgb(64,128,128)" class="cye-lm-tag">#A = flt(B,int(2**16*0.3))</span></pre>
<pre style="margin-top:0px;margin-bottom:0px;font-family:"Courier New";font-style:italic;color:rgb(64,128,128)"><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">ifBetween </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">=</span><span style="font-family:"Courier New"" class="cye-lm-tag"> </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">lambda</span><span style="font-family:"Courier New"" class="cye-lm-tag"> x,m,M: ifSmaller(x,m) </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">if</span><span style="font-family:"Courier New"" class="cye-lm-tag"> x</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag"><=</span><span style="font-family:"Courier New"" class="cye-lm-tag">M </span><span style="font-family:"Courier New";font-weight:600;color:rgb(0,128,0)" class="cye-lm-tag">else</span><span style="font-family:"Courier New"" class="cye-lm-tag"> </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">0</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">fbetw </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">=</span><span style="font-family:"Courier New"" class="cye-lm-tag"> numpy</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">frompyfunc(ifBetween,</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">3</span><span style="font-family:"Courier New"" class="cye-lm-tag">,</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">1</span><span style="font-family:"Courier New"" class="cye-lm-tag">)</span></pre>
<pre style="margin-top:0px;margin-bottom:0px;font-family:"Courier New""><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">A </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">=</span><span style="font-family:"Courier New"" class="cye-lm-tag"> fbetw(B,</span><span style="font-family:"Courier New";color:rgb(0,128,0)" class="cye-lm-tag">int</span><span style="font-family:"Courier New"" class="cye-lm-tag">(</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">2**16*0.3</span><span style="font-family:"Courier New"" class="cye-lm-tag">), </span><span style="font-family:"Courier New";color:rgb(0,128,0)" class="cye-lm-tag">int</span><span style="font-family:"Courier New"" class="cye-lm-tag">(</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">2**16*0.95</span><span style="font-family:"Courier New"" class="cye-lm-tag">))</span></pre>
<pre style="margin-top:0px;margin-bottom:0px;font-family:"Courier New""><br></pre>
<pre style="margin-top:0px;margin-bottom:0px;font-family:"Courier New""><br></pre>
<pre style="margin-top:0px;margin-bottom:0px">A <span class="cye-lm-tag" style="font-family:"Courier New";color:rgb(102,102,102)">=</span><span class="cye-lm-tag" style="font-family:"Courier New""> numpy</span><span class="cye-lm-tag" style="font-family:"Courier New";color:rgb(102,102,102)">.</span><span class="cye-lm-tag" style="font-family:"Courier New"">asarray(A,dtype</span><span class="cye-lm-tag" style="font-family:"Courier New";color:rgb(102,102,102)">=</span><span class="cye-lm-tag" style="font-family:"Courier New"">uint16)</span><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">dims </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">=</span><span style="font-family:"Courier New"" class="cye-lm-tag"> inputs[</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">0</span><span style="font-family:"Courier New"" class="cye-lm-tag">]</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">GetDimensions()</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">A </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">=</span><span style="font-family:"Courier New"" class="cye-lm-tag"> numpy</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">reshape(A, (dims[</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">2</span><span style="font-family:"Courier New"" class="cye-lm-tag">],dims[</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">1</span><span style="font-family:"Courier New"" class="cye-lm-tag">],dims[</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">0</span><span style="font-family:"Courier New"" class="cye-lm-tag">]), order</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">=</span><span style="font-family:"Courier New";color:rgb(186,33,33)" class="cye-lm-tag">'C'</span><span style="font-family:"Courier New"" class="cye-lm-tag">)</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">A </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">=</span><span style="font-family:"Courier New"" class="cye-lm-tag"> numpy</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">ascontiguousarray(A)</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">importer </span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">=</span><span style="font-family:"Courier New"" class="cye-lm-tag"> vtkImageImportFromArray</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">vtkImageImportFromArray()</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">importer</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">SetArray(A)</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">importer</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">SetDataSpacing(inputs[</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">0</span><span style="font-family:"Courier New"" class="cye-lm-tag">]</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">GetSpacing())</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">importer</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">SetDataOrigin(inputs[</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">0</span><span style="font-family:"Courier New"" class="cye-lm-tag">]</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">GetOrigin())</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">importer</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">Update()</span></pre>
<pre style="margin-top:0px;margin-bottom:0px;font-family:"Courier New""><br></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:"Courier New"" class="cye-lm-tag">output</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">DeepCopy(importer</span><span style="font-family:"Courier New";color:rgb(102,102,102)" class="cye-lm-tag">.</span><span style="font-family:"Courier New"" class="cye-lm-tag">GetOutput())</span></pre>
<pre style="margin-top:0px;margin-bottom:12px;font-family:"Courier New""><br></pre><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>Edo</div><div>I know you think you understand what you thought I said, but I'm not sure you realize that what you heard is not what I meant (prob. Alan Greenspan)<br>:wq</div></div></div></div></div>
</div></div>