# Generate STL file

**URL:** https://discourse.vtk.org/t/generate-stl-file/4443
**Category:** Support
**Created:** [October 19, 2020, 10:31am UTC](https://discourse.vtk.org/t/generate-stl-file/4443 "2020-10-19T10:31:59Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [October 19, 2020, 10:31am UTC](https://discourse.vtk.org/t/generate-stl-file/4443/1 "2020-10-19T10:31:59Z")

</div>

I am trying to convert simple jpg files, with some simple items (black and white) into a STL files. I have tried:

```
	vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();
	reader->SetFileName(sFile);
	reader->SetDataByteOrderToBigEndian();
	reader->Update();

```

and

```
	vtkSmartPointer<vtkOutlineFilter> outline = vtkSmartPointer<vtkOutlineFilter>::New();
	outline->SetInputConnection(reader->GetOutputPort());
	outline->SetGenerateFaces(1);
	outline->Update();

```

and

```
	vtkSmartPointer<vtkSTLWriter> writer = vtkSmartPointer<vtkSTLWriter>::New();
	writer->SetInputConnection(outline->GetOutputPort());
	writer->SetFileName(sSTL);
	writer->Write();

```

but the outcome file is not valid. I guess the step with _vtkOutlineFilter_ is not quite correct, so at that step I have tried also:

```
	vtkSmartPointer<vtkPolyDataNormals> normals = vtkSmartPointer<vtkPolyDataNormals>::New();
	normals->SetInputConnection(contour->GetOutputPort());
	normals->SetFeatureAngle(60.0);
	normals->Update();

```

and then tried to save STL file … the same result. What I am doing wrong here ?

P.S. BTW, I have tried the outcome file with [www.viewstl.com](http://www.viewstl.com)

Later edit:

I have succeeded in a way:

```
	vtkSmartPointer<vtkImageDataGeometryFilter> geometryfilter = vtkSmartPointer<vtkImageDataGeometryFilter>::New();
	geometryfilter->SetInputConnection(reader->GetOutputPort());
	geometryfilter->SetOutputTriangles(1);
	geometryfilter->SetThresholdCells(1);
	geometryfilter->Update();

	vtkSmartPointer<vtkSTLWriter> writer = vtkSmartPointer<vtkSTLWriter>::New();
	writer->SetInputConnection(geometryfilter->GetOutputPort());
	writer->SetFileName(sSTL);
	writer->SetFileTypeToBinary();
	writer->Write();

```

I only need to select only the outline of this picture:

 ![image](https://discourse.vtk.org/uploads/default/original/2X/3/3f087c2012b00fbe76abae58650ed0cdefcc3e86.png)  
How can I do that ?

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [October 19, 2020, 3:06pm UTC](https://discourse.vtk.org/t/generate-stl-file/4443/2 "2020-10-19T15:06:08Z")

</div>

I have the following jpg picture:

 ![House](https://discourse.vtk.org/uploads/default/original/2X/6/6864099d588b6ca32cef7295c206905925dff034.jpeg)  
How can select only the outline of this picture (the margins only) using vtkImageDataGeometryFilter ? Is possible that ?

Because taking this filter, I could easily export as STL file.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [October 20, 2020, 5:31pm UTC](https://discourse.vtk.org/t/generate-stl-file/4443/3 "2020-10-20T17:31:55Z")

</div>

What I am trying to do is impossible in VTK (to filter only margins of that house image) ?

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [October 21, 2020, 2:26pm UTC](https://discourse.vtk.org/t/generate-stl-file/4443/4 "2020-10-21T14:26:15Z")

</div>

Could be solvable this task if the input image would be not jpeg ?

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michael/32/16_2.png) [@Michael](https://discourse.vtk.org/u/Michael)
#### Post date: [October 21, 2020, 5:49pm UTC](https://discourse.vtk.org/t/generate-stl-file/4443/5 "2020-10-21T17:49:05Z")

</div>

are you looking for `vtkThreshold`?

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [October 22, 2020, 8:35am UTC](https://discourse.vtk.org/t/generate-stl-file/4443/6 "2020-10-22T08:35:06Z")

</div>

Kindly thank you Michael. You help me a lot, and I guess I am not far to solve my issue. Here is what I wrote:

```
	vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();
	reader->SetFileName(sFile);
	reader->SetDataByteOrderToBigEndian();
	reader->Update();

	vtkSmartPointer<vtkThreshold> threshold = vtkSmartPointer<vtkThreshold>::New();
	threshold->SetInputConnection(reader->GetOutputPort());
	threshold->ThresholdByLower(0);
	threshold->ThresholdByUpper(110);
	threshold->Update();

	vtkSmartPointer<vtkGeometryFilter> geometryfilter = vtkSmartPointer<vtkGeometryFilter>::New();
	geometryfilter->SetInputConnection(threshold->GetOutputPort());
	geometryfilter->Update();

	vtkSmartPointer<vtkSTLWriter> writer = vtkSmartPointer<vtkSTLWriter>::New();
	writer->SetInputConnection(geometryfilter->GetOutputPort());
	writer->SetFileName(sSTL);
	writer->SetFileTypeToBinary();
	writer->Write();

```

But when I try the STL file, I see nothing … of course, I have tried a lot of Threshold values, all of them with the same empty result. Obviously I do something wrong, but what ? Could you give me a help hand ?

---

<div class="post-metadata">

### Author: ![Michael](https://discourse.vtk.org/user_avatar/discourse.vtk.org/michael/32/16_2.png) [@Michael](https://discourse.vtk.org/u/Michael)
#### Post date: [October 22, 2020, 8:40am UTC](https://discourse.vtk.org/t/generate-stl-file/4443/7 "2020-10-22T08:40:31Z")

</div>

Try to triangulate the polydata before writing the STL (vtkTriangleFilter).

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [October 22, 2020, 5:06pm UTC](https://discourse.vtk.org/t/generate-stl-file/4443/8 "2020-10-22T17:06:24Z")

</div>

@Michael thank you a lot for your guidance. I have tried this:

```
	vtkSmartPointer<vtkThreshold> threshold = vtkSmartPointer<vtkThreshold>::New();
	threshold->SetInputConnection(reader->GetOutputPort());
	threshold->ThresholdByLower(0);
	threshold->ThresholdByUpper(110);
	threshold->Update();

	vtkSmartPointer<vtkGeometryFilter> geometryfilter = vtkSmartPointer<vtkGeometryFilter>::New();
	geometryfilter->SetInputConnection(threshold->GetOutputPort());
	geometryfilter->Update();

```

and then

```
	vtkSmartPointer<vtkTriangleFilter> triangle = vtkSmartPointer<vtkTriangleFilter>::New();
	triangle->SetInputConnection(geometryfilter->GetOutputPort());
	triangle->Update();

	vtkSmartPointer<vtkSTLWriter> writer = vtkSmartPointer<vtkSTLWriter>::New();
	writer->SetInputConnection(triangle->GetOutputPort());
	writer->SetFileName(sSTL);
	writer->SetFileTypeToBinary();
	writer->Write();

```

but I encounter an error:

```
Generic Warning: In D:\Project\VTK-8.2.0\Common\Core\vtkMath.cxx, line 561
Unable to factor linear system

```

Obviously I done something wrong, but what ?

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [October 23, 2020, 9:24am UTC](https://discourse.vtk.org/t/generate-stl-file/4443/9 "2020-10-23T09:24:53Z")

</div>

I am even close to finish line.  
I wrote this:

```
	vtkSmartPointer<vtkTriangleFilter> triangle = vtkSmartPointer<vtkTriangleFilter>::New();
	triangle->SetInputConnection(geometryfilter->GetOutputPort());
	triangle->Update();
	vtkSmartPointer<vtkPolyDataNormals> normals = vtkSmartPointer<vtkPolyDataNormals>::New();
	normals->SetInputConnection(triangle->GetOutputPort());
	normals->Update();

	vtkSmartPointer<vtkSTLWriter> writer = vtkSmartPointer<vtkSTLWriter>::New();
	writer->SetInputConnection(normals->GetOutputPort());
	writer->SetFileName(sSTL);
	writer->SetFileTypeToBinary();
	writer->Write();

```

and from this I got:

 ![image](https://discourse.vtk.org/uploads/default/original/2X/c/cdab72e89e6e6581d67a9887b8683de0074c4351.png)

 ![image](https://discourse.vtk.org/uploads/default/original/2X/1/18bbc1f2a810f6ca46615543bfa92b477bb6c5c9.png)

Now, I need just one thing: to filter the outline (contour) of the image, that is all. How can I achieve this ?

In my trials, I have tried to use **vtkMarchingSquares** , I don’t know if is a good idea:

```
	vtkSmartPointer<vtkMarchingSquares> contour = vtkSmartPointer<vtkMarchingSquares>::New();
	contour->SetInputConnection(reader->GetOutputPort());
	contour->GenerateValues(1, 0, 100);
	contour->Update();

```

at _contour-\>Update_ I got a crash:

Program:

```
HEAP CORRUPTION DETECTED: after Normal block (#32114) at 0x000002AAF694F070.
CRT detected that the application wrote to memory after end of heap buffer.

```

I cannot coupling **vtkJPEGReader** to a **vtkMarchingSquares** directly ? But how then ?

What I intend to do is what I found here: [http://vtk.1045678.n5.nabble.com/2D-Surface-Plot-how-to-get-quot-outlines-quot-td1236435.html#a1236436](http://vtk.1045678.n5.nabble.com/2D-Surface-Plot-how-to-get-quot-outlines-quot-td1236435.html#a1236436)

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [October 27, 2020, 10:07am UTC](https://discourse.vtk.org/t/generate-stl-file/4443/10 "2020-10-27T10:07:06Z")

</div>

Could **vtkMarchingSquares** to be applied on 2D images ? Or I go in wrong direction with this filter …

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [October 28, 2020, 11:31am UTC](https://discourse.vtk.org/t/generate-stl-file/4443/11 "2020-10-28T11:31:37Z")

</div>

Any hint, even a small one will be very helpful to me.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [November 3, 2020, 12:33pm UTC](https://discourse.vtk.org/t/generate-stl-file/4443/12 "2020-11-03T12:33:31Z")

</div>

Could [vtkThreshold](https://vtk.org/doc/nightly/html/classvtkThreshold.html) filter just a portion of a one colored surface ? I am asking this because I didn’t succeeded …

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [November 3, 2020, 7:52pm UTC](https://discourse.vtk.org/t/generate-stl-file/4443/13 "2020-11-03T19:52:18Z")

</div>

Searching through vtk classes, I found [vtkOutlineFilter](https://vtk.org/Wiki/VTK/Examples/Cxx/PolyData/Outline). On every examples I have found about this, I saw 3D images only. Is there any similar filter that work on 2D images ?

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [November 3, 2020, 8:07pm UTC](https://discourse.vtk.org/t/generate-stl-file/4443/14 "2020-11-03T20:07:50Z")

</div>

```
	vtkSmartPointer<vtkJPEGReader> reader = vtkSmartPointer<vtkJPEGReader>::New();
	reader->SetFileName(sFile);
	reader->Update();

	vtkSmartPointer<vtkOutlineFilter> outline = vtkSmartPointer<vtkOutlineFilter>::New();
	outline->SetInputConnection(reader->GetOutputPort());
	outline->GenerateFacesOn();
	outline->Update();

	vtkSmartPointer<vtkTriangleFilter> triangle = vtkSmartPointer<vtkTriangleFilter>::New();
	triangle->SetInputConnection(outline->GetOutputPort());
	triangle->Update();

	vtkSmartPointer<vtkSTLWriter> writer = vtkSmartPointer<vtkSTLWriter>::New();
	writer->SetInputConnection(triangle->GetOutputPort());
	writer->SetFileName(sSTL);
	writer->SetFileTypeToBinary();
	writer->Write();

```

the STL file is invalid. What filter should I use ?
