SOL9 Sample: HistgramEqualizedImage

SOL9 2.0 Samples

1 Screenshot







2 Source code

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


//2017/03/30

//See http://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/histogram_equalization/histogram_equalization.html


#define _CONSOLE_

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


namespace SOL {

class MainView :public OpenCVApplicationView {
private:
  typedef enum {
    COLOR_MODEL_HSV,
    COLOR_MODEL_YCRCB
  } COLOR_MODEL;
  
private:
  ////////////////////////////////////////////////////////////////////////////////////////
  //Inner class starts.
  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 HistramImageView :public OpenCVImageView {
  private:
    
    cv::Mat originalImage;
   // cv::Mat colorModeledImage;
    cv::Mat destImage;

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

    void display()
    {
      show(destImage);
    }
    
  public:
    HistramImageView(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);
      }
    }
    
    ~HistramImageView()
    {
    }
    
    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 equalizeHistgram(COLOR_MODEL colorModel)
    {
      try {
        int equalizeChannel  = 0;
        int toNewColorFormat = COLOR_BGR2HSV;
        int toOldColorFormat = COLOR_HSV2BGR;
        
        switch (colorModel) {
        case COLOR_MODEL_HSV:
            equalizeChannel  = 2;
            toNewColorFormat = COLOR_BGR2HSV;
            toOldColorFormat = COLOR_HSV2BGR;
          break;
          
        case COLOR_MODEL_YCRCB:
            equalizeChannel = 0;
            toNewColorFormat = COLOR_BGR2YCrCb;
            toOldColorFormat = COLOR_YCrCb2BGR;          
          break;
          
        default:
          break;
        }
    
        cv::Mat colorModeledImage;

        //Convert color-format from BGR to HSV or YCrCB
        cvtColor(originalImage, colorModeledImage, toNewColorFormat);
        
        //Split ycrcbImage to three channles.
        std::vector<Mat> channels(3);
        
        cv::split(colorModeledImage, channels);
    
        //Equalize a channel of channels specified equalizedChannel index.
        cv::equalizeHist(channels[equalizeChannel], channels[equalizeChannel]);
    
        //Merge three channels images to ycrcbImage
        cv::merge(channels, colorModeledImage);
        
        //Convert color-format HSV orYCrCb to  BGR
        cv::cvtColor(colorModeledImage, destImage, toOldColorFormat);
      } catch (cv::Exception& ex) {
        ; //
      }
      refresh();
    }
  };
  //Inner class ends.
  ////////////////////////////////////////////////////////////////////////////////////////
  
  
  StringT<char>                imageFile;
  
  SmartPtr<OriginalImageView>  originalImage;
  SmartPtr<HistramImageView>   histramImage;

  COLOR_MODEL                  colorModel;
  SmartPtr<LabeledComboBox>    colorModelComboBox;
  FileDialog                   filedlg;

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

  //Horizontal Scroll event by TrackBars
  void colorModelChanged(Action& action)
  {
    colorModel = (COLOR_MODEL)colorModelComboBox->getCurSel();
        
    histramImage -> equalizeHistgram(colorModel);
  }
  
  void resize(int w, int h)
  {
    if (originalImage && histramImage &&  colorModelComboBox) {
      originalImage   -> reshape(2,           2,  (w-160)/2-1,    h-4);
      histramImage    -> reshape((w-160)/2+1, 2,  (w-160)/2-1,    h-4);
      colorModelComboBox   -> reshape(w-155 + 10, 10, 140, 60);      
    }
  }

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

        histramImage -> equalizeHistgram(colorModel);
    } 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\\MaterializedSpheres.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);
      histramImage = new HistramImageView(this, "cvwindow2", ar); 

      //Create a thresholdTypeComboBx
      ar.reset();
      colorModelComboBox = new LabeledComboBox(this, "ColorModel", ar);
      const char* models[] = {
        "HSV",
        "YCrCb",
      };
      for (int i = 0; i<CountOf(models); i++) {
        colorModelComboBox -> addString(models[i]);
      }
      colorModel = COLOR_MODEL_HSV;
      
      colorModelComboBox -> setCurSel((int)colorModel);
      //Add a colorModelChanged callback to the colorModelComboBox.
      colorModelComboBox -> addCallback(XmNselChangeCallback, this,
        (Callback)&MainView::colorModelChanged, NULL);
      
      addCallback(XmNmenuCallback, IDM_EXIT, this,
          (Callback)&MainView::confirm, NULL);

      histramImage -> equalizeHistgram(colorModel);
   
      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, 380);
    MainView view(applet, name, args);
    view.realize();

    applet.run();
    
  } catch (SOL::Exception& ex) {
    caught(ex);
  }
}


Last modified: 27 Apr. 2017

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