电路板识别任务主要是对图形图像的处理,以前从来没有接触过这块,还好网上有好多的资源关于图像的处理。大致了解了下。学会了二值化处理图像。了解一小部分关于这块的内容,比如RGB,像素这些东西。我把图片的尺寸比如800*600先扫描一遍,然后每一个点就是一个像素它由RGB三个颜色分量组成颜色。处理的时候主要是根据这些RGB值进行处理。一幅图像就像一个大的数组,然后就是对数组里的特殊点进行处理。
下面介绍下这个简单的任务:原图是(突然发现原图太大了40多M传不上来)我就截个图吧
需求是找出图中的黑色电路部分的裂缝。
先二值化处理,很多的二值化算法,比如大津算法,迭代算法等处理后的不是我想要的。先来看下java自带的二值化处理后的图片:
其主要代码为:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageDemo {
public void binaryImage() throws IOException{
File file = new File("G://te.png");
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);//重点,技巧在这个参数BufferedImage.TYPE_BYTE_BINARY
for(int i= 0 ; i < width ; i++){
for(int j = 0 ; j < height; j++){
int rgb = image.getRGB(i, j);
grayImage.setRGB(i, j, rgb);
}
}
File newFile = new File("G://image//ttt.png");
ImageIO.write(grayImage, "jpg", newFile);
}
public void grayImage() throws IOException{
File file = new File("G://process.png");
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);//重点,技巧在这个参数BufferedImage.TYPE_BYTE_GRAY
for(int i= 0 ; i < width ; i++){
for(int j = 0 ; j < height; j++){
int rgb = image.getRGB(i, j);
grayImage.setRGB(i, j, rgb);
}
}
File newFile = new File("G://test16.png");
ImageIO.write(grayImage, "png", newFile);
}
public static void main(String[] args) throws IOException {
ImageDemo demo = new ImageDemo();
demo.binaryImage();
demo.grayImage();
}
}
但是这个并不是我想要的结果,我只想得到黑色的电路其他均为白色的背景。后来我发现这个图的特点,就是它的RGB分量占的不一样。尤其是blue分量,黑色的电路部分平均值小于50我利用这个值进行处理图像。得到下图:
虽然不是很好,但是够用了。主要是了解下图像的处理。
代码如下:
/**
*二值化图片找出黑色的部分
*
**/
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
public class DealPicture {
private BufferedImage bufferedImage = null;
private int image_width = 0;
private int image_heigh = 0;
private File file = null;
public DealPicture(String filePath) {
file = new File(filePath);
}
//获得BufferedImage
public BufferedImage getBufferedImage() {
BufferedImage buffer = null;
try {
buffer = ImageIO.read(file);
} catch(IOException e) {
System.out.println("读取图片发生错误");
e.printStackTrace();
}
return buffer;
}
//对获得BufferedImage进行处理获取图片的所有的像素
public int[][] getPiexl() {
bufferedImage = getBufferedImage();
image_heigh = bufferedImage.getHeight();
image_width = bufferedImage.getWidth();
int iamge_rgb = bufferedImage.getRGB(0,0);
System.out.println("图片的宽度是: " + image_width + " " + "图片的高度是: " + image_heigh + " 像素点共有 " + (image_width*image_heigh));
int[][] image_piexl = new int[image_width][image_heigh];
for(int x = 0;x<image_width;x++) {
for(int y = 0;y<image_heigh;y++ ) {
//image_piexl[x][y] = getGray(bufferedImage.getRGB(x,y));
image_piexl[x][y] = bufferedImage.getRGB(x,y);
}
}
return image_piexl;
}
//计算每个像素的灰度值
public int getGray(int rgb) {
Color color = new Color(rgb);
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
int rgb_averge = (r+g+b)/3;
//int rgb_gray =(int)( r*30 + g*29 + b*50)/100;
return rgb_averge;
//return rgb_gray;
}
//
/**
*
*
*
**/
public void createImage() {
int flag = 30;
int[][] newImage = getPiexl();
BufferedImage buff = new BufferedImage(image_width,image_heigh,BufferedImage.TYPE_BYTE_BINARY);
Color color = null;
for(int x = 0;x<image_width;x++) {
for(int y = 0;y<image_heigh;y++){
color = new Color(newImage[x][y]);
int g = color.getGreen();
//System.out.println("处理后的像素点灰度是: " + newImage[x][y]);
//if(Math.abs((newImage[x][y]-flag))<5 && newImage[x][y]<35) {
if(g<52) {
int min_rgb = new Color(0,0,0).getRGB();
buff.setRGB(x,y,min_rgb);
flag = newImage[x][y];
}
else {
int max_rgb = new Color(255,255,255).getRGB();
buff.setRGB(x,y,max_rgb);
}
//测试生成一副灰度图像
//buff.setRGB(x,y,newImage[x][y]);
}
}
try{
ImageIO.write(buff, "png", new File("G://image//text4.png"));
} catch(IOException e) {
System.out.println("生成图片失败");
e.printStackTrace();
}
}
public static void main(String[] args){
DealPicture deal = new DealPicture("G://Image2.png");
deal.createImage();
}
}
最后就是找裂缝了:最终图像如下(红色部分为裂缝):
这个任务历时三天,主要是接触下图像的处理,当然这个是处理的很不好的,就当学习下了
开始截的图穿不上来就从新搞下: