电路板识别。

来源:互联网 发布:node.js 搭建动态网站 编辑:程序博客网 时间:2024/05/01 14:32

分类: java 29人阅读 评论(0) 收藏 举报

电路板识别任务主要是对图形图像的处理,以前从来没有接触过这块,还好网上有好多的资源关于图像的处理。大致了解了下。学会了二值化处理图像。了解一小部分关于这块的内容,比如RGB,像素这些东西。我把图片的尺寸比如800*600先扫描一遍,然后每一个点就是一个像素它由RGB三个颜色分量组成颜色。处理的时候主要是根据这些RGB值进行处理。一幅图像就像一个大的数组,然后就是对数组里的特殊点进行处理。

下面介绍下这个简单的任务:原图是(突然发现原图太大了40多M传不上来)我就截个图吧




需求是找出图中的黑色电路部分的裂缝。 

先二值化处理,很多的二值化算法,比如大津算法,迭代算法等处理后的不是我想要的。先来看下java自带的二值化处理后的图片:




其主要代码为:

[java] view plaincopy
  1. import java.awt.image.BufferedImage;    
  2. import java.io.File;    
  3. import java.io.IOException;    
  4.     
  5. import javax.imageio.ImageIO;    
  6.     
  7. public class ImageDemo {    
  8.     
  9.     public void binaryImage() throws IOException{    
  10.         File file = new File("G://te.png");    
  11.         BufferedImage image = ImageIO.read(file);    
  12.         
  13.         int width = image.getWidth();    
  14.         int height = image.getHeight();    
  15.         
  16.         BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);//重点,技巧在这个参数BufferedImage.TYPE_BYTE_BINARY    
  17.         for(int i= 0 ; i < width ; i++){    
  18.             for(int j = 0 ; j < height; j++){    
  19.                 int rgb = image.getRGB(i, j);    
  20.                 grayImage.setRGB(i, j, rgb);    
  21.             }    
  22.         }    
  23.         
  24.         File newFile = new File("G://image//ttt.png");    
  25.         ImageIO.write(grayImage, "jpg", newFile);    
  26.     }    
  27.         
  28.     public void grayImage() throws IOException{    
  29.         File file = new File("G://process.png");    
  30.         BufferedImage image = ImageIO.read(file);    
  31.         
  32.         int width = image.getWidth();    
  33.         int height = image.getHeight();    
  34.         
  35.         BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);//重点,技巧在这个参数BufferedImage.TYPE_BYTE_GRAY    
  36.         for(int i= 0 ; i < width ; i++){    
  37.             for(int j = 0 ; j < height; j++){    
  38.                 int rgb = image.getRGB(i, j);    
  39.                 grayImage.setRGB(i, j, rgb);    
  40.             }    
  41.         }    
  42.        
  43.         File newFile = new File("G://test16.png");    
  44.         ImageIO.write(grayImage, "png", newFile);    
  45.     }    
  46.         
  47.     public static void main(String[] args) throws IOException {    
  48.         ImageDemo demo = new ImageDemo();    
  49.         demo.binaryImage();    
  50.         demo.grayImage();    
  51.    }    
  52.    
  53. }    

但是这个并不是我想要的结果,我只想得到黑色的电路其他均为白色的背景。后来我发现这个图的特点,就是它的RGB分量占的不一样。尤其是blue分量,黑色的电路部分平均值小于50我利用这个值进行处理图像。得到下图:



虽然不是很好,但是够用了。主要是了解下图像的处理。

代码如下:

