SOL9 Sample: EdgePreservingFilter

SOL9 2.0 Samples

1 Screenshot







2 Source code

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


//2017/04/18

//photo.hpp

/** @brief Filtering is the fundamental operation in image and video processing. Edge-preserving smoothing
filters are used in many different applications @cite EM11 .

@param src Input 8-bit 3-channel image.
@param dst Output 8-bit 3-channel image.
@param flags Edge preserving filters:
-   **RECURS_FILTER** = 1
-   **NORMCONV_FILTER** = 2
@param sigma_s Range between 0 to 200.
@param sigma_r Range between 0 to 1.

CV_EXPORTS_W void edgePreservingFilter(InputArray src, OutputArray dst, int flags = 1,
        float sigma_s = 60, float sigma_r = 0.4f);

*/
#define _CONSOLE_

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

#include <sol/DropFiles.h>

namespace SOL {

class MainView :public OpenCVApplicationView {

private:
  ////////////////////////////////////////////////////////////////////////////////////////
  //Inner classes start.
  class OriginalImageView :public OpenCVImageView {
  private:
    cv::Mat originalImage;
    
    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 FilteredImageView :public OpenCVImageView {
  private:
    cv::Mat originalImage;
    cv::Mat destImage;

    cv::Mat& getMat()
    {
      return destImage;
    }

    void display()
    {
      show(destImage);
    }
    
  public:
    FilteredImageView(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);
      }
    }
    
    ~FilteredImageView()
    {
    }
    
    void loadImage(const char* filename, int imageLoadingFlag= CV_LOAD_IMAGE_COLOR)
    {
      try {
        originalImage = readImage(filename, imageLoadingFlag);
        destImage = originalImage.clone();
      } catch (Exception& ex) {
        caught(ex);
      }
    }
    
    void filter(int ksize, int sigmaColor, int sigmaSpace)
    {
      try {
        int flag = RECURS_FILTER ; //NORMCONV_FILTER is very slow;
        cv::edgePreservingFilter(
            originalImage, 
            destImage, 
            flag,
            (double)sigmaSpace, 
            (double)sigmaColor/100.0f);
      } catch (cv::Exception& ex) {
        ; //Ignore
      }
      refresh();
    }
  };
  //Inner classes end.
  ////////////////////////////////////////////////////////////////////////////////////////
  
  
  StringT<char>                imageFile;
  StringT<char>                savedImageFile;
  
  SmartPtr<OriginalImageView>  originalImage;
  SmartPtr<FilteredImageView>  filteredImage;
    
  static const int             KERNEL_SIZE_MAX = 31;
  int                          ksize;
  
  SmartPtr<LabeledTrackBar>    kernelSizeTrackBar;
  
  static const int             SIGMA_COLOR_MAX =200;
  int sigmaColor;
  SmartPtr<LabeledTrackBar>    sigmaColorTrackBar;

  static const int             SIGMA_SPACE_MAX=100;
  int                          sigmaSpace;

  SmartPtr<LabeledTrackBar>    sigmaSpaceTrackBar;


  FileDialog            filedlg;

  void updateCaption()
  {
    char caption[1024];
    sprintf_s(caption, CountOf(caption), "%s - %s", (const char*)imageFile, 
      getAppName());
    setText(caption);
  }

  //Horizontal Scroll event by TrackBars
  void commonTrackBarCallback(Action& action)
  {
    ksize      = kernelSizeTrackBar->getPosition();
    sigmaColor = sigmaColorTrackBar->getPosition();
    sigmaSpace = sigmaSpaceTrackBar->getPosition();
    
    filteredImage -> filter(ksize, sigmaColor, sigmaSpace);

    updateCaption();
   
  }
  
  void resize(int w, int h)
  {
    if (originalImage && filteredImage &&  kernelSizeTrackBar && 
        sigmaColorTrackBar && sigmaSpaceTrackBar) {
      originalImage      -> reshape(2,            2,  (w-170)/2-1,    h-4);
      filteredImage      -> reshape((w-170)/2+1,   2,  (w-170)/2-1,    h-4);
      kernelSizeTrackBar -> reshape(w-170 + 2,   2, 160, 60);      
      sigmaColorTrackBar -> reshape(w-170 + 2,  70, 160, 60);      
      sigmaSpaceTrackBar -> reshape(w-170 + 2, 140, 160, 60);      
    }
  }

  
  void openFile(const char* filename)
  {
    try {
        
      originalImage -> loadImage(filename);
      filteredImage -> loadImage(filename);
      const char* fname = strrchr(filename, '\\');
      if (fname) {
        fname++;
      }
      imageFile = fname;
        
      filteredImage -> filter(ksize, sigmaColor, sigmaSpace);
      updateCaption();  
    } catch (Exception& ex) {
      caught(ex);
    }
  }
  
  
  void dropFiles(Action& action)
  {
    char fileName[MAX_PATH] = { 0 };
    DropFiles drop((HDROP)action.getWParam());
  
    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\\YellowFlower.png";
      
      int  imageLoadingFlag = CV_LOAD_IMAGE_COLOR;
      Args ar;
      ar.set(XmNimageFileName, imageFile);
      ar.set(XmNimageLoadingFlag, imageLoadingFlag);
      originalImage = new OriginalImageView(this, "", ar); 
      originalImage -> addCallback(XmNdropCallback, this,
        (Callback)&MainView::dropFiles, NULL);
 
      ar.reset();
      ar.set(XmNimageFileName, imageFile);
      ar.set(XmNimageLoadingFlag, imageLoadingFlag);
      filteredImage = new FilteredImageView(this, "", ar); 

      ksize      = 3;
      sigmaColor = 160;
      sigmaSpace = 40;
      
      ar.reset();
      ar.set(XmNminimum, 0);
      ar.set(XmNmaximum, KERNEL_SIZE_MAX);
      ar.set(XmNposition, ksize);
      ar.set(XmNdisplayOddValue,   false);
      ar.set(XmNdisplayFloatValue, false);
      kernelSizeTrackBar = new LabeledTrackBar(this, "KernelSize", ar);
      kernelSizeTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
        (Callback)&MainView::commonTrackBarCallback, NULL);
      
      ar.reset();
      ar.set(XmNminimum, 0);
      ar.set(XmNmaximum, SIGMA_COLOR_MAX);
      ar.set(XmNposition, sigmaColor);
      ar.set(XmNdisplayOddValue,   false);
      ar.set(XmNdisplayFloatValue, false);
      sigmaColorTrackBar = new LabeledTrackBar(this, "SigmaColor", ar);
      sigmaColorTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
        (Callback)&MainView::commonTrackBarCallback, NULL);
      
      ar.reset();
      ar.set(XmNminimum, 0);
      ar.set(XmNmaximum, SIGMA_SPACE_MAX);
      ar.set(XmNposition, sigmaSpace);
      ar.set(XmNdisplayFloatValue, true);
      ar.set(XmNdisplayOddValue,   false);
      sigmaSpaceTrackBar = new LabeledTrackBar(this, "SigmaSpace", ar);
      sigmaSpaceTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
        (Callback)&MainView::commonTrackBarCallback, NULL);

      
      addCallback(XmNmenuCallback, IDM_EXIT, this,
          (Callback)&MainView::confirm, NULL);

      filteredImage -> filter(ksize, sigmaColor, sigmaSpace);
   
      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 filtered image into the filename.
        filteredImage->writeImage(savedImageFile); 
      } else {
        saveAs(action);
      }
    } catch (Exception& ex) {
      caught(ex);
    }
  }  

  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 binarized image into the filename.
        filteredImage->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, 440);
    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.