1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19 #include <iostream>
20 #include "itkCannyEdgeDetectionImageFilter.h"
21 #include "itkImageFileReader.h"
22 #include "itkImageFileWriter.h"
23 #include "itkRescaleIntensityImageFilter.h"
24 #include "itkTestingMacros.h"
25
26
itkCannyEdgeDetectionImageFilterTest2(int argc,char * argv[])27 int itkCannyEdgeDetectionImageFilterTest2( int argc, char * argv[] )
28 {
29 if(argc < 4)
30 {
31 std::cerr << "Usage: " << argv[0] << " InputImage OutputImage1 OutputImage2" << std::endl;
32 return EXIT_FAILURE;
33 }
34
35 constexpr unsigned int Dimension = 2;
36 using InputPixelType = float;
37 using InputImage = itk::Image< InputPixelType, Dimension >;
38 using OutputPixelType = unsigned char;
39 using OutputImage = itk::Image< OutputPixelType, Dimension >;
40 using CannyEdgeDetectionImageFilterType =
41 itk::CannyEdgeDetectionImageFilter<InputImage, InputImage>;
42
43 itk::ImageFileReader<InputImage>::Pointer reader =
44 itk::ImageFileReader<InputImage>::New();
45 reader->SetFileName( argv[1] );
46
47 // Set up the filter
48 CannyEdgeDetectionImageFilterType::Pointer filter = CannyEdgeDetectionImageFilterType::New();
49
50 EXERCISE_BASIC_OBJECT_METHODS( filter, CannyEdgeDetectionImageFilter, ImageToImageFilter );
51
52 filter->SetInput( reader->GetOutput() );
53
54 CannyEdgeDetectionImageFilterType::OutputImagePixelType upperThreshold = 25;
55 filter->SetUpperThreshold( upperThreshold );
56 TEST_SET_GET_VALUE( upperThreshold, filter->GetUpperThreshold() );
57
58 CannyEdgeDetectionImageFilterType::OutputImagePixelType lowerThreshold = 10;
59 filter->SetLowerThreshold( lowerThreshold );
60 TEST_SET_GET_VALUE( lowerThreshold, filter->GetLowerThreshold() );
61
62 CannyEdgeDetectionImageFilterType::ArrayType variance = 1.0f;
63 filter->SetVariance(variance);
64 TEST_SET_GET_VALUE( variance, filter->GetVariance() );
65
66 CannyEdgeDetectionImageFilterType::ArrayType maximumError = .01f;
67 filter->SetMaximumError(maximumError );
68 TEST_SET_GET_VALUE( maximumError, filter->GetMaximumError() );
69
70
71 itk::RescaleIntensityImageFilter<InputImage, OutputImage>::Pointer rescale =
72 itk::RescaleIntensityImageFilter<InputImage, OutputImage>::New();
73
74 rescale->SetInput( filter->GetOutput() );
75
76 rescale->SetOutputMinimum( 0 );
77 rescale->SetOutputMaximum( 255 );
78
79 itk::ImageFileWriter<OutputImage>::Pointer writer = itk::ImageFileWriter<OutputImage>::New();
80 writer->SetInput( rescale->GetOutput() );
81 writer->SetFileName( argv[2] );
82
83 TRY_EXPECT_NO_EXCEPTION( writer->Update() );
84
85 // Set the Canny filter to other values
86 upperThreshold = 20;
87 filter->SetUpperThreshold( upperThreshold );
88 TEST_SET_GET_VALUE( upperThreshold, filter->GetUpperThreshold() );
89
90 lowerThreshold = 5;
91 filter->SetLowerThreshold( lowerThreshold );
92 TEST_SET_GET_VALUE( lowerThreshold, filter->GetLowerThreshold() );
93
94
95 TRY_EXPECT_NO_EXCEPTION( rescale->Update() );
96
97
98 // Set it back expecting the same values
99 upperThreshold = 25;
100 filter->SetUpperThreshold( upperThreshold );
101 TEST_SET_GET_VALUE( upperThreshold, filter->GetUpperThreshold() );
102
103 lowerThreshold = 10;
104 filter->SetLowerThreshold( lowerThreshold );
105 TEST_SET_GET_VALUE( lowerThreshold, filter->GetLowerThreshold() );
106
107 writer->SetFileName( argv[3] );
108 TRY_EXPECT_NO_EXCEPTION( writer->Update() );
109
110 return EXIT_SUCCESS;
111 }
112