Generate STL file

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

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:


How can I do that ?

I have the following jpg picture:


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.

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

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

are you looking for vtkThreshold?

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 ?

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

@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 ?

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:

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

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

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

Could vtkThreshold filter just a portion of a one colored surface ? I am asking this because I didn’t succeeded …

Searching through vtk classes, I found vtkOutlineFilter. On every examples I have found about this, I saw 3D images only. Is there any similar filter that work on 2D images ?

	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 ?