SOL9 Sample: SolImageMorphing
|
1 Screenshot
2 Source code
/*
* SolImageMorphing.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
//2017/04/10
//This is a simple example to use cv::morphologyEx.
// See: http://docs.opencv.org/3.2.0/db/df6/tutorial_erosion_dilatation.html
//2017/12/01 Added save, saveAs methods to MainView.
#define _CONSOLE_
#include <sol/ModuleFileName.h>
#include <sol/DropFiles.h>
#include <sol/LabeledTrackBar.h>
#include <sol/LabeledComboBox.h>
#include <sol/FileDialog.h>
#include <sol/Pair.h>
#include <sol/opencv/OpenCVApplicationView.h>
#include <sol/opencv/OpenCVImageView.h>
namespace SOL {
class MainView :public OpenCVApplicationView {
private:
private:
////////////////////////////////////////////////////////////////////////////////////////
//Inner classes start.
class OriginalImageView :public OpenCVImageView {
private:
cv::Mat originalImage;
//This is a mandatory method, because in parent class it's declared
//as a pure virtual function.
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 TransformedImageView :public OpenCVImageView {
private:
cv::Mat originalImage;
cv::Mat transformedImage;
cv::Mat destImage;
cv::Mat& getMat()
{
return transformedImage;
}
void display()
{
show(transformedImage);
}
public:
TransformedImageView(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);
}
}
~TransformedImageView()
{
}
void loadImage(const char* filename, int imageLoadingFlag= CV_LOAD_IMAGE_COLOR)
{
try {
originalImage = readImage(filename, imageLoadingFlag);
transformedImage = originalImage.clone();
refresh();
} catch (Exception& ex) {
caught(ex);
}
}
void transform(cv::MorphShapes morphShape, cv::MorphTypes morphType, int ksize)
{
cv::Mat element = getStructuringElement(morphShape,
cv::Size( (ksize/2)*2 +1, (ksize/2)*2 +1),
cv::Point( -1, -1) );
cv::morphologyEx(originalImage, destImage, morphType, element);
if (destImage.empty()) {
transformedImage = originalImage.clone();
refresh();
MessageBox(NULL, "Failed to cv::morphologyEx", "Fatal Error", MB_OK);
} else {
transformedImage = destImage;
refresh();
}
}
};
//Inner classes end.
////////////////////////////////////////////////////////////////////////////////////////
StringT<char> imageFile;
StringT<char> savedImageFile;
SmartPtr<OriginalImageView> originalImage;
SmartPtr<TransformedImageView> transformedImage;
int morphShape;
SmartPtr<LabeledComboBox> morphShapeComboBox;
int morphType;
SmartPtr<LabeledComboBox> morphTypeComboBox;
int ksize;
int maxKernelSize;
SmartPtr<LabeledTrackBar> ksizeTrackBar;
FileDialog filedlg;
void updateCaption()
{
char caption[MAX_PATH];
sprintf_s(caption, CountOf(caption), "%s - %s",
(const char*)imageFile,
getAppName());
setText(caption);
}
cv::MorphShapes getMorphShape()
{
int n = 0;
Pair<int, cv::MorphShapes> shapes[] = {
{n++, MORPH_RECT},
{n++, MORPH_CROSS},
{n++, MORPH_ELLIPSE}};
cv::MorphShapes shape = MORPH_RECT;
for (int i = 0; i<CountOf(shapes); i++) {
if (shapes[i].first == morphShape) {
shape = shapes[i].second;
break;
}
}
return shape;
}
cv::MorphTypes getMorphType()
{
int n = 0;
Pair<int, cv::MorphTypes> types[] = {
{n++, MORPH_OPEN},
{n++, MORPH_CLOSE},
{n++, MORPH_GRADIENT},
{n++, MORPH_TOPHAT},
{n++, MORPH_BLACKHAT}}
;
cv::MorphTypes type = MORPH_OPEN;
for (int i = 0; i<CountOf(types); i++) {
if (types[i].first == morphType) {
type = types[i].second;
break;
}
}
return type;
}
void selMorphShapeChanged(Action& event)
{
morphShape = morphShapeComboBox -> getCurSel();
transformedImage -> transform(getMorphShape(), getMorphType(), ksize);
}
void selMorphTypeChanged(Action& event)
{
morphType = morphTypeComboBox -> getCurSel();
transformedImage -> transform(getMorphShape(), getMorphType(), ksize);
}
//Horizontal Scroll event by a horizontal TrackBar
void trackBarScrolled(Action& action)
{
ksize = ksizeTrackBar->getPosition();
transformedImage -> transform(getMorphShape(), getMorphType(), ksize);
}
void resize(int w, int h)
{
if (originalImage && transformedImage && ksizeTrackBar) {
originalImage -> reshape(2, 2, (w-170)/2-1, h-4);
transformedImage -> reshape((w-170)/2+1, 2, (w-170)/2-1, h-4);
morphShapeComboBox -> reshape(w-160 , 2, 155, 50);
morphTypeComboBox -> reshape(w-160 , 60, 155, 50);
ksizeTrackBar -> reshape(w-160 , 130, 155, 60);
}
}
void openFile(const char* filename)
{
try {
originalImage -> loadImage(filename);
transformedImage -> loadImage(filename);
const char* fname = strrchr(filename, '\\');
if (fname) {
fname++;
}
imageFile = fname;
transformedImage -> transform(getMorphShape(), getMorphType(), ksize);
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 {
imageFile = "..\\images\\HelloWorld.png";
int imageLoadingFlag = CV_LOAD_IMAGE_COLOR;
Args ar;
ar.set(XmNimageFileName, imageFile);
ar.set(XmNimageLoadingFlag, imageLoadingFlag);
originalImage = new OriginalImageView(this, "", ar);
originalImage -> addCallback(XmNdropCallback, this,
(Callback)&MainView::dropFiles, NULL);
ar.reset();
ar.set(XmNimageFileName, imageFile);
ar.set(XmNimageLoadingFlag, imageLoadingFlag);
transformedImage = new TransformedImageView(this, "", ar);
ar.reset();
morphShapeComboBox = new LabeledComboBox(this, "MorphShape", ar);
const char* shapes[] = {
"MORPH_RECT", //0
"MORPH_CROSS", //1
"MORPH_ELLIPSE" //2
};
for (int i = 0; i<CountOf(shapes); i++) {
morphShapeComboBox -> addString(shapes[i]);
}
morphShape = 2;
morphShapeComboBox -> setCurSel(morphShape);
morphShapeComboBox -> addCallback(XmNselChangeCallback, this,
(Callback)&MainView::selMorphShapeChanged, NULL);
ar.reset();
morphTypeComboBox = new LabeledComboBox(this, "MorphType", ar);
const char* types[] = {
"MORPH_OPEN",
"MORPH_CLOSE",
"MORPH_GRADIENT",
"MORPH_TOPHAT",
"MORPH_BLACKHAT",
};
for (int i = 0; i<CountOf(types); i++) {
morphTypeComboBox -> addString(types[i]);
}
morphType = 2;
morphTypeComboBox -> setCurSel(morphType);
morphTypeComboBox -> addCallback(XmNselChangeCallback, this,
(Callback)&MainView::selMorphTypeChanged, NULL);
maxKernelSize = 31;
ksize = 4; //starting kernelSize
ar.reset();
ar.set(XmNminimum, 0);
ar.set(XmNmaximum, 31);
ar.set(XmNposition, ksize);
ksizeTrackBar = new LabeledTrackBar(this, "KernelSize", ar);
ksizeTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
(Callback)&MainView::trackBarScrolled, NULL);
addCallback(XmNmenuCallback, IDM_EXIT, this,
(Callback)&MainView::confirm, NULL);
transformedImage -> transform(getMorphShape(), getMorphType(), ksize);
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 transformed image into the filename.
transformedImage->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 transformed image into the filename.
transformedImage->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, 1000);
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.