4.28 How to transform an image by a changeable color filter in OpenCV?

In the latest SOL9 libary, we have implmented OpenCVColorFilter class, and the following ImageTransformationByDynamicColorFilter is an example to apply the filter to a cv::Mat image. In this example, the color filter can be controlled by three instance of ColorPositioner.
  View
   |
   +-- Composite
        |
        +-- RowColumnT
        |    |
        |    +-- OpenCVColorFilter
        |
        +-- Positioner
             |
             +-- ColorPositioner





/*
 * ImageTransformationByDynamicColorFilter.cpp 
 * Copyright (c) 2017 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */

//2017/10/01
// Simple image color transformer based on cv::transform API and ColorMatrix.
//
//  See: http://docs.opencv.org/3.0-beta/modules/imgproc/doc/filtering.html

#define _CONSOLE_

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

#include <sol/LabeledTrackBar.h>
#include <sol/FileDialog.h>

#include <sol/opencv/OpenCVApplicationView.h>
#include <sol/opencv/OpenCVScaleComboBox.h>
#include <sol/opencv/OpenCVScrolledImageView.h>
#include <sol/opencv/OpenCVColorFilter.h>

namespace SOL {

class MainView :public OpenCVApplicationView {
  
private:
  ////////////////////////////////////////////////////////////////////////////////////////
  //Inner classes start.
  class OriginalImageView :public OpenCVScrolledImageView {
  private:
    cv::Mat originalImage;
        
  public:
    OriginalImageView(View* parent, const char* name, Args& args)
    :OpenCVScrolledImageView(parent, name, args)
    {
      try {
        const char* filename = (const char*)args.get(XmNimageFileName);
        int imageLoadingFlag = args.get(XmNimageLoadingFlag);
        int scalingRatio     = args.get(XmNimageScalingRatio);
        loadImage(filename, imageLoadingFlag, scalingRatio);
      } catch (SOL::Exception ex) {
        caught(ex);
      }
    }
    
    ~OriginalImageView()
    {
    }
    
    void loadImage(const char* filename, int imageLoadingFlag= CV_LOAD_IMAGE_COLOR, int scalingRatio=100)
    {
      try {
        originalImage = readImage(filename, imageLoadingFlag);
        setScaledImage(originalImage, scalingRatio);
        
      } catch (Exception& ex) {
        caught(ex);
      }
    }
    
    void rescale(int scalingRatio)
    {
      setScaledImage(originalImage, scalingRatio);
    }
  };
  
  class TransformedImageView :public OpenCVScrolledImageView {
  private:
    cv::Mat originalImage;
    cv::Mat filteredImage;
  
  public:
    TransformedImageView(View* parent, const char* name, Args& args)
    :OpenCVScrolledImageView(parent, name, args)
    {
      try {
        const char* filename = (const char*)args.get(XmNimageFileName);
        int imageLoadingFlag = args.get(XmNimageLoadingFlag);
        int scalingRatio     = args.get(XmNimageScalingRatio);
        loadImage(filename, imageLoadingFlag, scalingRatio);
      } catch (SOL::Exception ex) {
        caught(ex);
      }
    }
    
    ~TransformedImageView()
    {
    }
    
    void loadImage(const char* filename, int imageLoadingFlag= CV_LOAD_IMAGE_COLOR, int scalingRatio=100)
    {
      try {
        originalImage = readImage(filename, imageLoadingFlag);
        filteredImage  = originalImage;
        setScaledImage(filteredImage, scalingRatio);
        
      } catch (Exception& ex) {
        caught(ex);
      }
    }
    
    void transform(cv::Mat& filter, int scalingRatio)
    {
      try {
        cv::Mat newImage = originalImage.clone();
      
        //Transform the original image using the color matrix(filter). 
        cv::transform(originalImage, newImage, filter); 
        filteredImage = newImage;
      
        setScaledImage(filteredImage, scalingRatio);
      
        refresh();
      } catch (SOL::Exception& ex) {
        caught(ex);
      } 
    }
    
    void rescale(int scalingRatio)
    {
      setScaledImage(filteredImage, scalingRatio);
    }
    
  };
  //Inner classes end.
  ////////////////////////////////////////////////////////////////////////////////////////
  
  
  StringT<char>                  imageFile;
  
  SmartPtr<OriginalImageView>    originalImage;
  
  SmartPtr<TransformedImageView> filteredImage;
  
  int                            imageScalingRatio;
  SmartPtr<OpenCVScaleComboBox>  scaleComboBox;
    
  static const int               COLORS = 3;
  
  SmartPtr<OpenCVColorFilter>    colorFilter;
  
  static const int               COLOR_CONTROLS = 3;
  SmartPtr<ColorPositioner>      colorPositioner[COLOR_CONTROLS];
  
