《itk实用demo》-RGB转Luminance

此程序将彩色图像转换为亮度图像,并将其保存为BMP格式。

彩色图像转亮度图像,并写成文件。

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkConnectedComponentImageFilter.h"
#include "itkLabelToRGBImageFilter.h"

#include "itksys/SystemTools.hxx"
#include <sstream>

#include "itkImageToVTKImageFilter.h"
#include "vtkImageViewer.h"
#include "vtkWin32RenderWindowInteractor.h"

#include "itkImageFileWriter.h"
#include <iostream>
#include <string>

#include "itkRGBToLuminanceImageFilter.h"
using namespace std;
int main( int argc, char *argv[])
{
 const unsigned int Dimension = 2;
 typedef unsigned char                       PixelType;
 typedef itk::RGBPixel<unsigned char>         RGBPixelType;
 typedef itk::Image<PixelType, Dimension>     ImageType;
 typedef itk::Image<RGBPixelType, Dimension>  RGBImageType;
 std::string bmp = ".bmp";
 RGBImageType::Pointer image;
 typedef itk::ImageFileReader<RGBImageType> ReaderType;

 for (int num= 0;num<214;num++)
 {
  std::string  inputFilename = "D:\\se6\\rgb\\IM";
  std::string  outputFilename = "D:\\se6\\bmp2\\IM";

  char t[256];
  string s;
  sprintf(t, "%d", num);
  s = t;
  inputFilename = inputFilename + s + bmp;
  outputFilename = outputFilename + s + bmp;

  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(inputFilename);
  reader->Update();

  image = reader->GetOutput();

  typedef itk::Image<unsigned char, 2>  ScalarImageType;
  typedef itk::RGBToLuminanceImageFilter<RGBImageType, ScalarImageType> LuminanceFilterType;
  LuminanceFilterType::Pointer luminanceFilter = LuminanceFilterType::New();
  luminanceFilter->SetInput(image);
  luminanceFilter->Update();

  typedef  itk::ImageFileWriter< ScalarImageType  > WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(outputFilename);
  writer->SetInput(luminanceFilter->GetOutput());
  writer->Update();
 }


 return EXIT_SUCCESS;
}
### 解决在 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 ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值