图像处理之开操作详解

简介: 图像处理之开操作详解

开操作概述:


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


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


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


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



开操作原理:


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


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


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


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


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



程序效果:- 原图

1341141127_8419.png


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


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

1341141246_3547.png


开操作源代码:

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;
  }
}

转载请注明出处

相关文章
|
19天前
|
编解码 算法 计算机视觉
图像处理之高斯金字塔
图像处理之高斯金字塔
15 3
|
19天前
|
计算机视觉
图像处理之水纹效果
图像处理之水纹效果
14 1
|
19天前
|
计算机视觉
图像处理之图像梯度效果
图像处理之图像梯度效果
11 0
|
1月前
|
传感器 算法 计算机视觉
LabVIEW开发图像采集和图像处理程序
LabVIEW开发图像采集和图像处理程序
33 2
|
1月前
|
算法 计算机视觉
LabVIEW使用图像处理检测显微图像中的白血病
LabVIEW使用图像处理检测显微图像中的白血病
21 0
|
1月前
|
传感器 算法 计算机视觉
LabVIEW 图像处理功能
LabVIEW 图像处理功能
14 0
|
9月前
|
算法 数据安全/隐私保护 计算机视觉
图像处理基础
图像处理基础
72 0
|
10月前
|
PHP 数据安全/隐私保护 计算机视觉
PHPImagine 图像处理库介绍
随着网络的发展,人们对图像的需求越来越高。作为一个PHP开发者,我们在处理图像时经常会遇到一些问题,比如裁剪、缩放、加水印等。这些问题都可以通过使用图像处理库来解决。PHPImagine就是一种优秀的图像处理库。
58 0
|
计算机视觉 异构计算
|
Java 数据安全/隐私保护 计算机视觉
thumbnails图像处理库的使用
thumbnails是Java一个优秀的图像处理库,可以对图片进行压缩、加水印、裁剪、更改格式等功能。下面为大家介绍使用这个图像处理库。
thumbnails图像处理库的使用