图像处理之开操作详解

简介: 开操作概述: 图像处理中的开闭运算是两个非常重要的数学形态学操作,它们同时都继承自基本的腐蚀与 膨胀操作,这些操作一般都会应用在二值图像的分析与处理上。开操作有点像腐蚀操作,主 要是会remove前景像素边缘,但是不会像腐蚀操作remove那么多边缘像素。

开操作概述:

图像处理中的开闭运算是两个非常重要的数学形态学操作,它们同时都继承自基本的腐蚀与

膨胀操作,这些操作一般都会应用在二值图像的分析与处理上。开操作有点像腐蚀操作,主

要是会remove前景像素边缘,但是不会像腐蚀操作remove那么多边缘像素。开操作主要

是用来保留某种结构操作,remove其他不符合结构元素的前景区域像素。

 

开操作原理:

一个开操作是一个腐蚀操作再接着一个膨胀操作使用相同的结构元素。开操作需要两个输入

数据一个是要开操作的像素数据,一个是开操作的结构元素,根据开操作的要求不同,结构

元素可以是圆形,正方形,矩形等。关于腐蚀与膨胀操作见博客文章:

二值图像膨胀操作 - http://blog.csdn.net/jia20003/article/details/7574214

二值图像腐蚀操作 - http://blog.csdn.net/jia20003/article/details/7582666


程序效果:- 原图


通过开操作,我们可以除去干扰线(竖线与斜线),通过过开操作也可以只保留竖线

唯一的秘诀就在于输入开操作的结构元素的形状决定,效果如下图:


开操作源代码:

package com.gloomyfish.morphology;

import java.awt.Color;
import java.awt.image.BufferedImage;

public class OpeningFilter extends BinaryFilter {
	private int[][] structure_element;
	private Color bgColor;
	private Color fgColor;
	
	public OpeningFilter() {
		structure_element = new int[3][10];
		// structure_element = new int[20][1];
		bgColor = Color.BLACK;
		fgColor = Color.WHITE;
	}
	
	public void setBackGroundColor(Color c) {
		this.bgColor = c;
	}
	
	public void setForeGroundColor(Color c) {
		this.fgColor = c;
	}
	
	public void setElement(int[][] element) {
		this.structure_element = element;
	}

	@Override
	public BufferedImage filter(BufferedImage src, BufferedImage dest) {
		int width = src.getWidth();
        int height = src.getHeight();

        if ( dest == null )
        	dest = createCompatibleDestImage( src, null );

        int[] inPixels = new int[width*height];
        int[] outPixels = new int[width*height];
        src = super.filter(src, null);
        getRGB( src, 0, 0, width, height, inPixels );
        int index = 0;
        int subrow = structure_element.length/2;
        int subcol = structure_element[0].length/2;
        int rowoffset = 0, coloffset = 0;
        int index2 = 0;
        for(int row=0; row<height; row++) {
        	int ta = 0, tr = 0, tg = 0, tb = 0;
        	for(int col=0; col<width; col++) {
        		index = row * width + col;
        		ta = (inPixels[index] >> 24) & 0xff;
                tr = (inPixels[index] >> 16) & 0xff;
                tg = (inPixels[index] >> 8) & 0xff;
                tb = inPixels[index] & 0xff;
                int ta2 = 0, tr2 = 0, tg2= 0, tb2 = 0;
                boolean isfound = false;
                for(int i=-subrow; i<=subrow; i++) {
                	for(int j=-subcol; j<=subcol; j++) {
                		rowoffset = row + i;
                		coloffset = col + j;
                		if(rowoffset >=0 && rowoffset < height) {
                			rowoffset = row + i;
                		} else {
                			rowoffset = 0;
                		}
                		
                		if(coloffset >= 0 && coloffset < width) {
                			coloffset = col + j;
                		} else {
                			coloffset = 0;
                		}
                		index2 = rowoffset * width + coloffset;
                		ta2 = (inPixels[index2] >> 24) & 0xff;
                        tr2 = (inPixels[index2] >> 16) & 0xff;
                        tg2 = (inPixels[index2] >> 8) & 0xff;
                        tb2 = inPixels[index2] & 0xff;
                        if(tr2 == bgColor.getRed() && tg2 == bgColor.getGreen()) {
                        	isfound = true;
                        	break;
                        }
                	}
                	if(isfound) break;
                }
                rowoffset = 0;
                coloffset = 0;
                if(isfound) {
                	tr = bgColor.getRed();
                	tg = bgColor.getGreen();
                	tb = bgColor.getBlue();
                	outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;
                } else {
                	outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;
                }
                
        	}
        }
        
        // copy the Erosion result pixels to input pixels data 
        // and ready to Dilation operation
        System.arraycopy(outPixels, 0, inPixels, 0, width*height);
        
        // start to dilate the pixels data...
        for(int row=0; row<height; row++) {
        	int ta = 0, tr = 0, tg = 0, tb = 0;
        	for(int col=0; col<width; col++) {
        		index = row * width + col;
        		ta = (inPixels[index] >> 24) & 0xff;
                tr = (inPixels[index] >> 16) & 0xff;
                tg = (inPixels[index] >> 8) & 0xff;
                tb = inPixels[index] & 0xff;
                int ta2 = 0, tr2 = 0, tg2= 0, tb2 = 0;
                boolean isfound = false;
                for(int i=-subrow; i<=subrow; i++) {
                	for(int j=-subcol; j<=subcol; j++) {
                		rowoffset = row + i;
                		coloffset = col + j;
                		if(rowoffset >=0 && rowoffset < height) {
                			rowoffset = row + i;
                		} else {
                			rowoffset = 0;
                		}
                		
                		if(coloffset >= 0 && coloffset < width) {
                			coloffset = col + j;
                		} else {
                			coloffset = 0;
                		}
                		index2 = rowoffset * width + coloffset;
                		ta2 = (inPixels[index2] >> 24) & 0xff;
                        tr2 = (inPixels[index2] >> 16) & 0xff;
                        tg2 = (inPixels[index2] >> 8) & 0xff;
                        tb2 = inPixels[index2] & 0xff;
                        if(tr2 == fgColor.getRed() && tg2 == fgColor.getGreen()) {
                        	isfound = true;
                        	break;
                        }
                	}
                	if(isfound) break;
                }
                rowoffset = 0;
                coloffset = 0;
                if(isfound) {
                	tr = fgColor.getRed();
                	tg = fgColor.getGreen();
                	tb = fgColor.getBlue();
                	outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;
                } else {
                	outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;
                }
                
        	}
        }
        
        setRGB( dest, 0, 0, width, height, outPixels );
        return dest;
	}
}
转载请注明出处


