//腐蚀操作 void segmentation::erosionOperation()//未用到 { const unsigned int Dimension = 3; typedef unsigned char InputPixelType; typedef unsigned char OutputPixelType; typedef itk::Image< InputPixelType, Dimension > InputImageType; typedef itk::Image< OutputPixelType, Dimension > OutputImageType; typedef itk::ImageFileReader< InputImageType > ReaderType; typedef itk::ImageFileWriter< OutputImageType > WriterType; typedef itk::BinaryThresholdImageFilter< InputImageType, InputImageType > ThresholdFilterType; typedef itk::BinaryBallStructuringElement< InputPixelType, Dimension > StructuringElementType; typedef itk::BinaryErodeImageFilter< InputImageType, OutputImageType, StructuringElementType > ErodeFilterType; ReaderType::Pointer reader = ReaderType::New(); WriterType::Pointer writerErosion = WriterType::New(); ThresholdFilterType::Pointer thresholder = ThresholdFilterType::New(); ErodeFilterType::Pointer binaryErode = ErodeFilterType::New(); StructuringElementType structuringElementErosion; reader->SetFileName( initFileName );//读入二值图 reader->Update(); InputImageType::Pointer inputImage = InputImageType::New(); InputImageType::IndexType voxelIndex; inputImage = reader->GetOutput(); InputImageType::SizeType imgSize = inputImage->GetLargestPossibleRegion().GetSize(); long vol = 0; unsigned char temp; float r; 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; temp = inputImage->GetPixel(voxelIndex); if(temp == 255)// 255 for PED vol += 1; } r = pow((3 * vol) / (4 * PI), (1.0 / 3)) ; r = r / 20;//experiment data structuringElementErosion.SetRadius( r ); // 3x3 structuring element structuringElementErosion.CreateStructuringElement(); binaryErode->SetKernel( structuringElementErosion ); //文件前缀名 filePrefix = inputFileName;//char* to string filePrefix = filePrefix.substr(0, filePrefix.length() - 4); string erosionFileName; erosionFileName = filePrefix + "_erosionResult.mhd"; strcpy(outputFileName, erosionFileName.c_str());//string to char* writerErosion->SetFileName(outputFileName); const InputPixelType lowerThreshold = 255; const InputPixelType upperThreshold = 255; thresholder->SetInput( reader->GetOutput() ); InputPixelType background = 0; InputPixelType foreground = 255; thresholder->SetOutsideValue( background ); thresholder->SetInsideValue( foreground ); thresholder->SetLowerThreshold( lowerThreshold ); thresholder->SetUpperThreshold( upperThreshold ); binaryErode->SetInput( thresholder->GetOutput() ); binaryErode->SetErodeValue( foreground ); writerErosion->SetInput( binaryErode->GetOutput() ); writerErosion->Update(); //binaryErode->GetOutput()->GetPixel(index);//获取像素值失败 //腐蚀结果叠加到原图 typedef itk::Image< unsigned short, Dimension > OriginalImageType; typedef itk::ImageFileReader<OriginalImageType>OriginalReaderType; OriginalReaderType::Pointer orignalImgreader = OriginalReaderType::New(); OriginalImageType::Pointer originalImage = OriginalImageType::New(); OriginalReaderType::IndexType originalImgVoxelIndex; reader->SetFileName(outputFileName);//读入腐蚀结果图像 reader->Update(); inputImage = reader->GetOutput(); orignalImgreader->SetFileName(inputFileName);//读入原图像 orignalImgreader->Update(); originalImage = orignalImgreader->GetOutput(); 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; originalImgVoxelIndex[0] = x; originalImgVoxelIndex[1] = y; originalImgVoxelIndex[2] = z; temp = inputImage->GetPixel(voxelIndex); if(temp == 255) originalImage->SetPixel(originalImgVoxelIndex, 65535); } //输出结果 typedef itk::ImageFileWriter<OriginalImageType>NewWriterType; NewWriterType::Pointer writer = NewWriterType::New(); //文件前缀名 filePrefix = inputFileName;//char* to string filePrefix = filePrefix.substr(0, filePrefix.length() - 4); filePrefix = filePrefix + "_refinedResult.mhd"; strcpy(outputFileName, filePrefix.c_str());//string to char* writer->SetFileName(outputFileName); writer->SetInput(originalImage); writer->Update(); emit returnInternalFileName(initResultFileName);//更新原视图 emit returnOutputFileName(outputFileName);//显示结果图 }
void mitk::SetRegionTool::OnMousePressed(StateMachineAction *, InteractionEvent *interactionEvent) { auto *positionEvent = dynamic_cast<mitk::InteractionPositionEvent *>(interactionEvent); if (!positionEvent) return; m_LastEventSender = positionEvent->GetSender(); m_LastEventSlice = m_LastEventSender->GetSlice(); // 1. Get the working image Image::Pointer workingSlice = FeedbackContourTool::GetAffectedWorkingSlice(positionEvent); if (workingSlice.IsNull()) return; // can't do anything without the segmentation // if click was outside the image, don't continue const BaseGeometry *sliceGeometry = workingSlice->GetGeometry(); itk::Index<3> projectedPointIn2D; sliceGeometry->WorldToIndex(positionEvent->GetPositionInWorld(), projectedPointIn2D); if (!sliceGeometry->IsIndexInside(projectedPointIn2D)) { MITK_ERROR << "point apparently not inside segmentation slice" << std::endl; return; // can't use that as a seed point } typedef itk::Image<DefaultSegmentationDataType, 2> InputImageType; typedef InputImageType::IndexType IndexType; typedef itk::ConnectedThresholdImageFilter<InputImageType, InputImageType> RegionGrowingFilterType; RegionGrowingFilterType::Pointer regionGrower = RegionGrowingFilterType::New(); // convert world coordinates to image indices IndexType seedIndex; sliceGeometry->WorldToIndex(positionEvent->GetPositionInWorld(), seedIndex); // perform region growing in desired segmented region InputImageType::Pointer itkImage = InputImageType::New(); CastToItkImage(workingSlice, itkImage); regionGrower->SetInput(itkImage); regionGrower->AddSeed(seedIndex); InputImageType::PixelType bound = itkImage->GetPixel(seedIndex); regionGrower->SetLower(bound); regionGrower->SetUpper(bound); regionGrower->SetReplaceValue(1); itk::BinaryFillholeImageFilter<InputImageType>::Pointer fillHolesFilter = itk::BinaryFillholeImageFilter<InputImageType>::New(); fillHolesFilter->SetInput(regionGrower->GetOutput()); fillHolesFilter->SetForegroundValue(1); // Store result and preview mitk::Image::Pointer resultImage = mitk::GrabItkImageMemory(fillHolesFilter->GetOutput()); resultImage->SetGeometry(workingSlice->GetGeometry()); // Get the current working color DataNode *workingNode(m_ToolManager->GetWorkingData(0)); if (!workingNode) return; mitk::ImageToContourModelFilter::Pointer contourextractor = mitk::ImageToContourModelFilter::New(); contourextractor->SetInput(resultImage); contourextractor->Update(); mitk::ContourModel::Pointer awesomeContour = contourextractor->GetOutput(); FeedbackContourTool::SetFeedbackContour(awesomeContour); FeedbackContourTool::SetFeedbackContourVisible(true); mitk::RenderingManager::GetInstance()->RequestUpdate(positionEvent->GetSender()->GetRenderWindow()); }
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);//发出信号 }