创建图像
vtkSmartPointer<vtkImageData> img = vtkSmartPointer<vtkImageData>::New();
vtkSmartPointer<vtkInformation> info = vtkSmartPointer<vtkInformation>::New();
img->SetDimensions(225, 225, 336);
img->SetScalarType(VTK_UNSIGNED_CHAR, info); //VTK_UNSIGNED_CHAR表示图像的类型
img->SetNumberOfScalarComponents(1, info);//每个像素需要表示的组份 =1是指标量图 一般是填1
img->AllocateScalars(info);//给图像分配内存
修改图像像素
int dims[3];
//获取图片的位数
img->GetDimensions(dims);
for (int k = 0; k < dims[2]; k++)
{
for (int j = 0; j < dims[1]; j++)
{
for (int i = 0; i < dims[0]; i++)
{
//i < 400 && i > 0 && j > 0 && j < 400表示要改修的范围
if (i < 400 && i > 0 && j > 0 && j < 400)
{
//如果是要修改彩色图片RGB的值 记得要转换成(unsigned char *)类型
unsigned char *pixel = (unsigned char *)(img->GetScalarPointer(i, j, k));
//修改R
*pixel = 255 - *pixel;
//修改G
*(pixel + 1) = 255 - *(pixel + 1);
//修改B
*(pixel + 2) = 255 - *(pixel + 2);
}
}
}
}