目录
相关文章
|
存储 算法 Cloud Native
Ganos地理网格引擎支撑无人机路径规划能力实践
随着新能源技术的迅猛发展,低空经济已经逐步成为新的战略性新兴产业,但不同于传统的地表活动,低空活动具有立体性、区域性、融合性等特点,这些特点对于如何安全引导低空活动的顺利开展带来了一系列需要解决的技术问题。Ganos地理网格引擎提供了基于网格的路径规划能力,可以使用DEM、DSM、倾斜摄影等数据构建复杂环境下的无人机路径规划应用。
|
11月前
|
人工智能 运维 监控
智能运维在现代数据中心的应用与挑战
随着云计算和大数据技术的迅猛发展,现代数据中心的运维管理面临着前所未有的挑战。本文探讨了智能运维技术在数据中心中的应用,包括自动化监控、故障预测与诊断、资源优化等方面,并分析了当前面临的主要挑战,如数据安全、系统集成复杂性等。通过实际案例分析,展示了智能运维如何帮助数据中心提高效率、降低成本,并提出了未来发展趋势和建议。
|
12月前
|
存储 人工智能 安全
数据治理:强化数据安全与隐私保护的基石
在当今这个数字化时代,数据已成为推动社会进步和企业发展的核心驱动力。从个人消费习惯到企业运营策略,从政府决策支持到科研创新突破,数据无处不在,其价值不言而喻。然而,随着数据量的爆炸性增长和流通范围的扩大,数据安全与隐私保护问题也日益凸显,成为制约数据价值最大化利用的重要瓶颈。因此,构建完善的数据治理体系,特别是强化数据安全与隐私保护,成为了时代发展的必然要求。
1150 5
|
关系型数据库 分布式数据库 数据库
PolarDB闪电助攻,《香肠派对》百亿好友关系实现毫秒级查询
PolarDB分布式版助力《香肠派对》实现百亿好友关系20万QPS的毫秒级查询。
PolarDB闪电助攻,《香肠派对》百亿好友关系实现毫秒级查询
|
12月前
|
网络协议 安全 生物认证
一文带你从了解到精通Nmap扫描器
一文带你从了解到精通Nmap扫描器
|
监控 前端开发 JavaScript
记录浏览器节能机制导致Websocket断连问题
近期,在使用WebSocket(WS)连接时遇到了频繁断连的问题,这种情况在单个用户上每天发生数百次。尽管利用了socket.io的自动重连机制能够在断连后迅速恢复连接,但这并不保证每一次重连都能成功接收WS消息。因此,我们进行了一些的排查和测试工作。
715 1
记录浏览器节能机制导致Websocket断连问题
|
SQL 开发框架 关系型数据库
基于SqlSugar的数据库访问处理的封装,支持多数据库并使之适应于实际业务开发中
基于SqlSugar的数据库访问处理的封装,支持多数据库并使之适应于实际业务开发中
|
云安全 安全 网络安全
云上安全基础防护知识
网络安全涉及核心概念如机密性、完整性、可用性、认证、授权、不可否认性、保密性、可靠性、可控性和隐私保护。这些属性是安全策略的基础。网络风险模型描绘了攻击过程,如洛克希德·马丁的网络杀伤链,包括侦察、武器化、交付、利用、安装、命令与控制及行动阶段。攻防双方的状态图表展示了防御者和攻击者的动态。在中国,互联网安全态势和云安全威胁日益严峻,云安全遵循责任共担原则,阿里云提供了多层安全架构来保障云上安全。
|
Python Windows
一、如何完全卸载Anaconda(如何下载Anaconda-Clean package)
一、如何完全卸载Anaconda(如何下载Anaconda-Clean package)
13080 0
一、如何完全卸载Anaconda(如何下载Anaconda-Clean package)
|
存储 安全 内存技术
ECU Bootloader的三部曲
ECU Bootloader的三部曲
ECU Bootloader的三部曲