**
Java中实现图像的卷积效果
**
图像的卷积: 指的是使用一个卷积核(kernel)对图像中的每一个像素进行一些列操作。卷积核(算子)是用来做图像处理时的矩阵,图像处理时也称为掩膜,是于原图像做运算的参数。卷积核通常是一个方形的网格结构,该区域上的每一个方格都有一个权重值,如下图,利用一个3 * 3的卷积核与图像上的相应大小的区域进行卷积运算,即对应相乘再相加,0 * 4 + 0 * 0 + 0 * 0 + 0 * 0 + 0 * 1 + 1 * 0 + 0 * 0 + 1 * 0 + 2 *(-4) = -8.
Java中实现的效果:
主类:
package com.yf1105;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class DrawF {
public static void main(String[] args) {
DrawF drawpanel = new DrawF();
drawpanel.showUI();
}
public void showUI() {
JFrame jf = new JFrame();
jf.setTitle("图片");
jf.setSize(800, 800);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new FlowLayout());
jf.setLocationRelativeTo(null);
jf.setVisible(true);
//获取画笔一定要在可见之后
Graphics g = jf.getGraphics();
//添加监听器
DrawListener mouse = new DrawListener();
jf.addMouseListener(mouse);
//将主类中的画笔传给监听器类中去
mouse.g = g;
}
}
监听类:
package com.yf1105;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class DrawListener implements MouseListener{
Graphics g;
public int[][] getImagePixel(String path){
File file = new File(path);
BufferedImage bufferImage = null;
try {
bufferImage = ImageIO.read(file);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
int w = bufferImage.getWidth();
int h = bufferImage.getHeight();
int [][] pixelArray = new int[w][h];
for(int i = 0; i < w; i++) {
for(int j = 0; j < h; j++) {
//读取每个位置的像素点对应的RGB值
int pixel = bufferImage.getRGB(i, j);
pixelArray[i][j] = pixel;
}
}
return pixelArray;
}
//在界面上画出ia数组图像,即卷积结果
public void drawjj(Graphics g) {
int[][] ia = getImagePixel("image/3.jpg");
ia = valide(ia, kArray);
for(int i = 0; i<ia.length; i++) {
for(int j = 0; j<ia[0].length; j++) {
int pie = ia[i][j];
Color color = new Color(pie,pie,pie);
g.setColor(color);
g.fillRect(i, j, 1, 1);
}
}
}
//卷积核
float[][] kArray = {{-1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1}, {-1, -1, 25, -1, -1}, {-1, -1, -1, -1, -1}, {-1, -1, -1, -1, -1}};
//实现卷积操作
public static int[][] valide(int[][] src, float[][] filter){
int[][] tem = new int[filter.length][filter[0].length];
int valideWidth = src[0].length - filter[0].length;
int valideHeigth = src.length - filter.length + 1;
int[][] valide = new int[valideHeigth][valideWidth];
for(int i = 0; i<valideHeigth; i++) {
for(int j = 0; j<valideWidth; j++) {
for(int y = 0; y<filter.length; y++) {
for(int z = 0; z<filter[0].length; z++ ) {
tem[y][z] = (int)((src[i+y][j+z])*filter[y][z]);
}
}
int kk = 0;
for(int y = 0; y<filter.length; y++) {
for(int z = 0; z<filter[0].length; z++) {
kk += tem[y][z];
}
}
if(kk<0)kk=0;
if(kk>255)kk=255;
valide[i][j] = kk;
}
}
return valide;
}
public void mouseClicked(MouseEvent e) {
System.out.println("点击");
drawjj(g);
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
实现效果:
原图为: