前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于一维级联快速膨胀与腐蚀算法

基于一维级联快速膨胀与腐蚀算法

作者头像
OpenCV学堂
发布2018-04-04 11:13:22
1.4K0
发布2018-04-04 11:13:22
举报

一:基本原理

膨胀与腐蚀是图像形态学两个基本操作之一,传统的代码实现都是基于二维窗口卷积模式,对于正常的3x3窗口要八次与运算,而基于一维级联方式先X方向后Y方向只需要4次与运算即可。对于结构元素比较大的矩形来说,我们还可以通过连续的3x3的级联腐蚀或者膨胀来替代,假设对于11x11窗口大小腐蚀来说,正常的计算需要120次的与操作,而通过一维级联腐蚀只需要在X方向10次与操作,Y方向10次与操作,总计2x10=20次与操作即可实现。这样就极大的提高了二值图像腐蚀与膨胀的计算效率。图示如下:

二:代码实现(Java语言实现)

基于一维级联快速腐蚀算法代码实现:

代码语言:javascript
复制
@Overridepublic void process(int width, int height) {    int size = width*height;    byte[] output = new byte[size];    System.arraycopy(data, 0, output, 0, size);    // X Direction    int xr = radius/2;    byte c = (byte)0;    int offset = 0;    for(int row=0; row<height; row++) {        for(int col=0; col<width; col++) {            c = data[row*width+col];            if((c&0xff) == 0)continue;            for(int x=-xr; x<=xr; x++) {                if(x==0)continue;                offset = x + col;                if(offset < 0) {                    offset = 0;                }                if(offset >= width) {                    offset = width - 1;                }                c &=data[row*width+offset];            }            if(c == 0){                output[row*width+col] = (byte)0;            }        }    }    System.arraycopy(output, 0, data, 0, size);    // Y Direction    int yr = radius/2;    c = 0;    offset = 0;    for(int col=0; col<width; col++) {        for(int row=0; row<height; row++) {            c = data[row*width+col];            if((c&0xff) == 0)continue;            for(int y=-yr; y<=yr; y++) {                if(y == 0)continue;                offset = y + row;                if(offset < 0) {                    offset = 0;                }                if(offset >= height) {                    offset = height - 1;                }                c &=data[offset*width+col];            }            if(c == 0){                output[row*width+col] = (byte)0;            }        }    }    System.arraycopy(output, 0, data, 0, size);}

基于传统卷积腐蚀算法代码实现:

代码语言:javascript
复制
@Overridepublic void process(int width, int height) {    int size = width*height;    byte[] output = new byte[size];    IntIntegralImage grayii = new IntIntegralImage();    grayii.setImage(data);    grayii.process(width, height);    int yr = radius/2;    int xr = radius/2;    System.arraycopy(data, 0, output, 0, size);    byte c = 0;    int nx=0, ny=0;    for(int row=0; row<height; row++) {        for(int col=0; col<width; col++) {            c = data[row*width+col];            if(c == 0)continue;            for(int y=-yr; y<=yr; y++) {                ny = y + row;                if(ny < 0 || ny >= height){                    ny = 0;                }                for(int x=-xr; x<=xr; x++) {                    nx = x+col;                    if(nx < 0 || nx >= width) {                        nx = 0;                    }                    c &= data[ny*width+nx]&0xff;                }            }            if(c == 0) {                output[row*width+col] = (byte)0;            }        }    }    System.arraycopy(output, 0, data, 0, size);}

三:耗时比较

对张大小为381x244大小二值图像一维快速与传统腐蚀操作耗时比较结果如下(Win64,CPU i5, JDK8 64位):

原图与对应不同结构元素大小腐蚀结果如下:

无论是卷积还是高斯模糊,还是形态学操作,理论上都是卷积计算,但是在实际编码过程中基于对计算耗时考虑都是进行了各种有效变换从而提高计算速度与减少执行时间,所以对于任何看似简单的图像操作,理论一旦脱离实践!长期如此的结果就是眼高手低! 愿与各位共勉!祝各位五一劳动节快乐!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-05-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 OpenCV学堂 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一:基本原理
  • 二:代码实现(Java语言实现)
  • 三:耗时比较
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档