[ITK] [ITK-users] ITK and VNL - SVD decomposition of image - Seg fault
Tejas
tejas.rox at gmail.com
Tue Nov 25 20:09:27 EST 2014
I have created a temporary solution to my problem by using the SVD routine
available in OpenCV.
I did not have the OpenCV bridge module in ITK built, so I had to
incorporate the relevant header files (core.hpp, imgproc.hpp, gpu.hpp,
highgui.hpp etc.) into my project, and link to the appropriate
configuration-specific OpenCV libraries as well. After completing my image
analysis operation, I used the ITK import image filter to import my OpenCV
data back into ITK.
My hope is that this fix will help someone in the future whenever they
stumble across the same problem I had faced while using vnl_svd.
The code is as follows:
typedef itk::VariableSizeMatrix< double > MatrixType;
// create a matrix structure in ITK
MatrixType matrix;
matrix.SetSize(rows, cols); // set the size of the ITK matrix
// create an image iterator
IterType inputIter( dAlignedIm, dAlignedIm->GetLargestPossibleRegion() );
inputIter.GoToBegin();
// copy the BRATS image to openCV
while(!inputIter.IsAtEnd()) {
// copy the input image intensity value into the corresponding matrix
matrix(inputIter.GetIndex()[1], inputIter.GetIndex()[0]) =
inputIter.Get();
cv_brats.at<double>(inputIter.GetIndex()[1], inputIter.GetIndex()[0]) =
matrix(inputIter.GetIndex()[1], inputIter.GetIndex()[0]);
++inputIter;
}
Mat U, S, Vt;
// compute svd of Brats image
SVD::compute(cv_brats, S, U, Vt);
// .............
// Do whatever you want with the singular values and orthonormal
matrices.
// I used it for reconstruction, so I will get back an OpenCV mat
structure called 'reconstruct'.
// IMPORT OpenCV MAT INTO ITK
ImportFilterType::Pointer importFilter = ImportFilterType::New();
// set size of the image
ImportFilterType::SizeType size;
size[0] = reconstruct.cols; // size along X
size[1] = reconstruct.rows; // size along Y
// set starting index of image - top left
ImportFilterType::IndexType start;
start.Fill( 0 );
// set region of the image
ImportFilterType::RegionType region;
region.SetIndex( start );
region.SetSize( size );
importFilter->SetRegion( region );
// set spacing of image
double spacing[ Dimension ];
spacing[0] = 1.0; // along X direction
spacing[1] = 1.0; // along Y direction
importFilter->SetSpacing( spacing );
// set origin of output image
double origin[ Dimension ];
origin[0] = 0.0; // X coordinate
origin[1] = 0.0; // Y coordinate
importFilter->SetOrigin( origin );
// allocate CPU image buffer after specifying size of image
const unsigned int numberOfPixels = size[0] * size[1];
double * localBuffer = new double[ numberOfPixels ];
// reconstruct.data is an (unsigned char*) type
// reinterpret cast it into the (double*) type, which is used by the local
buffer
localBuffer = reinterpret_cast<double *> (reconstruct.data);
// now, import the local image data
const bool importImageFilterWillOwnTheBuffer = false;
importFilter->SetImportPointer( localBuffer, numberOfPixels,
importImageFilterWillOwnTheBuffer );
// pass data from the imported image to the new ITK image
doubleImageType::Pointer newIm = importFilter->GetOutput();
--
View this message in context: http://itk-insight-users.2283740.n2.nabble.com/ITK-and-VNL-SVD-decomposition-of-image-Seg-fault-tp7586604p7586622.html
Sent from the ITK Insight Users mailing list archive at Nabble.com.
_____________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php
Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ
Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-users
More information about the Community
mailing list