//
//ImageTransformationByDynamicColorFilter.cpp
//Copyright (c) 2017 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
//
//2017/10/10
#include <oz++/motif/Label.h>
#include <oz++/motif/RowColumn.h>
#include <oz++/motif/LabeledTrackBar.h>
#include <oz++/motif/ColorPositioner.h>
#include <oz++/opencv/OpenCVScaleComboBox.h>
#include <oz++/opencv/OpenCVMainView.h>
#include <oz++/opencv/OpenCVColorFilter.h>
#include <oz++/opencv/OpenCVImageView.h>
#include <oz++/motif/FileOpenDialog.h>
namespace OZ {
class MainView :public OpenCVMainView {
private:
///////////////////////////////////////////////
//Inner classes start.
class OriginalImageView: public OpenCVImageView {
private:
cv::Mat originalImage;
cv::Mat scaledImage;
virtual void display()
{
show(scaledImage);
}
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);
int scalingRatio = (int)args.get(XmNimageScalingRatio);
loadImage(filename, imageLoadingFlag, scalingRatio);
} catch (OZ::Exception ex) {
caught(ex);
}
}
~OriginalImageView()
{
}
void loadImage(const char* filename,
int imageLoadingFlag= CV_LOAD_IMAGE_COLOR,
int scalingRatio=100)
{
originalImage = readImage(filename, imageLoadingFlag);
scaleImage(originalImage, scaledImage, scalingRatio);
}
void rescale(int scalingRatio)
{
scaledImage.release();
scaleImage(originalImage, scaledImage, scalingRatio);
}
};
class TransformedImageView: public OpenCVImageView {
private:
cv::Mat originalImage;
cv::Mat filteredImage;
cv::Mat scaledImage;
//The scale image is displayed on this image view.
virtual void display()
{
show(scaledImage);
}
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);
int scalingRatio = (int)args.get(XmNimageScalingRatio);
loadImage(filename, imageLoadingFlag, scalingRatio);
} catch (OZ::Exception ex) {
caught(ex);
}
}
~TransformedImageView()
{
}
void loadImage(const char* filename,
int imageLoadingFlag= CV_LOAD_IMAGE_COLOR,
int scalingRatio=100)
{
originalImage = readImage(filename, imageLoadingFlag);
filteredImage = originalImage.clone();
}
void rescale(int scalingRatio)
{
scaledImage.release();
scaleImage(filteredImage, scaledImage, scalingRatio);
}
void transform(cv::Mat& filter, int scalingRatio)
{
cv::Mat newImage = originalImage.clone();
cv::transform(originalImage,
newImage,
filter);
filteredImage = newImage;
//The transformedImage is scaled in the following way, and the scaled image
//is displayed in this image view.
scaledImage.release();
scaleImage(filteredImage, scaledImage, scalingRatio);
}
};
//Inner classes end.
private:
StringT<char> imageFile;
int imageLoadingFlag;
int imageScalingRatio; //percentage
SmartPtr<Label> label;
SmartPtr<OriginalImageView> originalImage;
SmartPtr<TransformedImageView> transformedImage;
SmartPtr<RowColumn> controlPane;
SmartPtr<OpenCVScaleComboBox> scaleComboBox;
static const int COLORS = 3;
SmartPtr<OpenCVColorFilter> colorFilter;
static const int COLOR_CONTROLS = 3;
SmartPtr<ColorPositioner> colorPositioner[COLOR_CONTROLS];
SmartPtr<FileOpenDialog> fileDialog;
public:
void scaleChanged(Action& action)
{
int val = scaleComboBox->getScale();
if (val > 0 && imageScalingRatio != val) {
imageScalingRatio = val;
originalImage -> rescale(imageScalingRatio);
transformedImage -> rescale(imageScalingRatio);
}
}
void fileOpen(Action& action)
{
fileDialog->popup();
}
void updateLabel(const char* filename)
{
CompoundString cs(filename);
label->set(XmNlabelString, cs);
}
void ok(Action& action)
{
try {
imageFile = fileDialog->getFileName();
const char* filename = (const char*)imageFile;
printf("filename: %s\n", filename);
fileDialog->popdown();
originalImage->invalidate();
originalImage->loadImage(filename,
imageLoadingFlag, imageScalingRatio);
originalImage->invalidate();
transformedImage ->loadImage(filename,
imageLoadingFlag, imageScalingRatio);
cv::Mat& filter = colorFilter->getFilter();
transformedImage -> transform(filter, imageScalingRatio);
updateLabel(imageFile);
resize(width(), height());
flush();
} catch (OZ::Exception& ex) {
caught(ex);
}
}
void resize(Dimension w, Dimension h)
{
int CP_WIDTH = 270;
int LB_HEIGHT = 30;
int ww = w-CP_WIDTH;
int hh = h - LB_HEIGHT;
if (label && originalImage && transformedImage && controlPane
) {
label -> reshape(0, 0, w, LB_HEIGHT);
originalImage-> reshape(0, LB_HEIGHT, ww/2-1, hh);
transformedImage -> reshape(ww/2, LB_HEIGHT, ww/2-1, hh);
controlPane -> reshape(ww-1, LB_HEIGHT, CP_WIDTH+1, hh);
//The following two lines are a workaround to erase garbage.
controlPane -> unmap();
controlPane -> map();
}
flush();
}
void positionerChanged(Action& action)
{
int i = action.getClientIntData();
int r, g, b;
colorPositioner[i]->getPosition(r, g, b);
colorFilter -> setValue(i, r, g, b);
cv::Mat& filter = colorFilter->getFilter();
transformedImage -> transform(filter, imageScalingRatio);
}
public:
MainView(OpenCVApplication& applet, const char* name, Args& args)
:OpenCVMainView(applet, name, args)
{
BulletinBoard* bboard = getBulletinBoard();
imageFile = "../images/flower.png";
imageLoadingFlag = CV_LOAD_IMAGE_COLOR;
imageScalingRatio = 60; //%
try {
Args ar;
ar.set(XmNalignment, XmALIGNMENT_BEGINNING);
label = new Label(bboard, "", ar);
updateLabel(imageFile);
ar.reset();
ar.set(XmNimageFileName, (const char*)imageFile);
ar.set(XmNimageLoadingFlag, imageLoadingFlag);
ar.set(XmNimageScalingRatio, imageScalingRatio);
originalImage = new OriginalImageView(bboard, "", ar);
ar.reset();
ar.set(XmNimageFileName, (const char*)imageFile);
ar.set(XmNimageLoadingFlag, imageLoadingFlag);
ar.set(XmNimageScalingRatio, imageScalingRatio);
transformedImage = new TransformedImageView(bboard, "", ar);
ar.reset();
controlPane = new RowColumn(bboard, "", ar);
const char* defaultScale= "60%";
ar.reset();
CompoundString scaler("Scale");
ar.set(XmNlabelString, scaler);
ar.set(XmNdefaultScale, defaultScale);
scaleComboBox = new OpenCVScaleComboBox(controlPane, "", ar);
scaleComboBox->addCallback(XmNselectionCallback, this,
(Callback)&MainView::scaleChanged, NULL);
CompoundString cscf("ColorFilter");
ar.reset();
ar.set(XmNlabelString, cscf);
ar.set(XmNitemWidth, 80);
ar.set(XmNitemHeight, 26);
colorFilter = new OpenCVColorFilter(controlPane, "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(controlPane, "Color", ar);
colorPositioner[i]->setPosition((int)r, (int)g, (int)b);
colorPositioner[i]->setId(i);
colorPositioner[i]->addCallback(XmNpositionerChanged,
(Callback)&MainView::positionerChanged, this, to_voidptr(i));
colorFilter -> setValue(i, (int)r, (int)g, (int)b);
}
cv::Mat& filter = colorFilter->getFilter();
transformedImage -> transform(filter, imageScalingRatio);
ar.reset();
fileDialog = new FileOpenDialog(this, "FileOpenDialog", ar);
fileDialog -> getOkButton()
-> addCallback(XmNactivateCallback, this,
(Callback)&MainView::ok, NULL);
sendConfigureEvent();
} catch(OZ::Exception& ex) {
caught(ex);
}
}
~MainView()
{
}
};
}
//
int main(int argc, char** argv)
{
try {
const char* appclass = argv[0];
OpenCVApplication applet(appclass, argc, argv);
Args args;
args.set(XmNwidth, 1000);
args.set(XmNheight, 540);
MainView view(applet, argv[0], args);
view.realize();
applet.run();
} catch (OZ::Exception& ex) {
caught(ex);
}
return 0;
}
|