Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save raunaqbn/6fc0c8f1d6c488591fc6c91847dbe9f8 to your computer and use it in GitHub Desktop.
Save raunaqbn/6fc0c8f1d6c488591fc6c91847dbe9f8 to your computer and use it in GitHub Desktop.
struct morphParameters{
int shape;
int size;
int oper;
Mat* src;
Mat* res;
};
void morphology(Mat* src, Mat *result, int size)
{
int choice;
morphParameters dil, ero, morph;
cout << "0: erosion,1:dilation,2:morph operation";
cin >> choice;
if (choice == 1)
{
namedWindow("Dilation", CV_WINDOW_AUTOSIZE);
dil.src = src;
dil.res = result;
dil.shape = 0;
dil.size = size;
createTrackbar("0:rectangle 1:cross 2:ellipse", "Dilation", &(dil.shape), 2, Dilation, (void*)(&dil));
createTrackbar("Size: 2n+1: Choose n", "Dilation", &dil.size, 21, Dilation, (void*)(&dil));
Dilation(0, (void*)&dil);
}
else if (choice == 0)
{
namedWindow("Erosion", CV_WINDOW_AUTOSIZE);
ero.res = result;
ero.src = src;
ero.shape = 0;
ero.size = size;
createTrackbar("0:rectangle 1:cross 2:ellipse", "Erosion", &(ero.shape), 2, Erosion, (void*)(&ero));
createTrackbar("Size: 2n+1: Choose n", "Erosion", &ero.size, 21, Erosion, (void*)(&ero));
Erosion(0, (void*)&ero);
}
else
{
namedWindow("Morphological", CV_WINDOW_AUTOSIZE);
morph.res = result;
morph.src = src;
morph.shape = 0;
morph.size = size;
morph.oper = 0;
createTrackbar("0:opening\n 1:closing \n2: gradient \n3:top hat 4: \nblack hat\n", "Morphological", &(morph.oper), 4, Morphological, (void*)(&morph));
createTrackbar("Size: 2n+1: Choose n", "Morphological", &morph.size, 21, Morphological, (void*)(&morph));
Morphological(0, (void*)&morph);
}
waitKey(0);
}
void Erosion(int ,void* input)
{
morphParameters *data;
data = (morphParameters*)input;
printParam(*data);
// We need to create a kernel of a particular size and shape
Mat kernel;
kernel = getStructuringElement(0, Size(2 * data->size + 1, 2 * data->size + 1), Point(data->size, data->size));
erode(*data->src, *data->res, kernel);
morphologyEx(*data->src, *data->res,data->shape, kernel);
imshow("Erosion", *data->res);
}
void Dilation(int, void* input)
{
morphParameters *data;
data = (morphParameters*)input;
printParam(*data);
// We need to create a kernel of a particular size and shape
Mat kernel;
kernel = getStructuringElement(0, Size(2 * data->size + 1, 2 * data->size + 1), Point(data->size, data->size));
dilate(*data->src, *data->res, kernel);
imshow("Dilation", *data->res);
}
void Morphological(int, void* input)
{
morphParameters *data;
data = (morphParameters*)input;
printParam(*data);
// We need to create a kernel of a particular size and shape
Mat kernel;
kernel = getStructuringElement(0, Size(2 * data->size + 1, 2 * data->size + 1), Point(data->size, data->size));
morphologyEx(*data->src, *data->res, data->oper , kernel);
imshow("Morphological", *data->res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment