栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

ITK入门教程(2)向索引添加偏移量

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ITK入门教程(2)向索引添加偏移量

1.函数说明

template struct Offset表示 n 维图像的两个 n 维索引之间的 n 维偏移。

Offset 是一个模板类,用于表示多维偏移,即 (i,j,k,…)。Offset是在空间维度上模板化的。

ITK假设size (bounds)的第一个元素是移动最快的索引。

为了效率,Offset 没有定义默认构造函数、复制构造函数或 operator=。我们依靠编译器来提供高效的按位复制。

Offset 是一个“聚合”类。它的数据是公开的 (m_InternalArray),允许快速方便的实例化/分配。

允许/建议使用以下语法来指定聚合类型:
Offset<3> var{{ 256, 256, 20 }}; // 避免使用Offset<3> var = {{ 256, 256, 20 }}
需要双大括号 {{ 和 }} 以防止 gcc -Wall(可能还有其他编译器)抱怨括号内的初始化程序。
作为旨在提供最高性能特征的聚合类型,此类不适合继承,因此将此结构设置为 final。

2.代码展示

(1)Python代码

#!/usr/bin/env python

import sys
import itk

from distutils.version import StrictVersion as VS

if VS(itk.Version.GetITKVersion()) < VS("4.9.0"):
    print("大于ITK 4.9.0 is required.")
    sys.exit(1)

Dimension = 2

index = itk.Index[Dimension]()
index.Fill(5)

offset = itk.Offset[Dimension]()
offset.Fill(1)

newIndex = index + offset

print("index: " + str([int(index[0]), int(index[1])]))
print("offset: " + str([int(offset[0]), int(offset[1])]))
print("index + offset: " + str([int(newIndex[0]), int(newIndex[1])]))
print("")

offset[0] = -1
newIndex = index + offset

print("index: " + str([int(index[0]), int(index[1])]))
print("offset: " + str([int(offset[0]), int(offset[1])]))
print("index + offset: " + str([int(newIndex[0]), int(newIndex[1])]))
print("")

# index: [5, 5]
# offset: [1, 1]
# index + offset: [6, 6]
# 
# index: [5, 5]
# offset: [-1, 1]
# index + offset: [4, 6]

(2)C++代码

#include "itkIndex.h"
#include "itkOffset.h"

#include 

int main(int, char *[])
{
  constexpr unsigned int Dimension = 2;

  itk::Index index;
  index.Fill(5);

  itk::Offset offset;
  offset.Fill(1);

  std::cout << "index: " << index << std::endl;
  std::cout << "offset: " << offset << std::endl;
  std::cout << "index + offset: " << index + offset << std::endl;
  std::cout << std::endl;

  offset[0] = -1;

  std::cout << "index: " << index << std::endl;
  std::cout << "offset: " << offset << std::endl;
  std::cout << "index + offset: " << index + offset << std::endl;
  std::cout << std::endl;

  return EXIT_SUCCESS;
}

参考目录
https://itk.org/ITKExamples/src/Core/Common/AddOffsetToIndex/documentation.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/384969.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号