comparison UnitTestsSources/ImageToolboxTests.cpp @ 1286:ddb6676bbcbf bugs/2020-02-invisible-slice

Added Histogram functions (new files: ImageToolbox...)
author Benjamin Golinvaux <bgo@osimis.io>
date Fri, 14 Feb 2020 14:59:32 +0100
parents
children 86400fa16091
comparison
equal deleted inserted replaced
1284:1bb54005e2bd 1286:ddb6676bbcbf
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21 #include <Framework/Toolbox/ImageToolbox.h>
22
23 // #include <boost/chrono.hpp>
24 // #include <boost/lexical_cast.hpp>
25
26 #include <Core/Images/Image.h>
27 #include <Core/Images/PixelTraits.h>
28
29 #include "stdint.h"
30
31 #include "gtest/gtest.h"
32
33 #include <cmath>
34
35
36 TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize1)
37 {
38 using OrthancStone::HistogramData;
39 using OrthancStone::DumpHistogramResult;
40 using OrthancStone::ComputeHistogram;
41
42 const unsigned int W = 16;
43 const unsigned int H = 16;
44
45 // 256/17 = 15,...
46 // 256 % 17 = 1
47 // 0 will be 16 times
48 // 1 will be 15 times
49 // 2 will be 15 times
50 // ...
51 // 16 will be 15 times
52
53 size_t pixCounter = 0;
54
55 std::auto_ptr<Orthanc::Image> image(new Orthanc::Image(
56 Orthanc::PixelFormat_Grayscale8, W, H, false));
57
58 for (unsigned int y = 0; y < H; ++y)
59 {
60 uint8_t* buffer = reinterpret_cast<uint8_t*>(image->GetRow(y));
61 for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter)
62 {
63 *buffer = static_cast<uint8_t>(pixCounter % 17);
64 }
65 }
66
67 HistogramData hd;
68 ComputeHistogram(*image, hd, 1);
69 ASSERT_EQ(-0.5, hd.minValue);
70 ASSERT_EQ(17u, hd.bins.size());
71 ASSERT_EQ(16u, hd.bins[0]);
72 for (size_t i = 1; i < hd.bins.size(); ++i)
73 ASSERT_EQ(15u, hd.bins[i]);
74 }
75
76 TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize1_FormatString)
77 {
78 using OrthancStone::HistogramData;
79 using OrthancStone::DumpHistogramResult;
80 using OrthancStone::ComputeHistogram;
81
82 const unsigned int W = 16;
83 const unsigned int H = 16;
84
85 // 256/17 = 15,...
86 // 256 % 17 = 1
87 // 0 will be 16 times
88 // 1 will be 15 times
89 // 2 will be 15 times
90 // ...
91 // 16 will be 15 times
92
93 size_t pixCounter = 0;
94
95 std::auto_ptr<Orthanc::Image> image(new Orthanc::Image(
96 Orthanc::PixelFormat_Grayscale8, W, H, false));
97
98 for (unsigned int y = 0; y < H; ++y)
99 {
100 uint8_t* buffer = reinterpret_cast<uint8_t*>(image->GetRow(y));
101 for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter)
102 {
103 *buffer = static_cast<uint8_t>(pixCounter % 17);
104 }
105 }
106
107 HistogramData hd;
108 ComputeHistogram(*image, hd, 1);
109
110 // void DumpHistogramResult(std::string& s, const HistogramData& hd)
111 std::string s;
112 DumpHistogramResult(s, hd);
113 std::cout << s;
114 }
115
116 template<Orthanc::PixelFormat Format>
117 void SimpleHisto_T_BinSize1_2()
118 {
119 using OrthancStone::HistogramData;
120 using OrthancStone::DumpHistogramResult;
121 using OrthancStone::ComputeHistogram;
122
123 const unsigned int W = 16;
124 const unsigned int H = 16;
125
126 // 256/17 = 15,...
127 // 256 % 17 = 1
128 // 0 will be 16 times
129 // 1 will be 15 times
130 // 2 will be 15 times
131 // ...
132 // 16 will be 15 times
133
134 size_t pixCounter = 0;
135
136 std::auto_ptr<Orthanc::Image> image(new Orthanc::Image(
137 Format, W, H, false));
138
139 typedef Orthanc::PixelTraits<Format>::PixelType PixelType;
140
141 PixelType pixValue = 0;
142
143 for (unsigned int y = 0; y < H; ++y)
144 {
145 PixelType* buffer = reinterpret_cast<PixelType*>(image->GetRow(y));
146 for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter)
147 {
148 // 0..99 0..99 0..55
149 *buffer = pixValue;
150 pixValue++;
151 if (pixValue >= 100)
152 pixValue = 0;
153 }
154 }
155
156 HistogramData hd;
157 ComputeHistogram(*image, hd, 1);
158 ASSERT_EQ(-0.5, hd.minValue);
159 ASSERT_EQ(100u, hd.bins.size());
160 for (size_t i = 0; i <= 55; ++i)
161 ASSERT_EQ(3u, hd.bins[i]);
162 for (size_t i = 56; i <= 99; ++i)
163 ASSERT_EQ(2u, hd.bins[i]);
164 }
165
166 TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize1_2)
167 {
168 SimpleHisto_T_BinSize1_2<Orthanc::PixelFormat_Grayscale8>();
169 }
170
171 TEST(ImageToolbox, SimpleHisto_Grayscale16_BinSize1_2)
172 {
173 SimpleHisto_T_BinSize1_2<Orthanc::PixelFormat_Grayscale16>();
174 }
175
176 TEST(ImageToolbox, SimpleHisto_SignedGrayscale16_BinSize1_2)
177 {
178 SimpleHisto_T_BinSize1_2<Orthanc::PixelFormat_SignedGrayscale16>();
179 }
180
181 TEST(ImageToolbox, SimpleHisto_Grayscale32_BinSize1_2)
182 {
183 SimpleHisto_T_BinSize1_2<Orthanc::PixelFormat_Grayscale32>();
184 }
185
186 template<Orthanc::PixelFormat Format>
187 void SimpleHisto_T_BinSize10_2()
188 {
189 using OrthancStone::HistogramData;
190 using OrthancStone::DumpHistogramResult;
191 using OrthancStone::ComputeHistogram;
192
193 const unsigned int W = 16;
194 const unsigned int H = 16;
195
196 // 256/17 = 15,...
197 // 256 % 17 = 1
198 // 0 will be 16 times
199 // 1 will be 15 times
200 // 2 will be 15 times
201 // ...
202 // 16 will be 15 times
203
204 size_t pixCounter = 0;
205
206 std::auto_ptr<Orthanc::Image> image(new Orthanc::Image(
207 Format, W, H, false));
208
209 typedef Orthanc::PixelTraits<Format>::PixelType PixelType;
210
211 PixelType pixValue = 0;
212
213 for (unsigned int y = 0; y < H; ++y)
214 {
215 PixelType* buffer = reinterpret_cast<PixelType*>(image->GetRow(y));
216 for (unsigned int x = 0; x < W; ++x, ++buffer, ++pixCounter)
217 {
218 // 0..99 0..99 0..55
219 *buffer = pixValue;
220 pixValue++;
221 if (pixValue >= 100)
222 pixValue = 0;
223 }
224 }
225
226 HistogramData hd;
227 ComputeHistogram(*image, hd, 10);
228 ASSERT_EQ(-0.5, hd.minValue);
229 ASSERT_EQ(10u, hd.bins.size());
230
231 for (size_t i = 0; i <= 4; ++i)
232 ASSERT_EQ(30u, hd.bins[i]);
233
234 ASSERT_EQ(26u, hd.bins[5]);
235
236 for (size_t i = 6; i <= 9; ++i)
237 ASSERT_EQ(20u, hd.bins[i]);
238 }
239
240 TEST(ImageToolbox, SimpleHisto_Grayscale8_BinSize10_2)
241 {
242 SimpleHisto_T_BinSize10_2<Orthanc::PixelFormat_Grayscale8>();
243 }
244
245 TEST(ImageToolbox, SimpleHisto_Grayscale16_BinSize10_2)
246 {
247 SimpleHisto_T_BinSize10_2<Orthanc::PixelFormat_Grayscale16>();
248 }
249
250 TEST(ImageToolbox, SimpleHisto_SignedGrayscale16_BinSize10_2)
251 {
252 SimpleHisto_T_BinSize10_2<Orthanc::PixelFormat_SignedGrayscale16>();
253 }
254
255 TEST(ImageToolbox, SimpleHisto_Grayscale32_BinSize10_2)
256 {
257 SimpleHisto_T_BinSize10_2<Orthanc::PixelFormat_Grayscale32>();
258 }
259