[vtkusers] vtkImageData

Amy Squillacote ahs at cfdrc.com
Tue Apr 8 10:26:08 EDT 2008


Hi Stefan,

I've added comments in the sections of code you showed below. The 
problem is not with GetScalarPointer().

- Amy

Stefan Huber wrote:
> Hi
>
> i tried the methode GetScalarPointer befor, but it wasn't working.
> i get an array with wrong numbers. my code:
>
> int* writeDataFromImageData(int x, int y){
> 	int size = x*y;
> 	int *arr = (int *) malloc(sizeof(int *)*size);
> 	int l = 0;
> 	int * p = (int *) img->GetScalarPointer();
> 	//I think somewhere here is the mistake
> 	
> 	for(int i= 0; i< x; i++){
> 		for (int j= 0; j <y; j++){
> 			arr[(i*x)+j]= (int)*p;
>   

I think the above line should be arr[i*y+j] = (int)*p;

> 			p++;
> 		}  //for
> 	}  //for
> 	writeArray(size,arr);
> 	return arr;
> }  //writeDataFromImageData
>
> bool readArray2ImageData(int* dat, int x, int y){
> 	//write array to vtkimagedata
> 	try{
> 		img->SetDimensions(x,y,1);
> 		img->SetScalarTypeToFloat();
> 		vtkFloatArray *scalars = vtkFloatArray::New();
> 		for(int i= 0; i< x; i++){
> 			for (int j= 0; j <y; j++){
> 				scalars->InsertTuple1((i*x)+j,dat[i*x+j]);
>   

Similar to the previous problem, the above line should be 
scalars->InsertTuple1(i*y+j, dat[i*x+j]);

With the indexing you're using, you're not managing to visit every index 
in your scalars array. Try drawing a very small grid (e.g., 3x5), and 
step through your code (on paper) to see what happens.

> 			}  //for
> 		}  //for
> 		img->GetPointData()->SetScalars(scalars);
> 		img->Update();
> 		return true;
> 	} catch(std::bad_alloc &ex){
> 		return false;
> 	}  //catch
> }  //readArray2ImageData
>
> int* forwardFFT(int *dat, int x, int y){
>
> 	if(!readArray2ImageData(dat,x,y))
> 		return NULL;
> 	else {
> 		vtkImageFFT *ffft = vtkImageFFT::New();
> 		ffft->SetDimensionality(2);
> 		ffft->SetInput(img);
> 		ffft->Update();
> 		img=ffft->GetOutput();
> 		img->Update();
> 		img->UpdateData();
>
> 		return writeDataFromImageData(x,y);
> 	}  //else
> }  //forwardFFT
>
> can you help me please?
>
> thanx Stefan
>
>
>
> -------- Original-Nachricht --------
>   
>> Datum: Tue, 08 Apr 2008 08:25:50 -0500
>> Von: Amy Squillacote <ahs at cfdrc.com>
>> An: Mike Jackson <imikejackson at gmail.com>
>> CC: Stefan Huber <Huber.Ste at gmx.at>, vtkusers at vtk.org
>> Betreff: Re: [vtkusers] vtkImageData
>>     
>
>   
>> You're close; the name of the method is GetScalarPointer(). See 
>> http://www.vtk.org/doc/nightly/html/classvtkImageData.html.
>>
>> - Amy
>>
>> Mike Jackson wrote:
>>     
>>> I _think_ there is a call in vtkImageData called "GetVoidPointer(0)" 
>>> which will return a void pointer to the first element of the array. 
>>> Simply cast this to the proper type and do what you need to do.
>>>
>>>       
>> -- 
>> Amy Squillacote                    Phone: (256) 726-4839
>> Computer Scientist                 Fax: (256) 726-4806
>> CFD Research Corporation           Web: http://www.cfdrc.com
>> 215 Wynn Drive, Suite 501
>> Huntsville, AL  35805
>>
>>     
>
>   

-- 
Amy Squillacote                    Phone: (256) 726-4839
Computer Scientist                 Fax: (256) 726-4806
CFD Research Corporation           Web: http://www.cfdrc.com
215 Wynn Drive, Suite 501
Huntsville, AL  35805





More information about the vtkusers mailing list