チーム14A5

課題名

画像の8傍鮮鋭化

研究者名

3年14組63番 依光 広樹

使用言語

Java

概要

輪郭がぼやけている画像をこのフィルターにかけることで、濃度値の変化を強調し、鮮明な画像として保存することができる。
実行前
bbb.png                

実行後
abc.png

ソースコード

package CS;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;


public class Fourier{
  public static void main(String[] args){
       int tempFilter[][] = new int[3][3];
       BufferedImage srchImg  = imgRead("Disney.jpg");

      for(int i=0;i<3;i++){
           for(int j=0;j<3;j++){
               tempFilter[i][j]=-1;
           }
       }
       tempFilter[1][1]=9;
       int[][] srch = imageToArray(srchImg);
       
       int[][] gray_srImg = arrayToGray(srch);

      sharp(tempFilter, gray_srImg);
   }    

  public static BufferedImage imgRead(String file_pass){
       BufferedImage image = null;    
       try{        
           image  = ImageIO.read(new File(file_pass));    
           return image;        
       }catch(Exception e){
           return null;    
       }    
   }    

  public static int[][] imageToArray(BufferedImage image){    
       int width = image.getWidth();    
       int height = image.getHeight();    
       int[][] image_Array = new int[height][width];
       for(int y = 0; y  < height; y++){
           for(int x = 0; x < width; x++){        
               image_Array[y][x] = image.getRGB(x, y);
           }        
       }        
       return image_Array;    
   }    

  public static int[][] arrayToGray(int[][] image){    
       int width = image[0].length;        
       int height = image.length;    

      int[][] gray_image = new int[height][width];    
       for(int y = 0; y < image.length; y++){    
           for(int x = 0; x < image[0].length; x++){    
               int rgb = image[y][x] - 0xFF000000;
               int blue = (rgb & 0xFF);
               int green = (rgb & 0xFF00) >> 8;    
               int red = (rgb & 0xFF0000) >> 16;
               int gray = (blue + green + red) / 3;
               gray_image[y][x] = gray;        
           }        
       }    
       return gray_image;    
   }    

  public static void sharp(int[][] tempFilter, int[][] srch){    
       int temp_width = tempFilter[0].length;        
       int temp_height  = tempFilter.length;    
       int srch_width  = srch[0].length;
       int srch_height  = srch.length;    
       int pixel;
       int m=0, n=0;
       BufferedImage newImage = new BufferedImage(srch_width,srch_height,BufferedImage.TYPE_INT_RGB);
       BufferedImage grayImage = new BufferedImage(srch_width,srch_height,BufferedImage.TYPE_INT_RGB);
       
       for(int y = 0; y < srch_height - temp_height; y++){    
           System.out.println(y);
           for(int x = 0; x < srch_width - temp_width; x++){    
               int multi = 0;    
               for(int yt = 0; yt < temp_height; yt++){        
                   for(int xt = 0; xt < temp_width; xt++){    
                       pixel = srch[y + yt][x + xt] * tempFilter[yt][xt];    
                       multi += pixel;        
                   }
                   newImage.setRGB(x,y,multi<<16|multi<<8|multi);
                   grayImage.setRGB(x, y, srch[y][x]<<16|srch[y][x]<<8|srch[y][x]);
                   if(multi<0)
                       newImage.setRGB(x,y,0);
                   if(multi>255)
                       newImage.setRGB(x,y,0XFFFFFFFF);
               }                        
           }
           try {
               ImageIO.write(newImage, "png", new File("aaa.png"));
               ImageIO.write(grayImage, "png", new File("bbb.png"));
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       } 
   }
}

考察

カラー画像のままだと効率が悪いので、画像を配列化し、グレースケール化したものを処理している。
しかし、それでも処理時間に4分程かかってしまう。恐らく、画像に対して少しずつずらしながらフィルターをかけていくため、
処理に時間がかかってしまっている。

  • 最終更新:2014-11-17 17:18:14

このWIKIを編集するにはパスワード入力が必要です

認証パスワード