SOL9 Sample: ShapeMatcher

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


//2017/04/12

// See: http://docs.opencv.org/3.2.0/d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57
// See: http://docs.opencv.org/3.2.0/db/d8e/tutorial_threshold.html
/*
double cv::threshold  ( InputArray  src,  
  OutputArray  dst,  
  double  thresh,  
  double  maxval,  
  int  type  
 )   
*/

#define _CONSOLE_

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

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

#include "resource.h"

namespace SOL {

class MainView :public OpenCVApplicationView {

private:
  ////////////////////////////////////////////////////////////////////////////////////////
  //Inner classes start.
  class BinarizedImageView  :public OpenCVImageView {
  private:
    cv::Mat originalImage;
    cv::Mat grayImage;
    cv::Mat destImage;
    
    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);
      }
    }
    
    cv::Mat& getOriginalImage()
    {
      return originalImage;
    }

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

    void binarize(int thresholdType, int thresholdValue)
    {
      cv::threshold( grayImage, destImage, thresholdValue, THRESHOLD_VALUE_MAX, thresholdType );

      refresh();
    }
  };
  
  class MatchedImageView: public OpenCVImageView {
  private:
    cv::Mat matchedImage; 

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

    //The scale image is displayed on this image view.
    virtual void display()
    {
      show(matchedImage);
    }
 
  public:
    MatchedImageView(View* parent, const char* name, Args& args)
    :OpenCVImageView(parent, name, args)
    {
      refresh();
    } 

    ~MatchedImageView()
    {
    }
    /*
    void rescale(int scalingRatio)
    {
      scaledImage.release();
      scaleImage(matchedImage, scaledImage, scalingRatio);
    }
    */
    
    void setMatched(cv::Mat& mat)//, int scalingRatio)
    {
      matchedImage.release();
      
      matchedImage = mat;
    //  scaleImage(matchedImage, scaledImage, scalingRatio);
      refresh();
    }

  };
  //Inner classes end.
  ////////////////////////////////////////////////////////////////////////////////////////
  
  
  static const int             IMAGES_COUNT = 2;
  static const int             FIRST  = 0;
  static const int             SECOND = 1;
  StringT<char>                imageFiles[IMAGES_COUNT];
  StringT<char>                savedImageFile;
  //int                          fileOpenIndex;

  int                          imageLoadingFlag;
  
  int                          imageScalingRatio; //percentage 
  
  SmartPtr<BinarizedImageView> binarizedImage[IMAGES_COUNT];

  SmartPtr<MatchedImageView>   matchedImage;


  int                          thresholdTypeIndex;
  
  SmartPtr<LabeledComboBox>    thresholdTypeComboBox;

  int                          thresholdValue;
  
  SmartPtr<LabeledTrackBar>    thresholdValueTrackBar;
  

  int                          minimumSize;

  SmartPtr<LabeledTrackBar>    minimumSizeTrackBar;

  int                          maximumSize;

  SmartPtr<LabeledTrackBar>    maximumSizeTrackBar;

  SmartPtr<PushButton>         clearButton;

  SmartPtr<PushButton>         matchButton;
 
  FileDialog                   filedlg;

  static const int THRESHOLD_VALUE_MAX = 255;

  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 },
      {n++, cv::THRESH_TRUNC      },
      {n++, cv::THRESH_TOZERO     },
      {n++, cv::THRESH_TOZERO_INV },
      {n++, cv::THRESH_OTSU       }, 
      {n++, cv::THRESH_TRIANGLE   }, 
    };
    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- %s", 
        (const char*)imageFiles[FIRST],
      (const char*)imageFiles[SECOND],
        getAppName() ); 
       
    setText(caption);
  }

  
  void selChanged(Action& event)
  {
    thresholdTypeIndex = thresholdTypeComboBox -> getCurSel();
    binarizedImage[FIRST]  -> binarize( getThresholdType(thresholdTypeIndex), thresholdValue );
    binarizedImage[SECOND] -> binarize( getThresholdType(thresholdTypeIndex), thresholdValue );
  }
  
  void trackBarScrolled(Action& event)
  {
    thresholdTypeIndex = thresholdTypeComboBox -> getCurSel();
    thresholdValue     = thresholdValueTrackBar -> getPosition();
    binarizedImage[FIRST]  -> binarize( getThresholdType(thresholdTypeIndex), thresholdValue );
    binarizedImage[SECOND] -> binarize( getThresholdType(thresholdTypeIndex), thresholdValue );
  }
  
  void minimumSizeScrolled(Action& action)
  {
    minimumSize         = minimumSizeTrackBar ->getPosition();

    thresholdTypeIndex = thresholdTypeComboBox -> getCurSel();

    thresholdValue      = thresholdValueTrackBar -> getPosition();
    binarizedImage[FIRST] -> binarize( getThresholdType(thresholdTypeIndex),
                                thresholdValue);
    binarizedImage[SECOND] -> binarize( getThresholdType(thresholdTypeIndex),
                                thresholdValue);
    shapeMatching();
  }

  void maximumSizeScrolled(Action& action)
  {
    maximumSize         = maximumSizeTrackBar ->getPosition();

    thresholdTypeIndex = thresholdTypeComboBox -> getCurSel();

    thresholdValue      = thresholdValueTrackBar -> getPosition();
    binarizedImage[FIRST] -> binarize( getThresholdType(thresholdTypeIndex),
                                thresholdValue);
    binarizedImage[SECOND] -> binarize( getThresholdType(thresholdTypeIndex),
                                thresholdValue);
    shapeMatching();
  }

  void resize(int w, int h)
  {
    try {
    if (binarizedImage[FIRST] && binarizedImage[SECOND] && matchedImage ) {
      /*&&
        thresholdTypeComboBox && thresholdValueTrackBar && 
        minimumSizeTrackBar && maximumSizeTrackBar && 
        clearButton && matchButton) {
          */
      binarizedImage[FIRST]   -> reshape(          2,   2, (w-170)/2-2, h/2-4);
      binarizedImage[SECOND]  -> reshape((w-170)/2+1,   2, (w-170)/2-2, h/2-4);
      matchedImage            -> reshape(          2,   h/2, w-170-2,   h/2-4);
      
      thresholdTypeComboBox   -> reshape((w-170)+1,   2,         165,  50);
      thresholdValueTrackBar  -> reshape((w-170)+1,  70,         165,  50);

      minimumSizeTrackBar  -> reshape((w-170)+1,  130,         165,  50);
      maximumSizeTrackBar  -> reshape((w-170)+1,  190,         165,  50);

      clearButton         -> reshape(w-160+4, 250, 140, 30);
      matchButton         -> reshape(w-160+4, 290, 140, 30); 
    }
    } catch (SOL::Exception& ex) {
      caught(ex);
    }
  }


  void openFile(int index, const char* filename)
  {
    try {
      if (index >= FIRST && index <= SECOND) {
        printf("filename: %s\n", filename);
       // binarizedImage[fileOpenIndex]->invalidate();
        binarizedImage[index]->loadImage(filename, 
                       imageLoadingFlag);
        
        binarizedImage[index] -> binarize(
                                getThresholdType(thresholdTypeIndex),
                                thresholdValue);
        imageFiles[index] = filename;
        updateCaption();
      }
    } catch (Exception& ex) {
      caught(ex);
    }
  }
  
  
  //Event handler for WM_DROPFILES
  long dropFiles(Event& event)
  {
    View* view = (View*)event.getData();
    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)) {
        if (view == binarizedImage[FIRST]) {
          openFile(FIRST, fileName);
        } else if (view == binarizedImage[SECOND]) {
          openFile(SECOND, fileName);          
        }
        bringUp();
      } else {        
        bringUp(); 
        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);
    }
  }
  
  void clear(Action& action)
  {
    //Detect connectedCommponentsWithStats on destImage 
    cv::Mat& originalImage = binarizedImage[FIRST ]->getOriginalImage();
    cv::Mat destImage      = originalImage.clone(); 
    matchedImage->setMatched(destImage);//, imageScalingRatio); 
  }

  void match(Action& action)
  {
    shapeMatching();
  }

  void shapeMatching()
  {
    //Detect connectedCommponentsWithStats on destImage 
    Mat labels, stats, centroids; 
    cv::Mat& originalImage = binarizedImage[FIRST ]->getOriginalImage();
    cv::Mat& srcBin = binarizedImage[FIRST ]->getBinarizedImage();
    cv::Mat& tmpBin = binarizedImage[SECOND]->getBinarizedImage();

    cv::connectedComponentsWithStats(srcBin,
            labels, stats, centroids);
     
    int tw = tmpBin.rows;
    int th = tmpBin.cols;
       
    cv::Mat destImage = originalImage.clone();
 
    const double MATCHING_THRESHOLD = 0.005;

    cv::Rect minimum(0, 0, 0, 0);
    double MIN_SIMILARITY = 1.0;
    bool found = false;
 
    for(int i=0; i < stats.rows; i++)  { 
      int x = stats.at<int>(Point(0, i)); 
      int y = stats.at<int>(Point(1, i)); 
      int w = stats.at<int>(Point(2, i)); 
      int h = stats.at<int>(Point(3, i)); 
      cv::Rect rect(x, y, w, h);

      cv::Mat rectOfInterest = srcBin(rect); 
      double similarity = cv::matchShapes(tmpBin, 
               rectOfInterest, CV_CONTOURS_MATCH_I1, 0);

      if ( (w >= minimumSize || h >= minimumSize ) && 
           (w <= maximumSize || h <= maximumSize )) { 
        if (similarity <= MIN_SIMILARITY) {
          MIN_SIMILARITY = similarity;       
          minimum = rect;
            printf("matching similarity=%.04f  x=%d y=%d w=%d h=%d\n", 
             similarity, x, y, w, h);
          found = true;
        }
      }
    } 

    if (found) {
      cv::rectangle(destImage, minimum, CV_RGB(255, 0, 0), 3);
    }

    matchedImage->setMatched(destImage);//, imageScalingRatio); 
  }
  
