SOL9 Sample: ImageTransformationByColorFilter
|
1 Screenshot
2 Source code
/*
* ImageTransformationByColorFilter.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
//2017/10/01
// Simple linear filter based on cv::filter2D API.
//
// 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/LabeledTrackBar.h>
#include <sol/FileDialog.h>
#include <sol/opencv/OpenCVApplicationView.h>
#include <sol/opencv/OpenCVScaleComboBox.h>
#include <sol/opencv/OpenCVScrolledImageView.h>
namespace SOL {
class MainView :public OpenCVApplicationView {
private:
typedef enum {
GOLD = 0,
GRAY,
GREEN,
SEPIA,
VIOLET,
} FILTER;
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(FILTER filter, int scalingRatio)
{
try {
cv::Mat filters[5];
//gold
filters[GOLD] = (cv::Mat_<float>(3,3) <<
0.114f, 0.114f, 0.114f,
0.299f, 0.299f, 0.299f,
0.587f, 0.587f, 0.587f
);
//gray
filters[GRAY] = (cv::Mat_<float>(3,3) <<
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f);
//green
filters[GREEN] = (cv::Mat_<float>(3,3) <<
0.114f, 0.114f, 0.114f,
0.587f, 0.587f, 0.587f,
0.299f, 0.299f, 0.299f
);
//sepia
filters[SEPIA] = (cv::Mat_<float>(3,3) <<
0.272, 0.534, 0.131,
0.349, 0.686, 0.168,
0.393, 0.769, 0.189);
//violet
filters[VIOLET] = (cv::Mat_<float>(3,3) <<
0.587f, 0.587f, 0.587f,
0.114f, 0.114f, 0.114f,
0.299f, 0.299f, 0.299f
);
cv::Mat newImage = originalImage.clone();
cv::transform(originalImage,
newImage,
filters[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;
StringT<char> savedImageFile;
SmartPtr<OriginalImageView> originalImage;
SmartPtr<TransformedImageView> filteredImage;
int imageScalingRatio;
SmartPtr<OpenCVScaleComboBox> scaleComboBox;
FILTER filter;
SmartPtr<LabeledComboBox> filterComboBox;
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);
}
void filterChanged(Action& action)
{
filter = (FILTER)filterComboBox->getCurSel();
filteredImage -> transform(filter, imageScalingRatio);
}
void resize(int w, int h)
{
if (originalImage && filteredImage && scaleComboBox) {
originalImage -> reshape(2, 2, (w-160)/2-1, h-4);
filteredImage -> reshape((w-160)/2+1, 2, (w-160)/2-1, h-4);
scaleComboBox -> reshape(w-155 + 10, 0, 140, 50);
filterComboBox -> reshape(w-155 + 10, 70, 140, 60);
}
}
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);
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);
ar.reset();
scaleComboBox = new OpenCVScaleComboBox(this, "Scale", ar);
scaleComboBox -> addCallback(XmNselChangeCallback, this,
(Callback)&MainView::scaleChanged, NULL);
const char* items[] = {
"Gold", "Gray", "Green", "Sepia", "Violet"};
filter = SEPIA;
ar.reset();
filterComboBox = new LabeledComboBox(this, "Filter", ar);
for (int i = 0; i<CountOf(items); i++) {
filterComboBox->addString(items[i]);
}
filterComboBox->setCurSel((int)filter);
filterComboBox -> addCallback(XmNselChangeCallback, this,
(Callback)&MainView::filterChanged, NULL);
addCallback(XmNmenuCallback, IDM_EXIT, this,
(Callback)&MainView::confirm, NULL);
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);
}
}
//2017/12/01
void save(Action& action)
{
try {
if (!savedImageFile.isEmpty()) {
//Write filtered image into the filename.
filteredImage->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 filtered image into the filename.
filteredImage->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, 380);
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.