您的位置:首页 > 其它

形态学滤波对图像进行边缘及角点检测

2015-08-22 21:17 393 查看
将灰度图像看做是“等高线”:亮的区域代表山峰,而暗的区域代表山谷,图像的边沿就对应于峭壁。如果腐蚀一幅图像,会导致山谷被扩展,而峭壁减少了。相反的,如果膨胀一幅图像,峭壁则会增加。但是这两种情况下,中间的部分(大片的谷底和高原)基本保持不变。 对图像的腐蚀和膨胀的结果做差,就能提取图像的边界:因为边界区域,二者完全不同。(实际上,我们也可以用腐蚀或者膨胀的结果与源图像做差得出类似结果,但提取的边界会比较细)。可以看出,结构元越大,边界越粗。在OpenCV中,将形态学操作函数morphologyEx 的第4个参数设为MORPH_GRADIENT,就能完成上述工作。 开运算可以 用来消除小物体、在纤细点处分离物体、平滑较大物体的边界的同时并不明显改变其面积。闭运算能够排除小型黑洞(黑色区域) 形态学梯度(Morphological Gradient)为膨胀图与腐蚀图之差,对二值图像进行这一操作可以将团块(blob)的边缘突出出来。我们可以用形态学梯度来保留物体的边缘轮廓 顶帽运算(Top Hat)为原图像与“开运算“的结果图之差,因为开运算带来的结果是放大了裂缝或者局部低亮度的区域,因此,从原图中减去开运算后的图,得到的效果图突出了比原图轮廓周围的区域更明亮的区域,且这一操作和选择的核的大小相关。顶帽运算往往用来分离比邻近点亮一些的斑块。当一幅图像具有大幅的背景的时候,而微小物品比较有规律的情况下,可以使用顶帽运算进行背景提取。黑帽(Black Hat)运算为”闭运算“的结果图与原图像之差,黑帽运算后的效果图突出了比原图轮廓周围的区域更暗的区域,且这一操作和选择的核的大小相关。所以,黑帽运算用来分离比邻近点暗一些的斑块。
void cv::morphologyEx( InputArray _src, OutputArray _dst, int op,
                       InputArray kernel, Point anchor, int iterations,
                       int borderType, const Scalar& borderValue );
