void itk::ConnectomicsNetworkToConnectivityMatrixImageFilter::GenerateData()
{

  this->AllocateOutputs();

  OutputImageType::Pointer output = this->GetOutput();

  if(m_InputNetwork.IsNull())
  {
    MITK_ERROR << "Failed to generate data, no valid network was set.";
    return;
  }

  typedef mitk::ConnectomicsNetwork::NetworkType NetworkType;
  typedef boost::graph_traits< NetworkType >::vertex_descriptor DescriptorType;
  typedef boost::graph_traits< NetworkType >::vertex_iterator IteratorType;

  // prepare connectivity matrix for data
  std::vector< std::vector< int > > connectivityMatrix;
  int numberOfVertices = m_InputNetwork->GetNumberOfVertices();

  connectivityMatrix.resize( numberOfVertices );
  for( int index(0); index < numberOfVertices; index++ )
  {
    connectivityMatrix[ index ].resize( numberOfVertices );
  }

  // Create LabelToIndex translation
  std::map< std::string, DescriptorType > labelToIndexMap;

  NetworkType boostGraph = *(m_InputNetwork->GetBoostGraph());

  // translate index to label
  IteratorType iterator, end;
  boost::tie(iterator, end) = boost::vertices( boostGraph );

  for ( ; iterator != end; ++iterator)
  {
    labelToIndexMap.insert(
      std::pair< std::string, DescriptorType >(
      boostGraph[ *iterator ].label, *iterator )
      );
  }

  std::vector< std::string > indexToLabel;

  // translate index to label
  indexToLabel.resize( numberOfVertices );

  boost::tie(iterator, end) = boost::vertices( boostGraph );

  for ( ; iterator != end; ++iterator)
  {
    indexToLabel[ *iterator ] = boostGraph[ *iterator ].label;
  }

  //translate label to position
  std::vector< std::string > positionToLabelVector;
  positionToLabelVector = indexToLabel;

  std::sort ( positionToLabelVector.begin(), positionToLabelVector.end() );

  for( int outerLoop( 0 ); outerLoop < numberOfVertices; outerLoop++  )
  {
    DescriptorType fromVertexDescriptor = labelToIndexMap.find( positionToLabelVector[ outerLoop ] )->second;
    for( int innerLoop( outerLoop + 1 ); innerLoop < numberOfVertices; innerLoop++  )
    {
      DescriptorType toVertexDescriptor = labelToIndexMap.find( positionToLabelVector[ innerLoop ] )->second;


      int weight( 0 );

      if( boost::edge(toVertexDescriptor, fromVertexDescriptor, boostGraph ).second )
      {
        weight = m_InputNetwork->GetEdge( fromVertexDescriptor , toVertexDescriptor ).weight;;
      }
      connectivityMatrix[outerLoop][innerLoop] = weight;
      connectivityMatrix[innerLoop][outerLoop] = weight;
    }
  }


  OutputImageType::SpacingType spacing;
  spacing[0] = 1.0;
  spacing[1] = 1.0;
  OutputImageType::PointType origin;
  origin[0] = 0.0;
  origin[1] = 0.0;
  OutputImageType::IndexType index;
  index[0] = 0.0;
  index[1] = 0.0;
  OutputImageType::SizeType size;
  size[0] = numberOfVertices;
  size[1] = numberOfVertices;
  OutputImageType::RegionType region;
  region.SetIndex( index );
  region.SetSize( size );

  output->SetSpacing( spacing );   // Set the image spacing
  output->SetOrigin( origin );     // Set the image origin
  output->SetRegions( region );
  output->Allocate();
  output->FillBuffer(0);


  itk::ImageRegionIterator< OutputImageType > it_connect(output, output->GetLargestPossibleRegion());

  int counter( 0 );

  double rescaleFactor( 1.0 );
  double rescaleMax( 255.0 );

  if( m_RescaleConnectivity )
  {
    rescaleFactor = rescaleMax / m_InputNetwork->GetMaximumWeight();
  }

  // Colour pixels according to connectivity
  if(  m_BinaryConnectivity )
  {
    // binary connectivity
    while( !it_connect.IsAtEnd() )
    {
      if( connectivityMatrix[ ( counter - counter % numberOfVertices ) / numberOfVertices][ counter % numberOfVertices ] )
      {
        it_connect.Set( 1 );
      }
      else
      {
        it_connect.Set( 0 );
      }
      ++it_connect;
      counter++;
    }
  }
  else
  {
    // if desired rescale to the 0-255 range
    while( !it_connect.IsAtEnd() )
    {
      it_connect.Set( ( unsigned short ) rescaleFactor *
        connectivityMatrix[ ( counter - counter % numberOfVertices ) / numberOfVertices][ counter % numberOfVertices ]
      );
      ++it_connect;
      counter++;
    }
  }

}
Exemplo n.º 2
0
void segmentation::on_retinalLayerSegButton_clicked()
{
    fstream fin1, fin7, fin11, fin12; 
    char* s1Txt = ("../data/ped/txt/15715_1.txt");//s1.txt
    char* s7Txt = ("../data/ped/txt/15715_7.txt"); 
    char* s11Txt = ("../data/ped/txt/15715_11.txt"); 
    char* s12Txt = ("../data/ped/txt/15715_12.txt");
    //这样开辟内存容易出现堆栈溢出问题, 选择new & delete
    //int s1[64][512], s7[64][512], s11[64][512], s12[64][512]; 
    int (*s1)[512] = new int [64][512];
    int (*s7)[512] = new int [64][512];
    int (*s11)[512] = new int [64][512];
    int (*s12)[512] = new int [64][512];
    fin1.open(s1Txt, ios::in);
    fin7.open(s7Txt, ios::in);
    fin11.open(s11Txt, ios::in);
    fin12.open(s12Txt, ios::in);
    for(int z=0; z<64; z++)
    {
        for(int x=0; x<512; x++)
        {
            fin1>>s1[z][x];
            fin7>>s7[z][x];
            fin11>>s11[z][x];
            fin12>>s12[z][x];
        }
    }
    fin1.close();
    fin7.close();
    fin11.close();
    fin12.close();

    typedef itk::Image<unsigned short,3>InputImageType; 
    typedef itk::Image<unsigned short,3>OutputImageType; 
    InputImageType::Pointer inputImage = InputImageType::New();
    InputImageType::Pointer outputImage = InputImageType::New();
    typedef itk::ImageFileReader<InputImageType>ReaderType;
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName(inputFileName); 
    reader->Update();
    inputImage = reader->GetOutput();
    InputImageType::IndexType voxelIndex;
    InputImageType::SizeType imgSize = inputImage->GetLargestPossibleRegion().GetSize();
    OutputImageType::IndexType index;
    index[0]=0;
    index[1]=0;
    index[2]=0;
    OutputImageType::SizeType size;
    size[0]=imgSize[0];
    size[1]=imgSize[1];
    size[2]=imgSize[2];  
    //create a region for enhance result
    OutputImageType::RegionType region;
    region.SetIndex(index);
    region.SetSize(size);
    outputImage->SetRegions(region);
    outputImage->Allocate();
    //make four surfaces into a MHD file
    for(int z = 0; z < imgSize[2]; z++)
        for(int y = 0; y < imgSize[1]; y++)
            for(int x = 0; x < imgSize[0]; x++)
            {
                voxelIndex[0] = x;
                voxelIndex[1] = y;
                voxelIndex[2] = z;
                if(y == s1[z][x])
                {
                    inputImage->SetPixel(voxelIndex, 65535);
                }
                if(y == s7[z][x])
                {
                    inputImage->SetPixel(voxelIndex, 65535);
                }
                if(y == s11[z][x])
                {
                    inputImage->SetPixel(voxelIndex, 65535);
                }
                if(y == s12[z][x])
                {
                    inputImage->SetPixel(voxelIndex, 65535);
                }
            }
            delete []s1;//释放内存
            delete []s7;
            delete []s11;
            delete []s12;

            //文件前缀名
            filePrefix = inputFileName;//char* to string
            filePrefix = filePrefix.substr(0, filePrefix.length() - 4);
            filePrefix = filePrefix + "_layerSeg.mhd";
            strcpy(outputFileName, filePrefix.c_str());//string to char*
            typedef itk::ImageFileWriter<OutputImageType>WriterType;
            WriterType::Pointer writer = WriterType::New();
            writer->SetFileName(outputFileName);
            writer->SetInput(inputImage);
            writer->Update();
            emit returnOutputFileName(outputFileName);//发出信号
}
Exemplo n.º 3
0
int runGrAnisDiff(unsigned char* imgIn, int r, int c, int z, int iter, int timeStep, int comductance)
{
	
	//pixel types
	typedef unsigned char InputPixelType;
	typedef float OutputPixelType;
	typedef itk::Image< InputPixelType, 3 > InputImageType;
	typedef itk::Image< OutputPixelType, 3 > OutputImageType;
	
	//create an ITK image from the input image
	OutputImageType::Pointer im;
	im = OutputImageType::New();
	OutputImageType::PointType origin;
    origin[0] = 0.; 
    origin[1] = 0.;    
	origin[2] = 0.;    
    im->SetOrigin( origin );

    OutputImageType::IndexType start;
    start[0] =   0;  // first index on X
    start[1] =   0;  // first index on Y    
	start[2] =   0;  // first index on Z    
    OutputImageType::SizeType  size;
    size[0]  = c;  // size along X
    size[1]  = r;  // size along Y
	size[2]  = z;  // size along Z
  
    OutputImageType::RegionType region;
    region.SetSize( size );
    region.SetIndex( start );
    
 //   double spacing[3];
	//spacing[0] = 1; //spacing along x
	//spacing[1] = 1; //spacing along y
	//spacing[2] = sampl_ratio; //spacing along z

    im->SetRegions( region );
	//im->SetSpacing(spacing);
    im->Allocate();
    im->FillBuffer(0);
	im->Update();	
	
	//copy the input image into the ITK image
	typedef itk::ImageRegionIteratorWithIndex< OutputImageType > IteratorType;
	IteratorType iterator1(im,im->GetRequestedRegion());
	
	for(int i=0; i<r*c*z; i++)
	{					
		iterator1.Set((float)imgIn[i]);
		++iterator1;		
	}

	//apply gradient anisotropic diffusion
	typedef itk::GradientAnisotropicDiffusionImageFilter< OutputImageType, OutputImageType > FilterType;
	FilterType::Pointer filter = FilterType::New();
	filter->SetInput( im );

	filter->SetNumberOfIterations( iter );
	filter->SetTimeStep( timeStep );
	filter->SetConductanceParameter( comductance );

	try
	{
		filter->Update();
	}
	catch( itk::ExceptionObject & err ) 
    { 
		std::cerr << err << std::endl;
		return 0;
	}

	typedef itk::RescaleIntensityImageFilter< OutputImageType, InputImageType > RescaleFilterType;

	RescaleFilterType::Pointer rescaler = RescaleFilterType::New();
	rescaler->SetOutputMinimum(   0 );
	rescaler->SetOutputMaximum( 255 );
	rescaler->SetInput( filter->GetOutput() );
	rescaler->Update();

	//overwrite the input image for now in order to save space
	typedef itk::ImageRegionIteratorWithIndex< InputImageType > IteratorType2;
	IteratorType2 iterator2(rescaler->GetOutput(),rescaler->GetOutput()->GetRequestedRegion());
	
	for(int i=0; i<r*c*z; i++)
	{					
		imgIn[i] = iterator2.Get();
		++iterator2;		
	}

	return 1;
}
Exemplo n.º 4
0
void segmentation::on_abnormalRegionSegButton_clicked()
{
    fstream fin11, fin12; 
    char* s11Txt = ("../data/ped/txt/15715_11.txt");//s11.txt
    char* s12Txt = ("../data/ped/txt/15715_12.txt");
    int (*s11)[512] = new int[64][512];
    int (*s12)[512] = new int[64][512];
    int (*p)[512] = new int[64][512];
    //read TXT
    fin11.open(s11Txt, ios::in); 
    fin12.open(s12Txt, ios::in);
    //get the p matrix
    for(int z = 0; z < 64; z++)
    {
        for(int x = 0; x < 512; x++)
        {
            fin11>>s11[z][x];
            fin12>>s12[z][x];
            if(s12[z][x] - s11[z][x] > 5)  //5 threshold  
                p[z][x] = 1;
            else 
                p[z][x] = 0;
        }
    }
    fin11.close();
    fin12.close();
    //read filtered MHD data
    typedef itk::Image<unsigned short, 3>InputImageType; 
    typedef itk::Image<unsigned short, 3>OutputImageType;
    typedef itk::Image<unsigned char, 3>InitOutputImageType;
    InputImageType::Pointer inputImage = InputImageType::New();
    OutputImageType::Pointer outputImage = OutputImageType::New();
    InitOutputImageType::Pointer initOutputImage = InitOutputImageType::New();
    typedef itk::ImageFileReader<InputImageType>ReaderType;
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName(inputFileName);//读入原图像
    reader->Update();
    inputImage = reader->GetOutput();
    InputImageType::IndexType voxelIndex;
    InitOutputImageType::IndexType initvoxelIndex;
    OutputImageType::IndexType newvoxelIndex;
    InputImageType::SizeType imgSize = inputImage->GetLargestPossibleRegion().GetSize();
    OutputImageType::IndexType index;
    index[0] = 0;
    index[1] = 0;
    index[2] = 0;
    OutputImageType::SizeType size;
    size[0] = imgSize[0];
    size[1] = imgSize[1];
    size[2] = imgSize[2];
    //create a region for initial result
    InitOutputImageType::RegionType initRegion;
    initRegion.SetIndex(index);
    initRegion.SetSize(size);
    initOutputImage->SetRegions( initRegion);
    initOutputImage->Allocate();
    //create a region for enhance result
    OutputImageType::RegionType region;
    region.SetIndex(index);
    region.SetSize(size);
    outputImage->SetRegions(region);
    outputImage->Allocate();
    //Initial result for PED segmentation  (a binary image)
    for(int z = 0; z < imgSize[2]; z++)
        for(int x = 0; x < imgSize[0]; x++)
        {
            initvoxelIndex[0] = x;
            initvoxelIndex[2] = z;
            for(int y = 0; y < imgSize[1]; y++)
            {
                //set all background a black region
                initvoxelIndex[1] = y;
                initOutputImage->SetPixel(initvoxelIndex, 0);
            }
            //set the same intensity for all PED region (empirical value)
            if(p[z][x] == 1)
            {
                for(int y = s11[z][x]; y <= s12[z][x]; y++) 
                {
                    initvoxelIndex[1] = y;
                    initOutputImage->SetPixel(initvoxelIndex, 255);//亮区域
                }
            }
        }
        //输出中间分割结果
        //文件前缀名
        filePrefix = inputFileName;//char* to string
        filePrefix = filePrefix.substr(0, filePrefix.length() - 4);
        string strInitFileName;
        strInitFileName = filePrefix + "_initBinaryImg.mhd";
        strcpy(initFileName, strInitFileName.c_str());//string to char*
        typedef itk::ImageFileWriter<InitOutputImageType>InitWriterType;
        InitWriterType::Pointer initWriter = InitWriterType::New();
        initWriter->SetFileName(initFileName);//生成二值图
        initWriter->SetInput(initOutputImage);
        initWriter->Update();
        //Enhance PED region and overlay it on the original image
        for(int z = 0; z < imgSize[2]; z++)
            for(int x = 0; x < imgSize[0]; x++)
            {
                voxelIndex[0] = x;
                voxelIndex[2] = z;
                newvoxelIndex[0] = x;
                newvoxelIndex[2] = z;
                for(int y = 0; y < imgSize[1]; y++)
                {
                    voxelIndex[1] = y;
                    newvoxelIndex[1] = y;
                    outputImage->SetPixel(newvoxelIndex, inputImage->GetPixel(voxelIndex));
                }
                //set the same intensity for all PED region (empirical value)
                if(p[z][x] == 1)
                {
                    for(int y = s11[z][x]; y <= s12[z][x]; y++) 
                    {
                        newvoxelIndex[1] = y;
                        outputImage->SetPixel(newvoxelIndex, 65535);//亮区域
                    }
                }
            }
            //释放内存不能缺省
            delete[]s11;
            delete[]s12;
            delete[]p;

            typedef itk::ImageFileWriter<OutputImageType>WriterType;
            WriterType::Pointer writer = WriterType::New();
            filePrefix = filePrefix + "_initResult.mhd";
            strcpy(outputFileName, filePrefix.c_str());//string to char*
            initResultFileName = outputFileName;//输出初始分割结果,二值图重叠在原图像上面
            writer->SetFileName(outputFileName);
            writer->SetInput(outputImage);
            writer->Update();

            emit returnOutputFileName(outputFileName);//发出信号
}