SOL9 Sample: ImageDenoising

SOL9 2.0 Samples

1 Screenshot







2 Source code

/*
 * ImageDenoising.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


//2017/04/12

/*
void fastNlMeansDenoising(InputArray src, 
        OutputArray dst, 
        float h=3, 
        int templateWindowSize=7, 
        int searchWindowSize=21 )
*/
//2017/12/01 Added save, saveAs methods to MainView.

#define _CONSOLE_

#include <sol/ModuleFileName.h>
#include <sol/LabeledTrackBar.h>
#include <sol/LabeledComboBox.h>
#include <sol/DropFiles.h>
#include <sol/FileDialog.h>
#include <sol/opencv/OpenCVApplicationView.h>
#include <sol/opencv/OpenCVImageView.h>


namespace SOL {

class MainView :public OpenCVApplicationView {
private:
  
private:
  ////////////////////////////////////////////////////////////////////////////////////////
  //Inner classes start.
  class OriginalImageView :public OpenCVImageView {
  private:
    cv::Mat originalImage;
    
    //This is a mandatory method, because in parent class it's declared
    //as a pure virtual function.
    cv::Mat& getMat()
    {
      return originalImage;
    }
    
    void display()
    {
      show(originalImage);
    }
    
  public:
    OriginalImageView(View* parent, const char* name, Args& args)
    :OpenCVImageView(parent, name, args)
    {
      try {
        const char* filename = (const char*)args.get(XmNimageFileName);
        int imageLoadingFlag = args.get(XmNimageLoadingFlag);
        loadImage(filename, imageLoadingFlag);
      } catch (SOL::Exception ex) {
        caught(ex);
      }
    }
    
    ~OriginalImageView()
    {
    }
    
    void loadImage(const char* filename, int imageLoadingFlag= CV_LOAD_IMAGE_COLOR)
    {
      try {
        originalImage = readImage(filename, imageLoadingFlag);
        refresh();
      } catch (Exception& ex) {
        caught(ex);
      }
    }
  };
  
  class DenoisedImageView :public OpenCVImageView {
  private:
    cv::Mat originalImage;
    cv::Mat denoisedImage;
    //cv::Mat destImage;
    
    cv::Mat& getMat()
    {
      return denoisedImage;
    }
    
    void display()
    {
      show(denoisedImage);
    }
    
  public:
    DenoisedImageView(View* parent, const char* name, Args& args)
    :OpenCVImageView(parent, name, args)
    {
      try {
        const char* filename = (const char*)args.get(XmNimageFileName);
        int imageLoadingFlag = args.get(XmNimageLoadingFlag);
        loadImage(filename, imageLoadingFlag);
      } catch (SOL::Exception ex) {
        caught(ex);
      }
    }
    
    ~DenoisedImageView()
    {
    }
    
    void loadImage(const char* filename, int imageLoadingFlag= CV_LOAD_IMAGE_COLOR)
    {
      try {
        originalImage = readImage(filename, imageLoadingFlag);
        denoisedImage  = originalImage.clone();
        refresh();
      } catch (Exception& ex) {
        caught(ex);
      }
    }
    
    void denoise(int h, int templateWindowSize, int searchWindowSize)
    {
      try {
       //float h = 3.0;
        
        cv::fastNlMeansDenoising(originalImage, denoisedImage, (float)h, 
          templateWindowSize, searchWindowSize);
      } catch (cv::Exception& ex) {
      }
      refresh();
    }
  };
  //Inner classes end.
  ////////////////////////////////////////////////////////////////////////////////////////
  
  
  StringT<char>             imageFile;
  StringT<char>             savedImageFile;
  
  SmartPtr<OriginalImageView>  originalImage;
  SmartPtr<DenoisedImageView> denoisedImage;
  
  static const int          TEMPLATE_WINDOW_SIZE_MAX = 31;
  int                       templateWindowSize;  //7
  SmartPtr<LabeledTrackBar> templateWindowSizeTrackBar;
 