<ul style="padding: 0px; margin: 0px 0px 0.75em 25px; list-style: none; font-size: 16px; line-height: 27.1875px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);"><li style="padding: 0px; margin: 0px; background-image: url(http://static.blog.csdn.net/images/wapall/list-bottom1.jpg); line-height: 1.7em; list-style-type: disc; background-position: 50% 100%; background-repeat: repeat no-repeat;">第一个参数,InputArray类型的src,输入图像,即源图像,填Mat类的对象即可。图像位深应该为以下五种之一:CV_8U, CV_16U,CV_16S, CV_32F 或CV_64F。</li><li style="padding: 0px; margin: 0px; background-image: url(http://static.blog.csdn.net/images/wapall/list-bottom1.jpg); line-height: 1.7em; list-style-type: disc; background-position: 50% 100%; background-repeat: repeat no-repeat;">第二个参数,OutputArray类型的dst,即目标图像,函数的输出参数,需要和源图片有一样的尺寸和类型。</li><li style="padding: 0px; margin: 0px; background-image: none; line-height: 1.7em; list-style-type: disc; background-position: initial initial; background-repeat: initial initial;">第三个参数,int类型的op,表示形态学运算的类型,可以是如下之一的标识符:</li></ul><ul style="padding: 0px; margin: 0px 0px 0.75em 25px; list-style: none; font-size: 16px; line-height: 27.1875px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);"><ul style="padding: 0px; margin: 0px 0px 0px 25px; list-style: none; line-height: 1.7em;"><li style="padding: 0px; margin: 0px; background-image: url(http://static.blog.csdn.net/images/wapall/list-bottom1.jpg); line-height: 1.7em; list-style-type: disc; background-position: 50% 100%; background-repeat: repeat no-repeat;">MORPH_OPEN – 开运算(Opening operation)</li><li style="padding: 0px; margin: 0px; background-image: url(http://static.blog.csdn.net/images/wapall/list-bottom1.jpg); line-height: 1.7em; list-style-type: disc; background-position: 50% 100%; background-repeat: repeat no-repeat;">MORPH_CLOSE – 闭运算(Closing operation)</li><li style="padding: 0px; margin: 0px; background-image: url(http://static.blog.csdn.net/images/wapall/list-bottom1.jpg); line-height: 1.7em; list-style-type: disc; background-position: 50% 100%; background-repeat: repeat no-repeat;">MORPH_GRADIENT -形态学梯度(Morphological gradient)</li><li style="padding: 0px; margin: 0px; background-image: url(http://static.blog.csdn.net/images/wapall/list-bottom1.jpg); line-height: 1.7em; list-style-type: disc; background-position: 50% 100%; background-repeat: repeat no-repeat;">MORPH_TOPHAT - “顶帽”(“Top hat”)</li><li style="padding: 0px; margin: 0px; background-image: none; line-height: 1.7em; list-style-type: disc; background-position: initial initial; background-repeat: initial initial;">MORPH_BLACKHAT - “黑帽”(“Black hat“)</li></ul></ul><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 10px; margin-bottom: 10px; font-family: Arial; font-size: 16px; line-height: 24px; background-color: rgb(237, 237, 237);"></p><p style="padding-top: 0px; padding-bottom: 0px; margin-top: 0px; margin-bottom: 0.75em; font-size: 16px; line-height: 27.1875px; text-indent: 1em; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);"></p><ul style="padding: 0px; margin: 0px 0px 0.75em 25px; list-style: none; font-size: 16px; line-height: 27.1875px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);"><li style="padding: 0px; margin: 0px; background-image: none; line-height: 1.7em; list-style-type: disc; background-position: initial initial; background-repeat: initial initial;">第四个参数,InputArray类型的kernel,形态学运算的内核。若为NULL时,表示的是使用参考点位于中心3x3的核。</li></ul><ul style="padding: 0px; margin: 0px 0px 0.75em 25px; list-style: none; font-size: 16px; line-height: 27.1875px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Tahoma, Arial, STXihei, 'Microsoft YaHei', 微软雅黑, sans-serif; background-color: rgb(254, 254, 254);"><li style="padding: 0px; margin: 0px; background-image: url(http://static.blog.csdn.net/images/wapall/list-bottom1.jpg); line-height: 1.7em; list-style-type: disc; background-position: 50% 100%; background-repeat: repeat no-repeat;">第五个参数,Point类型的anchor,锚的位置,其有默认值(-1,-1),表示锚位于中心。</li><li style="padding: 0px; margin: 0px; background-image: url(http://static.blog.csdn.net/images/wapall/list-bottom1.jpg); line-height: 1.7em; list-style-type: disc; background-position: 50% 100%; background-repeat: repeat no-repeat;">第六个参数,int类型的iterations,迭代使用函数的次数,默认值为1。</li><li style="padding: 0px; margin: 0px; background-image: url(http://static.blog.csdn.net/images/wapall/list-bottom1.jpg); line-height: 1.7em; list-style-type: disc; background-position: 50% 100%; background-repeat: repeat no-repeat;">第七个参数,int类型的borderType,用于推断图像外部像素的某种边界模式。注意它有默认值BORDER_ CONSTANT。</li><li style="padding: 0px; margin: 0px; background-image: none; line-height: 1.7em; list-style-type: disc; background-position: initial initial; background-repeat: initial initial;">第八个参数,const Scalar&类型的borderValue,当边界为常数时的边界值,有默认值morphologyDefaultBorderValue(),一般我们不用去管他。</li></ul>     形态学滤波对图像进行边缘及角点检测,关键代码如下:
//morphologyEx检测直线
Mat MorphoFeatures::getEdges(const Mat& image)
{
<span style="white-space:pre">	</span>//得到梯度图
<span style="white-space:pre">	</span>Mat result;
<span style="white-space:pre">	</span>morphologyEx(image, result, MORPH_GRADIENT, Mat());
<span style="white-space:pre">	</span>applyThreshold(result);

<span style="white-space:pre">	</span>return result;
}
//检测角点
Mat MorphoFeatures::getCorners(const Mat& image)
{
<span style="white-space:pre">	</span>Mat result;
<span style="white-space:pre">	</span>//十字形膨胀
<span style="white-space:pre">	</span>dilate(image, result, cross);
<span style="white-space:pre">	</span>//菱形腐蚀
<span style="white-space:pre">	</span>erode(result, result, diamond);
<span style="white-space:pre">	</span>Mat result2;
<span style="white-space:pre">	</span>//X形膨胀
<span style="white-space:pre">	</span>dilate(image, result2, x);
<span style="white-space:pre">	</span>//方形腐蚀
<span style="white-space:pre">	</span>erode(result2, result2, square);
<span style="white-space:pre">	</span>//通过两张图像做差值得到角点图像
<span style="white-space:pre">	</span>absdiff(result2, result, result);
<span style="white-space:pre">	</span>applyThreshold(result);//阈值化得到二值图像
<span style="white-space:pre">	</span>return result;
}
代码下载地址:
http://download.csdn.net/detail/lanmeng_smile/9036481

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: