2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

medianfilter.cpp 5.94 KB
Newer Older
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
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2015, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
// 
// ================================================================================================

#include "medianfilter.h"

#include "cgt/buffer.h"
#include "cgt/imageunit.h"
#include "cgt/logmanager.h"
#include "cgt/shadermanager.h"
#include "cgt/textureunit.h"
#include "cgt/texture.h"

#include "core/datastructures/imagedata.h"
#include "core/datastructures/imagerepresentationgl.h"
#include "core/datastructures/renderdata.h"

38
#include "core/tools/cshelper.h"
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
#include "core/tools/quadrenderer.h"
#include "core/tools/stringutils.h"

namespace campvis {

    const std::string MedianFilter::loggerCat_ = "CAMPVis.modules.preprocessing.MedianFilter";


    MedianFilter::MedianFilter()
        : AbstractProcessor()
        , p_inputImage("InputImage", "Input Image", "", DataNameProperty::READ)
        , p_outputImage("OutputImage", "Output Image", "GlGaussianFilter.out", DataNameProperty::WRITE)
        , p_windowSize("WindowSize", "Window Size", cgt::ivec3(1, 1, 0), cgt::ivec3(0), cgt::ivec3(30))
        , p_workgroupSize("WorkgroupSize", "Workgroup Size", cgt::ivec3(4), cgt::ivec3(1), cgt::ivec3(16))
        , _shader(0)
    {
        addProperty(p_inputImage);
        addProperty(p_outputImage);
        addProperty(p_windowSize);
        addProperty(p_workgroupSize);
    }

    MedianFilter::~MedianFilter() {

    }

    void MedianFilter::init() {
        AbstractProcessor::init();
   
        LGL_ERROR;
    }

    void MedianFilter::deinit() {
        ShdrMgr.dispose(_shader);
        
        AbstractProcessor::deinit();
    }

    void MedianFilter::updateResult(DataContainer& data) {
        ImageRepresentationGL::ScopedRepresentation img(data, p_inputImage.getValue());

        if (img != 0) {
            if (img->getParent()->getDimensionality() > 1) {

83
84
                cgt::ImageUnit outputUnit;
                generateShader(img, outputUnit);
85
86
87
88
89
90
91
92
93

                if (_shader) {
                    cgt::ivec3 size = img->getSize();
                    cgt::ivec3 wgSize = p_workgroupSize.getValue();
                    cgt::vec3 groups = cgt::ceil(cgt::vec3(size) / cgt::vec3(wgSize));

                    cgt::TextureUnit inputUnit;
                    inputUnit.activate();

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

                    _shader->activate();

                    // create texture for result
                    std::unique_ptr<cgt::Texture> resultTexture(new cgt::Texture(img->getTexture()->getType(), size, img->getTexture()->getInternalFormat(), cgt::Texture::LINEAR));
                    img->bind(_shader, inputUnit);
                    LGL_ERROR;
                    resultTexture->bindImage(outputUnit, GL_WRITE_ONLY);
                    LGL_ERROR;
                    _shader->setUniform("_outputImage", outputUnit.getUnitNumber());
                    LGL_ERROR;
                    
                    glDispatchCompute(groups.x, groups.y, groups.z);
                    LGL_ERROR;

                    _shader->deactivate();

                    glFinish();

                    // put resulting image into DataContainer
                    std::unique_ptr<ImageData> id(new ImageData(img->getParent()->getDimensionality(), size, img->getParent()->getNumChannels()));
                    ImageRepresentationGL::create(id.get(), resultTexture.release());
                    id->setMappingInformation(img->getParent()->getMappingInformation());
                    data.addData(p_outputImage.getValue(), id.release());

                    cgt::TextureUnit::setZeroUnit();
                    LGL_ERROR;
                }
            }
            else {
                LERROR("Supports only 2D and 3D Median Filtering.");
            }
        }
        else {
            LDEBUG("No suitable input image found.");
        }
    }

133
    void MedianFilter::generateShader(const ImageRepresentationGL::ScopedRepresentation& img, const cgt::ImageUnit& imgUnit)
134
135
136
137
138
139
140
141
142
143
144
    {
        auto wnd = p_windowSize.getValue();
        auto wg = p_workgroupSize.getValue();
        std::stringstream ss;
        ss << "#define MEDIAN_WINDOW_X " << wnd.x << std::endl;
        ss << "#define MEDIAN_WINDOW_Y " << wnd.y << std::endl;
        ss << "#define MEDIAN_WINDOW_Z " << wnd.z << std::endl;
        ss << "#define WORK_GROUP_SIZE_X " << wg.x << std::endl;
        ss << "#define WORK_GROUP_SIZE_Y " << wg.y << std::endl;
        ss << "#define WORK_GROUP_SIZE_Z " << wg.z << std::endl;
        ss << "#define OUTPUT_TEXTURE_FORMAT " << cgt::Texture::calcMatchingWriteFormat(img->getTexture()->getInternalFormat()) << std::endl;
145
        ss << CSHelper::generateGLSLImageDefinition(*(img->getTexture()), "_outputImage", imgUnit) << std::endl;
146
147
148
149
150
151
152
153
154

        // very simple caching to eliminate the glsl compiler as a bottleneck
        static std::string headers;
        if (!_shader || headers != ss.str()) {
            headers = ss.str();
            _shader = ShdrMgr.loadCompute("modules/preprocessing/glsl/medianfilter.comp", headers);
        }
    }
}