-
Notifications
You must be signed in to change notification settings - Fork 2
/
CannyEdgeDetection.cpp
146 lines (126 loc) · 5.91 KB
/
CannyEdgeDetection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "mainwindow.h"
#include "ui_mainwindow.h"
void MainWindow::OnShowCannyEdgeDetectorClicked()
{
vtkSmartPointer<vtkJPEGReader> src = vtkSmartPointer<vtkJPEGReader>::New();
src->SetFileName((const char*)filename.toStdString().c_str());
src->Update();
// Define viewport ranges
// (xmin, ymin, xmax, ymax)
double originalViewport[4] = {0.0, 0.0, 0.5, 1.0};
double edgeViewport[4] = {0.5, 0.0, 1.0, 1.0};
vtkSmartPointer<vtkRenderer> originalRenderer =
vtkSmartPointer<vtkRenderer>::New();
originalRenderer->SetViewport(originalViewport);
vtkSmartPointer<vtkRenderer> edgeRenderer =
vtkSmartPointer<vtkRenderer>::New();
edgeRenderer->SetViewport(edgeViewport);
vtkSmartPointer<vtkImageActor> imageActor =
vtkSmartPointer<vtkImageActor>::New();
#if VTK_MAJOR_VERSION <= 5
imageActor->SetInput(src->GetOutput());
#else
imageActor->SetInputData(src->GetOutput());
#endif
originalRenderer->AddActor(imageActor);
vtkSmartPointer<vtkImageLuminance> il =
vtkSmartPointer<vtkImageLuminance>::New();
il->SetInputConnection(src->GetOutputPort());
vtkSmartPointer<vtkImageCast> ic =
vtkSmartPointer<vtkImageCast>::New();
ic->SetOutputScalarTypeToFloat();
ic->SetInputConnection(il->GetOutputPort());
// Smooth the image
vtkSmartPointer<vtkImageGaussianSmooth> gs =
vtkSmartPointer<vtkImageGaussianSmooth>::New();
gs->SetInputConnection(ic->GetOutputPort());
gs->SetDimensionality(2);
gs->SetRadiusFactors(1, 1, 0);
// Gradient the image
vtkSmartPointer<vtkImageGradient> imgGradient =
vtkSmartPointer<vtkImageGradient>::New();
imgGradient->SetInputConnection(gs->GetOutputPort());
imgGradient->SetDimensionality(2);
vtkSmartPointer<vtkImageMagnitude> imgMagnitude =
vtkSmartPointer<vtkImageMagnitude>::New();
imgMagnitude->SetInputConnection(imgGradient->GetOutputPort());
// Non maximum suppression
vtkSmartPointer<vtkImageNonMaximumSuppression> nonMax =
vtkSmartPointer<vtkImageNonMaximumSuppression>::New();
#if VTK_MAJOR_VERSION <= 5
nonMax->SetMagnitudeInput(imgMagnitude->GetOutput());
nonMax->SetVectorInput(imgGradient->GetOutput());
#else
imgMagnitude->Update();
nonMax->SetMagnitudeInputData(imgMagnitude->GetOutput());
imgGradient->Update();
nonMax->SetVectorInputData(imgGradient->GetOutput());
#endif
nonMax->SetDimensionality(2);
vtkSmartPointer<vtkImageConstantPad> pad =
vtkSmartPointer<vtkImageConstantPad>::New();
pad->SetInputConnection(imgGradient->GetOutputPort());
pad->SetOutputNumberOfScalarComponents(3);
pad->SetConstant(0);
vtkSmartPointer<vtkImageToStructuredPoints> i2sp1 =
vtkSmartPointer<vtkImageToStructuredPoints>::New();
i2sp1->SetInputConnection(nonMax->GetOutputPort());
#if VTK_MAJOR_VERSION <= 5
i2sp1->SetVectorInput(pad->GetOutput());
#else
pad->Update();
i2sp1->SetVectorInputData(pad->GetOutput());
#endif
// Link edgles
vtkSmartPointer<vtkLinkEdgels> imgLink =
vtkSmartPointer<vtkLinkEdgels>::New();
imgLink->SetInputConnection(i2sp1->GetOutputPort());
imgLink->SetGradientThreshold(2);
// Threshold links
vtkSmartPointer<vtkThreshold> thresholdEdgels =
vtkSmartPointer<vtkThreshold>::New();
thresholdEdgels->SetInputConnection(imgLink->GetOutputPort());
thresholdEdgels->ThresholdByUpper(10);
thresholdEdgels->AllScalarsOff();
vtkSmartPointer<vtkGeometryFilter> gf =
vtkSmartPointer<vtkGeometryFilter>::New();
gf->SetInputConnection(thresholdEdgels->GetOutputPort());
vtkSmartPointer<vtkImageToStructuredPoints> i2sp =
vtkSmartPointer<vtkImageToStructuredPoints>::New();
i2sp->SetInputConnection(imgMagnitude->GetOutputPort());
#if VTK_MAJOR_VERSION <= 5
i2sp->SetVectorInput(pad->GetOutput());
#else
pad->Update();
i2sp->SetVectorInputData(pad->GetOutput());
#endif
// Subpixel them
vtkSmartPointer<vtkSubPixelPositionEdgels> spe =
vtkSmartPointer<vtkSubPixelPositionEdgels>::New();
spe->SetInputConnection(gf->GetOutputPort());
#if VTK_MAJOR_VERSION <= 5
spe->SetGradMaps(i2sp->GetStructuredPointsOutput());
#else
i2sp->Update();
spe->SetGradMapsData(i2sp->GetStructuredPointsOutput());
#endif
vtkSmartPointer<vtkStripper> strip =
vtkSmartPointer<vtkStripper>::New();
strip->SetInputConnection(spe->GetOutputPort());
vtkSmartPointer<vtkPolyDataMapper> dsm =
vtkSmartPointer<vtkPolyDataMapper>::New();
dsm->SetInputConnection(strip->GetOutputPort());
dsm->ScalarVisibilityOff();
vtkSmartPointer<vtkActor> planeActor =
vtkSmartPointer<vtkActor>::New();
planeActor->SetMapper(dsm);
planeActor->GetProperty()->SetAmbient(1.0);
planeActor->GetProperty()->SetDiffuse(0.0);
// Add the actors to the renderer, set the background and size
edgeRenderer->AddActor(planeActor);
//ui->qvtkWidget_4->resetInputContext();
ui->qvtkWidget_4->GetRenderWindow()->SetMultiSamples(0);
ui->qvtkWidget_4->GetRenderWindow()->AddRenderer(originalRenderer);
ui->qvtkWidget_4->GetRenderWindow()->AddRenderer(edgeRenderer);
ui->qvtkWidget_4->GetRenderWindow()->Render();
}