[vtkusers] Fast element access of vtkImageData?
Joseph D. Wieber Jr.
jdwieber at gmail.com
Wed Jul 27 20:48:47 EDT 2011
Hi Dominique,
Thanks for your response. What should I expect from
GetScalarPointer()? For example, if I have a vtkImageData object with
four float components (say RGBA), should I expect to be able to cast the
void* to float* and have it point to an array of four floats? I
modified my template to the below, but it didn't work. Am I expecting
the wrong return?
//ImageDataPtr is: typedef vtkSmartPointer< vtkImageData >
ImageDataPtr;
template< typename T >
void
fillImageData( ImageDataPtr pImageData, const T& val )
{
int dims[ 3 ];
pImageData->GetDimensions ( dims );
int& xMax = dims[ 0 ];
int& yMax = dims[ 1 ];
int& zMax = dims[ 2 ];
int numComponents = pImageData->GetNumberOfScalarComponents ();
for( int z = 0; z < zMax; ++z )
{
for ( int y = 0; y < yMax; ++y )
{
for( int x = 0; x < xMax; ++x )
{
T* ptr = static_cast< T* >(
pImageData->GetScalarPointer (x, y, z) );
for( int c = 0; c < numComponents; ++c )
{
*ptr = val; //*ptr++ = val;
++ptr;
}
}
}
}
}
Thanks again.
Regards,
Joseph
On 07/27/2011 04:22 PM, Dominique Töpfer wrote:
> Hi Joseph,
>
> you could use GetScalarPointer(), which gives you a void * pointer to
> the actual image data. After casting it to the pixel type you are
> using, you can access the array directly, which should be much faster.
> To access a certain point you could use ComputePointID() to get the
> correct index.
>
> HTH
> Dominique
>
> On 27.07.2011 22:07, Joseph D. Wieber Jr. wrote:
>>
>> Hello,
>>
>> Is there a fast way to access (get and set) elements in a
>> vtkImageData object? For instance I have two image data object with
>> dimensions 512x512x1 that I'm using as part of a cost table (it's
>> convenient) for an algorithm. One of them gets the output of a
>> convolution filter and I do lookups into it. The other one has to be
>> initialized to all 1.0 values at the beginning of the algorithm.
>> Then based on information from the first one, I set values in the
>> second one. The algorithm is a specialized Laplacian zero crossing.
>> I've been using the following template to initialize all the values
>> to 1.0:
>>
>> //-----------------------------------------------------------------------------
>>
>> //TODO: This is horribly slow. Find a smarter way!
>>
>> template< typename T >
>> void
>> fillImageData( ImageDataPtr pImageData, const T& val )
>> {
>> int dims[ 3 ];
>> pImageData->GetDimensions ( dims );
>> int& xMax = dims[ 0 ];
>> int& yMax = dims[ 1 ];
>> int& zMax = dims[ 2 ];
>> int numComponents = pImageData->GetNumberOfScalarComponents ();
>>
>> for( int z = 0; z < zMax; ++z )
>> {
>> for ( int y = 0; y < yMax; ++y )
>> {
>> for( int x = 0; x < xMax; ++x )
>> {
>> for( int c = 0; c < numComponents; ++c )
>> {
>> pImageData->SetScalarComponentFromFloat ( x, y,
>> z, c, val );
>> }
>> }
>> }
>> }
>> }
>>
>> To get and set values during the zero crossing computation I've been
>> using combinations of GetScalarComponentAsFloat, and
>> SetScalarComponentFromFloat and this part is also very slow.
>>
>> Can someone please point me to an example that accomplishes similar
>> tasks in a more efficient manner, or offer a suggestion on where to
>> look? Thank you.
>>
>> Regards,
>>
>> Joseph
>>
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the VTK FAQ at:
>> http://www.vtk.org/Wiki/VTK_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.vtk.org/mailman/listinfo/vtkusers
>
>
>
More information about the vtkusers
mailing list