1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#include <iostream>
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/cudafilters.hpp"
#include "opencv2/cudaimgproc.hpp"
using namespace std;
using namespace cv;
class App
{
public:
App(int argc, const char* argv[]);
int run();
private:
void help();
void OpenClose();
void ErodeDilate();
static void OpenCloseCallback(int, void*);
static void ErodeDilateCallback(int, void*);
cuda::GpuMat src, dst;
int element_shape;
int max_iters;
int open_close_pos;
int erode_dilate_pos;
};
App::App(int argc, const char* argv[])
{
element_shape = MORPH_RECT;
open_close_pos = erode_dilate_pos = max_iters = 10;
if (argc == 2 && String(argv[1]) == "--help")
{
help();
exit(0);
}
String filename = argc == 2 ? argv[1] : "../data/baboon.jpg";
Mat img = imread(filename);
if (img.empty())
{
cerr << "Can't open image " << filename.c_str() << endl;
exit(-1);
}
src.upload(img);
if (src.channels() == 3)
{
// gpu support only 4th channel images
cuda::GpuMat src4ch;
cuda::cvtColor(src, src4ch, COLOR_BGR2BGRA);
src = src4ch;
}
help();
cuda::printShortCudaDeviceInfo(cuda::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);
for(;;)
{
OpenClose();
ErodeDilate();
char c = (char) waitKey();
switch (c)
{
case 27:
return 0;
break;
case 'e':
element_shape = MORPH_ELLIPSE;
break;
case 'r':
element_shape = MORPH_RECT;
break;
case 'c':
element_shape = MORPH_CROSS;
break;
case ' ':
element_shape = (element_shape + 1) % 3;
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, ellipse 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<cuda::Filter> openFilter = cuda::createMorphologyFilter(MORPH_OPEN, src.type(), element);
openFilter->apply(src, dst);
}
else
{
Ptr<cuda::Filter> closeFilter = cuda::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<cuda::Filter> erodeFilter = cuda::createMorphologyFilter(MORPH_ERODE, src.type(), element);
erodeFilter->apply(src, dst);
}
else
{
Ptr<cuda::Filter> dilateFilter = cuda::createMorphologyFilter(MORPH_DILATE, src.type(), element);
dilateFilter->apply(src, dst);
}
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();
}
|