opencv/samples/gpu/morphology.cpp

187 lines
4.1 KiB
C++
Raw Normal View History

2013-04-29 20:03:16 +08:00
#include <iostream>
2012-10-17 07:18:30 +08:00
2013-04-29 20:03:16 +08:00
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
2013-07-23 20:24:55 +08:00
#include "opencv2/cudafilters.hpp"
2013-07-23 20:59:34 +08:00
#include "opencv2/cudaimgproc.hpp"
2012-10-17 07:18:30 +08:00
using namespace std;
2012-10-17 07:18:30 +08:00
using namespace cv;
2013-04-29 20:03:16 +08:00
class App
2012-10-17 07:18:30 +08:00
{
2013-04-29 20:03:16 +08:00
public:
App(int argc, const char* argv[]);
2012-10-17 07:18:30 +08:00
2013-04-29 20:03:16 +08:00
int run();
2012-10-17 07:18:30 +08:00
2013-04-29 20:03:16 +08:00
private:
void help();
2012-10-17 07:18:30 +08:00
2013-04-29 20:03:16 +08:00
void OpenClose();
void ErodeDilate();
2012-10-17 07:18:30 +08:00
2013-04-29 20:03:16 +08:00
static void OpenCloseCallback(int, void*);
static void ErodeDilateCallback(int, void*);
2012-10-17 07:18:30 +08:00
2013-08-28 19:45:13 +08:00
cuda::GpuMat src, dst;
2012-10-17 07:18:30 +08:00
2013-04-29 20:03:16 +08:00
int element_shape;
2012-10-17 07:18:30 +08:00
2013-04-29 20:03:16 +08:00
int max_iters;
int open_close_pos;
int erode_dilate_pos;
};
2012-10-17 07:18:30 +08:00
2013-04-29 20:03:16 +08:00
App::App(int argc, const char* argv[])
2012-10-17 07:18:30 +08:00
{
2013-04-29 20:03:16 +08:00
element_shape = MORPH_RECT;
open_close_pos = erode_dilate_pos = max_iters = 10;
2012-10-17 07:18:30 +08:00
2013-04-29 20:03:16 +08:00
if (argc == 2 && String(argv[1]) == "--help")
2012-10-17 07:18:30 +08:00
{
help();
2013-04-29 20:03:16 +08:00
exit(0);
2012-10-17 07:18:30 +08:00
}
2014-09-13 22:28:41 +08:00
String filename = argc == 2 ? argv[1] : "../data/baboon.jpg";
2012-10-17 07:18:30 +08:00
2013-04-29 20:03:16 +08:00
Mat img = imread(filename);
if (img.empty())
{
cerr << "Can't open image " << filename.c_str() << endl;
exit(-1);
}
2012-10-17 07:18:30 +08:00
2013-04-29 20:03:16 +08:00
src.upload(img);
2012-10-17 07:18:30 +08:00
if (src.channels() == 3)
{
// gpu support only 4th channel images
2013-08-28 19:45:13 +08:00
cuda::GpuMat src4ch;
cuda::cvtColor(src, src4ch, COLOR_BGR2BGRA);
2012-10-17 07:18:30 +08:00
src = src4ch;
}
2013-04-29 20:03:16 +08:00
help();
2012-10-17 07:18:30 +08:00
2013-08-28 19:45:13 +08:00
cuda::printShortCudaDeviceInfo(cuda::getDevice());
2013-04-29 20:03:16 +08:00
}
int App::run()
{
// create windows for output images
namedWindow("Open/Close");
namedWindow("Erode/Dilate");
createTrackbar("iterations", "Open/Close", &open_close_pos, max_iters * 2 + 1, OpenCloseCallback, this);
createTrackbar("iterations", "Erode/Dilate", &erode_dilate_pos, max_iters * 2 + 1, ErodeDilateCallback, this);
2012-10-17 07:18:30 +08:00
for(;;)
{
2013-04-29 20:03:16 +08:00
OpenClose();
ErodeDilate();
2012-10-17 07:18:30 +08:00
2013-04-29 20:03:16 +08:00
char c = (char) waitKey();
2012-10-17 07:18:30 +08:00
2013-04-29 20:03:16 +08:00
switch (c)
{
case 27:
return 0;
2012-10-17 07:18:30 +08:00
break;
2013-04-29 20:03:16 +08:00
case 'e':
2012-10-17 07:18:30 +08:00
element_shape = MORPH_ELLIPSE;
2013-04-29 20:03:16 +08:00
break;
case 'r':
2012-10-17 07:18:30 +08:00
element_shape = MORPH_RECT;
2013-04-29 20:03:16 +08:00
break;
case 'c':
2012-10-17 07:18:30 +08:00
element_shape = MORPH_CROSS;
2013-04-29 20:03:16 +08:00
break;
case ' ':
2012-10-17 07:18:30 +08:00
element_shape = (element_shape + 1) % 3;
2013-04-29 20:03:16 +08:00
break;
}
}
}
void App::help()
{
cout << "Show off image morphology: erosion, dialation, open and close \n";
cout << "Call: \n";
cout << " gpu-example-morphology [image] \n";
Merge remote-tracking branch 'origin/2.4' into merge-2.4 Conflicts: modules/calib3d/perf/perf_pnp.cpp modules/contrib/src/imagelogpolprojection.cpp modules/contrib/src/templatebuffer.hpp modules/core/perf/opencl/perf_gemm.cpp modules/cudafeatures2d/doc/feature_detection_and_description.rst modules/cudafeatures2d/perf/perf_features2d.cpp modules/cudafeatures2d/src/fast.cpp modules/cudafeatures2d/test/test_features2d.cpp modules/features2d/doc/feature_detection_and_description.rst modules/features2d/include/opencv2/features2d/features2d.hpp modules/features2d/perf/opencl/perf_brute_force_matcher.cpp modules/gpu/include/opencv2/gpu/gpu.hpp modules/gpu/perf/perf_imgproc.cpp modules/gpu/perf4au/main.cpp modules/imgproc/perf/opencl/perf_blend.cpp modules/imgproc/perf/opencl/perf_color.cpp modules/imgproc/perf/opencl/perf_moments.cpp modules/imgproc/perf/opencl/perf_pyramid.cpp modules/objdetect/perf/opencl/perf_hogdetect.cpp modules/ocl/perf/perf_arithm.cpp modules/ocl/perf/perf_bgfg.cpp modules/ocl/perf/perf_blend.cpp modules/ocl/perf/perf_brute_force_matcher.cpp modules/ocl/perf/perf_canny.cpp modules/ocl/perf/perf_filters.cpp modules/ocl/perf/perf_gftt.cpp modules/ocl/perf/perf_haar.cpp modules/ocl/perf/perf_imgproc.cpp modules/ocl/perf/perf_imgwarp.cpp modules/ocl/perf/perf_match_template.cpp modules/ocl/perf/perf_matrix_operation.cpp modules/ocl/perf/perf_ml.cpp modules/ocl/perf/perf_moments.cpp modules/ocl/perf/perf_opticalflow.cpp modules/ocl/perf/perf_precomp.hpp modules/ocl/src/cl_context.cpp modules/ocl/src/opencl/haarobjectdetect.cl modules/video/src/lkpyramid.cpp modules/video/src/precomp.hpp samples/gpu/morphology.cpp
2014-03-11 21:20:01 +08:00
cout << "This program also shows use of rect, ellipse and cross kernels \n" << endl;
2013-04-29 20:03:16 +08:00
cout << "Hot keys: \n";
cout << "\tESC - quit the program \n";
cout << "\tr - use rectangle structuring element \n";
cout << "\te - use elliptic structuring element \n";
cout << "\tc - use cross-shaped structuring element \n";
cout << "\tSPACE - loop through all the options \n" << endl;
}
void App::OpenClose()
{
int n = open_close_pos - max_iters;
int an = n > 0 ? n : -n;
Mat element = getStructuringElement(element_shape, Size(an*2+1, an*2+1), Point(an, an));
if (n < 0)
{
2013-08-28 19:45:13 +08:00
Ptr<cuda::Filter> openFilter = cuda::createMorphologyFilter(MORPH_OPEN, src.type(), element);
2013-04-29 20:03:16 +08:00
openFilter->apply(src, dst);
}
else
{
2013-08-28 19:45:13 +08:00
Ptr<cuda::Filter> closeFilter = cuda::createMorphologyFilter(MORPH_CLOSE, src.type(), element);
2013-04-29 20:03:16 +08:00
closeFilter->apply(src, dst);
}
Mat h_dst(dst);
imshow("Open/Close", h_dst);
}
void App::ErodeDilate()
{
int n = erode_dilate_pos - max_iters;
int an = n > 0 ? n : -n;
Mat element = getStructuringElement(element_shape, Size(an*2+1, an*2+1), Point(an, an));
if (n < 0)
{
2013-08-28 19:45:13 +08:00
Ptr<cuda::Filter> erodeFilter = cuda::createMorphologyFilter(MORPH_ERODE, src.type(), element);
2013-04-29 20:03:16 +08:00
erodeFilter->apply(src, dst);
}
else
{
2013-08-28 19:45:13 +08:00
Ptr<cuda::Filter> dilateFilter = cuda::createMorphologyFilter(MORPH_DILATE, src.type(), element);
2013-04-29 20:03:16 +08:00
dilateFilter->apply(src, dst);
2012-10-17 07:18:30 +08:00
}
2013-04-29 20:03:16 +08:00
Mat h_dst(dst);
imshow("Erode/Dilate", h_dst);
}
void App::OpenCloseCallback(int, void* data)
{
App* thiz = (App*) data;
thiz->OpenClose();
}
void App::ErodeDilateCallback(int, void* data)
{
App* thiz = (App*) data;
thiz->ErodeDilate();
}
int main(int argc, const char* argv[])
{
App app(argc, argv);
return app.run();
2012-10-17 07:18:30 +08:00
}