gpt4 book ai didi

c++ - 如何将 vtkImageData 转换为 cv::Mat?

转载 作者:太空宇宙 更新时间:2023-11-03 22:42:17 25 4
gpt4 key购买 nike

这个问题已经在这里得到了回答: Copy vtkImageData to cv::Mat

但是答案的链接不再有效。那么如何将 vtkImageData 转换为 cv::Mat

最佳答案

我在这里使用此函数将 vtkImageData 转换为 cv::Mat:

cv::Mat convertVtkImageDataToCVMat(const vtkSmartPointer<vtkImageData> &vtkImage) {
int imageDimensions[3] = {0, 0, 0}; // Width, Hight, Depth --> Depth is not equal to number of image channels!
vtkImage->GetDimensions(imageDimensions);
int imageWidth = imageDimensions[0];
int imageHeight = imageDimensions[1];
int numberOfImageChannels = vtkImage->GetNumberOfScalarComponents();
int cvType = 0;
switch(numberOfImageChannels){
case 1: cvType = CV_8UC1; break;
case 3: cvType = CV_8UC3; break;
case 4: cvType = CV_8UC4; break;
default: std::cerr << "Check number of vtk image channels!" << std::endl;
}
auto resultingCVMat = cv::Mat(imageHeight, imageWidth, cvType);
// Loop over the vtkImageData contents.
for ( int heightPos = 0; heightPos < imageHeight; heightPos++ ){
for ( int widthPos = 0; widthPos < imageWidth; widthPos++ ){
auto pixel = static_cast<unsigned char *>(vtkImage->GetScalarPointer(widthPos, heightPos, 0));
resultingCVMat.at<unsigned char>(heightPos, widthPos) = *pixel;
}
}
return resultingCVMat;
}

关于c++ - 如何将 vtkImageData 转换为 cv::Mat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51842931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com