  FileDialog                     filedlg;

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

  void scaleChanged(Action& action)
  {
    imageScalingRatio = scaleComboBox->getScale();
    originalImage -> rescale(imageScalingRatio);
    filteredImage -> rescale(imageScalingRatio);
  }

  //Common colorPosition changed event handler for three colorPositioners.
  virtual long colorPositionChanged(Event& event)
  {
    //Get an index for ColorPositioners array from the event parameter. 
    int index = event.getWParam();
    ColorPositioner* positioner = (ColorPositioner*)event.getLParam();
    if (index >= 0 && index <COLOR_CONTROLS  && positioner) {
      int r, g, b;
      positioner -> getRGBColor(r, g, b);
      colorFilter-> setValue(index, r, g, b);
    }
    cv::Mat& filter = colorFilter->getFilter();
    filteredImage -> transform(filter, imageScalingRatio);    
    return 0;
  }
  
  void resize(int w, int h)
  {
    const int CPW = ColorPositioner::getDefaultWidth();
    const int PH  = ColorPositioner::getDefaultHeight();
    if (originalImage && filteredImage &&  scaleComboBox) {
      originalImage  -> reshape(2,           2,  (w-CPW)/2-1,    h-4);
      filteredImage  -> reshape((w-CPW)/2+1, 2,  (w-CPW)/2-1,    h-4);
      scaleComboBox  -> reshape(w-CPW +10,    2,   CPW-20,      40);
      colorFilter    -> reshape(w-CPW +10,    45,  CPW-20,     110);
      int y = 165;
    
      for (int i = 0; i<COLOR_CONTROLS; i++) {
        colorPositioner[i]  -> reshape(w-CPW + 1,  y+(PH+2)*i,        CPW-2,    PH);
      }
    }
  }

  void openFile(const char* filename)
  {
    try {
      originalImage -> loadImage(filename);
      filteredImage  -> loadImage(filename);
      const char* fname = strrchr(filename, '\\');
      if (fname) {
        fname++;
      }
      imageFile = fname;
      originalImage -> rescale(imageScalingRatio);
      cv::Mat& filter = colorFilter->getFilter();
      filteredImage -> transform(filter, imageScalingRatio);
    

      updateCaption();
      
    } 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 {
      imageScalingRatio = 60;
      
      imageFile = "..\\images\\flower.png";
      
      int  imageLoadingFlag = CV_LOAD_IMAGE_COLOR;
      Args ar;
      ar.set(XmNimageFileName, imageFile);
      ar.set(XmNimageLoadingFlag, imageLoadingFlag);
      ar.set(XmNimageScalingRatio, imageScalingRatio);
      originalImage = new OriginalImageView(this, "", ar); 
      
      originalImage -> addCallback(XmNdropCallback, this,
        (Callback)&MainView::dropFiles, NULL);
      
      ar.reset();
      ar.set(XmNimageFileName, imageFile);
      ar.set(XmNimageLoadingFlag, imageLoadingFlag);
      ar.set(XmNimageScalingRatio, imageScalingRatio);
      filteredImage = new TransformedImageView(this, "", ar); 
      
      const char* defaultScale = "60%";
      
      imageScalingRatio = atoi(defaultScale);
      
      ar.reset();
      ar.set(XmNdefaultScale, defaultScale);
      
      scaleComboBox = new OpenCVScaleComboBox(this, "Scale", ar);
      scaleComboBox -> addCallback(XmNselChangeCallback, this,
        (Callback)&MainView::scaleChanged, NULL);
      
      ar.reset();
      ar.set(XmNitemWidth,  60);
      ar.set(XmNitemHeight, 28);
      colorFilter = new OpenCVColorFilter(this, "ColorFilter", ar);
      colorFilter->disable();
      
      float r = 0.4 * 255.0f;
      float g = 0.4 * 255.0f;
      float b = 0.2 * 255.0f;
     
      for (int i = 0; i<COLOR_CONTROLS; i++) {
        ar.reset();
        colorPositioner[i] = new ColorPositioner(this, "Color", ar);
        colorPositioner[i]->setThumbPosition((int)r, (int)g, (int)b);
        colorPositioner[i]->setId(i);
        addEventHandler(colorPositioner[i]->getPositionChangedMessage(), this, 
            (Handler)&MainView::colorPositionChanged, NULL);
   
        colorFilter -> setValue(i, (int)r, (int)g, (int)b);
      }

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

      cv::Mat& filter = colorFilter->getFilter();
      filteredImage -> transform(filter, imageScalingRatio);
    
      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,  1000);
    args.set(XmNheight, 600);
    MainView view(applet, name, args);
    view.realize();

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


Last modified: 8 Oct. 2017

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