opencv morphologyEX 图像数学形态学

#include
#include
#include
#include
using namespace cv;
using namespace std;
Mat  g_srcImage,g_dstImage;
int g_nTrackbarNumber = 0; 
int g_nStructELementSize = 3;
enum opt
{
OPEN = 0,//开运算
CLOSE,    //闭运算
GRADIENT, //形态学梯度
TOPHAT,//定帽
BLACKHAT,//黑帽
ERODE, //腐蚀
DILATE  //膨胀
};
void Process();
void on_TrcackBarNUmChange(int ,void*);
void on_ElementSizeChange(int,void*);
int main()
{
g_srcImage = imread("1.jpg");
if(!g_srcImage.data)
{
printf("read jpg error\n");
return -1;
}
namedWindow("yuantu",WINDOW_AUTOSIZE);
imshow("yuantu",g_srcImage);



namedWindow("morphologyEx", WINDOW_AUTOSIZE);
createTrackbar("g_nTrackbarNumber","morphologyEx",&g_nTrackbarNumber,6,on_TrcackBarNUmChange);
while(char(waitKey(1)) != 'q')
{}
}
void Process()
{


int op;
switch(g_nTrackbarNumber)
{
case OPEN:
op = MORPH_OPEN;
break;
case CLOSE:
op = MORPH_CLOSE;
break;
case GRADIENT:
op = MORPH_GRADIENT;
break;
case TOPHAT:
op = MORPH_TOPHAT;
break;
case BLACKHAT:
op = MORPH_BLACKHAT;
break;
case ERODE:
op = MORPH_ERODE;
break;
case DILATE:
op = MORPH_DILATE;
default:
op = MORPH_OPEN;
}
Mat element = getStructuringElement(MORPH_RECT,Size(g_nStructELementSize*2+1,g_nStructELementSize*2+1),Point(g_nStructELementSize,g_nStructELementSize));
morphologyEx(g_srcImage,g_dstImage,op,element);
imshow("morphologyEx",g_dstImage);
}


void on_TrcackBarNUmChange(int,void *)
{
Process();
}
void on_ElementSizeChange(int,void *)
{
Process();

}

运行图片如下opencv morphologyEX 图像数学形态学_第1张图片


你可能感兴趣的:(opencv)