void GenerateData_3DImage_CompareToReference() { int upperThr = 255; int lowerThr = 60; int outsideVal = 0; int insideVal = 100; us::ServiceReference<OclResourceService> ref = GetModuleContext()->GetServiceReference<OclResourceService>(); OclResourceService* resources = GetModuleContext()->GetService<OclResourceService>(ref); resources->GetContext(); //todo why do i need to call this before GetMaximumImageSize()? if(resources->GetMaximumImageSize(2, CL_MEM_OBJECT_IMAGE3D) == 0) { //GPU device does not support 3D images. Skip this test. MITK_INFO << "Skipping test."; return; } try{ m_oclBinaryFilter->SetInput( m_Random3DImage ); m_oclBinaryFilter->SetUpperThreshold( upperThr ); m_oclBinaryFilter->SetLowerThreshold( lowerThr ); m_oclBinaryFilter->SetOutsideValue( outsideVal ); m_oclBinaryFilter->SetInsideValue( insideVal ); m_oclBinaryFilter->Update(); mitk::Image::Pointer outputImage = mitk::Image::New(); outputImage = m_oclBinaryFilter->GetOutput(); // reference computation //This is not optimal here, but since we use a random image //we cannot know the reference image at this point. typedef itk::Image< unsigned char, 3> ImageType; typedef itk::BinaryThresholdImageFilter< ImageType, ImageType > ThresholdFilterType; ImageType::Pointer itkInputImage = ImageType::New(); CastToItkImage( m_Random3DImage, itkInputImage ); ThresholdFilterType::Pointer refThrFilter = ThresholdFilterType::New(); refThrFilter->SetInput( itkInputImage ); refThrFilter->SetLowerThreshold( lowerThr ); refThrFilter->SetUpperThreshold( upperThr ); refThrFilter->SetOutsideValue( outsideVal ); refThrFilter->SetInsideValue( insideVal ); refThrFilter->Update(); mitk::Image::Pointer referenceImage = mitk::Image::New(); mitk::CastToMitkImage(refThrFilter->GetOutput(), referenceImage); MITK_ASSERT_EQUAL( referenceImage, outputImage, "OclBinaryThresholdFilter should be equal to regular itkBinaryThresholdImageFilter."); } catch(mitk::Exception &e) { std::string errorMessage = "Caught unexpected exception "; errorMessage.append(e.what()); CPPUNIT_FAIL(errorMessage.c_str()); } }
void mitk::RegionGrow3DTool::ConfirmSegmentation( std::string name, mitk::Color color) { mitk::DataNode::Pointer new_node = mitk::DataNode::New(); new_node->SetColor(color); new_node->SetName(name); new_node->SetProperty("binary", mitk::BoolProperty::New("true")); mitk::Image* image = dynamic_cast<mitk::Image*> (m_FeedbackNode->GetData()); mitk::LevelWindow tempLevelWindow; m_FeedbackNode->GetLevelWindow( tempLevelWindow, NULL, "levelWindow"); int upperThresholdLabeledImage = (short int) tempLevelWindow.GetRangeMax(); int lowerThresholdLabeledImage = (short int) tempLevelWindow.GetLowerWindowBound() + 1; typedef itk::Image<int, 3> InputImageType; typedef itk::Image<unsigned char, 3> SegmentationType; typedef itk::BinaryThresholdImageFilter<InputImageType, SegmentationType> ThresholdFilterType; ThresholdFilterType::Pointer filter = ThresholdFilterType::New(); InputImageType::Pointer itkImage; mitk::CastToItkImage(image, itkImage); filter->SetInput(itkImage); filter->SetInsideValue(1); filter->SetOutsideValue(0); filter->SetUpperThreshold(upperThresholdLabeledImage); filter->SetLowerThreshold(lowerThresholdLabeledImage); filter->Update(); mitk::Image::Pointer new_image = mitk::Image::New(); mitk::CastToMitkImage(filter->GetOutput(), new_image); //pad to original size if (m_OriginalImageNode.GetPointer() != m_NodeToProceed.GetPointer()) { mitk::PadImageFilter::Pointer padFilter = mitk::PadImageFilter::New(); padFilter->SetInput(0, new_image); padFilter->SetInput(1, dynamic_cast<mitk::Image*> (m_OriginalImageNode->GetData())); padFilter->SetBinaryFilter(true); padFilter->SetUpperThreshold(1); padFilter->SetLowerThreshold(1); padFilter->Update(); new_image = padFilter->GetOutput(); } new_node->SetData(new_image); m_ToolManager->GetDataStorage()->Add(new_node); mitk::RenderingManager::GetInstance()->RequestUpdateAll(); m_ToolManager->ActivateTool(-1); }
BinaryImageType::Pointer readBinaryImage(const std::string& filename) { ImageReaderType::Pointer reader = ImageReaderType::New(); reader->SetFileName(filename.c_str()); reader->Update(); // we make sure that the image really has value 0 and 1 typedef itk::BinaryThresholdImageFilter<BinaryImageType, BinaryImageType> ThresholdFilterType; ThresholdFilterType::Pointer thresholdFilter = ThresholdFilterType::New(); thresholdFilter->SetInsideValue(1); thresholdFilter->SetOutsideValue(0); thresholdFilter->SetLowerThreshold(1); thresholdFilter->SetUpperThreshold(255); thresholdFilter->SetInput(reader->GetOutput()); thresholdFilter->Update(); BinaryImageType::Pointer img = thresholdFilter->GetOutput(); img->DisconnectPipeline(); return img; }
void mitk::BinaryThresholdULTool::UpdatePreview() { typedef itk::Image<int, 3> ImageType; typedef itk::Image<unsigned char, 3> SegmentationType; typedef itk::BinaryThresholdImageFilter<ImageType, SegmentationType> ThresholdFilterType; mitk::Image::Pointer thresholdimage = dynamic_cast<mitk::Image*> (m_NodeForThresholding->GetData()); if(thresholdimage) { ImageType::Pointer itkImage = ImageType::New(); CastToItkImage(thresholdimage, itkImage); ThresholdFilterType::Pointer filter = ThresholdFilterType::New(); filter->SetInput(itkImage); filter->SetLowerThreshold(m_CurrentLowerThresholdValue); filter->SetUpperThreshold(m_CurrentUpperThresholdValue); filter->SetInsideValue(1); filter->SetOutsideValue(0); filter->Update(); mitk::Image::Pointer new_image = mitk::Image::New(); CastToMitkImage(filter->GetOutput(), new_image); m_ThresholdFeedbackNode->SetData(new_image); } RenderingManager::GetInstance()->RequestUpdateAll(); }
void QmitkTbssSkeletonizationView::Project() { typedef itk::SkeletonizationFilter<FloatImageType, FloatImageType> SkeletonisationFilterType; typedef itk::ProjectionFilter ProjectionFilterType; typedef itk::DistanceMapFilter<FloatImageType, FloatImageType> DistanceMapFilterType; SkeletonisationFilterType::Pointer skeletonizer = SkeletonisationFilterType::New(); std::vector<mitk::DataNode*> nodes = this->GetDataManagerSelection(); mitk::Image::Pointer meanImage = mitk::Image::New(); mitk::Image::Pointer subjects = mitk::Image::New(); mitk::Image::Pointer tubular = mitk::Image::New(); for ( int i=0; i<nodes.size(); i++ ) { // process only on valid nodes mitk::BaseData* nodeData = nodes[i]->GetData(); if(nodeData) { if(QString("Image").compare(nodeData->GetNameOfClass())==0) { mitk::Image* img = static_cast<mitk::Image*>(nodeData); if(img->GetDimension() == 3) { bool isBinary(false); nodes[i]->GetBoolProperty("binary", isBinary); if(isBinary) { tubular = img; } else { meanImage = img; } } else if(img->GetDimension() == 4) { subjects = img; } } } } Float4DImageType::Pointer allFA = ConvertToItk(subjects); // Calculate skeleton FloatImageType::Pointer itkImg = FloatImageType::New(); mitk::CastToItkImage(meanImage, itkImg); skeletonizer->SetInput(itkImg); skeletonizer->Update(); FloatImageType::Pointer output = skeletonizer->GetOutput(); mitk::Image::Pointer mitkOutput = mitk::Image::New(); mitk::CastToMitkImage(output, mitkOutput); AddToDataStorage(mitkOutput, "mean_FA_skeletonised"); // Retrieve direction image needed later by the projection filter DirectionImageType::Pointer directionImg = skeletonizer->GetVectorImage(); // Calculate distance image DistanceMapFilterType::Pointer distanceMapFilter = DistanceMapFilterType::New(); distanceMapFilter->SetInput(output); distanceMapFilter->Update(); FloatImageType::Pointer distanceMap = distanceMapFilter->GetOutput(); if(m_Controls->m_OutputDistanceMap->isChecked()) { mitk::Image::Pointer mitkDistance = mitk::Image::New(); mitk::CastToMitkImage(distanceMap, mitkDistance); AddToDataStorage(mitkDistance, "distance map"); } // Do projection // Ask a threshold to create a skeleton mask double threshold = -1.0; while(threshold == -1.0) { threshold = QInputDialog::getDouble(m_Controls->m_Skeletonize, tr("Specify the FA threshold"), tr("Threshold:"), QLineEdit::Normal, 0.2); if(threshold < 0.0 || threshold > 1.0) { QMessageBox msgBox; msgBox.setText("Please choose a value between 0 and 1"); msgBox.exec(); threshold = -1.0; } } typedef itk::BinaryThresholdImageFilter<FloatImageType, CharImageType> ThresholdFilterType; ThresholdFilterType::Pointer thresholder = ThresholdFilterType::New(); thresholder->SetInput(output); thresholder->SetLowerThreshold(threshold); thresholder->SetUpperThreshold(std::numeric_limits<float>::max()); thresholder->SetOutsideValue(0); thresholder->SetInsideValue(1); thresholder->Update(); CharImageType::Pointer thresholdedImg = thresholder->GetOutput(); if(m_Controls->m_OutputMask->isChecked()) { mitk::Image::Pointer mitkThresholded = mitk::Image::New(); mitk::CastToMitkImage(thresholdedImg, mitkThresholded); std::string maskName = "skeleton_mask_at_" + boost::lexical_cast<std::string>(threshold); AddToDataStorage(mitkThresholded, maskName); } CharImageType::Pointer itkTubular = CharImageType::New(); mitk::CastToItkImage(tubular, itkTubular); ProjectionFilterType::Pointer projectionFilter = ProjectionFilterType::New(); projectionFilter->SetDistanceMap(distanceMap); projectionFilter->SetDirections(directionImg); projectionFilter->SetAllFA(allFA); projectionFilter->SetTube(itkTubular); projectionFilter->SetSkeleton(thresholdedImg); projectionFilter->Project(); Float4DImageType::Pointer projected = projectionFilter->GetProjections(); mitk::Image::Pointer mitkProjections = mitk::Image::New(); mitk::CastToMitkImage(projected, mitkProjections); AddToDataStorage(mitkProjections, "all_FA_projected"); }
void unmix_threshold(InputImageType::Pointer im[],InputImageType::Pointer om[],int n,int GFP_channel,int AF_channel, int thresh) { printf("Unnmixing %d channels ...\n",n); InputImageType::SizeType size = im[0]->GetLargestPossibleRegion().GetSize(); printf("I'm here\n"); MedianFilterType::Pointer filt[15]; IteratorType iterator[15]; //IteratorType assigniter[15]; InputImageType::SizeType radius; radius[0]=1; radius[1]=1; radius[2]=1; InputImageType::SizeType imagesize = im[0]->GetLargestPossibleRegion().GetSize(); InputImageType::IndexType imageindex; imageindex.Fill(0); InputImageType::RegionType region; region.SetSize(imagesize); region.SetIndex(imageindex); double max_values[15]; for(int counter=0; counter<n; counter++) { printf("\tPerforming median filtering on channel %d ...",counter+1); filt[counter]=MedianFilterType::New(); filt[counter]->SetRadius(radius); filt[counter]->SetInput(im[counter]); filt[counter]->Update(); om[counter]=filt[counter]->GetOutput(); iterator[counter]=IteratorType(om[counter],om[counter]->GetLargestPossibleRegion()); iterator[counter].GoToBegin(); max_values[counter]=-1; printf(" Done %d.\n",counter+1); for(;!iterator[counter].IsAtEnd();++iterator[counter]) { if(max_values[counter]<iterator[counter].Value()) max_values[counter] = iterator[counter].Value(); } // printf("Max%d = %lf\n",counter,max_values[counter]); iterator[counter].GoToBegin(); } printf("Binarizing the Autofluoroscence Channel\n"); ThresholdFilterType::Pointer tfilter = ThresholdFilterType::New(); tfilter->SetInput(om[AF_channel]); tfilter->SetLowerThreshold(thresh); tfilter->SetUpperThreshold(255); tfilter->SetInsideValue(255); tfilter->SetOutsideValue(0); om[AF_channel] = tfilter->GetOutput(); tfilter->Update(); //int total_voxels = size[0]*size[1]*size[2]; int num_processed = 0; //Removing Small components om[AF_channel] = getLargeComponents(om[AF_channel],thresh); printf("\tComputing foreground in AF channel ... "); for(;!iterator[0].IsAtEnd();) { num_processed++; unsigned char temp = iterator[2].Value();///max_values[co]; if(temp > 0) { iterator[GFP_channel].Set(0); //iterator[0].Set(0); } for(int co = 0; co<n; co++) { ++iterator[co]; } } printf(" Done.\n"); }