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"
#include "opencv2/gpufilters.hpp"
#include "opencv2/gpuimgproc.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-04-29 20:03:16 +08:00
gpu::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
}
2013-04-29 20:03:16 +08:00
String filename = argc == 2 ? argv[1] : "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-04-29 20:03:16 +08:00
gpu::GpuMat src4ch;
gpu::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-04-29 20:03:16 +08:00
gpu::printShortCudaDeviceInfo(gpu::getDevice());
}
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";
cout << "This program also shows use of rect, elipse and cross kernels \n" << endl;
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)
{
Ptr<gpu::Filter> openFilter = gpu::createMorphologyFilter(MORPH_OPEN, src.type(), element);
openFilter->apply(src, dst);
}
else
{
Ptr<gpu::Filter> closeFilter = gpu::createMorphologyFilter(MORPH_CLOSE, src.type(), element);
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)
{
Ptr<gpu::Filter> erodeFilter = gpu::createMorphologyFilter(MORPH_ERODE, src.type(), element);
erodeFilter->apply(src, dst);
}
else
{
Ptr<gpu::Filter> dilateFilter = gpu::createMorphologyFilter(MORPH_DILATE, src.type(), element);
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
}