  static const int          SEARCH_WINDOW_SIZE_MAX = 31;
  int                       searchWindowSize;  //21
  SmartPtr<LabeledTrackBar> searchWindowSizeTrackBar;

  static const int          H_PARAMETER_MAX = 31;
  int                       hParameter;  //3
  SmartPtr<LabeledTrackBar> hParameterTrackBar;

  FileDialog                filedlg;

  void updateCaption()
  {
    char caption[MAX_PATH];
    sprintf_s(caption, CountOf(caption), "%s - %s", 
        (const char*)imageFile, 
        getAppName() ); 
       
    setText(caption);
  }
  
  void resize(int w, int h)
  {
    if (originalImage && denoisedImage &&  
          templateWindowSizeTrackBar   &&
          searchWindowSizeTrackBar) {
      originalImage              -> reshape(2,           2,  (w-170)/2-1,    h-4);
      denoisedImage              -> reshape((w-170)/2+1, 2,  (w-170)/2-1,    h-4);

      hParameterTrackBar         -> reshape(w-170 + 4,   2, 160, 60);      
      templateWindowSizeTrackBar -> reshape(w-170 + 4,  70, 160, 60);      
      searchWindowSizeTrackBar   -> reshape(w-170 + 4, 140, 160, 60);      
    }
  }

  void templateWindowSizeScrolled(Action& action)
  {
    templateWindowSize = templateWindowSizeTrackBar->getPosition();
    denoisedImage -> denoise(hParameter, templateWindowSize, searchWindowSize);
  }
  
  void searchWindowSizeScrolled(Action& action)
  {
    searchWindowSize = searchWindowSizeTrackBar->getPosition();
    denoisedImage -> denoise(hParameter, templateWindowSize, searchWindowSize);
  }

  void hParameterScrolled(Action& action)
  {
    hParameter = hParameterTrackBar->getPosition();
    denoisedImage -> denoise(hParameter, templateWindowSize, searchWindowSize);
  }

  void openFile(const char* filename)
  {
    try {
        originalImage     -> loadImage(filename);
        denoisedImage  -> loadImage(filename);
        const char* fname = strrchr(filename, '\\');
        if (fname) {
          fname++;
        }
        imageFile = fname;
        updateCaption();

        denoisedImage -> denoise(hParameter, templateWindowSize, searchWindowSize);
    } catch (Exception& ex) {
      caught(ex);
    }
  }
  
  void dropFiles(Action& action)
  {
    char fileName[MAX_PATH] = { 0 };
    DropFiles drop((HDROP)action.getWParam());
    //fileName[0] = ZERO;
    int num = drop.queryFile(0, fileName, CountOf(fileName));
    if(num > 0) {
      if (filedlg.isImageFileName(fileName)) {
        openFile(fileName);
        bringUp();
      } else {        
        bringUp(); //Activate and raise this view
        showErrorDialog("Invalid image filename", fileName,  MB_OK);
      }
    }    
  }

  void confirm(Action& action)
  {
    int rc = MessageBox(NULL, "Are you sure to close this window?", "Confirmation", 
                MB_OKCANCEL|MB_ICONEXCLAMATION);
    if (rc == IDOK) {
      exit(action);
    }
  }
  
public:
  MainView(OpenCVApplication& applet, const char* name, Args& args)
  :OpenCVApplicationView(applet, name, args)
  {
    try {
      imageFile = "..\\images\\MeshedNioh.png";
      
      int  imageLoadingFlag = CV_LOAD_IMAGE_COLOR;
      //1 Create an originalImageView.
      Args ar;
      ar.set(XmNimageFileName, imageFile);
      ar.set(XmNimageLoadingFlag, imageLoadingFlag);
      originalImage = new OriginalImageView(this, "", ar); 
      originalImage -> addCallback(XmNdropCallback, this,
        (Callback)&MainView::dropFiles, NULL);
      
      //2 Create a denoisedImageView
      ar.reset();
      ar.set(XmNimageFileName, imageFile);
      ar.set(XmNimageLoadingFlag, imageLoadingFlag);
      denoisedImage = new DenoisedImageView(this, "", ar); 

      //3 Create a hParameterTrackBar.
      hParameter    = 27;
      ar.reset();
      ar.set(XmNminimum, 1);
      ar.set(XmNmaximum, H_PARAMETER_MAX);
      ar.set(XmNposition, hParameter);
      hParameterTrackBar = new LabeledTrackBar(this, "H Parameter", ar);
      hParameterTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
        (Callback)&MainView::hParameterScrolled, NULL);


      //4 Create a templateWindowSizeTrackBar.
      templateWindowSize    = 15;
      ar.reset();
      ar.set(XmNminimum, 1);
      ar.set(XmNmaximum, TEMPLATE_WINDOW_SIZE_MAX);
      ar.set(XmNposition, templateWindowSize);
      templateWindowSizeTrackBar = new LabeledTrackBar(this, "TemplateWindowSize", ar);
      templateWindowSizeTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
        (Callback)&MainView::templateWindowSizeScrolled, NULL);
            