public:
  MainView(OpenCVApplication& applet, const char* name, Args& args)
  :OpenCVApplicationView(applet, name, args)

  {
    try {
      imageFiles[FIRST]  = "../images/CatImage.png";
      imageFiles[SECOND] = "../images/CatFace.png";

      imageLoadingFlag = CV_LOAD_IMAGE_COLOR;

      //1 Create two binariziedImage views.
      Args ar;
      for (int i = 0; i<IMAGES_COUNT; i++) {
        ar.reset();
        ar.set(XmNimageFileName, (const char*)imageFiles[i]);
        ar.set(XmNimageLoadingFlag, imageLoadingFlag);
        binarizedImage[i] = new BinarizedImageView(this, "", ar);
      }
      //2 Create a matched image view.
      ar.reset();
      matchedImage = new MatchedImageView(this, "", ar);


      //3 Create a thresholdTypeComboBx
      ar.reset();
      thresholdTypeComboBox = new LabeledComboBox(this, "ThresholdType", ar);
      const char* types[] = {
        "Binary",
        "Binary Inverted",
        "Threshold Truncated",
        "Threshold to Zero",
        "Threshold to Zero Inverted",
        //"Mask", 
        "Otsu", 
        "Triangle", 
      };
      for (int i = 0; i<CountOf(types); i++) {
        thresholdTypeComboBox -> addString(types[i]);
      }
      thresholdTypeIndex = 0; //Binary
      
      thresholdTypeComboBox -> setCurSel(thresholdTypeIndex);
      //Add a selChanged callback.
      thresholdTypeComboBox -> addCallback(XmNselChangeCallback, this,
        (Callback)&MainView::selChanged, NULL);
     
      //4 Create a thresholdValueTrackBar
      thresholdValue = 60;
      ar.reset();
      ar.set(XmNminimum, 0);
      ar.set(XmNmaximum, THRESHOLD_VALUE_MAX);
      ar.set(XmNposition, thresholdValue);
      ar.set(XmNdisplayOddValue, FALSE);
      thresholdValueTrackBar = new LabeledTrackBar(this, "ThresholdValue", ar);
      //Add a trackBarScrolled callback to the thresoldValueTackBar
      thresholdValueTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
        (Callback)&MainView::trackBarScrolled, NULL);
       
      //5 Create a minimumSizeTrackBar
      minimumSize = 60;
      ar.reset();
      ar.set(XmNminimum, 10);
      ar.set(XmNmaximum, 100);
      ar.set(XmNvalue,   minimumSize);
      minimumSizeTrackBar = new LabeledTrackBar(this, "MatchMinSize", ar);
      minimumSizeTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
        (Callback)&MainView::minimumSizeScrolled, NULL);

      //6 Create a maximumSizeTrackBar
      maximumSize = 240;
      ar.reset();
      ar.set(XmNminimum, 100);
      ar.set(XmNmaximum, 400);
      ar.set(XmNvalue,   maximumSize);
      maximumSizeTrackBar = new LabeledTrackBar(this, "MatchMaxSize", ar);
      maximumSizeTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
        (Callback)&MainView::maximumSizeScrolled, NULL);
  
      //7 Create a clearButton.
      ar.reset();
      clearButton = new PushButton(this, "Clear", ar);
      clearButton -> addCallback(XmNactivateCallback, this,
        (Callback)&MainView::clear, NULL);
 
      //8 Create a matchButton.
      ar.reset();
      matchButton = new PushButton(this, "Match", ar);
      matchButton -> addCallback(XmNactivateCallback, this,
        (Callback)&MainView::match, NULL);
 
      //9 Binarize an image on each binarized image view.
      binarizedImage[FIRST] -> binarize(
                                getThresholdType(thresholdTypeIndex),
                                thresholdValue);//,
      binarizedImage[SECOND] -> binarize(
                                getThresholdType(thresholdTypeIndex),
                                thresholdValue);//,
      //10 Do a shape matching operation on the binarized images. 
      shapeMatching();
   
      updateCaption();  

      ar.reset();
      ar.set(XmNfilter, FileDialog::getImageFilesFilter());
      filedlg.create(this, "OpenFile", ar);

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

      addCallback(XmNmenuCallback, IDM_OPEN_FIRST_FILE, this,
          (Callback)&MainView::openFile, (void*)FIRST);
      
      addCallback(XmNmenuCallback, IDM_OPEN_SECOND_FILE, this,
          (Callback)&MainView::openFile, (void*)SECOND);
          
      postResizeRequest();
      
    } catch (Exception& ex) {
      caught(ex);
    }
  }

  ~MainView()
  {
  }

  void openFile(Action& action)
  {
    Args ar;
    int index = (int)action.getData();
    
    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(index, filename);
        
      }
    } catch (Exception& ex) {
      caught(ex);
    }
  } 
  

  //2017/12/01
  void save(Action& action)
  {
    try {
      if (!savedImageFile.isEmpty()) {
        //Write matched image into the filename.
        matchedImage->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 matched image into the filename.
        matchedImage->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.