<div>Hello all. I am (still) having problems getting my data correct read into vtk. I have a really strange problem which i absolutely dont understand. I read in the data from binary files...that goes well. Then i put it into a vnl_matrix(goes also well) and&nbsp;extract a certain part of this matrix, import this subpart into a vtkImageData with 3 dimensions using the itkImageImport class.
</div>
<div>When i then try to use this image data, the application crashes because the data has not been put there correctly is my assumption. </div>
<div>When i call the print() function for the vtkImageData object it tells me that there are the 97*110*129 = 1376430 which there should be. The GetEstimatedMemorySize() and GetActualMemorySize() also returns more or less the correct amount in kb. To check that the values of data is correct I have tried calling GetDataScalarPointer() to get access to the data kept inside this object. 
</div>
<div>When i then iterate this data in my unique(...) function(my own, see below), and print the values, it seems that at position&nbsp;344107 it assumes value -4.2e037, and thereafter -9.1e041and followed by zeros up until index 345072 where the application crashes.
</div>
<div>And it should be able to print out 1376430 elements, since this is the size of the image.</div>
<div>&nbsp;</div>
<div>Furthermore, the application crashes if i try to render the image.</div>
<div>&nbsp;</div>
<div>So I was hoping someone with a bit more experience with me could take a quick look at my code and see if it looks completely wrong. I am really stuck here, and I dont understand why the data is not there, when it should be, and vtk actually tells me that it is there.
</div>
<div>&nbsp;</div>
<div>I have copied my program listing below for ease of understanding my problem. I really hope for help since i am pretty stuck here.</div>
<div>Thank you very much in advance</div>
<div>- H</div>
<div>&nbsp;</div>
<div>#include&lt;cstdio&gt;<br>#include &lt;fstream&gt;<br>#include &lt;vtkImageData.h&gt;<br>#include &quot;vtkImageImport.h&quot; //Import images for VTK<br>#include &quot;vtkActor.h&quot;<br>#include &quot;vtkOutlineFilter.h
&quot;<br>#include &quot;vtkPolyDataMapper.h&quot;<br>#include &quot;vtkRenderWindow.h&quot;<br>#include &quot;vtkRenderer.h&quot;<br>#include &quot;vtkRenderWindowInteractor.h&quot;<br>#include &quot;vtkLight.h&quot;<br>
#include &quot;vtkCamera.h&quot;<br>#include &quot;vtkContourFilter.h&quot;<br>#include &quot;vtkProperty.h&quot;</div>
<p>#include&lt;map&gt;<br>#include &lt;vnl/vnl_matrix.h&gt;</p>
<p>typedef float SDMdatatype;<br>typedef vnl_matrix&lt;SDMdatatype&gt; mat;</p>
<p>int D[]={110,129,97};<br>unsigned int N=((*D)*(*(D+1))*(*(D+2)));&nbsp;//numel(vol)<br>unsigned short m=1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Shape count<br>unsigned short sm=7;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//Significant modes chosen<br>mat meanShape(N*m,1);</p>
<p>SDMdatatype* readArrayFromFile(char* fn,long size){<br>&nbsp;std::ifstream file;<br>&nbsp;file.open(fn,std::ios::in|std::ios::binary);<br>&nbsp;if (!file) { printf(&quot;file was not opened, issue error msg&quot;); }<br>&nbsp;SDMdatatype* buffer;&nbsp;
<br>&nbsp;buffer = new SDMdatatype[size];<br>&nbsp;file.read(reinterpret_cast&lt;char*&gt;(buffer), sizeof (SDMdatatype) * size);//static_cast &lt;char *&gt; buffer<br>&nbsp;file.close();<br>&nbsp;return buffer;<br>}</p>
<p>vtkImageData* importData(SDMdatatype *data1,int *dims){<br>&nbsp;vtkImageImport *importer = vtkImageImport::New();<br>&nbsp;importer-&gt;SetDataScalarTypeToFloat();<br>&nbsp;importer-&gt;SetDataOrigin(0,0,0);<br>&nbsp;//importer-&gt;SetImportVoidPointer(data1);
<br>&nbsp;importer-&gt;CopyImportVoidPointer(data1,N);<br>&nbsp;<br>&nbsp;importer-&gt;SetWholeExtent(0,dims[0]-1,0,dims[1]-1,0,dims[2]-1);//If not 3D image set 3D extent = 0.<br>&nbsp;importer-&gt;SetDataExtentToWholeExtent();</p>
<p>&nbsp;importer-&gt;Update();</p>
<p>&nbsp;vtkImageData *img=importer-&gt;GetOutput();<br>&nbsp;img-&gt;Update();<br>&nbsp;return img;<br>}</p>
<p>void loadPCAResults(){<br>&nbsp;SDMdatatype* b1=readArrayFromFile(<br>&nbsp;&nbsp;&quot;C:\\thesisIntermediate\\meanshape.dat&quot;,<br>&nbsp;N*m);<br>&nbsp;<br>&nbsp;meanShape.copy_in(b1);<br>&nbsp;delete b1;<br>}<br>void unique(SDMdatatype* data, int size,bool print){
<br>&nbsp;typedef std::map&lt;SDMdatatype,long&gt; pixHistType;<br>&nbsp;pixHistType hist;</p>
<p>&nbsp;for(int k=0;k&lt;size;k++){<br>&nbsp;&nbsp;//printf(&quot;%x\n&quot;,data[k]);<br>&nbsp;&nbsp;std::cout&lt;&lt;k&lt;&lt;&quot; : &quot;&lt;&lt;*(data+k)&lt;&lt;std::endl;<br>&nbsp;&nbsp;SDMdatatype val=data[k];<br>&nbsp;&nbsp;//mexPrintf(&quot;%4i\n&quot;,val);
<br>&nbsp;&nbsp;hist[val]++;<br>&nbsp;}<br>&nbsp;if(print){<br>&nbsp;&nbsp;pixHistType::iterator iter;<br>&nbsp;&nbsp;for(iter = hist.begin(); iter != hist.end(); ++iter)<br>&nbsp;&nbsp;&nbsp;&nbsp;std::cout&lt;&lt;&quot;[&quot;&lt;&lt;iter-&gt;first&lt;&lt;&quot;]:[&quot;&lt;&lt;iter-&gt;second&lt;&lt;&quot;]&quot;&lt;&lt;std::endl;
<br>&nbsp;}<br>}</p>
<p>int main(int argc,char* argv[])<br>{<br>&nbsp;loadPCAResults();</p>
<p>&nbsp;//structure actors<br>&nbsp;vtkActor *Temporopolar_region_left_actor&nbsp;&nbsp;= vtkActor::New();</p>
<p>&nbsp;//rendering objects<br>&nbsp;vtkRenderer *aRenderer = vtkRenderer::New();<br>&nbsp;vtkRenderWindow *renWin = vtkRenderWindow::New();<br>&nbsp;vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();<br>&nbsp;vtkCamera *aCamera = vtkCamera::New();
</p>
<p>&nbsp;// Get vols from SDM<br>&nbsp;//extract(height,width,top,left)<br>&nbsp;mat t=meanShape.extract(N,1, 0,0);<br>&nbsp;SDMdatatype* b=t.data_block();<br>&nbsp;vtkImageData *Temporopolar_region_left_data = importData(b,D);</p>
<p>&nbsp;Temporopolar_region_left_data-&gt;Update();<br>&nbsp;Temporopolar_region_left_data-&gt;UpdateInformation();<br>&nbsp;Temporopolar_region_left_data-&gt;PropagateUpdateExtent();</p>
<p>&nbsp;SDMdatatype* a = (SDMdatatype*)Temporopolar_region_left_data-&gt;GetScalarPointer();<br>&nbsp;<br>&nbsp;unique(a,N,1);</p>
<p>&nbsp;vtkContourFilter *contours = vtkContourFilter::New();<br>&nbsp;contours-&gt;SetInput(Temporopolar_region_left_data);<br>&nbsp;contours-&gt;SetValue(0,0);</p>
<p>&nbsp;vtkPolyDataMapper *mpr = vtkPolyDataMapper::New();<br>&nbsp;mpr-&gt;SetInput(contours-&gt;GetOutput());<br>&nbsp;<br>&nbsp;Temporopolar_region_left_actor -&gt; SetMapper(mpr);<br>&nbsp;Temporopolar_region_left_actor -&gt; GetProperty() -&gt; SetDiffuseColor(1., 0., 0.);
<br>&nbsp;Temporopolar_region_left_actor -&gt; GetProperty() -&gt; SetSpecularPower(50);<br>&nbsp;Temporopolar_region_left_actor -&gt; GetProperty() -&gt; SetSpecular(0.5);<br>&nbsp;Temporopolar_region_left_actor -&gt; GetProperty() -&gt; SetDiffuse(
0.8);</p>
<p>&nbsp;// Actors are added to the renderer. <br>&nbsp;aRenderer-&gt;AddActor(Temporopolar_region_left_actor);</p>
<p>&nbsp;iren-&gt;SetRenderWindow(renWin);<br>&nbsp;renWin-&gt;AddRenderer(aRenderer);<br>&nbsp;aRenderer-&gt;SetActiveCamera(aCamera);</p>
<p>&nbsp;// Setting the color and size of the renderwindow,<br>&nbsp;// initializing the camera position</p>
<p>&nbsp;// Here we go!<br>&nbsp;iren-&gt;Initialize();<br>&nbsp;renWin-&gt;Render();<br>&nbsp;iren-&gt;Start();<br>&nbsp;//////////////////////////////////////////////////////////////////////////////////<br>&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CLEAN UP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//<br>&nbsp;//////////////////////////////////////////////////////////////////////////////////
<br>&nbsp;<br>&nbsp;aCamera-&gt;Delete();<br>&nbsp;aRenderer-&gt;Delete();<br>&nbsp;renWin-&gt;Delete();<br>&nbsp;iren-&gt;Delete();<br>}</p>