SOL9 Sample: NonPhotorealisticRendering

SOL9 2.0 Samples

1 Screenshot













2 Source code

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


//2017/04/12

// See: https://github.com/opencv/opencv/blob/master/samples/cpp/tutorial_code/photo/non_photorealistic_rendering/npr_demo.cpp
//2017/12/01 Added save, saveAs methods to MainView.

#define _CONSOLE_

#include <sol/ModuleFileName.h>
#include <sol/DropFiles.h>
#include <sol/LabeledTrackBar.h>
#include <sol/LabeledComboBox.h>

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


namespace SOL {

class MainView :public OpenCVApplicationView {
private:
  typedef enum {
     EdgePreserveSmoothingByNormalizedConvolutionFilter,
     EdgePreserveSmoothingByRecursiveFilter,
    
     DetailEnhancement,    
     MonochromePencilSketch, 
     ColorPencilSketch,
     Stylization,          
  } RendererType;
  
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 NonPhotorealisticView  :public OpenCVImageView {
  private:
    cv::Mat originalImage;
    cv::Mat destImage;
    
    cv::Mat& getMat()
    {
      return destImage;
    }
    
    void display()
    {
      show(destImage);
    }
    
  public:
    NonPhotorealisticView(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);
      }
    }
    
    ~NonPhotorealisticView()
    {
    }
    
    void loadImage(const char* filename, int imageLoadingFlag= CV_LOAD_IMAGE_COLOR)
    {
      try {
        originalImage = readImage(filename, imageLoadingFlag);
        destImage  = originalImage.clone();
        refresh();
      } catch (Exception& ex) {
        caught(ex);
      }
    }
    
    void transform(RendererType rendererTypeIndex)
    {
      switch(rendererTypeIndex) {
        case EdgePreserveSmoothingByNormalizedConvolutionFilter: {
          edgePreservingFilter(originalImage, destImage,1);
        }
        break;
        
        case EdgePreserveSmoothingByRecursiveFilter: {
          edgePreservingFilter(originalImage, destImage,2);

        }
        break;
        
        case DetailEnhancement: {
          cv::detailEnhance(originalImage, destImage);
        }
        break;
        
        case MonochromePencilSketch: {
          Mat img; 
          cv::pencilSketch(originalImage, destImage, img, 10 , 0.1f, 0.03f); 
        }
        break;

        case ColorPencilSketch: {
          Mat img; 
          cv::pencilSketch(originalImage, img, destImage, 10 , 0.1f, 0.03f); 
          
        }
        break;
        
        case Stylization: {
          stylization(originalImage, destImage);
        }
        break;
        
       default:
        break;
      }
      refresh();
    }
  };
  //Inner classes end.
  ////////////////////////////////////////////////////////////////////////////////////////
  
  
  StringT<char>        imageFile;
  StringT<char>        savedImageFile;
  
  SmartPtr<OriginalImageView>  originalImage;
  SmartPtr<NonPhotorealisticView> nonPhotorealisticImage;

  RendererType              rendererTypeIndex;
  
  SmartPtr<LabeledComboBox> rendererComboBox;

  FileDialog                filedlg;

  void updateCaption()
  {
    char caption[MAX_PATH];
    sprintf_s(caption, CountOf(caption), "%s - %s", 
        (const char*)imageFile, 
        getAppName() ); 
       
    setText(caption);
  }
  
  void selChanged(Action& event)
  {
    rendererTypeIndex = (RendererType)rendererComboBox -> getCurSel();
    nonPhotorealisticImage -> transform(rendererTypeIndex);
  }
  
  
  void resize(int w, int h)
  {
    if (originalImage && nonPhotorealisticImage) {
      originalImage          -> reshape(2,       2+60,     (w/2)-2, (h-60)-4);
      nonPhotorealisticImage -> reshape(w/2+1,   2+60,     (w/2)-2, (h-60)-4);
      rendererComboBox       -> reshape(w/2+1,      2,     (w/2)-2,       50);      
    }
  }

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

      nonPhotorealisticImage -> transform(rendererTypeIndex);
    } 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\\Figure.png";
      
      int  imageLoadingFlag = CV_LOAD_IMAGE_COLOR;
      Args ar;
      ar.set(XmNimageFileName, imageFile);
      ar.set(XmNimageLoadingFlag, imageLoadingFlag);
      originalImage = new OriginalImageView(this, "cvwindow1", ar); 
      originalImage -> addCallback(XmNdropCallback, this,
        (Callback)&MainView::dropFiles, NULL);
      
      ar.reset();
      ar.set(XmNimageFileName, imageFile);
      ar.set(XmNimageLoadingFlag, imageLoadingFlag);
      nonPhotorealisticImage = new NonPhotorealisticView(this, "cvwindow2", ar); 

      ar.reset();
      rendererComboBox = new LabeledComboBox(this, "RendererType", ar);
      const char* rendererTypes[] = {
        "EdgePreserve Smoothing By Normalized Convolution Filter",
        "EdgePreserve Smoothing By Recursive Filter",
        "Detail Enhancement",       
        "Monochrome Pencil Sketch",
        "Color Pencil Sketch",
        "Stylization"       
      };
      for (int i = 0; i<CountOf(rendererTypes); i++) {
        rendererComboBox -> addString(rendererTypes[i]);
      }
      rendererTypeIndex = EdgePreserveSmoothingByRecursiveFilter;
      
      rendererComboBox -> setCurSel(rendererTypeIndex);
      rendererComboBox -> addCallback(XmNselChangeCallback, this,
        (Callback)&MainView::selChanged, NULL);
            
      addCallback(XmNmenuCallback, IDM_EXIT, this,
          (Callback)&MainView::confirm, NULL);

      nonPhotorealisticImage -> transform(rendererTypeIndex);
   
      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 nonPhotorealisticImage into the filename.
        nonPhotorealisticImage->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 nonPhotorealisticImage into the filename.
        nonPhotorealisticImage->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,  800);
    args.set(XmNheight, 400);  
    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.