SOL9 Sample: GaussianBlur

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


//2017/03/30
// See: "http://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html?highlight=gaussianblur#gaussianblur"

#define _CONSOLE_

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

namespace SOL {

class MainView :public OpenCVApplicationView {

private:
  ////////////////////////////////////////////////////////////////////////////////////////
  //Inner class starts.
  class OriginalImageView :public OpenCVNamedWindow {
  private:
    cv::Mat originalImage;
    
    void display()
    {
      show(originalImage);
    }
    
  public:
    OriginalImageView(View* parent, const char* name, Args& args)
    :OpenCVNamedWindow(parent, name, args)
    {
      //1 Let this view accept WM_DROPFILES message.
      dragAcceptFiles(TRUE);
      
      //2 DropFiles event hanlder.
      //When this view gets WM_DROPFILES event, then dropFiles method in parent will be called 
      addEventHandler(WM_DROPFILES, parent,
        (Handler)&MainView::dropFiles, NULL);

      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 BlurredImageView :public OpenCVNamedWindow {
  private:
    cv::Mat originalImage;
    cv::Mat blurredImage;
    
    void display()
    {
      show(blurredImage);
    }
    
  public:
    BlurredImageView(View* parent, const char* name, Args& args)
    :OpenCVNamedWindow(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);
      }
    }
    
    ~BlurredImageView()
    {
    }
    
    void loadImage(const char* filename, int imageLoadingFlag= CV_LOAD_IMAGE_COLOR)
    {
      try {
        originalImage = readImage(filename, imageLoadingFlag);
        blurredImage = originalImage;
        refresh();
      } catch (Exception& ex) {
        caught(ex);
      }
    }
    
    void blur(int ksize, int sigma)
    {
      //void GaussianBlur(const Mat& src, Mat& dst, Size ksize, 
      //       double sigmaX, 
      //       double sigmaY  = 0, 
      //       int borderType = BORDER_DEFAULT)
      cv::Mat newImage = cv::Mat::zeros(originalImage.size(), originalImage.type() );
      cv::GaussianBlur(originalImage, newImage, cv::Size(ksize, ksize), 
            (double)sigma, //sigmaX, 
            (double)sigma, //sigmaY
            BORDER_DEFAULT);
      blurredImage = newImage;
      refresh();
    }
  };
  //Inner class ends.
  ////////////////////////////////////////////////////////////////////////////////////////
  
  void updateCaption()
  {
    char caption[1024];
    sprintf_s(caption, CountOf(caption), "%s - %s", (const char*)imageFile, 
      getAppName());
    setText(caption);
  }
  
  StringT<char>               imageFile;
  
  SmartPtr<OriginalImageView> originalImage;
  SmartPtr<BlurredImageView>  blurredImage;
  
  int                         ksize;
  SmartPtr<LabeledTrackBar>    ksizeTrackBar;
  
  int                         sigma;
  SmartPtr<LabeledTrackBar>    sigmaTrackBar;
  FileDialog                  filedlg;
  
  //Horizontal Scroll event by TrackBars
  void trackBarScrolled(Action& action)
  {
    ksize = ksizeTrackBar->getPosition();
    sigma = sigmaTrackBar->getPosition();

    int ks = (ksize/2)*2 +1;
    
    blurredImage -> blur(ks, sigma);
    updateCaption();
  }
  
  void resize(int w, int h)
  {
    if (originalImage && blurredImage && 
          ksizeTrackBar && sigmaTrackBar) {
      originalImage  -> reshape(2,            2,  (w-160)/2-1,    h-4);
      blurredImage   -> reshape((w-160)/2+1,  2,  (w-160)/2-1,    h-4);
      ksizeTrackBar  -> reshape(w-155 + 10,  40, 140, 60);      
      sigmaTrackBar  -> reshape(w-155 + 10, 140, 140, 60);      

    }
  }

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

      int ks = (ksize/2)*2 +1;
    
      blurredImage -> blur(ks, sigma);
      updateCaption();
      
    } catch (Exception& ex) {
      caught(ex);
    }
  }
  
  
  long dropFiles(Event& event)
  {
    char fileName[MAX_PATH] = { 0 };
    DropFiles drop((HDROP)event.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);
      }
    }    
    return 0;
  }

  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, "cvwindow1", ar); 

      ar.reset();
      ar.set(XmNimageFileName, imageFile);
      ar.set(XmNimageLoadingFlag, imageLoadingFlag);
      blurredImage = new BlurredImageView(this, "cvwindow2", ar); 

      ksize = 10;
      sigma = 12;
      ar.reset();
      ar.set(XmNminimum, 0);
      ar.set(XmNmaximum, 31);
      ar.set(XmNposition, ksize);
      ar.set(XmNdisplayOddValue, true);
      ksizeTrackBar = new LabeledTrackBar(this, "KernelSize", ar);
      ksizeTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
        (Callback)&MainView::trackBarScrolled, NULL);
      
      ar.reset();
      ar.set(XmNminimum, 0);
      ar.set(XmNmaximum, 20);
      ar.set(XmNposition, sigma);
      ar.set(XmNdisplayOddValue, false);
      sigmaTrackBar = new LabeledTrackBar(this, "Sigma", ar);
      sigmaTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
        (Callback)&MainView::trackBarScrolled, NULL);

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

      int ks = (ksize/2)*2 +1;
      blurredImage -> blur(ks, sigma);
   
      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);
    }
  }  
};
}

//
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: 20 Apr. 2017

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