Skip to content

Commit 4cb1a45

Browse files
committed
Added normalize first option to ImageCaster
1 parent 5a4354e commit 4cb1a45

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

source/FAST/Algorithms/ImageCaster/ImageCaster.cl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ void writeImageAsFloat2D(__write_only image2d_t image, int2 position, float4 val
2525
__kernel void cast2D(
2626
__read_only image2d_t input,
2727
__write_only image2d_t output,
28-
__private float scaleFactor
28+
__private float scaleFactor,
29+
__private char normalize,
30+
__private float minimum,
31+
__private float maximum
2932
) {
3033
const int2 pos = {get_global_id(0), get_global_id(1)};
31-
writeImageAsFloat2D(output, pos, readImageAsFloat2D(input, sampler, pos)*scaleFactor);
34+
if(normalize == 1) {
35+
float4 value = readImageAsFloat2D(input, sampler, pos);
36+
value = (value - minimum) / (maximum - minimum);
37+
writeImageAsFloat2D(output, pos, value*scaleFactor);
38+
} else {
39+
writeImageAsFloat2D(output, pos, readImageAsFloat2D(input, sampler, pos)*scaleFactor);
40+
}
3241
}

source/FAST/Algorithms/ImageCaster/ImageCaster.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,24 @@ ImageCaster::ImageCaster() {
99
createOpenCLProgram(Config::getKernelSourcePath() + "/Algorithms/ImageCaster/ImageCaster.cl");
1010
}
1111

12-
ImageCaster::ImageCaster(DataType outputType, float scaleFactor) : ImageCaster() {
12+
ImageCaster::ImageCaster(DataType outputType, float scaleFactor, bool normalizeFirst) : ImageCaster() {
1313
m_outputType = outputType;
1414
m_scaleFactor = scaleFactor;
15+
m_normalizeFirst = normalizeFirst;
1516
}
1617

1718
void ImageCaster::execute() {
1819
auto input = getInputData<Image>();
1920
if(input->getDimensions() == 3)
2021
throw Exception("Image caster only supports 2D for now");
22+
23+
float minimum = 0.0f;
24+
float maximum = 0.0f;
25+
if(m_normalizeFirst) {
26+
minimum = input->calculateMinimumIntensity();
27+
maximum = input->calculateMaximumIntensity();
28+
}
29+
2130
auto output = Image::create(input->getSize(), m_outputType, input->getNrOfChannels());
2231
output->setSpacing(input->getSpacing());
2332
SceneGraph::setParentNode(output, input);
@@ -31,6 +40,9 @@ void ImageCaster::execute() {
3140
kernel.setArg(0, *inputAccess->get2DImage());
3241
kernel.setArg(1, *outputAccess->get2DImage());
3342
kernel.setArg(2, m_scaleFactor);
43+
kernel.setArg(3, (char)(m_normalizeFirst ? 1 : 0));
44+
kernel.setArg(4, minimum);
45+
kernel.setArg(5, maximum);
3446

3547
queue.enqueueNDRangeKernel(
3648
kernel,

source/FAST/Algorithms/ImageCaster/ImageCaster.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ class FAST_EXPORT ImageCaster : public ProcessObject {
1616
* @brief Create instance
1717
* @param outputType The data type to cast the input image to
1818
* @param scaleFactor Value to multiply each pixel with when casting to other type
19+
* @param normalizeFirst Whether to apply [0, 1] intensity normalization (e.g. (value - minimum)/(maximum - minimum))
20+
* on input before casting.
1921
* @return instance
2022
*/
2123
FAST_CONSTRUCTOR(ImageCaster,
2224
DataType, outputType,,
23-
float, scaleFactor, = 1.0f
25+
float, scaleFactor, = 1.0f,
26+
bool, normalizeFirst, = false
2427
)
2528
private:
2629
ImageCaster();
2730
void execute() override;
2831
float m_scaleFactor;
2932
DataType m_outputType;
33+
bool m_normalizeFirst = false;
3034
};
3135

3236
}

0 commit comments

Comments
 (0)