<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 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 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> </div>
<div>Furthermore, the application crashes if i try to render the image.</div>
<div> </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> </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> </div>
<div>#include<cstdio><br>#include <fstream><br>#include <vtkImageData.h><br>#include "vtkImageImport.h" //Import images for VTK<br>#include "vtkActor.h"<br>#include "vtkOutlineFilter.h
"<br>#include "vtkPolyDataMapper.h"<br>#include "vtkRenderWindow.h"<br>#include "vtkRenderer.h"<br>#include "vtkRenderWindowInteractor.h"<br>#include "vtkLight.h"<br>
#include "vtkCamera.h"<br>#include "vtkContourFilter.h"<br>#include "vtkProperty.h"</div>
<p>#include<map><br>#include <vnl/vnl_matrix.h></p>
<p>typedef float SDMdatatype;<br>typedef vnl_matrix<SDMdatatype> mat;</p>
<p>int D[]={110,129,97};<br>unsigned int N=((*D)*(*(D+1))*(*(D+2))); //numel(vol)<br>unsigned short m=1; //Shape count<br>unsigned short sm=7; //Significant modes chosen<br>mat meanShape(N*m,1);</p>
<p>SDMdatatype* readArrayFromFile(char* fn,long size){<br> std::ifstream file;<br> file.open(fn,std::ios::in|std::ios::binary);<br> if (!file) { printf("file was not opened, issue error msg"); }<br> SDMdatatype* buffer;
<br> buffer = new SDMdatatype[size];<br> file.read(reinterpret_cast<char*>(buffer), sizeof (SDMdatatype) * size);//static_cast <char *> buffer<br> file.close();<br> return buffer;<br>}</p>
<p>vtkImageData* importData(SDMdatatype *data1,int *dims){<br> vtkImageImport *importer = vtkImageImport::New();<br> importer->SetDataScalarTypeToFloat();<br> importer->SetDataOrigin(0,0,0);<br> //importer->SetImportVoidPointer(data1);
<br> importer->CopyImportVoidPointer(data1,N);<br> <br> importer->SetWholeExtent(0,dims[0]-1,0,dims[1]-1,0,dims[2]-1);//If not 3D image set 3D extent = 0.<br> importer->SetDataExtentToWholeExtent();</p>
<p> importer->Update();</p>
<p> vtkImageData *img=importer->GetOutput();<br> img->Update();<br> return img;<br>}</p>
<p>void loadPCAResults(){<br> SDMdatatype* b1=readArrayFromFile(<br> "C:\\thesisIntermediate\\meanshape.dat",<br> N*m);<br> <br> meanShape.copy_in(b1);<br> delete b1;<br>}<br>void unique(SDMdatatype* data, int size,bool print){
<br> typedef std::map<SDMdatatype,long> pixHistType;<br> pixHistType hist;</p>
<p> for(int k=0;k<size;k++){<br> //printf("%x\n",data[k]);<br> std::cout<<k<<" : "<<*(data+k)<<std::endl;<br> SDMdatatype val=data[k];<br> //mexPrintf("%4i\n",val);
<br> hist[val]++;<br> }<br> if(print){<br> pixHistType::iterator iter;<br> for(iter = hist.begin(); iter != hist.end(); ++iter)<br> std::cout<<"["<<iter->first<<"]:["<<iter->second<<"]"<<std::endl;
<br> }<br>}</p>
<p>int main(int argc,char* argv[])<br>{<br> loadPCAResults();</p>
<p> //structure actors<br> vtkActor *Temporopolar_region_left_actor = vtkActor::New();</p>
<p> //rendering objects<br> vtkRenderer *aRenderer = vtkRenderer::New();<br> vtkRenderWindow *renWin = vtkRenderWindow::New();<br> vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();<br> vtkCamera *aCamera = vtkCamera::New();
</p>
<p> // Get vols from SDM<br> //extract(height,width,top,left)<br> mat t=meanShape.extract(N,1, 0,0);<br> SDMdatatype* b=t.data_block();<br> vtkImageData *Temporopolar_region_left_data = importData(b,D);</p>
<p> Temporopolar_region_left_data->Update();<br> Temporopolar_region_left_data->UpdateInformation();<br> Temporopolar_region_left_data->PropagateUpdateExtent();</p>
<p> SDMdatatype* a = (SDMdatatype*)Temporopolar_region_left_data->GetScalarPointer();<br> <br> unique(a,N,1);</p>
<p> vtkContourFilter *contours = vtkContourFilter::New();<br> contours->SetInput(Temporopolar_region_left_data);<br> contours->SetValue(0,0);</p>
<p> vtkPolyDataMapper *mpr = vtkPolyDataMapper::New();<br> mpr->SetInput(contours->GetOutput());<br> <br> Temporopolar_region_left_actor -> SetMapper(mpr);<br> Temporopolar_region_left_actor -> GetProperty() -> SetDiffuseColor(1., 0., 0.);
<br> Temporopolar_region_left_actor -> GetProperty() -> SetSpecularPower(50);<br> Temporopolar_region_left_actor -> GetProperty() -> SetSpecular(0.5);<br> Temporopolar_region_left_actor -> GetProperty() -> SetDiffuse(
0.8);</p>
<p> // Actors are added to the renderer. <br> aRenderer->AddActor(Temporopolar_region_left_actor);</p>
<p> iren->SetRenderWindow(renWin);<br> renWin->AddRenderer(aRenderer);<br> aRenderer->SetActiveCamera(aCamera);</p>
<p> // Setting the color and size of the renderwindow,<br> // initializing the camera position</p>
<p> // Here we go!<br> iren->Initialize();<br> renWin->Render();<br> iren->Start();<br> //////////////////////////////////////////////////////////////////////////////////<br> // CLEAN UP //<br> //////////////////////////////////////////////////////////////////////////////////
<br> <br> aCamera->Delete();<br> aRenderer->Delete();<br> renWin->Delete();<br> iren->Delete();<br>}</p>