SOL9 Sample: AdaptiveImageThresholding

SOL9 2.0 Samples

1 Screenshot




2 Source code

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


//2017/04/12
//2017/12/01 Updated BinarizedImageView::binarize method.
//2017/12/01 Added save method to MainView.

// See: http://docs.opencv.org/3.2.0/d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57
// See: 
/*
 void cvAdaptiveThreshold(const CvArr* src, CvArr* dst, 
      double max_value, int adaptive_method=CV_ADAPTIVE_THRESH_MEAN_C, 
      int threshold_type=CV_THRESH_BINARY, int block_size=3, double param1=5 )
Parameters:
 src       : Source 8-bit single-channel image.
 dst       : Destination image of the same size and the same type as src .
 maxValue  : Non-zero value assigned to the pixels for which the condition is satisfied. See the details below.
 adaptiveMethod : Adaptive thresholding algorithm to use, ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C 
 thresholdType  : Thresholding type that must be either THRESH_BINARY or THRESH_BINARY_INV .
 blockSize      : Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
 C              : Constant subtracted from the mean or weighted mean. Normally, 
  it is positive but may be zero or negative as well.
*/

#define _CONSOLE_

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

#include <sol/Pair.h>
#include <sol/LabeledTrackBar.h>
#include <sol/LabeledComboBox.h>

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

namespace SOL {

class MainView :public OpenCVApplicationView {

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 BinarizedImageView  :public OpenCVImageView {
  private:
    cv::Mat originalImage;
    cv::Mat grayImage;
    cv::Mat destImage;
    
    static const int MAX_PIXEL_VALUE = 255;
    static const int C               = 9;    //Constant subtracted from the mean or weighted mean
    
    cv::Mat& getMat()
    {
      return destImage;
    }
    
    void display()
    {
      show(destImage);
    }
    
  public:
    BinarizedImageView(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);
      }
    }
    
    ~BinarizedImageView()
    {
    }
    
    void loadImage(const char* filename, int imageLoadingFlag= CV_LOAD_IMAGE_COLOR)
    {
      try {
        originalImage = readImage(filename, imageLoadingFlag);
        cv::cvtColor( originalImage, grayImage, COLOR_BGR2GRAY); 
        destImage  = grayImage.clone();
        refresh();
      } catch (Exception& ex) {
        caught(ex);
      }
    }
    
