资源简介
代码片段和文件信息
/*
* Edge.java
*
* Created on 2007年12月10日 下午8:42
*
*/
package edu.zjut.edgedetection;
import java.awt.*;
import java.awt.image.*;
import java.awt.color.ColorSpace;
/**
* 边缘检测
* 实现了Canny边缘检测算法,提供静态函数 canny
* @author 余乐伟
*/
public class Edge
{
/**
* Canny边缘检测
* 函数 Canny 采用 CANNY 算法发现输入图像的边缘而且在输出图像中标识这些边缘。
* threshold1和threshold2 当中的小阈值用来控制边缘连接,大的阈值用来控制强边缘的初始分割。
* Canny边缘检测算法步骤:
* step1:用高斯滤波器平滑图象;
* step2:计算梯度的幅值和方向;
* step3:对梯度幅值进行非极大值抑制;
* step4:用双阈值算法检测和连接边缘
* @param sourceImage 输入图像
* @param lowThreshold 第一个阈值(低)
* @param highThreshold 第二个阈值(高)
* @return 用 Canny 边缘检测算法得到的边缘图
*/
public static BufferedImage canny(BufferedImage sourceImage int lowThreshold int highThreshold)
{
height = sourceImage.getHeight();
width = sourceImage.getWidth();
int picsize = width * height;
//先把图片转换为灰度图
ColorSpace grayCS = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp colorConvertOp = new ColorConvertOp(grayCSnull);
sourceImage=colorConvertOp.filter(sourceImage null);
//sourceImage = ColorConvert.grayImage(sourceImage);
//对原图高斯滤波
sourceImage = gaussianSmooth(sourceImage);
//梯度幅值表
int [] gradeMagnitude = new int[picsize];
//梯度方向表
int [] gradeOrientation = new int[picsize];
//计算方向导数和梯度的幅度
grade(sourceImage gradeMagnitude gradeOrientation);
//应用非最大抑制细化
int [] edgeImage= NonmMaxSuppress(gradeMagnitude gradeOrientation);
//边界提取与轮廓跟踪
return thresholdingTracker(edgeImage gradeMagnitude lowThreshold highThreshold);
}
/**
* 用高斯滤波器平滑原图像
* @param sourceImage 输入图像
* @return 高斯滤波后的图像
*/
private static BufferedImage gaussianSmooth(BufferedImage sourceImage)
{
//高斯模板
float[] elements =
{1.0f/16.0f 2.0f/16.0f 1.0f/16.0f
2.0f/16.0f 4.0f/16.0f 2.0f/16.0f
1.0f/16.0f 2.0f/16.0f 1.0f/16.0f};
BufferedImage bi = new BufferedImage(sourceImage.getWidth() sourceImage.getHeight() BufferedImage.TYPE_INT_RGB);
Graphics2D big = bi.createGraphics();
big.drawImage(sourceImage 0 0 null);
//创建一个Kernel
Kernel kernel = new Kernel(3 3 elements);
ConvolveOp blur = new ConvolveOp(kernel ConvolveOp.EDGE_NO_OPnull);
sourceImage = blur.filter(bi null);
return sourceImage;
}
/**
* 计算方向导数和梯度的幅度
* @param grayImage 要计算的图象
* @param gradeMagnitude 要在其中存储结果的梯度数组
* @param gradeOrientation 要在其中存储结果的方向导数组
*/
private static void grade(BufferedImage grayImage int [] gradeMagnitude int [] gradeOrientation)
{
int he
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 8493 2009-07-15 15:02 CannyEdge.java
----------- --------- ---------- ----- ----
8493 1
评论
共有 条评论