[vtkusers] Smooth Data Unstructured Grid

Seth Gilchrist seth at mech.ubc.ca
Thu Oct 20 14:40:50 EDT 2011


Hi Greg,
Sure, the code that I wrote is at the end of this email - I'm always happy
to have someone else to look for bugs!  It creates a temp grid to store the
original version of the unstructured grid, and then modifies the values in
the original grid in place, with new values being calculated from the
unmodified temp grid.

I'm using itk and vtk to measure strains in solids, where I use image
registration to find the displacements of the nodes in an FE mesh and then
use FE techniques to calculate the strains.  The registration is carried out
between an image of the solid unloaded, and another image of the solid
loaded (deformed).  Basically I'm doing digital image correlation in 3D, and
using an FE mesh to decide where to perform my registration.

The unstructured grid is read in from a gmsh mesh file.

Cheers,
Seth

/** A function to smooth the image using a weighted moving average using
 * a Gaussian kernel for weight calculation. The function is given R,
 * the radius of points to consider; sigma, the standard deviation of the
 * of the Gaussian kernel; and mean, the mean of the Gaussian kernel in
 * terms of distance from the point being averaged (this will in 99.99%
 * of cases need to be set to 0).*/
void WeightedMovingAverageFilter( double R , double sigma, double mean )
{
 // create a duplicate of the data image
DataImagePointer tempImage = DataImagePointer::New();
 tempImage->DeepCopy(this->m_DataImage);
// loop through the points of both the images
 unsigned int nPoints = tempImage->GetNumberOfPoints();
for( unsigned int i = 0; i < nPoints; ++i ){
 // find the points withing the specified radius of the current point
position
double *cPoint = tempImage->GetPoint( i ); // current point position
 vtkIdList *pointList = vtkIdList::New(); // id list to hold ids of the
closest points
this->m_KDTree->FindPointsWithinRadius( R, cPoint, pointList );
 // move through the points in the list and use them to calculate the new
point value
unsigned int N = pointList->GetNumberOfIds();
 double totalNumerator[3] = { 0, 0, 0 };
double totalDenominator = 0;
for( unsigned int j = 0; j < N; ++j){
 vtkIdType cId = pointList->GetId( j );
// calcualate the weight based on the distance and the gaussian parameters
 double *tempLocation = tempImage->GetPoint( cId );
double cDistance = sqrt( pow((*tempLocation-*cPoint),2) +
pow((*(tempLocation+1)-(*cPoint+1)),2) +
pow((*(tempLocation+2)-(*cPoint+2)),2) );
 double weight =
1/(2.50662827*sqrt(sigma))*pow(2.718281828,-pow((cDistance-mean),2)/(2*sigma*sigma));
// 1/sqrt(2*pi*sigma)*e^(-(X-mean)^2/(s*sigma^2)) (the Gaussian
distribution)
 // add the weight to the total denominator
totalDenominator = totalDenominator + weight;
 // add to the numerators
double *tempPixel =
tempImage->GetPointData()->GetArray("Displacement")->GetTuple( cId ); // the
original data in the current point
 totalNumerator[0] = totalNumerator[0] + weight * *tempPixel;
totalNumerator[1] = totalNumerator[1] + weight * *(tempPixel+1);
 totalNumerator[2] = totalNumerator[2] + weight * *(tempPixel+2);
}
// use the total numerators and denominator to calculate the new pixel
 double *newPixel = new double[3];
*newPixel = totalNumerator[0] / totalDenominator;
 *(newPixel+1) = totalNumerator[1] / totalDenominator;
*(newPixel+2) = totalNumerator[2] / totalDenominator;
 // set the data image pixel to the value of the new pixel
this->m_DataImage->GetPointData()->GetArray("Displacement")->SetTuple( i,
newPixel );
 }
}




On Thu, Oct 20, 2011 at 11:19 AM, greg aiken <gregaiken at hotmail.com> wrote:

>  I dont suppose you have any sample code to share with someone who is
> interested to learn how you did this?
>
> Greg
>
> ps - what is your application?  where did your unstructured grid data
> originate from?  thanks.
>
> ------------------------------
> From: seth at mech.ubc.ca
> Date: Thu, 20 Oct 2011 11:00:21 -0700
> To: vtkusers at vtk.org
> Subject: Re: [vtkusers] Smooth Data Unstructured Grid
>
>
> Hello all,
> There were no suggestions on how to do this, so in the end I implemented a
> weighted moving average filter, with a Gaussian kernel, based on the
> distance from the current point to the neighbouring points.
>
> Cheers,
> Seth
>
>
>
> On Tue, Oct 18, 2011 at 11:15 AM, Seth Gilchrist <seth at mech.ubc.ca> wrote:
>
> Hello,
> I have an unstructured grid that contains point data.  The point data has a
> certain amount of noise associated with it and I would like to smooth the
> data.  The classes vtkImageGaussianSmooth and vtkImageMedian3D require
> variables in pixel units, so I assume that they won't work on unstructured
> grid data.
>
> Is there a class/way to smooth/filter the data in an unstructured grid?
>
> Thanks,
> Seth
>
>
>
> _______________________________________________ Powered by www.kitware.comVisit 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20111020/e81afba4/attachment.htm>


More information about the vtkusers mailing list