    void binarize(int adaptiveMethod, int thresholdType, int blockSize)
    {
      try {
        //2017/12/01 Added the following line.   
        blockSize = (blockSize/2)*2 + 1;
        
        if (blockSize <3) {
          blockSize=3;
        }
        cv::adaptiveThreshold( grayImage, destImage, MAX_PIXEL_VALUE, 
          adaptiveMethod, thresholdType, blockSize,  (double)C);
      } catch (cv::Exception& ex) {
        //Sometimes we get an exception; I don't know the reason why it happens.
        ;//MessageBox(NULL, "Failed to adaptiveThreshold", "Error", MB_OK);
      }
      refresh();
    }
  };
  //Inner classes end.
  ////////////////////////////////////////////////////////////////////////////////////////
  
  
  StringT<char>                imageFile;
  StringT<char>                savedImageFile;
  
  SmartPtr<OriginalImageView>  originalImage;
  SmartPtr<BinarizedImageView> binarizedImage;

  int                          adaptiveMethodIndex;
  
  SmartPtr<LabeledComboBox>    adaptiveMethodComboBox;

  int                          thresholdTypeIndex;
  
  SmartPtr<LabeledComboBox>    thresholdTypeComboBox;
  
  static const int             BLOCK_SIZE_MIN = 3;
  static const int             BLOCK_SIZE_MAX = 43;
  
  int                          blockSize;
  
  SmartPtr<LabeledTrackBar>    blockSizeTrackBar;
  
  FileDialog                   filedlg;


  static int getAdaptiveMethod(int index) 
  {
    int n = 0;
    //We don't include the THRESH_OTSU type
    Pair<int, int> methods[] = {
      {n++, ADAPTIVE_THRESH_MEAN_C},  
      {n++, ADAPTIVE_THRESH_GAUSSIAN_C},  
    };
    int method = ADAPTIVE_THRESH_MEAN_C; 
    if (index >= 0 && index <n) {
      method = methods[index].second;
    } 
    return method;
  }
  

  static int getThresholdType(int index) 
  {
    int n = 0;
    //We don't include the THRESH_OTSU type
    Pair<int, int> types[] = {
      {n++, cv::THRESH_BINARY     },
      {n++, cv::THRESH_BINARY_INV },
    };
    int type = THRESH_BINARY; 
    if (index >= 0 && index <n) {
      type = types[index].second;
    } 
    return type;
  }

  void updateCaption()
  {
    char caption[MAX_PATH];
    sprintf_s(caption, CountOf(caption), "%s - %s", 
        (const char*)imageFile, 
        getAppName() ); 
       
    setText(caption);
  }
  
  void adaptiveMethodSelChanged(Action& event)
  {
    adaptiveMethodIndex = adaptiveMethodComboBox -> getCurSel();
    thresholdTypeIndex  = thresholdTypeComboBox -> getCurSel();

    blockSize           = blockSizeTrackBar -> getPosition();
    binarizedImage -> binarize( getAdaptiveMethod(adaptiveMethodIndex),
                                getThresholdType(thresholdTypeIndex),
                                blockSize );
  }
  
  void thresholdTypeSelChanged(Action& event)
  {
    
    adaptiveMethodIndex = adaptiveMethodComboBox -> getCurSel();
    thresholdTypeIndex  = thresholdTypeComboBox -> getCurSel();
    
    blockSize           = blockSizeTrackBar -> getPosition();
    binarizedImage -> binarize( getAdaptiveMethod(adaptiveMethodIndex), 
                                getThresholdType(thresholdTypeIndex),
                                blockSize );
  }

  void trackBarScrolled(Action& event)
  {
    adaptiveMethodIndex = adaptiveMethodComboBox -> getCurSel();
    thresholdTypeIndex  = thresholdTypeComboBox -> getCurSel();
 
    blockSize           = blockSizeTrackBar -> getPosition();
    binarizedImage -> binarize( getAdaptiveMethod(adaptiveMethodIndex), 
                                getThresholdType(thresholdTypeIndex),
                                blockSize );
  }
  
  void resize(int w, int h)
  {
    if (originalImage && binarizedImage) {
      originalImage           -> reshape(          2,   2, (w-190)/2-4, h-4);
      binarizedImage          -> reshape((w-190)/2+1,   2, (w-190)/2-4, h-4);
      adaptiveMethodComboBox  -> reshape((w-190)+1,   2,         180,  50);
      thresholdTypeComboBox   -> reshape((w-190)+1,  70,         180,  50);

      blockSizeTrackBar       -> reshape((w-190)+1,  140,        180,  50);
    }
  }

  void openFile(const char* filename)
  {
    try {
        
      originalImage   -> loadImage(filename);
      binarizedImage  -> loadImage(filename);
      const char* fname = strrchr(filename, '\\');
      if (fname) {
        fname++;
      }
      imageFile = fname;
      updateCaption();
      binarizedImage -> binarize( getAdaptiveMethod(adaptiveMethodIndex), 
                                getThresholdType(thresholdTypeIndex),
                                blockSize );
    } 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\\GinzaWako.png";
      
      int  imageLoadingFlag = CV_LOAD_IMAGE_COLOR;
      Args ar;
      ar.set(XmNimageFileName, imageFile);
      ar.set(XmNimageLoadingFlag, imageLoadingFlag);
      originalImage = new OriginalImageView(this,  "", ar); //You don't need specify windows name parameter 
      originalImage -> addCallback(XmNdropCallback, this,
        (Callback)&MainView::dropFiles, NULL);
      
      ar.reset();
      ar.set(XmNimageFileName, imageFile);
      ar.set(XmNimageLoadingFlag, imageLoadingFlag);
      binarizedImage = new BinarizedImageView(this, "", ar); //You don't need specify windows name parameter

      //Create a adaptiveMethodComboBox
      ar.reset();
      adaptiveMethodComboBox = new LabeledComboBox(this, "ThresholdType", ar);
      const char* methods[] = {
        "Adaptive_Thresh Mean_C",
        "Adaptive_Thresh_Gaussian_C",
      };
      for (int i = 0; i<CountOf(methods); i++) {
        adaptiveMethodComboBox -> addString(methods[i]);
      }
      adaptiveMethodIndex = 0;
      
      adaptiveMethodComboBox -> setCurSel(adaptiveMethodIndex);
      //Add a selChanged callback.
      adaptiveMethodComboBox -> addCallback(XmNselChangeCallback, this,
        (Callback)&MainView::adaptiveMethodSelChanged, NULL);
     
      //
      //Create a thresholdTypeComboBox
      ar.reset();
      thresholdTypeComboBox = new LabeledComboBox(this, "ThresholdType", ar);
      const char* types[] = {
        "Binary",
        "Binary Inverted",
      };
      for (int i = 0; i<CountOf(types); i++) {
        thresholdTypeComboBox -> addString(types[i]);
      }
      thresholdTypeIndex = 0;
      
      thresholdTypeComboBox -> setCurSel(thresholdTypeIndex);
      //Add a selChanged callback.
      thresholdTypeComboBox -> addCallback(XmNselChangeCallback, this,
        (Callback)&MainView::thresholdTypeSelChanged, NULL);
     
      
      //Create a blockSizeTrackBar
      blockSize = 7;
      ar.reset();
      ar.set(XmNminimum, BLOCK_SIZE_MIN); //3);
      ar.set(XmNmaximum, BLOCK_SIZE_MAX);
      ar.set(XmNposition, blockSize);
      ar.set(XmNdisplayOddValue, TRUE);
      blockSizeTrackBar = new LabeledTrackBar(this, "BlockValue", ar);
      blockSizeTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
        (Callback)&MainView::trackBarScrolled, NULL);
 
      addCallback(XmNmenuCallback, IDM_EXIT, this,
          (Callback)&MainView::confirm, NULL);

      binarizedImage -> binarize( getAdaptiveMethod(adaptiveMethodIndex), 
                                getThresholdType(thresholdTypeIndex),
                                blockSize );
   
      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 binarized image into the filename.
        binarizedImage->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.
        binarizedImage->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, 360);  
    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.