LaplacianSharpening.cpp 5.78 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
5
 * (c) 2001-2016 Univ. Grenoble Alpes, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Visit http://camitk.imag.fr for more information
 *
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * CamiTK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $CAMITK_LICENCE_END$
 ****************************************************************************/
25 26
#include "LaplacianSharpening.h"

27 28 29 30 31 32 33
#include <Application.h>
#include <ItkProgressObserver.h>
#include <QMessageBox>
#include <QString>
#include <QTextStream>
#include <itkImageToVTKImageFilter.h>
#include <itkVTKImageToImageFilter.h>
34 35 36 37 38 39 40
#include <itkLaplacianSharpeningImageFilter.h>

using namespace camitk;


// --------------- constructor -------------------
LaplacianSharpening::LaplacianSharpening(ActionExtension * extension) : Action(extension) {
41
    // Setting name, description and input component
42 43 44 45 46
    setName("Laplacian Sharpening");
    setDescription("<br>This filter <b> <i>sharpens an image using a Laplacian filter</i> </b>.<br> \
				   Laplacian Sharpening highlights regions of rapid intensity change and therefore highlights or enhances the edges. The result is an image that appears more in focus.<br/>");
    setComponent("ImageComponent");

47
    // Setting classification family and tags
48 49 50 51 52 53
    this->setFamily("ITK Filter");
    this->addTag("Derivative");
    this->addTag("Edge Detection");
    this->addTag("Contours");
    this->addTag("Sharpen");

54
    // Setting the widget containing the parameters
55
    actionWidget = NULL;
56 57 58 59 60 61 62 63

}

// --------------- destructor -------------------
LaplacianSharpening::~LaplacianSharpening() {
}

// --------------- apply -------------------
64 65
Action::ApplyStatus LaplacianSharpening::apply() {
    foreach (Component *comp, getTargets()) {
66
        ImageComponent * input = dynamic_cast<ImageComponent *> ( comp );
67
        process(input);
68
    }
69
    return SUCCESS;
70 71 72
}

void LaplacianSharpening::process(ImageComponent * comp) {
73 74
    // ITK filter implementation using templates
    vtkSmartPointer<vtkImageData> inputImage = comp->getImageData();
75
    vtkSmartPointer<vtkImageData> outputImage = implementProcess (inputImage);
76 77
    QString newName;
    QTextStream(&newName) << comp->getName() << "_sharpened";
saubatn's avatar
saubatn committed
78 79

    ImageComponent* outputComp = new ImageComponent(outputImage, newName);
80

saubatn's avatar
saubatn committed
81 82
    // consider frame policy on new image created
    Action::applyTargetPosition(comp, outputComp);
83

84
    Application::refresh();
85 86 87

}

88
#include "LaplacianSharpening.impl"
89 90

// ITK filter implementation
91 92 93 94 95 96 97 98 99 100 101 102
template <class InputPixelType, class OutputPixelType, const int dim>
vtkSmartPointer<vtkImageData> LaplacianSharpening::itkProcess(vtkSmartPointer<vtkImageData> img) {
    vtkSmartPointer<vtkImageData> result = vtkSmartPointer<vtkImageData>::New();

    // --------------------- Filters declaration and creation ----------------------
    // Define ITK input and output image types with respect to the instanciation
    //    types of the tamplate.
    typedef itk::Image< InputPixelType,  dim > InputImageType;
    typedef itk::Image< OutputPixelType, dim > OutputImageType;

    // Convert the image from CamiTK in VTK format to ITK format to use ITK filters.
    typedef itk::VTKImageToImageFilter<InputImageType> vtkToItkFilterType;
103 104
    typename vtkToItkFilterType::Pointer vtkToItkFilter = vtkToItkFilterType::New();

105 106 107
    // Declare and create your own private ITK filter here...
    typedef itk::LaplacianSharpeningImageFilter<InputImageType, OutputImageType> LaplacianSharpeningFilterType;
    typename LaplacianSharpeningFilterType::Pointer laplacianSharpeningFilter = LaplacianSharpeningFilterType::New();
108

109 110 111 112 113
    // In the same way, once the image is filtered, we need to convert it again to
    // VTK format to give it to CamiTK.
    typedef itk::ImageToVTKImageFilter<OutputImageType> itkToVtkFilterType;
    typename itkToVtkFilterType::Pointer itkToVtkFilter = itkToVtkFilterType::New();
// ------------------------- WRITE YOUR CODE HERE ----------------------------------
114

115 116 117
    // To update CamiTK progress bar while filtering, add an ITK observer to the filters.
    ItkProgressObserver::Pointer observer = ItkProgressObserver::New();
    // ITK observers generally give values between 0 and 1, and CamiTK progress bar
118
    //    wants values between 0 and 100...
119
    observer->SetCoef(100.0);
120

121 122 123
    // --------------------- Plug filters and parameters ---------------------------
    // From VTK to ITK
    vtkToItkFilter->SetInput(img);
124
    laplacianSharpeningFilter->SetInput(vtkToItkFilter->GetOutput());
125
    laplacianSharpeningFilter->AddObserver(itk::ProgressEvent(), observer);
126

127 128 129 130 131 132
    // From ITK to VTK
    itkToVtkFilter->SetInput(laplacianSharpeningFilter->GetOutput());

    // --------------------- Actually execute all filters parts --------------------
    itkToVtkFilter->Update();
    observer->Reset();
133 134

    // --------------------- Create and return a copy (the filters will be deleted)--
135
    vtkSmartPointer<vtkImageData> resultImage = itkToVtkFilter->GetOutput();
136
    int extent[6];
137 138 139
    resultImage->GetExtent(extent);
    result->SetExtent(extent);
    result->DeepCopy(resultImage);
140

141
    // Set CamiTK progress bar back to zero (the processing filter is over)
142 143
    observer->Reset();

144 145
    return result;
}
146