[java] view plaincopy
  1. /** 
  2.  *二值化图片找出黑色的部分 
  3.  * 
  4.  **/  
  5.   
  6.  import java.awt.image.BufferedImage;  
  7.  import javax.imageio.ImageIO;  
  8.  import java.awt.Color;  
  9.  import java.io.File;  
  10.  import java.io.IOException;  
  11.   
  12.  public class DealPicture {  
  13.   
  14.     private BufferedImage bufferedImage = null;  
  15.     private int image_width = 0;  
  16.     private int image_heigh = 0;  
  17.     private File file = null;  
  18.   
  19.     public DealPicture(String filePath) {  
  20.         file = new File(filePath);  
  21.     }  
  22.       
  23.   
  24.     //获得BufferedImage  
  25.   
  26.     public BufferedImage getBufferedImage() {  
  27.           
  28.         BufferedImage buffer = null;  
  29.         try {  
  30.             buffer = ImageIO.read(file);  
  31.         } catch(IOException e) {  
  32.             System.out.println("读取图片发生错误");  
  33.             e.printStackTrace();  
  34.         }  
  35.   
  36.         return buffer;  
  37.     }  
  38.   
  39.     //对获得BufferedImage进行处理获取图片的所有的像素  
  40.   
  41.     public int[][] getPiexl() {  
  42.         bufferedImage = getBufferedImage();  
  43.         image_heigh = bufferedImage.getHeight();  
  44.         image_width = bufferedImage.getWidth();  
  45.         int iamge_rgb = bufferedImage.getRGB(0,0);  
  46.         System.out.println("图片的宽度是: " + image_width + "  " + "图片的高度是: " + image_heigh + "  像素点共有 " + (image_width*image_heigh));  
  47.         int[][] image_piexl = new int[image_width][image_heigh];  
  48.         for(int x = 0;x<image_width;x++) {  
  49.             for(int y = 0;y<image_heigh;y++ ) {  
  50.                 //image_piexl[x][y] = getGray(bufferedImage.getRGB(x,y));  
  51.                 image_piexl[x][y] = bufferedImage.getRGB(x,y);  
  52.             }  
  53.         }  
  54.   
  55.         return image_piexl;  
  56.     }  
  57.   
  58.     //计算每个像素的灰度值  
  59.     public int getGray(int rgb) {  
  60.         Color color = new Color(rgb);  
  61.         int r = color.getRed();  
  62.         int g = color.getGreen();  
  63.         int b = color.getBlue();  
  64.         int rgb_averge = (r+g+b)/3;  
  65.         //int rgb_gray =(int)( r*30 + g*29 + b*50)/100;  
  66.         return rgb_averge;  
  67.         //return rgb_gray;  
  68.     }  
  69.   
  70.     //  
  71.     /** 
  72.      * 
  73.      * 
  74.      * 
  75.      **/  
  76.   
  77.     public void createImage() {  
  78.         int flag = 30;  
  79.         int[][] newImage = getPiexl();  
  80.         BufferedImage buff = new BufferedImage(image_width,image_heigh,BufferedImage.TYPE_BYTE_BINARY);  
  81.         Color color = null;  
  82.         for(int x = 0;x<image_width;x++) {  
  83.             for(int y = 0;y<image_heigh;y++){  
  84.                 color = new Color(newImage[x][y]);  
  85.                 int g = color.getGreen();  
  86.                 //System.out.println("处理后的像素点灰度是: " + newImage[x][y]);  
  87.                   
  88.                 //if(Math.abs((newImage[x][y]-flag))<5 && newImage[x][y]<35) {  
  89.                 if(g<52) {  
  90.                     int min_rgb = new Color(0,0,0).getRGB();  
  91.                     buff.setRGB(x,y,min_rgb);  
  92.                     flag = newImage[x][y];  
  93.                       
  94.                 }   
  95.                 else {  
  96.                     int max_rgb = new Color(255,255,255).getRGB();  
  97.                     buff.setRGB(x,y,max_rgb);  
  98.                       
  99.                 }  
  100.                   
  101.   
  102.                 //测试生成一副灰度图像  
  103.                 //buff.setRGB(x,y,newImage[x][y]);  
  104.                   
  105.             }  
  106.         }  
  107.         try{  
  108.             ImageIO.write(buff, "png"new File("G://image//text4.png"));   
  109.         } catch(IOException e) {  
  110.             System.out.println("生成图片失败");  
  111.             e.printStackTrace();  
  112.         }  
  113.     }  
  114.   
  115.       
  116.   
  117.     public static void main(String[] args){  
  118.         DealPicture deal = new DealPicture("G://Image2.png");  
  119.         deal.createImage();  
  120.     }  
  121.  }  
最后就是找裂缝了:最终图像如下(红色部分为裂缝):




这个任务历时三天,主要是接触下图像的处理,当然这个是处理的很不好的,就当学习下了

开始截的图穿不上来就从新搞下:


0 0
原创粉丝点击