      //5 Create a searchWindowSizeTrackBar.
      searchWindowSize    = 13;
      ar.reset();
      ar.set(XmNminimum, 1);
      ar.set(XmNmaximum, SEARCH_WINDOW_SIZE_MAX);
      ar.set(XmNposition, searchWindowSize);
      searchWindowSizeTrackBar = new LabeledTrackBar(this, "SearchWindowSize", ar);
      searchWindowSizeTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
        (Callback)&MainView::searchWindowSizeScrolled, NULL);
            
      addCallback(XmNmenuCallback, IDM_EXIT, this,
          (Callback)&MainView::confirm, NULL);

      denoisedImage -> denoise(hParameter, templateWindowSize, searchWindowSize);
   
      updateCaption();  

      ar.reset();
      ar.set(XmNfilter, FileDialog::getImageFilesFilter());
      filedlg.create(this, "OpenFile", ar);
      
    } catch (Exception& ex) {
      caught(ex);
    }
  }

  ~MainView()
  {
  }

  void open(Action& action)
  {
    Args ar;
    
    char dir[MAX_PATH] = { 0 };
    if (restoreFileFolder(dir, CountOf(dir))) {
      ar.set(XmNdirectory, dir);
      filedlg.setValues(ar);
    }
    
    try {    
      if(filedlg.open()) {
    
        const char* filename = filedlg.getFileName();
        saveFileFolder(filename);

        openFile(filename);
        
      }
    } catch (Exception& ex) {
      caught(ex);
    }
  }  
  
  //2017/12/01
  void save(Action& action)
  {
    try {
      if (!savedImageFile.isEmpty()) {
        //Write denoised image into the filename.
        denoisedImage->writeImage(savedImageFile);
      } else {
        saveAs(action);
      }
    } catch (Exception& ex) {
      caught(ex);
    }
  }
  
  //2017/12/01
  void saveAs(Action& action)
  {
    Args ar;
    
    char dir[MAX_PATH] = { 0 };
    if (restoreFileFolder(dir, CountOf(dir))) {
      ar.set(XmNdirectory, dir);
      filedlg.setValues(ar);
    }
    
    try {    
      if(filedlg.save()) {
    
        const char* filename = filedlg.getFileName();
        saveFileFolder(filename);
        
        //Write denoised image into the filename.
        denoisedImage->writeImage(filename);
        savedImageFile = filename;
      }
    } catch (Exception& ex) {
      caught(ex);
    }
  }  
};
}

//
void main(int argc, char** argv) 
{
  try {
    ModuleFileName module(argv[0]);
    
    const char*  name = module.getAppName();
        
    OpenCVApplication applet(name, argc, argv);

    Args args;
    args.set(XmNwidth,  900);
    args.set(XmNheight, 380);  
    MainView view(applet, name, args);
    view.realize();

    applet.run();
    
  } catch (SOL::Exception& ex) {
    caught(ex);
  }
}


Last modified: 2 Dec. 2017

Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.