我开始了一个c ++程序,它可以打开一个RAW 3D图像并保存它。
CProjet::CProjet(int L,int C, int P, char* pimage) {
NBL=L;
NBC=C;
NBP=P;
size=NBL*NBC*NBP;
if(pimage != NULL) {
m_pimage=new char[strlen(pimage)+1];
strcpy(m_pimage,pimage);
}
else if(size != 0) {
m_pimage=new char[size+1];
}
else {
m_pimage=NULL;
}
}
CProjet::~CProjet() {
if(m_pimage != NULL) {
delete m_pimage;
m_pimage = NULL;
}
}
void CProjet::LireImageRAW() {
char nom_image[256];
FILE * fic_image;
cout<<"Nom de l'image a ouvrir : ";
cin>>nom_image;
fic_image = fopen(nom_image,"rb");
if (fic_image == NULL)
{
cout<<"Erreur a l'ouverture de l'image ..."<<endl;
exit(0);
}
else
cout<<"Image ouverte avec succes ..."<<endl;
fread(m_pimage,1,size,fic_image);
fclose(fic_image);
}
void CProjet::EcrireImageRAW() {
char nom_image[256];
FILE * fic_image;
cout<<"Nom de l'image a ecrire : ";
cin>>nom_image;
fic_image = fopen(nom_image,"wb");
if (fic_image == NULL)
{
cout<<"Erreur a la creation de l'image ..."<<endl;
exit(0);
}
else
cout<<"Image créée avec succes ..."<<endl;
fwrite(m_pimage,1,size,fic_image);
fclose(fic_image);
}
但是,我想将此3D图像保存为STL格式。
你知道吗?
提前致谢。
答案 0 :(得分:1)
感谢您的回答。我可以得到一个结果!
然而,这并不是很好。当我将3D图像保存为STL格式或只是查看它时,我们可以看到它已被复制4次。
http://imageshack.com/a/img673/7050/Tw7kNs.png
我尝试了一些改变但没有成功。
//Read 3D RAW image
vtkImageReader *imgReader = vtkImageReader::New();
imgReader->SetFileDimensionality(3);
imgReader->SetFileName(IMG_FILE);
imgReader->SetNumberOfScalarComponents(1);
//imgReader->SetDataSpacing(1.0,1.0,1.0);
//imgReader->SetDataOrigin(-0.5,-0.5,-0.5);
imgReader->SetDataByteOrderToBigEndian();
imgReader->SetDataExtent(0,199,0,199,0,199);
imgReader->Update();
//Visualization
vtkContourFilter *contour = vtkContourFilter::New();
contour->SetInput(imgReader->GetOutput());
contour->SetValue(0,0);
vtkPolyDataMapper *volMapper = vtkPolyDataMapper::New();
volMapper->SetInput(contour->GetOutput());
volMapper->ScalarVisibilityOff();
vtkActor *volActor = vtkActor::New();
volActor->SetMapper(volMapper);
vtkRenderer *renderer = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(renderer);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
renderer->AddActor(volActor);
renderer->SetBackground(1,1,1);
renWin->SetSize(200*3,200*3);
// interact with data
renWin->Render();
iren->Start();
//Write in STL
vtkTriangleFilter *triangle = vtkTriangleFilter::New();
triangle->SetInput(contour->GetOutput());
triangle->PassVertsOff();
triangle->PassLinesOff();
vtkQuadricDecimation *decimation = vtkQuadricDecimation::New();
decimation->SetInput(triangle->GetOutput());
vtkCleanPolyData *clean = vtkCleanPolyData::New();
clean->SetInput(triangle->GetOutput());
vtkTriangleFilter *triangle2 = vtkTriangleFilter::New();
triangle2->SetInput(clean->GetOutput());
triangle2->PassVertsOff();
triangle2->PassLinesOff();
vtkSTLWriter *writer = vtkSTLWriter::New();
writer->SetInput(triangle2->GetOutput());
writer->SetFileName("test.stl");
writer->Write();
有没有人有想法?
答案 1 :(得分:0)
当我需要操作3D文件时,我使用PCL库或VTK库。 对于你的问题,我认为VTK是一个更好的选择,因为它包含一些Raw 3D和STL的驱动程序。
查看this example,它将向您展示如何打开.Raw文件 This example将向您展示如何编写STL文件。
如果您不想使用第三方库,我将无法帮助您......