SOL9 Sample: AdaptiveThresholdingCannyEdgeDetector
|
1 Screenshot
2 Source code
/*
* AdaptiveThresholdingCannyEdgeDetector.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
//2017/04/12
//2017/12/01 Updated DetectedImageView::detect method.
//2017/12/01 Added save method to MainView.
// See: http://docs.opencv.org/3.2.0/d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57
// See:
/*
void cvAdaptiveThreshold(const CvArr* src, CvArr* dst,
double max_value, int adaptive_method=CV_ADAPTIVE_THRESH_MEAN_C,
int threshold_type=CV_THRESH_BINARY, int block_size=3, double param1=5 )
Parameters:
src : Source 8-bit single-channel image.
dst : Destination image of the same size and the same type as src .
maxValue : Non-zero value assigned to the pixels for which the condition is satisfied. See the details below.
adaptiveMethod : Adaptive thresholding algorithm to use, ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C
thresholdType : Thresholding type that must be either THRESH_BINARY or THRESH_BINARY_INV .
blockSize : Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
C : Constant subtracted from the mean or weighted mean. Normally,
it is positive but may be zero or negative as well.
*/
#define _CONSOLE_
#include <sol/ModuleFileName.h>
#include <sol/DropFiles.h>
#include <sol/Pair.h>
#include <sol/LabeledTrackBar.h>
#include <sol/LabeledComboBox.h>
#include <sol/FileDialog.h>
#include <sol/opencv/OpenCVApplicationView.h>
#include <sol/opencv/OpenCVImageView.h>
#include <sol/opencv/OpenCVImageView.h>
namespace SOL {
class MainView :public OpenCVApplicationView {
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 DetectedImageView :public OpenCVImageView {
private:
cv::Mat originalImage;
cv::Mat adaptedImage;
cv::Mat grayImage;
cv::Mat detectedImage;
static const int MAX_PIXEL_VALUE = 255;
static const int C = 9; //Constant subtracted from the mean or weighted mean
cv::Mat& getMat()
{
return detectedImage;
}
void display()
{
show(detectedImage);
}
public:
DetectedImageView(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);
}
}
~DetectedImageView()
{
}
void loadImage(const char* filename, int imageLoadingFlag= CV_LOAD_IMAGE_COLOR)
{
try {
originalImage = readImage(filename, imageLoadingFlag);
cv::cvtColor(originalImage, grayImage, COLOR_BGR2GRAY);
detectedImage = grayImage.clone();
refresh();
} catch (Exception& ex) {
caught(ex);
}
}
void detect(int adaptiveMethod, int thresholdType, int blockSize, int threshold1, int threshold2)
{
try {
blockSize = (blockSize/2)*2 + 1;
if (blockSize <3) {
blockSize=3;
}
//1 Convert the originalImage to a grayImage.
cv::cvtColor(originalImage, grayImage, COLOR_BGR2GRAY);
//2 Apply adaptiveThreshold to the grayImage.
cv::adaptiveThreshold( grayImage, adaptedImage, MAX_PIXEL_VALUE,
adaptiveMethod, thresholdType, blockSize, (double)C);
//3 Apply CannyEdgeDetector to the adaptedImage.
cv::Canny(adaptedImage, detectedImage, (double)threshold1, (double)threshold2);
} catch (cv::Exception& ex) {
//Sometimes we get an exception; I don't know the reason why it happens.
;//MessageBox(NULL, "Failed to adaptiveThreshold", "Error", MB_OK);
}
refresh();
}
};
//Inner classes end.
////////////////////////////////////////////////////////////////////////////////////////
StringT<char> imageFile;
StringT<char> savedImageFile;
SmartPtr<OriginalImageView> originalImage;
SmartPtr<DetectedImageView> detectedImage;
int adaptiveMethodIndex;
SmartPtr<LabeledComboBox> adaptiveMethodComboBox;
int thresholdTypeIndex;
SmartPtr<LabeledComboBox> thresholdTypeComboBox;
static const int BLOCK_SIZE_MIN = 3;
static const int BLOCK_SIZE_MAX = 43;
int blockSize;
SmartPtr<LabeledTrackBar> blockSizeTrackBar;
static const int THRESHOLD1_MAX = 300;
int threshold1;
SmartPtr<LabeledTrackBar> threshold1TrackBar;
static const int THRESHOLD2_MAX = 300;
int threshold2;
SmartPtr<LabeledTrackBar> threshold2TrackBar;
FileDialog filedlg;
static int getAdaptiveMethod(int index)
{
int n = 0;
//We don't include the THRESH_OTSU type
Pair<int, int> methods[] = {
{n++, ADAPTIVE_THRESH_MEAN_C},
{n++, ADAPTIVE_THRESH_GAUSSIAN_C},
};
int method = ADAPTIVE_THRESH_MEAN_C;
if (index >= 0 && index <n) {
method = methods[index].second;
}
return method;
}
static int getThresholdType(int index)
{
int n = 0;
//We don't include the THRESH_OTSU type
Pair<int, int> types[] = {
{n++, cv::THRESH_BINARY },
{n++, cv::THRESH_BINARY_INV },
};
int type = THRESH_BINARY;
if (index >= 0 && index <n) {
type = types[index].second;
}
return type;
}
void updateCaption()
{
char caption[MAX_PATH];
sprintf_s(caption, CountOf(caption), "%s - %s",
(const char*)imageFile,
getAppName() );
setText(caption);
}
void detectEdge()
{
adaptiveMethodIndex = adaptiveMethodComboBox -> getCurSel();
thresholdTypeIndex = thresholdTypeComboBox -> getCurSel();
blockSize = blockSizeTrackBar -> getPosition();
threshold1 = threshold1TrackBar->getPosition();
threshold2 = threshold2TrackBar->getPosition();
detectedImage -> detect( getAdaptiveMethod(adaptiveMethodIndex),
getThresholdType(thresholdTypeIndex),
blockSize,
threshold1,
threshold2);
}
void adaptiveMethodSelChanged(Action& event)
{
detectEdge();
}
void thresholdTypeSelChanged(Action& event)
{
detectEdge();
}
void commonTrackBarCallback(Action& event)
{
detectEdge();
}
void resize(int w, int h)
{
if (originalImage && detectedImage) {
originalImage -> reshape( 2, 2, (w-190)/2-4, h-4);
detectedImage -> reshape((w-190)/2+1, 2, (w-190)/2-4, h-4);
adaptiveMethodComboBox -> reshape((w-190) +1, 2, 180, 50);
thresholdTypeComboBox -> reshape((w-190) +1, 10+70, 180, 50);
blockSizeTrackBar -> reshape((w-190)+1, 10+70*2, 180, 50);
threshold1TrackBar -> reshape((w-190) + 10, 10+70*3, 180, 70);
threshold2TrackBar -> reshape((w-190) + 10, 10+70*4, 180, 70);
}
}
void openFile(const char* filename)
{
try {
originalImage -> loadImage(filename);
detectedImage -> loadImage(filename);
const char* fname = strrchr(filename, '\\');
if (fname) {
fname++;
}
imageFile = fname;
updateCaption();
detectEdge();
savedImageFile = "";
} catch (Exception& ex) {
caught(ex);
}
}
void dropFiles(Action& action)
{
char fileName[MAX_PATH] = { 0 };
DropFiles drop((HDROP)action.getWParam());
//fileName[0] = ZERO;
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\\SuperCar2.png";
int imageLoadingFlag = CV_LOAD_IMAGE_COLOR;
Args ar;
ar.set(XmNimageFileName, imageFile);
ar.set(XmNimageLoadingFlag, imageLoadingFlag);
originalImage = new OriginalImageView(this, "", ar); //You don't need specify windows name parameter
originalImage -> addCallback(XmNdropCallback, this,
(Callback)&MainView::dropFiles, NULL);
ar.reset();
ar.set(XmNimageFileName, imageFile);
ar.set(XmNimageLoadingFlag, imageLoadingFlag);
detectedImage = new DetectedImageView(this, "", ar); //You don't need specify windows name parameter
//Create a adaptiveMethodComboBox
ar.reset();
adaptiveMethodComboBox = new LabeledComboBox(this, "ThresholdType", ar);
const char* methods[] = {
"Adaptive_Thresh Mean_C",
"Adaptive_Thresh_Gaussian_C",
};
for (int i = 0; i<CountOf(methods); i++) {
adaptiveMethodComboBox -> addString(methods[i]);
}
adaptiveMethodIndex = 0;
adaptiveMethodComboBox -> setCurSel(adaptiveMethodIndex);
//Add a selChanged callback.
adaptiveMethodComboBox -> addCallback(XmNselChangeCallback, this,
(Callback)&MainView::adaptiveMethodSelChanged, NULL);
//
//Create a thresholdTypeComboBox
ar.reset();
thresholdTypeComboBox = new LabeledComboBox(this, "ThresholdType", ar);
const char* types[] = {
"Binary",
"Binary Inverted",
};
for (int i = 0; i<CountOf(types); i++) {
thresholdTypeComboBox -> addString(types[i]);
}
thresholdTypeIndex = 0;
thresholdTypeComboBox -> setCurSel(thresholdTypeIndex);
//Add a selChanged callback.
thresholdTypeComboBox -> addCallback(XmNselChangeCallback, this,
(Callback)&MainView::thresholdTypeSelChanged, NULL);
//Create a blockSizeTrackBar
blockSize = 7;
ar.reset();
ar.set(XmNminimum, BLOCK_SIZE_MIN);
ar.set(XmNmaximum, BLOCK_SIZE_MAX);
ar.set(XmNposition, blockSize);
ar.set(XmNdisplayOddValue, TRUE);
blockSizeTrackBar = new LabeledTrackBar(this, "BlockValue", ar);
blockSizeTrackBar -> addCallback(XmNtrackBarScrollCallback, this,
(Callback)&MainView::commonTrackBarCallback, NULL);
threshold1 = 43;
threshold2 = 110;
ar.reset();
ar.set(XmNminimum, 0);
ar.set(XmNmaximum, THRESHOLD1_MAX);
ar.set(XmNposition, threshold1);
ar.set(XmNdisplayOddValue, false);
threshold1TrackBar = new LabeledTrackBar(this, "Canny Threshold1", ar);
threshold1TrackBar -> addCallback(XmNtrackBarScrollCallback, this,
(Callback)&MainView::commonTrackBarCallback, NULL);
ar.reset();
ar.set(XmNminimum, 0);
ar.set(XmNmaximum, THRESHOLD2_MAX);
ar.set(XmNposition, threshold2);
ar.set(XmNdisplayOddValue, false);
threshold2TrackBar = new LabeledTrackBar(this, "Canny Threshold2", ar);
threshold2TrackBar -> addCallback(XmNtrackBarScrollCallback, this,
(Callback)&MainView::commonTrackBarCallback, NULL);
addCallback(XmNmenuCallback, IDM_EXIT, this,
(Callback)&MainView::confirm, NULL);
detectEdge();
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);
}
}
void save(Action& action)
{
try {
if (!savedImageFile.isEmpty()) {
//Write detected image into the filename.
detectedImage->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.
detectedImage->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, 360);
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.