SOL9 Sample: VideoImageBlurring
|
1 Screenshot
2 Source code
/*
* VideoImageBlurring.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
//2017/07/25
//This is a simple example to capture videoDevice, and to display the capturued frames
//to original imageview and blurred image view based on cv::boxFiter.
#define _CONSOLE_
#include <sol/ModuleFileName.h>
#include <sol/DropFiles.h>
#include <sol/LabeledTrackBar.h>
#include <sol/FileDialog.h>
#include <sol/opencv/OpenCVVideoApplication.h>
#include <sol/opencv/OpenCVVideoCaptureView.h>
#include <sol/opencv/OpenCVNamedWindow.h>
namespace SOL {
class MainView :public OpenCVVideoCaptureView {
private:
////////////////////////////////////////////////////////////////////////////////////////
//Inner classes start.
class VideoImageView :public OpenCVNamedWindow {
private:
cv::Mat originalImage;
public:
void display()
{
show(originalImage);
}
public:
VideoImageView(View* parent, const char* name, Args& args)
:OpenCVNamedWindow(parent, name, args)
{
}
void setImage(cv::Mat& image)
{
originalImage = image;
}
};
class BlurredImageView :public OpenCVNamedWindow {
private:
cv::Mat originalImage;
cv::Mat blurredImage;
public:
void display()
{
show(blurredImage);
}
public:
BlurredImageView(View* parent, const char* name, Args& args)
:OpenCVNamedWindow(parent, name, args)
{
}
void setImage(cv::Mat& image)
{
originalImage = image;
}
void blur(int ksize)
{
//void boxFilter(InputArray src, OutputArray dst, int ddepth, Size ksize,
// Point anchor=Point(-1,-1),
// bool normalize=true,
// int borderType=BORDER_DEFAULT )
cv::Mat newImage = cv::Mat::zeros(originalImage.size(), originalImage.type() );
cv::boxFilter(originalImage, newImage,
originalImage.type(),
cv::Size(ksize, ksize),
cv::Point(-1, -1),
true,
BORDER_DEFAULT);
blurredImage = newImage;
}
};
//Inner classes end.
////////////////////////////////////////////////////////////////////////////////////////
SmartPtr<VideoImageView> originalImage;
SmartPtr<BlurredImageView> blurredImage;
int ksize;
SmartPtr<LabeledTrackBar> ksizeTrackBar;
//Horizontal Scroll event by TrackBars
void trackBarScrolled(Action& action)
{
ksize = ksizeTrackBar->getPosition();
ksize = (ksize/2)*2 +1;
}
void resize(int w, int h)
{
if (originalImage && blurredImage && ksizeTrackBar) {
originalImage -> reshape(2, 2, (w-160)/2-1, h-4);
blurredImage -> reshape((w-160)/2+1, 2, (w-160)/2-1, h-4);
ksizeTrackBar -> reshape(w-155 + 10, 10, 140, 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)
:OpenCVVideoCaptureView(applet, name, args)
{
try {
Args ar;
originalImage = new VideoImageView(this, "cvwindow1", ar);
ar.reset();
blurredImage = new BlurredImageView(this, "cvwindow2", ar);
ksize = 10;
ar.reset();
ar.set(XmNminimum, 0);
ar.set(XmNmaximum, 31);
ar.set(XmNposition, 10);
ksizeTrackBar = new LabeledTrackBar(this, "KernelSize", ar);
ksizeTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
(Callback)&MainView::trackBarScrolled, NULL);
addCallback(XmNmenuCallback, IDM_EXIT, this,
(Callback)&MainView::confirm, NULL);
} catch (Exception& ex) {
caught(ex);
}
}
~MainView()
{
}
//This method will be called from a OpenCVTimerThread::run.
virtual void render()
{
cv::Mat frame;
if (readVideoFrame(frame)) {
if (!frame.empty() && originalImage && blurredImage) {
originalImage -> setImage(frame);
originalImage -> display();
blurredImage -> setImage(frame);
blurredImage -> blur(ksize);
blurredImage -> display();
}
}
}
};
}
void main(int argc, char** argv)
{
try {
ModuleFileName module(argv[0]);
const char* name = module.getAppName();
OpenCVVideoApplication applet(name, argc, argv);
Args args;
args.set(XmNwidth, 900);
args.set(XmNheight, 380);
args.set(XmNvideoDeviceIndex, 0);
args.set(XmNfps, 20);
args.set(XmNcaptureAutoStart, true);
MainView view(applet, name, args);
view.realize();
applet.run(view);
} catch (SOL::Exception& ex) {
caught(ex);
}
}
Last modified: 2 Dec. 2017
Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.