void OcrNet::CreateInput(fann_type input[], int num_input, cv::Mat &image, ImageRegion region) { int min_x = 0, max_x = 0, min_y = 0, max_y = 0; double ratio_x = 0.0, ratio_y = 0.0; min_x = region.get_min_x(); min_y = region.get_min_y(); max_x = region.get_max_x(); max_y = region.get_max_y(); ratio_x = ((double) (max_x - min_x)) / ((double) Cols); ratio_y = ((double) (max_y - min_y)) / ((double) Rows); // std::cout << std::endl << "Input Elements:" << std::endl; for(int j=0; j < Rows; j++) { for(int i=0; i < Cols; i++) { input[j + i] = DownSampleRegion(image, region, i, j, ratio_x, ratio_y); // std::cout << input[j + i] << " "; } // std::cout << std::endl; } }
float OcrNet::DownSampleRegion(cv::Mat &image, ImageRegion region, int x, int y, double ratio_x, double ratio_y) { int min_x = 0, max_x = 0, min_y = 0, max_y = 0, begin_x = 0, begin_y = 0, end_x = 0, end_y = 0; unsigned char value; min_x = region.get_min_x(); min_y = region.get_min_y(); max_x = region.get_max_x(); max_y = region.get_max_y(); for(begin_y = (min_y + (y * ratio_y)), end_y = (min_y + ((y + 1) * ratio_y)); begin_y <= end_y; begin_y++) { for(begin_x = (min_x + (x * ratio_x)), end_x = (min_x + ((x + 1) * ratio_x)); begin_x <= end_x; begin_x++) { value = image.at<unsigned char>(begin_y, begin_x); if(value == 0) return UPPER_VAL; } } return LOWER_VAL; }
void AddArtifactsToDwiImageFilter< TPixelType >::UpdateOutputInformation() { // Calls to superclass updateoutputinformation Superclass::UpdateOutputInformation(); typename InputImageType::Pointer inputImage = static_cast< InputImageType* >( this->ProcessObject::GetInput(0) ); itk::ImageRegion<3> inputRegion = inputImage->GetLargestPossibleRegion(); typename itk::ImageDuplicator<InputImageType>::Pointer duplicator = itk::ImageDuplicator<InputImageType>::New(); duplicator->SetInputImage( inputImage ); duplicator->Update(); typename InputImageType::Pointer outputImage = duplicator->GetOutput(); if ( m_Parameters.m_SignalGen.m_CroppingFactor<1.0) { ImageRegion<3> croppedRegion = inputRegion; croppedRegion.SetSize(1, croppedRegion.GetSize(1)* m_Parameters.m_SignalGen.m_CroppingFactor); itk::Point<double,3> shiftedOrigin = inputImage->GetOrigin(); shiftedOrigin[1] += (inputRegion.GetSize(1)-croppedRegion.GetSize(1))*inputImage->GetSpacing()[1]/2; outputImage = InputImageType::New(); outputImage->SetSpacing( inputImage->GetSpacing() ); outputImage->SetOrigin( shiftedOrigin ); outputImage->SetDirection( inputImage->GetDirection() ); outputImage->SetLargestPossibleRegion( croppedRegion ); outputImage->SetBufferedRegion( croppedRegion ); outputImage->SetRequestedRegion( croppedRegion ); outputImage->SetVectorLength( inputImage->GetVectorLength() ); outputImage->Allocate(); typename InputImageType::PixelType temp; temp.SetSize(inputImage->GetVectorLength()); temp.Fill(0.0); outputImage->FillBuffer(temp); } this->GetOutput()->SetOrigin( outputImage->GetOrigin() ); this->GetOutput()->SetLargestPossibleRegion( outputImage->GetLargestPossibleRegion() ); this->GetOutput()->SetBufferedRegion( outputImage->GetLargestPossibleRegion() ); this->GetOutput()->SetRequestedRegion( outputImage->GetLargestPossibleRegion() ); }
// EXTEND_PAD won't help us here; we have to create a temporary surface to hold // the subimage of pixels we're allowed to sample. static already_AddRefed<gfxDrawable> CreateSamplingRestrictedDrawable(gfxDrawable* aDrawable, gfxContext* aContext, const ImageRegion& aRegion, const SurfaceFormat aFormat) { PROFILER_LABEL("gfxUtils", "CreateSamplingRestricedDrawable", js::ProfileEntry::Category::GRAPHICS); gfxRect clipExtents = aContext->GetClipExtents(); // Inflate by one pixel because bilinear filtering will sample at most // one pixel beyond the computed image pixel coordinate. clipExtents.Inflate(1.0); gfxRect needed = aRegion.IntersectAndRestrict(clipExtents); needed.RoundOut(); // if 'needed' is empty, nothing will be drawn since aFill // must be entirely outside the clip region, so it doesn't // matter what we do here, but we should avoid trying to // create a zero-size surface. if (needed.IsEmpty()) return nullptr; gfxIntSize size(int32_t(needed.Width()), int32_t(needed.Height())); RefPtr<DrawTarget> target = gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(ToIntSize(size), aFormat); if (!target) { return nullptr; } nsRefPtr<gfxContext> tmpCtx = new gfxContext(target); tmpCtx->SetOperator(OptimalFillOperator()); aDrawable->Draw(tmpCtx, needed - needed.TopLeft(), true, GraphicsFilter::FILTER_FAST, 1.0, gfxMatrix::Translation(needed.TopLeft())); RefPtr<SourceSurface> surface = target->Snapshot(); nsRefPtr<gfxDrawable> drawable = new gfxSurfaceDrawable(surface, size, gfxMatrix::Translation(-needed.TopLeft())); return drawable.forget(); }
void TractsToFiberEndingsImageFilter< TInputImage, TOutputPixelType > ::GenerateData() { MITK_INFO << "Generating 2D fiber endings image"; if(&typeid(TOutputPixelType) != &typeid(unsigned char)) { MITK_INFO << "Only 'unsigned char' and 'itk::RGBAPixel<unsigned char> supported as OutputPixelType"; return; } mitk::Geometry3D::Pointer geometry = m_FiberBundle->GetGeometry(); typename OutputImageType::Pointer outImage = static_cast< OutputImageType * >(this->ProcessObject::GetOutput(0)); outImage->SetSpacing( geometry->GetSpacing()/m_UpsamplingFactor ); // Set the image spacing mitk::Point3D origin = geometry->GetOrigin(); mitk::Point3D indexOrigin; geometry->WorldToIndex(origin, indexOrigin); indexOrigin[0] = indexOrigin[0] - .5 * (1.0-1.0/m_UpsamplingFactor); indexOrigin[1] = indexOrigin[1] - .5 * (1.0-1.0/m_UpsamplingFactor); indexOrigin[2] = indexOrigin[2] - .5 * (1.0-1.0/m_UpsamplingFactor); mitk::Point3D newOrigin; geometry->IndexToWorld(indexOrigin, newOrigin); outImage->SetOrigin( newOrigin ); // Set the image origin itk::Matrix<double, 3, 3> matrix; for (int i=0; i<3; i++) for (int j=0; j<3; j++) matrix[j][i] = geometry->GetMatrixColumn(i)[j]/geometry->GetSpacing().GetElement(i); outImage->SetDirection( matrix ); // Set the image direction float* bounds = m_FiberBundle->GetBounds(); ImageRegion<3> upsampledRegion; upsampledRegion.SetSize(0, bounds[0]); upsampledRegion.SetSize(1, bounds[1]); upsampledRegion.SetSize(2, bounds[2]); typename InputImageType::RegionType::SizeType upsampledSize = upsampledRegion.GetSize(); for (unsigned int n = 0; n < 3; n++) { upsampledSize[n] = upsampledSize[n] * m_UpsamplingFactor; } upsampledRegion.SetSize( upsampledSize ); outImage->SetRegions( upsampledRegion ); outImage->Allocate(); int w = upsampledSize[0]; int h = upsampledSize[1]; int d = upsampledSize[2]; unsigned char* accuout; accuout = reinterpret_cast<unsigned char*>(outImage->GetBufferPointer()); for (int i=0; i<w*h*d; i++) accuout[i] = 0; typedef mitk::FiberBundle::ContainerTractType ContainerTractType; typedef mitk::FiberBundle::ContainerType ContainerType; typedef mitk::FiberBundle::ContainerPointType ContainerPointType; ContainerType::Pointer tractContainer = m_FiberBundle->GetTractContainer(); for (int i=0; i<tractContainer->Size(); i++) { ContainerTractType::Pointer tract = tractContainer->GetElement(i); int tractsize = tract->Size(); if (tractsize>1) { ContainerPointType start = tract->GetElement(0); ContainerPointType end = tract->GetElement(tractsize-1); start[0] = (start[0]+0.5) * m_UpsamplingFactor; start[1] = (start[1]+0.5) * m_UpsamplingFactor; start[2] = (start[2]+0.5) * m_UpsamplingFactor; // int coordinates inside image? int px = (int) (start[0]); if (px < 0 || px >= w) continue; int py = (int) (start[1]); if (py < 0 || py >= h) continue; int pz = (int) (start[2]); if (pz < 0 || pz >= d) continue; accuout[( px + w*(py + h*pz ))] += 1; end[0] = (end[0]+0.5) * m_UpsamplingFactor; end[1] = (end[1]+0.5) * m_UpsamplingFactor; end[2] = (end[2]+0.5) * m_UpsamplingFactor; // int coordinates inside image? px = (int) (end[0]); if (px < 0 || px >= w) continue; py = (int) (end[1]); if (py < 0 || py >= h) continue; pz = (int) (end[2]); if (pz < 0 || pz >= d) continue; accuout[( px + w*(py + h*pz ))] += 1; } } MITK_INFO << "2D fiber endings image generated"; }
void TractDensityImageFilter< OutputImageType >::GenerateData() { // generate upsampled image mitk::Geometry3D::Pointer geometry = m_FiberBundle->GetGeometry(); typename OutputImageType::Pointer outImage = this->GetOutput(); // calculate new image parameters mitk::Vector3D newSpacing; mitk::Point3D newOrigin; itk::Matrix<double, 3, 3> newDirection; ImageRegion<3> upsampledRegion; if (m_UseImageGeometry && !m_InputImage.IsNull()) { MITK_INFO << "TractDensityImageFilter: using image geometry"; newSpacing = m_InputImage->GetSpacing()/m_UpsamplingFactor; upsampledRegion = m_InputImage->GetLargestPossibleRegion(); newOrigin = m_InputImage->GetOrigin(); typename OutputImageType::RegionType::SizeType size = upsampledRegion.GetSize(); size[0] *= m_UpsamplingFactor; size[1] *= m_UpsamplingFactor; size[2] *= m_UpsamplingFactor; upsampledRegion.SetSize(size); newDirection = m_InputImage->GetDirection(); } else { MITK_INFO << "TractDensityImageFilter: using fiber bundle geometry"; newSpacing = geometry->GetSpacing()/m_UpsamplingFactor; newOrigin = geometry->GetOrigin(); mitk::Geometry3D::BoundsArrayType bounds = geometry->GetBounds(); newOrigin[0] += bounds.GetElement(0); newOrigin[1] += bounds.GetElement(2); newOrigin[2] += bounds.GetElement(4); for (int i=0; i<3; i++) for (int j=0; j<3; j++) newDirection[j][i] = geometry->GetMatrixColumn(i)[j]; upsampledRegion.SetSize(0, geometry->GetExtent(0)*m_UpsamplingFactor); upsampledRegion.SetSize(1, geometry->GetExtent(1)*m_UpsamplingFactor); upsampledRegion.SetSize(2, geometry->GetExtent(2)*m_UpsamplingFactor); } typename OutputImageType::RegionType::SizeType upsampledSize = upsampledRegion.GetSize(); // apply new image parameters outImage->SetSpacing( newSpacing ); outImage->SetOrigin( newOrigin ); outImage->SetDirection( newDirection ); outImage->SetRegions( upsampledRegion ); outImage->Allocate(); outImage->FillBuffer(0.0); int w = upsampledSize[0]; int h = upsampledSize[1]; int d = upsampledSize[2]; // set/initialize output OutPixelType* outImageBufferPointer = (OutPixelType*)outImage->GetBufferPointer(); // resample fiber bundle float minSpacing = 1; if(newSpacing[0]<newSpacing[1] && newSpacing[0]<newSpacing[2]) minSpacing = newSpacing[0]; else if (newSpacing[1] < newSpacing[2]) minSpacing = newSpacing[1]; else minSpacing = newSpacing[2]; MITK_INFO << "TractDensityImageFilter: resampling fibers to ensure sufficient voxel coverage"; m_FiberBundle = m_FiberBundle->GetDeepCopy(); m_FiberBundle->ResampleFibers(minSpacing); MITK_INFO << "TractDensityImageFilter: starting image generation"; vtkSmartPointer<vtkPolyData> fiberPolyData = m_FiberBundle->GetFiberPolyData(); vtkSmartPointer<vtkCellArray> vLines = fiberPolyData->GetLines(); vLines->InitTraversal(); int numFibers = m_FiberBundle->GetNumFibers(); boost::progress_display disp(numFibers); for( int i=0; i<numFibers; i++ ) { ++disp; vtkIdType numPoints(0); vtkIdType* points(NULL); vLines->GetNextCell ( numPoints, points ); // fill output image for( int j=0; j<numPoints; j++) { itk::Point<float, 3> vertex = GetItkPoint(fiberPolyData->GetPoint(points[j])); itk::Index<3> index; itk::ContinuousIndex<float, 3> contIndex; outImage->TransformPhysicalPointToIndex(vertex, index); outImage->TransformPhysicalPointToContinuousIndex(vertex, contIndex); float frac_x = contIndex[0] - index[0]; float frac_y = contIndex[1] - index[1]; float frac_z = contIndex[2] - index[2]; if (frac_x<0) { index[0] -= 1; frac_x += 1; } if (frac_y<0) { index[1] -= 1; frac_y += 1; } if (frac_z<0) { index[2] -= 1; frac_z += 1; } frac_x = 1-frac_x; frac_y = 1-frac_y; frac_z = 1-frac_z; // int coordinates inside image? if (index[0] < 0 || index[0] >= w-1) continue; if (index[1] < 0 || index[1] >= h-1) continue; if (index[2] < 0 || index[2] >= d-1) continue; if (m_BinaryOutput) { outImageBufferPointer[( index[0] + w*(index[1] + h*index[2] ))] = 1; outImageBufferPointer[( index[0] + w*(index[1]+1+ h*index[2] ))] = 1; outImageBufferPointer[( index[0] + w*(index[1] + h*index[2]+h))] = 1; outImageBufferPointer[( index[0] + w*(index[1]+1+ h*index[2]+h))] = 1; outImageBufferPointer[( index[0]+1 + w*(index[1] + h*index[2] ))] = 1; outImageBufferPointer[( index[0]+1 + w*(index[1] + h*index[2]+h))] = 1; outImageBufferPointer[( index[0]+1 + w*(index[1]+1+ h*index[2] ))] = 1; outImageBufferPointer[( index[0]+1 + w*(index[1]+1+ h*index[2]+h))] = 1; } else { outImageBufferPointer[( index[0] + w*(index[1] + h*index[2] ))] += ( frac_x)*( frac_y)*( frac_z); outImageBufferPointer[( index[0] + w*(index[1]+1+ h*index[2] ))] += ( frac_x)*(1-frac_y)*( frac_z); outImageBufferPointer[( index[0] + w*(index[1] + h*index[2]+h))] += ( frac_x)*( frac_y)*(1-frac_z); outImageBufferPointer[( index[0] + w*(index[1]+1+ h*index[2]+h))] += ( frac_x)*(1-frac_y)*(1-frac_z); outImageBufferPointer[( index[0]+1 + w*(index[1] + h*index[2] ))] += (1-frac_x)*( frac_y)*( frac_z); outImageBufferPointer[( index[0]+1 + w*(index[1] + h*index[2]+h))] += (1-frac_x)*( frac_y)*(1-frac_z); outImageBufferPointer[( index[0]+1 + w*(index[1]+1+ h*index[2] ))] += (1-frac_x)*(1-frac_y)*( frac_z); outImageBufferPointer[( index[0]+1 + w*(index[1]+1+ h*index[2]+h))] += (1-frac_x)*(1-frac_y)*(1-frac_z); } } } if (!m_OutputAbsoluteValues && !m_BinaryOutput) { MITK_INFO << "TractDensityImageFilter: max-normalizing output image"; OutPixelType max = 0; for (int i=0; i<w*h*d; i++) if (max < outImageBufferPointer[i]) max = outImageBufferPointer[i]; if (max>0) for (int i=0; i<w*h*d; i++) outImageBufferPointer[i] /= max; } if (m_InvertImage) { MITK_INFO << "TractDensityImageFilter: inverting image"; for (int i=0; i<w*h*d; i++) outImageBufferPointer[i] = 1-outImageBufferPointer[i]; } MITK_INFO << "TractDensityImageFilter: finished processing"; }
void TractsToProbabilityImageFilter< TInputImage, TOutputPixelType > ::GenerateData() { bool isRgba = false; if(&typeid(TOutputPixelType) == &typeid(itk::RGBAPixel<unsigned char>)) { isRgba = true; } else if(&typeid(TOutputPixelType) != &typeid(unsigned char)) { MITK_INFO << "Only 'unsigned char' and 'itk::RGBAPixel<unsigned char> supported as OutputPixelType"; return; } mitk::Geometry3D::Pointer geometry = m_FiberBundle->GetGeometry(); typename OutputImageType::Pointer outImage = static_cast< OutputImageType * >(this->ProcessObject::GetOutput(0)); outImage->SetSpacing( geometry->GetSpacing()/m_UpsamplingFactor ); // Set the image spacing mitk::Point3D origin = geometry->GetOrigin(); mitk::Point3D indexOrigin; geometry->WorldToIndex(origin, indexOrigin); indexOrigin[0] = indexOrigin[0] - .5 * (1.0-1.0/m_UpsamplingFactor); indexOrigin[1] = indexOrigin[1] - .5 * (1.0-1.0/m_UpsamplingFactor); indexOrigin[2] = indexOrigin[2] - .5 * (1.0-1.0/m_UpsamplingFactor); mitk::Point3D newOrigin; geometry->IndexToWorld(indexOrigin, newOrigin); outImage->SetOrigin( newOrigin ); // Set the image origin itk::Matrix<double, 3, 3> matrix; for (int i=0; i<3; i++) for (int j=0; j<3; j++) matrix[j][i] = geometry->GetMatrixColumn(i)[j]/geometry->GetSpacing().GetElement(i); outImage->SetDirection( matrix ); // Set the image direction float* bounds = m_FiberBundle->GetBounds(); ImageRegion<3> upsampledRegion; upsampledRegion.SetSize(0, bounds[0]); upsampledRegion.SetSize(1, bounds[1]); upsampledRegion.SetSize(2, bounds[2]); typename InputImageType::RegionType::SizeType upsampledSize = upsampledRegion.GetSize(); for (unsigned int n = 0; n < 3; n++) { upsampledSize[n] = upsampledSize[n] * m_UpsamplingFactor; } upsampledRegion.SetSize( upsampledSize ); outImage->SetRegions( upsampledRegion ); outImage->Allocate(); // itk::RGBAPixel<unsigned char> pix; // pix.Set(0,0,0,0); // outImage->FillBuffer(pix); int w = upsampledSize[0]; int h = upsampledSize[1]; int d = upsampledSize[2]; unsigned char* accuout; float* accu; accuout = reinterpret_cast<unsigned char*>(outImage->GetBufferPointer()); if(isRgba) { // accuout = static_cast<unsigned char*>( outImage->GetBufferPointer()[0].GetDataPointer()); accu = new float[w*h*d*4]; for (int i=0; i<w*h*d*4; i++) accu[i] = 0; } else { accu = new float[w*h*d]; for (int i=0; i<w*h*d; i++) accu[i] = 0; } // for each tract int numTracts = m_FiberBundle->GetNumTracts(); for( int i=0; i<numTracts; i++ ) { //////////////////// // upsampling std::vector< itk::Point<float, 3> > vertices; // for each vertex int numVertices = m_FiberBundle->GetNumPoints(i); for( int j=0; j<numVertices-1; j++) { itk::Point<float, 3> point = m_FiberBundle->GetPoint(i,j); itk::Point<float, 3> nextPoint = m_FiberBundle->GetPoint(i,j+1); point[0] += 0.5 - 0.5/m_UpsamplingFactor; point[1] += 0.5 - 0.5/m_UpsamplingFactor; point[2] += 0.5 - 0.5/m_UpsamplingFactor; nextPoint[0] += 0.5 - 0.5/m_UpsamplingFactor; nextPoint[1] += 0.5 - 0.5/m_UpsamplingFactor; nextPoint[2] += 0.5 - 0.5/m_UpsamplingFactor; for(int k=1; k<=m_UpsamplingFactor; k++) { itk::Point<float, 3> newPoint; newPoint[0] = point[0] + ((double)k/(double)m_UpsamplingFactor)*(nextPoint[0]-point[0]); newPoint[1] = point[1] + ((double)k/(double)m_UpsamplingFactor)*(nextPoint[1]-point[1]); newPoint[2] = point[2] + ((double)k/(double)m_UpsamplingFactor)*(nextPoint[2]-point[2]); vertices.push_back(newPoint); } } //////////////////// // calc directions (which are used as weights) std::list< itk::Point<float, 3> > rgbweights; std::list<float> intensities; // for each vertex numVertices = vertices.size(); for( int j=0; j<numVertices-1; j++) { itk::Point<float, 3> vertex = vertices.at(j); itk::Point<float, 3> vertexPost = vertices.at(j+1); itk::Point<float, 3> dir; dir[0] = fabs((vertexPost[0] - vertex[0]) * outImage->GetSpacing()[0]); dir[1] = fabs((vertexPost[1] - vertex[1]) * outImage->GetSpacing()[1]); dir[2] = fabs((vertexPost[2] - vertex[2]) * outImage->GetSpacing()[2]); if(isRgba) { rgbweights.push_back(dir); } float intensity = sqrt(dir[0]*dir[0]+dir[1]*dir[1]+dir[2]*dir[2]); intensities.push_back(intensity); // last point gets same as previous one if(j==numVertices-2) { if(isRgba) { rgbweights.push_back(dir); } intensities.push_back(intensity); } } //////////////////// // fill output image // for each vertex for( int j=0; j<numVertices; j++) { itk::Point<float, 3> vertex = vertices.at(j); itk::Point<float, 3> rgbweight; if(isRgba) { rgbweight = rgbweights.front(); rgbweights.pop_front(); } float intweight = intensities.front(); intensities.pop_front(); // scaling coordinates (index coords scale with upsampling) vertex[0] = vertex[0] * m_UpsamplingFactor; vertex[1] = vertex[1] * m_UpsamplingFactor; vertex[2] = vertex[2] * m_UpsamplingFactor; // int coordinates inside image? int px = (int) (vertex[0]); if (px < 0 || px >= w-1) continue; int py = (int) (vertex[1]); if (py < 0 || py >= h-1) continue; int pz = (int) (vertex[2]); if (pz < 0 || pz >= d-1) continue; // float fraction of coordinates float frac_x = vertex[0] - px; float frac_y = vertex[1] - py; float frac_z = vertex[2] - pz; float scale = 100 * pow((float)m_UpsamplingFactor,3); if(isRgba) { // add to r-channel in output image accu[0+4*( px + w*(py + h*pz ))] += (1-frac_x)*(1-frac_y)*(1-frac_z) * rgbweight[0] * scale; accu[0+4*( px + w*(py+1+ h*pz ))] += (1-frac_x)*( frac_y)*(1-frac_z) * rgbweight[0] * scale; accu[0+4*( px + w*(py + h*pz+h))] += (1-frac_x)*(1-frac_y)*( frac_z) * rgbweight[0] * scale; accu[0+4*( px + w*(py+1+ h*pz+h))] += (1-frac_x)*( frac_y)*( frac_z) * rgbweight[0] * scale; accu[0+4*( px+1 + w*(py + h*pz ))] += ( frac_x)*(1-frac_y)*(1-frac_z) * rgbweight[0] * scale; accu[0+4*( px+1 + w*(py + h*pz+h))] += ( frac_x)*(1-frac_y)*( frac_z) * rgbweight[0] * scale; accu[0+4*( px+1 + w*(py+1+ h*pz ))] += ( frac_x)*( frac_y)*(1-frac_z) * rgbweight[0] * scale; accu[0+4*( px+1 + w*(py+1+ h*pz+h))] += ( frac_x)*( frac_y)*( frac_z) * rgbweight[0] * scale; // add to g-channel in output image accu[1+4*( px + w*(py + h*pz ))] += (1-frac_x)*(1-frac_y)*(1-frac_z) * rgbweight[1] * scale; accu[1+4*( px + w*(py+1+ h*pz ))] += (1-frac_x)*( frac_y)*(1-frac_z) * rgbweight[1] * scale; accu[1+4*( px + w*(py + h*pz+h))] += (1-frac_x)*(1-frac_y)*( frac_z) * rgbweight[1] * scale; accu[1+4*( px + w*(py+1+ h*pz+h))] += (1-frac_x)*( frac_y)*( frac_z) * rgbweight[1] * scale; accu[1+4*( px+1 + w*(py + h*pz ))] += ( frac_x)*(1-frac_y)*(1-frac_z) * rgbweight[1] * scale; accu[1+4*( px+1 + w*(py + h*pz+h))] += ( frac_x)*(1-frac_y)*( frac_z) * rgbweight[1] * scale; accu[1+4*( px+1 + w*(py+1+ h*pz ))] += ( frac_x)*( frac_y)*(1-frac_z) * rgbweight[1] * scale; accu[1+4*( px+1 + w*(py+1+ h*pz+h))] += ( frac_x)*( frac_y)*( frac_z) * rgbweight[1] * scale; // add to b-channel in output image accu[2+4*( px + w*(py + h*pz ))] += (1-frac_x)*(1-frac_y)*(1-frac_z) * rgbweight[2] * scale; accu[2+4*( px + w*(py+1+ h*pz ))] += (1-frac_x)*( frac_y)*(1-frac_z) * rgbweight[2] * scale; accu[2+4*( px + w*(py + h*pz+h))] += (1-frac_x)*(1-frac_y)*( frac_z) * rgbweight[2] * scale; accu[2+4*( px + w*(py+1+ h*pz+h))] += (1-frac_x)*( frac_y)*( frac_z) * rgbweight[2] * scale; accu[2+4*( px+1 + w*(py + h*pz ))] += ( frac_x)*(1-frac_y)*(1-frac_z) * rgbweight[2] * scale; accu[2+4*( px+1 + w*(py + h*pz+h))] += ( frac_x)*(1-frac_y)*( frac_z) * rgbweight[2] * scale; accu[2+4*( px+1 + w*(py+1+ h*pz ))] += ( frac_x)*( frac_y)*(1-frac_z) * rgbweight[2] * scale; accu[2+4*( px+1 + w*(py+1+ h*pz+h))] += ( frac_x)*( frac_y)*( frac_z) * rgbweight[2] * scale; // add to a-channel in output image accu[3+4*( px + w*(py + h*pz ))] += (1-frac_x)*(1-frac_y)*(1-frac_z) * intweight * scale; accu[3+4*( px + w*(py+1+ h*pz ))] += (1-frac_x)*( frac_y)*(1-frac_z) * intweight * scale; accu[3+4*( px + w*(py + h*pz+h))] += (1-frac_x)*(1-frac_y)*( frac_z) * intweight * scale; accu[3+4*( px + w*(py+1+ h*pz+h))] += (1-frac_x)*( frac_y)*( frac_z) * intweight * scale; accu[3+4*( px+1 + w*(py + h*pz ))] += ( frac_x)*(1-frac_y)*(1-frac_z) * intweight * scale; accu[3+4*( px+1 + w*(py + h*pz+h))] += ( frac_x)*(1-frac_y)*( frac_z) * intweight * scale; accu[3+4*( px+1 + w*(py+1+ h*pz ))] += ( frac_x)*( frac_y)*(1-frac_z) * intweight * scale; accu[3+4*( px+1 + w*(py+1+ h*pz+h))] += ( frac_x)*( frac_y)*( frac_z) * intweight * scale; } else if (m_BinaryEnvelope) { accu[( px + w*(py + h*pz ))] = 1; accu[( px + w*(py+1+ h*pz ))] = 1; accu[( px + w*(py + h*pz+h))] = 1; accu[( px + w*(py+1+ h*pz+h))] = 1; accu[( px+1 + w*(py + h*pz ))] = 1; accu[( px+1 + w*(py + h*pz+h))] = 1; accu[( px+1 + w*(py+1+ h*pz ))] = 1; accu[( px+1 + w*(py+1+ h*pz+h))] = 1; } else { accu[( px + w*(py + h*pz ))] += (1-frac_x)*(1-frac_y)*(1-frac_z) * intweight * scale; accu[( px + w*(py+1+ h*pz ))] += (1-frac_x)*( frac_y)*(1-frac_z) * intweight * scale; accu[( px + w*(py + h*pz+h))] += (1-frac_x)*(1-frac_y)*( frac_z) * intweight * scale; accu[( px + w*(py+1+ h*pz+h))] += (1-frac_x)*( frac_y)*( frac_z) * intweight * scale; accu[( px+1 + w*(py + h*pz ))] += ( frac_x)*(1-frac_y)*(1-frac_z) * intweight * scale; accu[( px+1 + w*(py + h*pz+h))] += ( frac_x)*(1-frac_y)*( frac_z) * intweight * scale; accu[( px+1 + w*(py+1+ h*pz ))] += ( frac_x)*( frac_y)*(1-frac_z) * intweight * scale; accu[( px+1 + w*(py+1+ h*pz+h))] += ( frac_x)*( frac_y)*( frac_z) * intweight * scale; } } } float maxRgb = 0.000000001; float maxInt = 0.000000001; int numPix; if(isRgba) { numPix = w*h*d*4; // calc maxima for(int i=0; i<numPix; i++) { if((i-3)%4 != 0) { if(accu[i] > maxRgb) { maxRgb = accu[i]; } } else { if(accu[i] > maxInt) { maxInt = accu[i]; } } } // write output, normalized uchar 0..255 for(int i=0; i<numPix; i++) { if((i-3)%4 != 0) { accuout[i] = (unsigned char) (255.0 * accu[i] / maxRgb); } else { accuout[i] = (unsigned char) (255.0 * accu[i] / maxInt); } } } else if (m_BinaryEnvelope) { numPix = w*h*d; // write output, normalized uchar 0..255 for(int i=0; i<numPix; i++) { if(m_InvertImage) { accuout[i] = (unsigned char) ((int)(accu[i]+1)%2); } else { accuout[i] = (unsigned char) accu[i]; } } } else { numPix = w*h*d; // calc maxima for(int i=0; i<numPix; i++) { if(accu[i] > maxInt) { maxInt = accu[i]; } } // write output, normalized uchar 0..255 for(int i=0; i<numPix; i++) { accuout[i] = (unsigned char) (255.0 * accu[i] / maxInt); } } delete[] accu; }
void AddArtifactsToDwiImageFilter< TPixelType > ::GenerateData() { if (m_UseConstantRandSeed) // always generate the same random numbers? m_RandGen->SetSeed(0); else m_RandGen->SetSeed(); m_StartTime = clock(); m_StatusText = "Starting simulation\n"; typename InputImageType::Pointer inputImage = static_cast< InputImageType* >( this->ProcessObject::GetInput(0) ); itk::ImageRegion<3> inputRegion = inputImage->GetLargestPossibleRegion(); typename itk::ImageDuplicator<InputImageType>::Pointer duplicator = itk::ImageDuplicator<InputImageType>::New(); duplicator->SetInputImage( inputImage ); duplicator->Update(); typename InputImageType::Pointer outputImage = duplicator->GetOutput(); // is input slize size even? int xMax=inputRegion.GetSize(0); int yMax=inputRegion.GetSize(1); if ( xMax%2 == 1 ) xMax += 1; if ( yMax%2 == 1 ) yMax += 1; // create slice object typename SliceType::Pointer slice = SliceType::New(); ImageRegion<2> sliceRegion; sliceRegion.SetSize(0, xMax); sliceRegion.SetSize(1, yMax); slice->SetLargestPossibleRegion( sliceRegion ); slice->SetBufferedRegion( sliceRegion ); slice->SetRequestedRegion( sliceRegion ); slice->Allocate(); slice->FillBuffer(0.0); ImageRegion<2> upsampledSliceRegion; if ( m_Parameters.m_SignalGen.m_DoAddGibbsRinging) { upsampledSliceRegion.SetSize(0, xMax*2); upsampledSliceRegion.SetSize(1, yMax*2); } m_Parameters.m_SignalGen.m_SignalScale = 1; m_Parameters.m_SignalGen.m_DoSimulateRelaxation = false; if ( m_Parameters.m_SignalGen.m_Spikes>0 || m_Parameters.m_SignalGen.m_FrequencyMap.IsNotNull() || m_Parameters.m_SignalGen.m_KspaceLineOffset>0.0 || m_Parameters.m_SignalGen.m_DoAddGibbsRinging || m_Parameters.m_SignalGen.m_EddyStrength>0 || m_Parameters.m_SignalGen.m_CroppingFactor<1.0) { ImageRegion<3> croppedRegion = inputRegion; croppedRegion.SetSize(1, croppedRegion.GetSize(1)* m_Parameters.m_SignalGen.m_CroppingFactor); itk::Point<double,3> shiftedOrigin = inputImage->GetOrigin(); shiftedOrigin[1] += (inputRegion.GetSize(1)-croppedRegion.GetSize(1))*inputImage->GetSpacing()[1]/2; outputImage = InputImageType::New(); outputImage->SetSpacing( inputImage->GetSpacing() ); outputImage->SetOrigin( shiftedOrigin ); outputImage->SetDirection( inputImage->GetDirection() ); outputImage->SetLargestPossibleRegion( croppedRegion ); outputImage->SetBufferedRegion( croppedRegion ); outputImage->SetRequestedRegion( croppedRegion ); outputImage->SetVectorLength( inputImage->GetVectorLength() ); outputImage->Allocate(); typename InputImageType::PixelType temp; temp.SetSize(inputImage->GetVectorLength()); temp.Fill(0.0); outputImage->FillBuffer(temp); int tempY=croppedRegion.GetSize(1); tempY += tempY%2; croppedRegion.SetSize(1, tempY); m_StatusText += this->GetTime()+" > Adjusting complex signal\n"; if ( m_Parameters.m_SignalGen.m_FrequencyMap.IsNotNull()) m_StatusText += "Simulating distortions\n"; if ( m_Parameters.m_SignalGen.m_DoAddGibbsRinging) m_StatusText += "Simulating ringing artifacts\n"; if ( m_Parameters.m_SignalGen.m_EddyStrength>0) m_StatusText += "Simulating eddy currents\n"; if ( m_Parameters.m_SignalGen.m_Spikes>0) m_StatusText += "Simulating spikes\n"; if ( m_Parameters.m_SignalGen.m_CroppingFactor<1.0) m_StatusText += "Simulating aliasing artifacts\n"; if ( m_Parameters.m_SignalGen.m_KspaceLineOffset>0) m_StatusText += "Simulating ghosts\n"; std::vector< unsigned int > spikeVolume; for (unsigned int i=0; i< m_Parameters.m_SignalGen.m_Spikes; i++) spikeVolume.push_back(m_RandGen->GetIntegerVariate()%inputImage->GetVectorLength()); std::sort (spikeVolume.begin(), spikeVolume.end()); std::reverse (spikeVolume.begin(), spikeVolume.end()); FiberfoxParameters<double> doubleParam = m_Parameters.CopyParameters<double>(); m_StatusText += "0% 10 20 30 40 50 60 70 80 90 100%\n"; m_StatusText += "|----|----|----|----|----|----|----|----|----|----|\n*"; unsigned long lastTick = 0; boost::progress_display disp(inputImage->GetVectorLength()*inputRegion.GetSize(2)); for (unsigned int g=0; g<inputImage->GetVectorLength(); g++) { std::vector< unsigned int > spikeSlice; while (!spikeVolume.empty() && spikeVolume.back()==g) { spikeSlice.push_back(m_RandGen->GetIntegerVariate()%inputImage->GetLargestPossibleRegion().GetSize(2)); spikeVolume.pop_back(); } std::sort (spikeSlice.begin(), spikeSlice.end()); std::reverse (spikeSlice.begin(), spikeSlice.end()); for (unsigned int z=0; z<inputRegion.GetSize(2); z++) { if (this->GetAbortGenerateData()) { m_StatusText += "\n"+this->GetTime()+" > Simulation aborted\n"; return; } std::vector< SliceType::Pointer > compartmentSlices; // extract slice from channel g for (unsigned int y=0; y<inputRegion.GetSize(1); y++) for (unsigned int x=0; x<inputRegion.GetSize(0); x++) { typename SliceType::IndexType index2D; index2D[0]=x; index2D[1]=y; typename InputImageType::IndexType index3D; index3D[0]=x; index3D[1]=y; index3D[2]=z; SliceType::PixelType pix2D = (SliceType::PixelType)inputImage->GetPixel(index3D)[g]; slice->SetPixel(index2D, pix2D); } if ( m_Parameters.m_SignalGen.m_DoAddGibbsRinging) { itk::ResampleImageFilter<SliceType, SliceType>::Pointer resampler = itk::ResampleImageFilter<SliceType, SliceType>::New(); resampler->SetInput(slice); resampler->SetOutputParametersFromImage(slice); resampler->SetSize(upsampledSliceRegion.GetSize()); resampler->SetOutputSpacing(slice->GetSpacing()/2); itk::NearestNeighborInterpolateImageFunction<SliceType, double>::Pointer nn_interpolator = itk::NearestNeighborInterpolateImageFunction<SliceType, double>::New(); resampler->SetInterpolator(nn_interpolator); resampler->Update(); typename SliceType::Pointer upslice = resampler->GetOutput(); compartmentSlices.push_back(upslice); } else compartmentSlices.push_back(slice); // fourier transform slice typename ComplexSliceType::Pointer fSlice; itk::Size<2> outSize; outSize.SetElement(0, xMax); outSize.SetElement(1, croppedRegion.GetSize()[1]); typename itk::KspaceImageFilter< SliceType::PixelType >::Pointer idft = itk::KspaceImageFilter< SliceType::PixelType >::New(); idft->SetUseConstantRandSeed(m_UseConstantRandSeed); idft->SetParameters(&doubleParam); idft->SetCompartmentImages(compartmentSlices); idft->SetDiffusionGradientDirection( m_Parameters.m_SignalGen.GetGradientDirection(g)); idft->SetZ((double)z-(double)inputRegion.GetSize(2)/2.0); int numSpikes = 0; while (!spikeSlice.empty() && spikeSlice.back()==z) { numSpikes++; spikeSlice.pop_back(); } idft->SetSpikesPerSlice(numSpikes); idft->Update(); fSlice = idft->GetOutput(); // inverse fourier transform slice typename ComplexSliceType::Pointer newSlice; typename itk::DftImageFilter< SliceType::PixelType >::Pointer dft = itk::DftImageFilter< SliceType::PixelType >::New(); dft->SetInput(fSlice); dft->Update(); newSlice = dft->GetOutput(); // put slice back into channel g for (unsigned int y=0; y<outputImage->GetLargestPossibleRegion().GetSize(1); y++) for (unsigned int x=0; x<outputImage->GetLargestPossibleRegion().GetSize(0); x++) { typename InputImageType::IndexType index3D; index3D[0]=x; index3D[1]=y; index3D[2]=z; typename InputImageType::PixelType pix3D = outputImage->GetPixel(index3D); typename ComplexSliceType::IndexType index2D; index2D[0]=x; index2D[1]=y; ComplexSliceType::PixelType cPix = newSlice->GetPixel(index2D); double signal = sqrt(cPix.real()*cPix.real()+cPix.imag()*cPix.imag()); if (signal>0) signal = floor(signal+0.5); else signal = ceil(signal-0.5); pix3D[g] = signal; outputImage->SetPixel(index3D, pix3D); } ++disp; unsigned long newTick = 50*disp.count()/disp.expected_count(); for (unsigned int tick = 0; tick<(newTick-lastTick); tick++) m_StatusText += "*"; lastTick = newTick; } } m_StatusText += "\n\n"; } if ( m_Parameters.m_NoiseModel!=NULL) { m_StatusText += this->GetTime()+" > Adding noise\n"; m_StatusText += "0% 10 20 30 40 50 60 70 80 90 100%\n"; m_StatusText += "|----|----|----|----|----|----|----|----|----|----|\n*"; unsigned long lastTick = 0; ImageRegionIterator<InputImageType> it1 (outputImage, outputImage->GetLargestPossibleRegion()); boost::progress_display disp(outputImage->GetLargestPossibleRegion().GetNumberOfPixels()); while(!it1.IsAtEnd()) { if (this->GetAbortGenerateData()) { m_StatusText += "\n"+this->GetTime()+" > Simulation aborted\n"; return; } ++disp; unsigned long newTick = 50*disp.count()/disp.expected_count(); for (unsigned int tick = 0; tick<(newTick-lastTick); tick++) m_StatusText += "*"; lastTick = newTick; typename InputImageType::PixelType signal = it1.Get(); m_Parameters.m_NoiseModel->AddNoise(signal); it1.Set(signal); ++it1; } m_StatusText += "\n\n"; } this->SetNthOutput(0, outputImage); m_StatusText += "Finished simulation\n"; m_StatusText += "Simulation time: "+GetTime(); }
void TractsToVectorImageFilter< PixelType >::GenerateData() { mitk::BaseGeometry::Pointer geometry = m_FiberBundle->GetGeometry(); // calculate new image parameters itk::Vector<double> spacing; itk::Point<double> origin; itk::Matrix<double, 3, 3> direction; ImageRegion<3> imageRegion; if (!m_MaskImage.IsNull()) { spacing = m_MaskImage->GetSpacing(); imageRegion = m_MaskImage->GetLargestPossibleRegion(); origin = m_MaskImage->GetOrigin(); direction = m_MaskImage->GetDirection(); } else { spacing = geometry->GetSpacing(); origin = geometry->GetOrigin(); mitk::BaseGeometry::BoundsArrayType bounds = geometry->GetBounds(); origin[0] += bounds.GetElement(0); origin[1] += bounds.GetElement(2); origin[2] += bounds.GetElement(4); for (int i=0; i<3; i++) for (int j=0; j<3; j++) direction[j][i] = geometry->GetMatrixColumn(i)[j]; imageRegion.SetSize(0, geometry->GetExtent(0)); imageRegion.SetSize(1, geometry->GetExtent(1)); imageRegion.SetSize(2, geometry->GetExtent(2)); m_MaskImage = ItkUcharImgType::New(); m_MaskImage->SetSpacing( spacing ); m_MaskImage->SetOrigin( origin ); m_MaskImage->SetDirection( direction ); m_MaskImage->SetRegions( imageRegion ); m_MaskImage->Allocate(); m_MaskImage->FillBuffer(1); } OutputImageType::RegionType::SizeType outImageSize = imageRegion.GetSize(); m_OutImageSpacing = m_MaskImage->GetSpacing(); m_ClusteredDirectionsContainer = ContainerType::New(); // initialize num directions image m_NumDirectionsImage = ItkUcharImgType::New(); m_NumDirectionsImage->SetSpacing( spacing ); m_NumDirectionsImage->SetOrigin( origin ); m_NumDirectionsImage->SetDirection( direction ); m_NumDirectionsImage->SetRegions( imageRegion ); m_NumDirectionsImage->Allocate(); m_NumDirectionsImage->FillBuffer(0); // initialize direction images m_DirectionImageContainer = DirectionImageContainerType::New(); // resample fiber bundle double minSpacing = 1; if(m_OutImageSpacing[0]<m_OutImageSpacing[1] && m_OutImageSpacing[0]<m_OutImageSpacing[2]) minSpacing = m_OutImageSpacing[0]; else if (m_OutImageSpacing[1] < m_OutImageSpacing[2]) minSpacing = m_OutImageSpacing[1]; else minSpacing = m_OutImageSpacing[2]; if (m_UseWorkingCopy) m_FiberBundle = m_FiberBundle->GetDeepCopy(); // resample fiber bundle for sufficient voxel coverage m_FiberBundle->ResampleSpline(minSpacing/10); // iterate over all fibers vtkSmartPointer<vtkPolyData> fiberPolyData = m_FiberBundle->GetFiberPolyData(); int numFibers = m_FiberBundle->GetNumFibers(); m_DirectionsContainer = ContainerType::New(); VectorContainer< unsigned int, std::vector< double > >::Pointer peakLengths = VectorContainer< unsigned int, std::vector< double > >::New(); MITK_INFO << "Generating directions from tractogram"; boost::progress_display disp(numFibers); for( int i=0; i<numFibers; i++ ) { ++disp; vtkCell* cell = fiberPolyData->GetCell(i); int numPoints = cell->GetNumberOfPoints(); vtkPoints* points = cell->GetPoints(); if (numPoints<2) continue; vnl_vector_fixed<double, 3> dir; itk::Point<double, 3> worldPos; vnl_vector<double> v; float fiberWeight = m_FiberBundle->GetFiberWeight(i); for( int j=0; j<numPoints-1; j++) { // get current position along fiber in world coordinates double* temp = points->GetPoint(j); worldPos = GetItkPoint(temp); itk::Index<3> index; m_MaskImage->TransformPhysicalPointToIndex(worldPos, index); if (!m_MaskImage->GetLargestPossibleRegion().IsInside(index) || m_MaskImage->GetPixel(index)==0) continue; // get fiber tangent direction at this position v = GetVnlVector(temp); dir = GetVnlVector(points->GetPoint(j+1))-v; if (dir.is_zero()) continue; dir.normalize(); // add direction to container unsigned int idx = index[0] + outImageSize[0]*(index[1] + outImageSize[1]*index[2]); DirectionContainerType::Pointer dirCont; if (m_DirectionsContainer->IndexExists(idx)) { peakLengths->ElementAt(idx).push_back(fiberWeight); dirCont = m_DirectionsContainer->GetElement(idx); if (dirCont.IsNull()) { dirCont = DirectionContainerType::New(); dirCont->push_back(dir); m_DirectionsContainer->InsertElement(idx, dirCont); } else dirCont->push_back(dir); } else { dirCont = DirectionContainerType::New(); dirCont->push_back(dir); m_DirectionsContainer->InsertElement(idx, dirCont); std::vector< double > lengths; lengths.push_back(fiberWeight); peakLengths->InsertElement(idx, lengths); } } } vtkSmartPointer<vtkCellArray> m_VtkCellArray = vtkSmartPointer<vtkCellArray>::New(); vtkSmartPointer<vtkPoints> m_VtkPoints = vtkSmartPointer<vtkPoints>::New(); itk::ImageRegionIterator<ItkUcharImgType> dirIt(m_NumDirectionsImage, m_NumDirectionsImage->GetLargestPossibleRegion()); MITK_INFO << "Clustering directions"; boost::progress_display disp2(outImageSize[0]*outImageSize[1]*outImageSize[2]); while(!dirIt.IsAtEnd()) { ++disp2; OutputImageType::IndexType index = dirIt.GetIndex(); int idx = index[0]+(index[1]+index[2]*outImageSize[1])*outImageSize[0]; if (!m_DirectionsContainer->IndexExists(idx)) { ++dirIt; continue; } DirectionContainerType::Pointer dirCont = m_DirectionsContainer->GetElement(idx); if (dirCont.IsNull() || dirCont->empty()) { ++dirIt; continue; } // std::vector< double > lengths; lengths.resize(dirCont->size(), 1); // all peaks have size 1 DirectionContainerType::Pointer directions; if (m_MaxNumDirections>0) { directions = FastClustering(dirCont, peakLengths->GetElement(idx)); std::sort( directions->begin(), directions->end(), CompareVectorLengths ); } else directions = dirCont; unsigned int numDir = directions->size(); if (m_MaxNumDirections>0 && numDir>m_MaxNumDirections) numDir = m_MaxNumDirections; int count = 0; for (unsigned int i=0; i<numDir; i++) { vtkSmartPointer<vtkPolyLine> container = vtkSmartPointer<vtkPolyLine>::New(); itk::ContinuousIndex<double, 3> center; center[0] = index[0]; center[1] = index[1]; center[2] = index[2]; itk::Point<double> worldCenter; m_MaskImage->TransformContinuousIndexToPhysicalPoint( center, worldCenter ); DirectionType dir = directions->at(i); if (dir.magnitude()<m_SizeThreshold) continue; if (m_NormalizeVectors) dir.normalize(); count++; if (m_CreateDirectionImages && i<10) { if (i==m_DirectionImageContainer->size()) { ItkDirectionImageType::Pointer directionImage = ItkDirectionImageType::New(); directionImage->SetSpacing( spacing ); directionImage->SetOrigin( origin ); directionImage->SetDirection( direction ); directionImage->SetRegions( imageRegion ); directionImage->Allocate(); Vector< float, 3 > nullVec; nullVec.Fill(0.0); directionImage->FillBuffer(nullVec); m_DirectionImageContainer->InsertElement(i, directionImage); } // set direction image pixel ItkDirectionImageType::Pointer directionImage = m_DirectionImageContainer->GetElement(i); Vector< float, 3 > pixel; pixel.SetElement(0, dir[0]); pixel.SetElement(1, dir[1]); pixel.SetElement(2, dir[2]); directionImage->SetPixel(index, pixel); } // add direction to vector field (with spacing compensation) itk::Point<double> worldStart; worldStart[0] = worldCenter[0]-dir[0]/2*minSpacing; worldStart[1] = worldCenter[1]-dir[1]/2*minSpacing; worldStart[2] = worldCenter[2]-dir[2]/2*minSpacing; vtkIdType id = m_VtkPoints->InsertNextPoint(worldStart.GetDataPointer()); container->GetPointIds()->InsertNextId(id); itk::Point<double> worldEnd; worldEnd[0] = worldCenter[0]+dir[0]/2*minSpacing; worldEnd[1] = worldCenter[1]+dir[1]/2*minSpacing; worldEnd[2] = worldCenter[2]+dir[2]/2*minSpacing; id = m_VtkPoints->InsertNextPoint(worldEnd.GetDataPointer()); container->GetPointIds()->InsertNextId(id); m_VtkCellArray->InsertNextCell(container); } dirIt.Set(count); ++dirIt; } vtkSmartPointer<vtkPolyData> directionsPolyData = vtkSmartPointer<vtkPolyData>::New(); directionsPolyData->SetPoints(m_VtkPoints); directionsPolyData->SetLines(m_VtkCellArray); m_OutputFiberBundle = mitk::FiberBundle::New(directionsPolyData); }
void TractsToDWIImageFilter::GenerateData() { DWIImageType::Pointer originalDwiImage = dynamic_cast<DWIImageType*>(this->ProcessObject::GetInput(0)); if (originalDwiImage.IsNull()) { MITK_INFO << "no dwi image to extract b0 specified"; return; } short* originalDwiImageBuffer = (short*) originalDwiImage->GetBufferPointer(); if (this->m_FiberBundle.IsNotNull()) this->GenerateParticleGrid(); else { MITK_INFO << "no fiber bundle specified"; return; } MITK_INFO << "reconstructing dwi-image from particle grid"; float* bounds = m_FiberBundle->GetBounds(); ImageRegion<3> region; region.SetSize(0, bounds[0]); region.SetSize(1, bounds[1]); region.SetSize(2, bounds[2]); m_Size[0] = bounds[0]; m_Size[1] = bounds[1]; m_Size[2] = bounds[2]; mitk::Geometry3D::Pointer geom = this->m_FiberBundle->GetGeometry(); int numDirections = m_GradientDirections->Size(); int notNullDirections = 0; for (int i=0; i<numDirections; i++){ GradientDirectionType dir = m_GradientDirections->GetElement(i); if (dir[0]!=0 || dir[1]!=0 || dir[2]!=0) notNullDirections++; } MITK_INFO << "Gradient directions: " << notNullDirections; MITK_INFO << "B0 images: " << numDirections-notNullDirections; // short *averageImageBuffer = new short[m_Size[0]*m_Size[1]*m_Size[2]]; // for (int x=0; x<m_Size[0]; x++) // for (int y=0; y<m_Size[1]; y++) // for (int z=0; z<m_Size[2]; z++) // { // short val = 0; // for(unsigned int i=0; i<numDirections; i++) // { // int index = i + (x + m_Size[0]*(y+m_Size[1]*z))*numDirections; // GradientDirectionType dir = m_GradientDirections->GetElement(i); // if (dir[0]!=0 || dir[1]!=0 || dir[2]!=0) // val += originalDwiImageBuffer[index]; // } // averageImageBuffer[x + m_Size[0]*(y+m_Size[1]*z)] = (short)val/notNullDirections; // } // allocate output image m_OutImage = static_cast< DWIImageType* >(this->ProcessObject::GetOutput(0)); m_OutImage->SetSpacing( geom->GetSpacing() ); // Set the image spacing m_OutImage->SetOrigin( geom->GetOrigin() ); // Set the image origin itk::Matrix<double, 3, 3> matrix; for (int i=0; i<3; i++) for (int j=0; j<3; j++) matrix[j][i] = geom->GetMatrixColumn(i)[j]; m_OutImage->SetDirection( matrix ); // Set the image direction m_OutImage->SetRegions( region ); // Set image region m_OutImage->SetVectorLength( numDirections ); m_OutImage->Allocate(); short* imageBuffer = (short*) m_OutImage->GetBufferPointer(); m_BesselApproxCoeff = this->ComputeFiberCorrelation(); int dist = 1; if (this->m_ParticleWidth==0) { double voxsize[3]; voxsize[0] = geom->GetSpacing().GetElement(0); voxsize[1] = geom->GetSpacing().GetElement(1); voxsize[2] = geom->GetSpacing().GetElement(2); float vox_half; if(voxsize[0]<voxsize[1] && voxsize[0]<voxsize[2]) vox_half = voxsize[0] / 2.0; else if (voxsize[1] < voxsize[2]) vox_half = voxsize[1] / 2.0; else vox_half = voxsize[2] / 2.0; m_ParticleWidth = 0.5*vox_half; } float width = this->m_ParticleWidth*this->m_ParticleWidth; int bufferSize = m_Size[0]*m_Size[1]*m_Size[2]*numDirections; int counter = 0; short maxVal = 0; for (int x=0; x<m_Size[0]; x++) for (int y=0; y<m_Size[1]; y++) for (int z=0; z<m_Size[2]; z++) { vnl_vector_fixed<float, 3> pos; pos[0] = (float)x; pos[1] = (float)y; pos[2] = (float)z; for(unsigned int i=0; i<numDirections; i++) { int index = i + (x + m_Size[0]*(y+m_Size[1]*z))*numDirections; float val = 0; if (i>=notNullDirections){ imageBuffer[index] = originalDwiImageBuffer[index]; continue; } vnl_vector_fixed<double, 3> temp = m_GradientDirections->GetElement(i); // gradient direction vnl_vector_fixed<float, 3> n; n[0] = temp[0]; n[1] = temp[1]; n[2] = temp[2]; for (int nx=-dist; nx<=dist; nx++) if((x+nx)>=0 && (x+nx)<m_Size[0]) for (int ny=-dist; ny<=dist; ny++) if((y+ny)>=0 && (y+ny)<m_Size[1]) for (int nz=-dist; nz<=dist; nz++) if((z+nz)>=0 && (z+nz)<m_Size[2]){ ParticleContainerType::Pointer container = m_ParticleGrid->GetParticleContainer(x+nx, y+ny, z+nz); if(container.IsNotNull()) for (int j=0; j<container->Size(); j++) { mitk::Particle* particle = container->GetElement(j); // Particle direction vnl_vector_fixed<float, 3> ni = particle->GetDirection(); // Particle position vnl_vector_fixed<float, 3> xi = particle->GetPosition(); vnl_vector_fixed<float, 3> diff = pos-xi; float angle = fabs(dot_product(n, ni)); val += std::exp(-diff.squared_magnitude()/width)*(std::exp(-m_bD*angle*angle)); } } //MITK_INFO << val; averageImageBuffer[x + m_Size[0]*(y+m_Size[1]*z)] imageBuffer[index] = (short)(val*100+1); if (counter%((int)(bufferSize/10))==0) MITK_INFO << 100*counter/bufferSize << "%"; counter++; if (imageBuffer[index] > maxVal) maxVal = imageBuffer[index]; } } //delete(averageImageBuffer); for (int x=0; x<m_Size[0]; x++) for (int y=0; y<m_Size[1]; y++) for (int z=0; z<m_Size[2]; z++) { for(unsigned int i=0; i<numDirections; i++) { int index = i + (x + m_Size[0]*(y+m_Size[1]*z))*numDirections; if (i>=notNullDirections){ imageBuffer[index] += maxVal; continue; } } } }
void TractsToFiberEndingsImageFilter< OutputImageType >::GenerateData() { // generate upsampled image mitk::Geometry3D::Pointer geometry = m_FiberBundle->GetGeometry(); typename OutputImageType::Pointer outImage = this->GetOutput(); // calculate new image parameters mitk::Vector3D newSpacing; mitk::Point3D newOrigin; itk::Matrix<double, 3, 3> newDirection; ImageRegion<3> upsampledRegion; if (m_UseImageGeometry && !m_InputImage.IsNull()) { newSpacing = m_InputImage->GetSpacing()/m_UpsamplingFactor; upsampledRegion = m_InputImage->GetLargestPossibleRegion(); newOrigin = m_InputImage->GetOrigin(); typename OutputImageType::RegionType::SizeType size = upsampledRegion.GetSize(); size[0] *= m_UpsamplingFactor; size[1] *= m_UpsamplingFactor; size[2] *= m_UpsamplingFactor; upsampledRegion.SetSize(size); newDirection = m_InputImage->GetDirection(); } else { newSpacing = geometry->GetSpacing()/m_UpsamplingFactor; newOrigin = geometry->GetOrigin(); mitk::Geometry3D::BoundsArrayType bounds = geometry->GetBounds(); newOrigin[0] += bounds.GetElement(0); newOrigin[1] += bounds.GetElement(2); newOrigin[2] += bounds.GetElement(4); for (int i=0; i<3; i++) for (int j=0; j<3; j++) newDirection[j][i] = geometry->GetMatrixColumn(i)[j]; upsampledRegion.SetSize(0, geometry->GetExtent(0)*m_UpsamplingFactor); upsampledRegion.SetSize(1, geometry->GetExtent(1)*m_UpsamplingFactor); upsampledRegion.SetSize(2, geometry->GetExtent(2)*m_UpsamplingFactor); } typename OutputImageType::RegionType::SizeType upsampledSize = upsampledRegion.GetSize(); // apply new image parameters outImage->SetSpacing( newSpacing ); outImage->SetOrigin( newOrigin ); outImage->SetDirection( newDirection ); outImage->SetRegions( upsampledRegion ); outImage->Allocate(); int w = upsampledSize[0]; int h = upsampledSize[1]; int d = upsampledSize[2]; // set/initialize output OutPixelType* outImageBufferPointer = (OutPixelType*)outImage->GetBufferPointer(); for (int i=0; i<w*h*d; i++) outImageBufferPointer[i] = 0; // resample fiber bundle float minSpacing = 1; if(newSpacing[0]<newSpacing[1] && newSpacing[0]<newSpacing[2]) minSpacing = newSpacing[0]; else if (newSpacing[1] < newSpacing[2]) minSpacing = newSpacing[1]; else minSpacing = newSpacing[2]; vtkSmartPointer<vtkPolyData> fiberPolyData = m_FiberBundle->GetFiberPolyData(); vtkSmartPointer<vtkCellArray> vLines = fiberPolyData->GetLines(); vLines->InitTraversal(); int numFibers = m_FiberBundle->GetNumFibers(); boost::progress_display disp(numFibers); for( int i=0; i<numFibers; i++ ) { ++disp; vtkIdType numPoints(0); vtkIdType* points(NULL); vLines->GetNextCell ( numPoints, points ); // fill output image if (numPoints>0) { itk::Point<float, 3> vertex = GetItkPoint(fiberPolyData->GetPoint(points[0])); itk::Index<3> index; outImage->TransformPhysicalPointToIndex(vertex, index); if (m_BinaryOutput) outImage->SetPixel(index, 1); else outImage->SetPixel(index, outImage->GetPixel(index)+1); } if (numPoints>2) { itk::Point<float, 3> vertex = GetItkPoint(fiberPolyData->GetPoint(points[numPoints-1])); itk::Index<3> index; outImage->TransformPhysicalPointToIndex(vertex, index); if (m_BinaryOutput) outImage->SetPixel(index, 1); else outImage->SetPixel(index, outImage->GetPixel(index)+1); } } if (m_InvertImage) for (int i=0; i<w*h*d; i++) outImageBufferPointer[i] = 1-outImageBufferPointer[i]; }