边缘检测
1、基于模板的边缘检测(Sobel和Prewitt)
这种边缘检测是将灰度图像使用对应的锐化算子进行增强处理,然后对其进行二值化。
实现代码:
/*****************************************************************************
函数名 : ImgEdgeSobel
功能 : Sobel边缘检测
算法实现 : <可选项>
参数说明 : tSrcImg 原图像[in]
ptDstImg 目标图像[out]
u8Thresh 阈值[in]
u8SenDir 敏感方向(SOBEL_SENDIRH 水平;SOBEL_SENDIRV 垂直)[in]
返回值说明 : 无
其他说明 : 无
******************************************************************************/
void ImgEdgeSobel(MyImage tSrcImg, MyImage *ptDstImg, u8 u8Thresh, u8 u8SenDir)
{
f32 f32SobelFilterV[9] = { -1, 0, 1,
-2, 0, 2,
-1, 0, 1 };
f32 f32SobelFilterH[9] = { -1, -2, -1,
0, 0, 0,
1, 2, 1 };
MyImage MyImgGray, MyImgSobel;
ImgRGBToGray(tSrcImg, &MyImgGray);
switch(u8SenDir)
{
case SOBEL_SENDIRH:
ImgFilter(MyImgGray, &MyImgSobel, f32SobelFilterH, 3, 3);
break;
case SOBEL_SENDIRV:
ImgFilter(MyImgGray, &MyImgSobel, f32SobelFilterV, 3, 3);
break;
default:break;
}
ImgGrayToBina(MyImgSobel, ptDstImg, u8Thresh);
}
/*****************************************************************************
函数名 : ImgEdgePrewitt
功能 : Prewitt边缘检测
算法实现 : <可选项>
参数说明 : tSrcImg 原图像[in]
ptDstImg 目标图像[out]
u8Thresh 阈值[in]
u8SenDir 敏感方向(PREWITT_SENDIRH 水平;PREWITT_SENDIRV 垂直)[in]
返回值说明 : 无
其他说明 : 无
******************************************************************************/
void ImgEdgePrewitt(MyImage tSrcImg, MyImage *ptDstImg, u8 u8Thresh, u8 u8SenDir)
{
f32 f32PrewittFilterV[9] = { -1, 0, 1,
-1, 0, 1,
-1, 0, 1 };
f32 f32PrewittFilterH[9] = { -1, -1, -1,
0, 0, 0,
1, 1, 1 };
MyImage MyImgGray, MyImgPrewitt;
ImgRGBToGray(tSrcImg, &MyImgGray);
switch (u8SenDir)
{
case PREWITT_SENDIRH:
ImgFilter(MyImgGray, &MyImgPrewitt, f32PrewittFilterH, 3, 3);
break;
case PREWITT_SENDIRV:
ImgFilter(MyImgGray, &MyImgPrewitt, f32PrewittFilterV, 3, 3);
break;
default:break;
}
ImgGrayToBina(MyImgPrewitt, ptDstImg, u8Thresh);
}
2、Canny边缘检测