图像边缘处理Sobel(Java代码实现)
一、简介
边缘检测是图像中十分重要的一部分,边缘检测是图像处理和计算机视觉中,尤其是特征提取中的一个研究领域。
二、思想
Sobel算子中:
- Gx= [f(x+1,y-1)+2f(x+1,y)+f(x+1,y+1)]-[f(x-1,y-1)+2f(x-1,y)+f(x-1,y+1)]
- Gy = = [f(x-1,y-1) + 2f(x,y-1) + f(x+1,y-1)]-[f(x-1, y+1) + 2*f(x,y+1)+f(x+1,y+1)]
- 注意:在opencv中最后的结果是:<0的就按照0计算,>255的会按照255计算
- 这里我们采用的是:<0取绝对值,>255的取值255**
详细信息请看;
sobel算法
三、代码演示
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* 一般都是行和列都是减2行和列
* Sobel算子中:
* Gx= [f(x+1,y-1)+2*f(x+1,y)+f(x+1,y+1)]-[f(x-1,y-1)+2*f(x-1,y)+f(x-1,y+1)]
* Gy = = [f(x-1,y-1) + 2f(x,y-1) + f(x+1,y-1)]-[f(x-1, y+1) + 2*f(x,y+1)+f(x+1,y+1)]
* 注意:在opencv中最后的结果是:<0的就按照0计算,>255的会按照255计算
* 这里我们采用的是:<0取绝对值,>255的取值255
* */
public class ImageSobel extends Canvas {
JFrame jframe = new JFrame("图像Sobel处理");
BufferedImage bufferedImage,bufferedImage_gray,bufferedImage_sobel;
Image image,image_gray,image_sobel;
int rgb[][][],width,height; // x,y所对应的灰度值;
BufferedImage bufferedImage_end;
// 构造方法,面板显示
public ImageSobel(){
try {
bufferedImage = ImageIO.read(new File("D:\\EDge下载地址\\lena.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
width = bufferedImage.getWidth();
height =bufferedImage.getHeight();
// 设置最后的图片
bufferedImage_end = new BufferedImage(width,height, BufferedImage.TYPE_3BYTE_BGR );
rgb = new int[width+1][height+1][1];
jframe.setLayout(null);
// 定义位置
jframe.setBounds(200,200,800,600);
this.setBounds(0,0,800,600);
jframe.add(this);
jframe.setVisible(true);
bufferedImage_gray =gray(bufferedImage);
bufferedImage_end = sobel(bufferedImage_gray);
// 存起来
try{
ImageIO.write(bufferedImage_end,"png",new File("人脸9.png"));
System.out.println("修改后的图片已保存");
} catch(IOException e){
System.out.println("保存图片有错误");
e.printStackTrace();
}
}
@Override
public void paint(Graphics g) {
image =(Image)bufferedImage;
bufferedImage_gray =gray(bufferedImage);
image_sobel =(Image)sobel(bufferedImage_gray);
g.drawImage(image,0,0,null); // 显示原图
g.drawLine(350,10,350,600); // 中间的线
g.drawImage(image_sobel,370,0,null); // 显示灰度图片
}
// 先进行灰度处理
public BufferedImage gray(BufferedImage b){
bufferedImage_gray = new BufferedImage(width,height, BufferedImage.TYPE_3BYTE_BGR );
// 双层循环更改图片的RGB值,把得到的灰度值存到bufferedImage_end中,然后返回bufferedImage_end
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Color color = new Color(bufferedImage.getRGB(x,y));
int gray = (int)(color.getRed() * 0.299 + color.getGreen() * 0.587 + color.getBlue() *0.114);
Color color_end = new Color(gray,gray,gray);
bufferedImage_gray.setRGB(x,y,color_end.getRGB());
rgb[x][y][0] = gray;
}
}
return bufferedImage_gray;
}
// 然后进行Sobel运算;传进去一个bufferedImage是灰度处理后的,传出来的就是Sobel结果
// 这里我们采用的是:<0取绝对值,>255的取值255
public BufferedImage sobel(BufferedImage bufferedImage){
bufferedImage_sobel = new BufferedImage(width,height, BufferedImage.TYPE_3BYTE_BGR );
// 去掉两行和两列
for (int y = 1; y < height-1; y++) {
for (int x = 1; x < width-1; x++) {
int Gx = rgb[x+1][y-1][0] +2*rgb[x+1][y][0]+rgb[x+1][y+1][0]-rgb[x-1][y-1][0] -2*rgb[x-1][y][0]-rgb[x-1][y+1][0];
int Gy = rgb[x-1][y-1][0] +2*rgb[x][y-1][0]+rgb[x+1][y-1][0] -rgb[x-1][y+1][0] - 2*rgb[x][y+1][0] -rgb[x+1][y+1][0];
if (Gx < 0){
Gx = -1*Gx;
}
if (Gy <0){
Gy = -1*Gy;
}
int s = Gx +Gy;
// 里面的这个就是阈值
if (s < 70){
s = 255;
}else{
s = 0;
}
Color color = new Color(s,s,s);
bufferedImage_sobel.setRGB(x,y,color.getRGB());
}
}
return bufferedImage_sobel;
}
public static void main(String[] args) {
new ImageSobel();
}
}