SOL9 Sample: BilateralFilter
|
1 Screenshot
2 Source code
/*
* BilateralFilter.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
//2017/09/25
// See: "http://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html"
//2017/12/01 Added save, saveAs methods to MainView.
#define _CONSOLE_
#include <sol/ModuleFileName.h>
#include <sol/LabeledTrackBar.h>
#include <sol/FileDialog.h>
#include <sol/opencv/OpenCVApplicationView.h>
#include <sol/opencv/OpenCVNamedWindow.h>
namespace SOL {
class MainView :public OpenCVApplicationView {
private:
////////////////////////////////////////////////////////////////////////////////////////
//Inner classes start.
class OriginalImageView :public OpenCVNamedWindow {
private:
cv::Mat originalImage;
void display()
{
show(originalImage);
}
public:
OriginalImageView(View* parent, const char* name, Args& args)
:OpenCVNamedWindow(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 BlurredImageView :public OpenCVNamedWindow {
private:
cv::Mat originalImage;
cv::Mat blurredImage;
void display()
{
show(blurredImage);
}
public:
BlurredImageView(View* parent, const char* name, Args& args)
:OpenCVNamedWindow(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);
}
}
~BlurredImageView()
{
}
void loadImage(const char* filename, int imageLoadingFlag= CV_LOAD_IMAGE_COLOR)
{
try {
originalImage = readImage(filename, imageLoadingFlag);
blurredImage = originalImage;
} catch (Exception& ex) {
caught(ex);
}
}
void blur(int diameter, int sigmaColor, int sigmaSpace)
{
//void bilateralFilter(InputArray src, OutputArray dst, int diameter,
// double sigmaColor,
// double sigmaSpace,
// int borderType=BORDER_DEFAULT )
cv::Mat newImage = cv::Mat::zeros(originalImage.size(), originalImage.type() );
cv::bilateralFilter(originalImage, newImage, diameter,
(double)sigmaColor,
(double)sigmaSpace,
BORDER_DEFAULT);
blurredImage = newImage;
refresh();
}
void writeImage(const char* filename)
{
OpenCVNamedWindow::writeImage(filename, blurredImage);
}
};
//Inner classes end.
////////////////////////////////////////////////////////////////////////////////////////
void updateCaption()
{
char caption[1024];
sprintf_s(caption, CountOf(caption), "%s - %s", (const char*)imageFile,
getAppName() );
setText(caption);
}
StringT<char> imageFile;
StringT<char> savedImageFile;
SmartPtr<OriginalImageView> originalImage;
SmartPtr<BlurredImageView> blurredImage;
int diameter;
SmartPtr<LabeledTrackBar> diameterTrackBar;
int sigmaColor;
SmartPtr<LabeledTrackBar> sigmaColorTrackBar;
int sigmaSpace;
SmartPtr<LabeledTrackBar> sigmaSpaceTrackBar;
FileDialog filedlg;
Profile profile;
//Common callback for diameterTrackBar, sigmaColorTrackBar, sigmaSpaceTrackBar
void trackBarScrolled(Action& action)
{
diameter = diameterTrackBar ->getPosition();
sigmaColor = sigmaColorTrackBar->getPosition();
sigmaSpace = sigmaSpaceTrackBar->getPosition();
blurredImage -> blur(diameter, sigmaColor, sigmaSpace);
}
void resize(int w, int h)
{
int CPW = 180;
if (originalImage && blurredImage &&
diameterTrackBar && sigmaColorTrackBar && sigmaSpaceTrackBar) {
originalImage -> reshape(2, 2, (w-CPW)/2-1, h-4);
blurredImage -> reshape((w-CPW)/2+1, 2, (w-CPW)/2-1, h-4);
diameterTrackBar -> reshape(w-CPW + 2, 40, CPW-4, 60);
sigmaColorTrackBar -> reshape(w-CPW + 2, 140, CPW-4, 60);
sigmaSpaceTrackBar -> reshape(w-CPW + 2, 240, CPW-4, 60);
}
}
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\\MeshedNioh.png";
int imageLoadingFlag = CV_LOAD_IMAGE_COLOR;
Args ar;
ar.set(XmNimageFileName, imageFile);
ar.set(XmNimageLoadingFlag, imageLoadingFlag);
originalImage = new OriginalImageView(this, "cvwindow1", ar);
ar.reset();
ar.set(XmNimageFileName, imageFile);
ar.set(XmNimageLoadingFlag, imageLoadingFlag);
blurredImage = new BlurredImageView(this, "cvwindow2", ar);
diameter = 4;
sigmaColor = 160;
sigmaSpace = 40;
ar.reset();
ar.set(XmNminimum, 0);
ar.set(XmNmaximum, 5);
ar.set(XmNposition, diameter);
diameterTrackBar = new LabeledTrackBar(this, "Diameter", ar);
diameterTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
(Callback)&MainView::trackBarScrolled, NULL);
ar.reset();
ar.set(XmNminimum, 0);
ar.set(XmNmaximum, 255);
ar.set(XmNposition, sigmaColor);
sigmaColorTrackBar = new LabeledTrackBar(this, "SigmaColor", ar);
sigmaColorTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
(Callback)&MainView::trackBarScrolled, NULL);
ar.reset();
ar.set(XmNminimum, 0);
ar.set(XmNmaximum, 255);
ar.set(XmNposition, sigmaSpace);
sigmaSpaceTrackBar = new LabeledTrackBar(this, "SigmaSpace", ar);
sigmaSpaceTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
(Callback)&MainView::trackBarScrolled, NULL);
addCallback(XmNmenuCallback, IDM_EXIT, this,
(Callback)&MainView::confirm, NULL);
blurredImage -> blur(diameter, sigmaColor, sigmaSpace);
updateCaption();
ar.reset();
ar.set(XmNfilter, FileDialog::getImageFilesFilter());
filedlg.create(this, "OpenFile", ar);
} catch (Exception& ex) {
caught(ex);
}
}
~MainView()
{
}
void saveFileFolder(const char* filePath)
{
if (strlen(filePath)>0) {
Folder folder(filePath);
String dirOnly = "";
folder.getDirectory(dirOnly);
const char* dir = (const char*)dirOnly;
profile.setFileFolder(dir);
}
}
void open(Action& action)
{
Args ar;
char dir[MAX_PATH] = { 0 };
if (profile.getFileFolder(dir, CountOf(dir))) {
ar.set(XmNdirectory, dir);
filedlg.setValues(ar);
}
try {
if(filedlg.open()) {
const char* filename = filedlg.getFileName();
saveFileFolder(filename);
originalImage -> loadImage(filename);
blurredImage -> loadImage(filename);
const char* fname = strrchr(filename, '\\');
if (fname) {
fname++;
}
imageFile = fname;
post(WM_HSCROLL, 0, 0);
}
} catch (Exception& ex) {
caught(ex);
}
}
//2017/12/01
void save(Action& action)
{
try {
if (!savedImageFile.isEmpty()) {
//Write detected image into the filename.
blurredImage->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 detected image into the filename.
blurredImage->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.