[Insight-users] Having problems importing data to vtk from itk/vnl
Arne Hansen
bsd.diverse at gmail.com
Sat Jan 13 07:39:35 EST 2007
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.
When i then try to use this image data, the application crashes because the
data has not been put there correctly is my assumption.
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.
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.
And it should be able to print out 1376430 elements, since this is the size
of the image.
Furthermore, the application crashes if i try to render the image.
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.
I have copied my program listing below for ease of understanding my problem.
I really hope for help since i am pretty stuck here.
Thank you very much in advance
- H
#include<cstdio>
#include <fstream>
#include <vtkImageData.h>
#include "vtkImageImport.h" //Import images for VTK
#include "vtkActor.h"
#include "vtkOutlineFilter.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkLight.h"
#include "vtkCamera.h"
#include "vtkContourFilter.h"
#include "vtkProperty.h"
#include<map>
#include <vnl/vnl_matrix.h>
typedef float SDMdatatype;
typedef vnl_matrix<SDMdatatype> mat;
int D[]={110,129,97};
unsigned int N=((*D)*(*(D+1))*(*(D+2))); //numel(vol)
unsigned short m=1; //Shape count
unsigned short sm=7; //Significant modes chosen
mat meanShape(N*m,1);
SDMdatatype* readArrayFromFile(char* fn,long size){
std::ifstream file;
file.open(fn,std::ios::in|std::ios::binary);
if (!file) { printf("file was not opened, issue error msg"); }
SDMdatatype* buffer;
buffer = new SDMdatatype[size];
file.read(reinterpret_cast<char*>(buffer), sizeof (SDMdatatype) *
size);//static_cast <char *> buffer
file.close();
return buffer;
}
vtkImageData* importData(SDMdatatype *data1,int *dims){
vtkImageImport *importer = vtkImageImport::New();
importer->SetDataScalarTypeToFloat();
importer->SetDataOrigin(0,0,0);
//importer->SetImportVoidPointer(data1);
importer->CopyImportVoidPointer(data1,N);
importer->SetWholeExtent(0,dims[0]-1,0,dims[1]-1,0,dims[2]-1);//If not 3D
image set 3D extent = 0.
importer->SetDataExtentToWholeExtent();
importer->Update();
vtkImageData *img=importer->GetOutput();
img->Update();
return img;
}
void loadPCAResults(){
SDMdatatype* b1=readArrayFromFile(
"C:\\thesisIntermediate\\meanshape.dat",
N*m);
meanShape.copy_in(b1);
delete b1;
}
void unique(SDMdatatype* data, int size,bool print){
typedef std::map<SDMdatatype,long> pixHistType;
pixHistType hist;
for(int k=0;k<size;k++){
//printf("%x\n",data[k]);
std::cout<<k<<" : "<<*(data+k)<<std::endl;
SDMdatatype val=data[k];
//mexPrintf("%4i\n",val);
hist[val]++;
}
if(print){
pixHistType::iterator iter;
for(iter = hist.begin(); iter != hist.end(); ++iter)
std::cout<<"["<<iter->first<<"]:["<<iter->second<<"]"<<std::endl;
}
}
int main(int argc,char* argv[])
{
loadPCAResults();
//structure actors
vtkActor *Temporopolar_region_left_actor = vtkActor::New();
//rendering objects
vtkRenderer *aRenderer = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
vtkCamera *aCamera = vtkCamera::New();
// Get vols from SDM
//extract(height,width,top,left)
mat t=meanShape.extract(N,1, 0,0);
SDMdatatype* b=t.data_block();
vtkImageData *Temporopolar_region_left_data = importData(b,D);
Temporopolar_region_left_data->Update();
Temporopolar_region_left_data->UpdateInformation();
Temporopolar_region_left_data->PropagateUpdateExtent();
SDMdatatype* a =
(SDMdatatype*)Temporopolar_region_left_data->GetScalarPointer();
unique(a,N,1);
vtkContourFilter *contours = vtkContourFilter::New();
contours->SetInput(Temporopolar_region_left_data);
contours->SetValue(0,0);
vtkPolyDataMapper *mpr = vtkPolyDataMapper::New();
mpr->SetInput(contours->GetOutput());
Temporopolar_region_left_actor -> SetMapper(mpr);
Temporopolar_region_left_actor -> GetProperty() -> SetDiffuseColor(1., 0.,
0.);
Temporopolar_region_left_actor -> GetProperty() -> SetSpecularPower(50);
Temporopolar_region_left_actor -> GetProperty() -> SetSpecular(0.5);
Temporopolar_region_left_actor -> GetProperty() -> SetDiffuse(0.8);
// Actors are added to the renderer.
aRenderer->AddActor(Temporopolar_region_left_actor);
iren->SetRenderWindow(renWin);
renWin->AddRenderer(aRenderer);
aRenderer->SetActiveCamera(aCamera);
// Setting the color and size of the renderwindow,
// initializing the camera position
// Here we go!
iren->Initialize();
renWin->Render();
iren->Start();
//////////////////////////////////////////////////////////////////////////////////
// CLEAN UP //
//////////////////////////////////////////////////////////////////////////////////
aCamera->Delete();
aRenderer->Delete();
renWin->Delete();
iren->Delete();
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/insight-users/attachments/20070113/f4d93f53/attachment.html
More information about the Insight-users
mailing list