《itk实用demo》-Hough检测圆

本文介绍了一种使用经典Hough变换检测二维图像中圆形的方法。通过设置最小半径和最大半径,该方法能够在输入图像中寻找指定大小范围内的圆形,并将其标记出来。文章详细展示了如何利用ITK库实现这一过程。

经典hough变换

#include "itkHoughTransform2DCirclesImageFilter.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageRegionIterator.h"
#include "itkThresholdImageFilter.h"
#include "itkMinimumMaximumImageCalculator.h"
#include <itkGradientMagnitudeImageFilter.h>
#include <itkDiscreteGaussianImageFilter.h>
#include <list>
#include "itkCastImageFilter.h"
#include "vnl/vnl_math.h"

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageToVTKImageFilter.h"
#include "vtkImageViewer.h"
#include "vtkWin32RenderWindowInteractor.h" 
#include "vtkImageCast.h"

int main( int argc, char *argv[] )
{
  // Software Guide : BeginCodeSnippet
  typedef   unsigned char   PixelType;
  typedef   float           AccumulatorPixelType;
  const     unsigned int    Dimension = 2;
  typedef itk::Image< PixelType, Dimension >  ImageType;
  ImageType::IndexType localIndex;
  typedef itk::Image< AccumulatorPixelType, Dimension > AccumulatorImageType;  

  typedef  itk::ImageFileReader< ImageType > ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
 reader->SetFileName("D:\\1\\IM208");
    reader->Update();

  ImageType::Pointer localImage = reader->GetOutput();

  // Software Guide : BeginCodeSnippet
  std::cout << "Computing Hough Map" << std::endl;

  typedef itk::HoughTransform2DCirclesImageFilter<PixelType,
               AccumulatorPixelType> HoughTransformFilterType;
  HoughTransformFilterType::Pointer houghFilter = HoughTransformFilterType::New();
  // Software Guide : BeginCodeSnippet
  houghFilter->SetInput( reader->GetOutput() );

  houghFilter->SetNumberOfCircles(1);
  houghFilter->SetMinimumRadius(5);
  houghFilter->SetMaximumRadius(45);

  houghFilter->Update();
  AccumulatorImageType::Pointer localAccumulator = houghFilter->GetOutput();  
  // Software Guide : BeginCodeSnippet
  HoughTransformFilterType::CirclesListType circles;
  circles = houghFilter->GetCircles(1);
  std::cout << "Found " << circles.size() << " circle(s)." << std::endl;

  // Software Guide : BeginCodeSnippet
  typedef  unsigned char                            OutputPixelType;
  typedef  itk::Image< OutputPixelType, Dimension > OutputImageType;  

  OutputImageType::Pointer  localOutputImage = OutputImageType::New();

  OutputImageType::RegionType region;
  region.SetSize(localImage->GetLargestPossibleRegion().GetSize());
  region.SetIndex(localImage->GetLargestPossibleRegion().GetIndex());
  localOutputImage->SetRegions( region );
  localOutputImage->SetOrigin(localImage->GetOrigin());
  localOutputImage->SetSpacing(localImage->GetSpacing());
  localOutputImage->Allocate();
  localOutputImage->FillBuffer(0);
  // Software Guide : BeginCodeSnippet
  typedef HoughTransformFilterType::CirclesListType CirclesListType;
  CirclesListType::const_iterator itCircles = circles.begin();

  while( itCircles != circles.end() )
    {
    std::cout << "Center: ";
    std::cout << (*itCircles)->GetObjectToParentTransform()->GetOffset()
              << std::endl;
    std::cout << "Radius: " << (*itCircles)->GetRadius()[0] << std::endl;

    // Software Guide : BeginCodeSnippet
    for(double angle = 0;angle <= 2*vnl_math::pi; angle += vnl_math::pi/60.0 )
      {
      localIndex[0] =
         (long int)((*itCircles)->GetObjectToParentTransform()->GetOffset()[0]
                                  + (*itCircles)->GetRadius()[0]*vcl_cos(angle));
      localIndex[1] =
         (long int)((*itCircles)->GetObjectToParentTransform()->GetOffset()[1]
                                  + (*itCircles)->GetRadius()[0]*vcl_sin(angle));
      OutputImageType::RegionType outputRegion =
                                  localOutputImage->GetLargestPossibleRegion();

      if( outputRegion.IsInside( localIndex ) )
        {
        localOutputImage->SetPixel( localIndex, 255 );
        }
      }
    itCircles++;
    }


  typedef itk::ImageToVTKImageFilter<ImageType> ConnectorType;
  ConnectorType::Pointer connector= ConnectorType::New();  connector->SetInput( localOutputImage );
  vtkImageViewer* viewer= vtkImageViewer::New();
  vtkWin32RenderWindowInteractor* renderWindowInteractor=
   vtkWin32RenderWindowInteractor::New();
  viewer->SetupInteractor( renderWindowInteractor);
  viewer->SetInput( connector->GetOutput() );
  viewer->Render();
  viewer->SetColorWindow( 255);
  viewer->SetColorLevel( 128);
   renderWindowInteractor->Start();

  ConnectorType::Pointer connector2= ConnectorType::New();  connector2->SetInput( reader->GetOutput() );
  vtkImageViewer* viewer2= vtkImageViewer::New();
  vtkWin32RenderWindowInteractor* renderWindowInteractor2=
   vtkWin32RenderWindowInteractor::New();
  viewer2->SetupInteractor( renderWindowInteractor2);
  viewer2->SetInput( connector2->GetOutput() );
  viewer2->Render();
  viewer2->SetColorWindow( 255);
  viewer2->SetColorLevel( 128);
 renderWindowInteractor2->Start();

  return 0;
}
### 解决在 Jupyter 中安装 itk-jupyter-widgets 失败的问题 在使用 `itk-jupyter-widgets` 时,安装失败可能由多种原因导致,包括依赖项缺失、环境配置错误、JupyterLab 与 nbextension 不兼容等。以下是一些常见问题的解决方案。 #### 1. 确保正确安装依赖项 `itk-jupyter-widgets` 依赖于 `ipywidgets` 和 `itk`,因此必须确保这些库已正确安装。可以通过以下命令安装: ```bash pip install itk-jupyter-widgets ``` 如果使用 Conda 环境,也可以使用: ```bash conda install -c conda-forge itk-jupyter-widgets ``` 安装完成后,需要启用 Jupyter Notebook 扩展: ```bash jupyter nbextension enable --py itk-jupyter-widgets ``` 对于 JupyterLab 用户,还需安装相应的扩展: ```bash jupyter labextension install itk-jupyter-widgets ``` 安装完成后,建议执行以下命令以确保 JupyterLab 正确加载扩展: ```bash jupyter lab build ``` #### 2. 解决前端依赖冲突 在某些情况下,安装 `itk-jupyter-widgets` 后可能会遇到前端依赖冲突问题,例如多个版本的 `babel-polyfill` 被引入。可以通过以下方式解决此类问题: - **检查依赖树**:使用 `npm ls babel-polyfill` 查看当前环境中是否引入了多个版本的 `babel-polyfill`。 - **移除多余依赖**:如果发现多个版本,可以通过以下命令移除多余的依赖: ```bash npm uninstall babel-polyfill ``` - **使用替代方案**:由于 `babel-polyfill` 已被弃用,推荐使用 `core-js` 或 `regenerator-runtime` 替代。可以通过以下命令安装: ```bash npm install core-js regenerator-runtime ``` 然后在入口文件(如 `index.js`)中添加: ```javascript import 'core-js/stable'; import 'regenerator-runtime/runtime'; ``` #### 3. 清除浏览器缓存 有时浏览器缓存可能导致资源加载失败,建议清除浏览器缓存或使用无痕模式运行 Jupyter Notebook。 #### 4. 检查 JupyterLab 与 nbextension 兼容性 在 JupyterLab 环境中使用 `nbextension` 时,可能需要手动链接或重新构建扩展。可以尝试以下命令: ```bash jupyter lab build ``` #### 5. 验证安装 安装完成后,可以通过以下代码验证 `itk-jupyter-widgets` 是否正常工作: ```python import itk from itk_jupyter_widgets import Viewer # 加载图像数据 image = itk.imread('path_to_your_image.nii.gz') viewer = Viewer(image=image) viewer ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值