图像处理之开操作详解

简介: 开操作概述: 图像处理中的开闭运算是两个非常重要的数学形态学操作,它们同时都继承自基本的腐蚀与 膨胀操作,这些操作一般都会应用在二值图像的分析与处理上。开操作有点像腐蚀操作,主 要是会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;
	}
}
转载请注明出处


目录
相关文章
|
23天前
|
计算机视觉 Python
图像处理工具设计
该文章提供了一个Python图像处理工具的设计,包括提高图像对比度、提取边缘、视频帧拆分、图像滚动测试和像素连接等功能的实现代码。
|
3月前
|
编解码 算法 计算机视觉
图像处理之高斯金字塔
图像处理之高斯金字塔
31 3
|
3月前
|
计算机视觉
图像处理之水纹效果
图像处理之水纹效果
24 1
|
3月前
|
计算机视觉
图像处理之开操作详解
图像处理之开操作详解
19 0
|
3月前
|
计算机视觉
图像处理之图像梯度效果
图像处理之图像梯度效果
24 0
|
12月前
|
机器学习/深度学习 文字识别 自然语言处理
文档图像处理:大模型的突破与新探索
丁凯博士分享了当前文档图像处理面临的困难,并讨论大模型在该领域的突破和新探索。
1129 5
|
算法 数据安全/隐私保护 计算机视觉
图像处理基础
图像处理基础
93 0
|
PHP 数据安全/隐私保护 计算机视觉
PHPImagine 图像处理库介绍
随着网络的发展,人们对图像的需求越来越高。作为一个PHP开发者,我们在处理图像时经常会遇到一些问题,比如裁剪、缩放、加水印等。这些问题都可以通过使用图像处理库来解决。PHPImagine就是一种优秀的图像处理库。
66 0
|
计算机视觉 异构计算
|
Java 数据安全/隐私保护 计算机视觉
thumbnails图像处理库的使用
thumbnails是Java一个优秀的图像处理库,可以对图片进行压缩、加水印、裁剪、更改格式等功能。下面为大家介绍使用这个图像处理库。
thumbnails图像处理库的使用