/** 內容包含laplacian、sobel、box filter、power law
* output為graylevel image
*/
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.*;
/**
*
* @author Zucker
*/
public class Function extends javax.swing.JPanel {
int mode = 0,height,width,maxfilter=0,minfilter=255;
BufferedImage processing = null,source = null;
BufferedImage Grayimage=null;
BufferedImage a,b,c,d,e,f,gi,h;
Image A,B,C,D,E,F,GI,H;
Image img =null;
Image img_pross=null;
Frame ancestor = null;
/** Creates new form Function */
public Function() {
initComponents();
}
public void open(File src, Frame ancestor) throws IOException {
// this.mode = mode;
source = ImageIO.read(src);
processing = new BufferedImage(source.getWidth(), source.getHeight(), source.getType());
processing.setData(source.getData());
this.ancestor = ancestor;
ancestor.setBounds(ancestor.getX(), ancestor.getY(), processing.getWidth() + ancestor.getWidth() - this.getWidth(),
processing.getHeight() + ancestor.getHeight() - this.getHeight());
width=processing.getWidth();
height=processing.getHeight();
/*Part A: 原始圖片*/
a=processing;
mode=0;
repaint();
}
public void HW2(int mode)
{
this.mode= mode;
BufferedImage Lapimage = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
BufferedImage Sobimage = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
int[][] Lapbinary=new int[width][height];
/*Part B: Laplacian*/
float[] elements = { 0.0f, -1.0f, 0.0f,
-1.0f, 4.f,-1.0f,
0.0f, -1.0f, 0.0f};
Kernel kernel = new Kernel(3, 3, elements);
ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP,null);
Lapimage= cop.filter(processing,null);
b=Lapimage;
/*Part C: 原圖+Laplacian*/
for(int i=0;i<width;i++)
for(int j=0;j<height;j++)
{ int rgbA=processing.getRGB(i,j);
int r=(rgbA&0x00ff0000)>>16; // 取得紅色的資料
int g1=(rgbA&0x0000ff00)>>8; // 取得綠色資料
int b1=rgbA&0x000000ff; // 取得藍色資料
int grayA=(r+g1+b1)/3; // 計算灰階值
int rgbB=Lapimage.getRGB(i,j);
int grayB=(rgbB&0x00ff0000)>>16;
int sum=grayA+grayB;
if (sum>maxfilter)
maxfilter=sum;
else if (sum<minfilter)
minfilter=sum;
}
/*合併之後之正規化*/
BufferedImage Laplician = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
for(int i=0;i<width;i++)
for(int j=0;j<height;j++)
{
int rgbA=processing.getRGB(i,j);
int r=(rgbA&0x00ff0000)>>16; // 取得紅色的資料
int g2=(rgbA&0x0000ff00)>>8; // 取得綠色資料
int b2=rgbA&0x000000ff; // 取得藍色資料
int grayA=(r+g2+b2)/3; // 計算灰階值
int rgbB=Lapimage.getRGB(i,j);
int grayB=(rgbB&0x00ff0000)>>16;
int sum=grayA+grayB;
sum=(sum-minfilter)*255/maxfilter;
int rgb=(0xff000000|(sum<<16)|(sum<<8)|sum);
Lapbinary[i][j]=sum; //儲存Lapimage的灰階值
Laplician.setRGB(i,j,rgb);
c=Laplician;
}
/*Part D: Sobel*/
int[][] rgbArray=new int[width][height];
int[][] G=new int[width][height];
int[][] Gx=new int[width][height];
int[][] Gy=new int[width][height];
maxfilter=0; minfilter=255;
for( int i = 0; i < width; i++ )
for( int j = 0; j < height; j++ )
{
rgbArray[i][j] = processing.getRGB( i, j );
int r=(rgbArray[i][j]&0x00ff0000)>>16; // 取得紅色的資料
int g=(rgbArray[i][j]&0x0000ff00)>>8; // 取得綠色資料
int b=rgbArray[i][j]&0x000000ff; // 取得藍色資料
rgbArray[i][j]=(r+g+b)/3; // 計算灰階值
}
//對灰階影像做sobel
for( int i = 0; i < width; i++ )
for( int j = 0; j < height; j++ )
{
if (i==0 || i==width-1 || j==0 || j==height-1)
Gx[i][j] = Gy[i][j] = G[i][j] = 0; // Image boundary cleared
else{
Gx[i][j] = rgbArray[i+1][j-1] + 2*rgbArray[i+1][j] + rgbArray[i+1][j+1] -
rgbArray[i-1][j-1] - 2*rgbArray[i-1][j] - rgbArray[i-1][j+1];
Gy[i][j] = rgbArray[i-1][j+1] + 2*rgbArray[i][j+1] + rgbArray[i+1][j+1] -
rgbArray[i-1][j-1] - 2*rgbArray[i][j-1] - rgbArray[i+1][j-1];
G[i][j] = Math.abs(Gx[i][j]) + Math.abs(Gy[i][j]);
if(G[i][j]>maxfilter)
maxfilter=G[i][j];
else if (G[i][j]<minfilter)
minfilter=G[i][j];
}
}
//合併後正規化
for( int i = 0; i < width; i++ )
for( int j = 0; j < height; j++ )
{
G[i][j]=(G[i][j]-minfilter)*255/maxfilter-minfilter;
}
//灰階矩陣轉image
for( int i = 0; i < width; i++ )
for( int j = 0; j < height; j++ )
{
int temp=(0xff000000|(G[i][j]<<16)|(G[i][j]<<8)|G[i][j]);
Sobimage.setRGB(i,j,temp);
}
d=Sobimage;
/*Part E: Bluring Sobel image (d)*/
Kernel Box = new Kernel(5, 5, new float[] {0.04f, 0.04f, 0.04f, 0.04f, 0.04f,
0.04f, 0.04f, 0.04f, 0.04f, 0.04f,
0.04f, 0.04f, 0.04f, 0.04f, 0.04f,
0.04f, 0.04f, 0.04f, 0.04f, 0.04f,
0.04f, 0.04f, 0.04f, 0.04f, 0.04f });
ConvolveOp BoxF = new ConvolveOp(Box, ConvolveOp.EDGE_ZERO_FILL, null);
Sobimage= BoxF.filter(Sobimage,null);
e=Sobimage;
/*Part F: Product C and E*/
maxfilter=0; minfilter=255;
BufferedImage Product = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
for( int i = 0; i < width; i++ )
for( int j = 0; j < height; j++ )
{
int C=(c.getRGB(i,j)&0x00ff0000)>>16;
int E=(e.getRGB(i,j)&0x00ff0000)>>16;
int temp=C*E;
if(temp>maxfilter)
maxfilter=temp;
else if (temp<minfilter)
minfilter=temp;
}
//乘積之後的正規化
for( int i = 0; i < width; i++ )
for( int j = 0; j < height; j++ )
{
int C=(c.getRGB(i,j)&0x00ff0000)>>16;
int E=(e.getRGB(i,j)&0x00ff0000)>>16;
int temp=(C*E-minfilter)*255/(maxfilter-minfilter);
temp=(0xff000000|(temp<<16)|(temp<<8)|temp);
Product.setRGB(i,j,temp);
}
f=Product;
/*Part G: A Pluse F*/
maxfilter=0; minfilter=255;
int[][] rgbArrayA=new int[width][height];
int[][] rgbArrayB=new int[width][height];
BufferedImage APF = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
for( int i = 0; i < width; i++ )
for( int j = 0; j < height; j++ )
{
rgbArrayA[i][j] = processing.getRGB( i, j );
int r=(rgbArrayA[i][j]&0x00ff0000)>>16; // 取得紅色的資料
int g=(rgbArrayA[i][j]&0x0000ff00)>>8; // 取得綠色資料
int b=rgbArrayA[i][j]&0x000000ff; // 取得藍色資料
rgbArrayA[i][j]=(r+g+b)/3; // 計算灰階值
rgbArrayB[i][j] = f.getRGB( i, j );
r=(rgbArrayB[i][j]&0x00ff0000)>>16; // 取得紅色的資料
g=(rgbArrayB[i][j]&0x0000ff00)>>8; // 取得綠色資料
b=rgbArrayB[i][j]&0x000000ff; // 取得藍色資料
rgbArrayB[i][j]=(r+g+b)/3; // 計算灰階值
rgbArrayA[i][j]=rgbArrayA[i][j]+rgbArrayB[i][j];
if(rgbArrayA[i][j]>maxfilter)
maxfilter=rgbArrayA[i][j];
else if (rgbArrayA[i][j]<minfilter)
minfilter=rgbArrayA[i][j];
}
//G正規化
for( int i = 0; i < width; i++ )
for( int j = 0; j < height; j++ )
{
rgbArrayA[i][j]=rgbArrayA[i][j]*255/maxfilter;
int temp=(0xff000000|(rgbArrayA[i][j]<<16)|(rgbArrayA[i][j]<<8)|rgbArrayA[i][j]);
APF.setRGB(i,j,temp);
}
gi=APF;
/*Part H: PowerLaw G*/
double dtemp,dmaxfilter=0.0,dminfilter=255.0,gamma=0.6;
BufferedImage Po = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
for(int i=0;i<width;i++)
for(int j=0;j<height;j++)
{
int rgbA=APF.getRGB(i,j);
int r=(rgbA&0x00ff0000)>>16;
dtemp=Math.pow(r,gamma);
if(dtemp>dmaxfilter)
dmaxfilter=dtemp;
else if (dtemp<dminfilter)
dminfilter=dtemp;
}
for(int i=0;i<width;i++)
for(int j=0;j<height;j++)
{
int rgbA=APF.getRGB(i,j);
int r=(rgbA&0x00ff0000)>>16;
dtemp=Math.pow(r,gamma);
int temp=(int)((dtemp-dminfilter)*255/dmaxfilter);
int rgb=((0xff000000|(temp<<16)|(temp<<8)|temp));
Po.setRGB(i,j,rgb);
}
h=Po;
repaint();
}
文章標籤
全站熱搜