2010年11月2日 星期二

JAVA影像處理程式設計系列 (一)影像直方圖(histogram)

影像的直方圖是以影像的像素值當橫座標,像素值出現的次數(或比例)當縱座標,所繪製的圖形。在影像分析上由直方圖可取得基本的分析資訊,彩色的影像可對RGB三種成分分別作直方圖分析,亦可先求出像素的平均值再作直方圖分析。本文中的程式包含兩個類別,PlotHistogram類別的功能是繪製直方圖,PlotHistogramTest類別內包含main()方法,主要是輸入及顯示影像、改變影像為Banded Sample Model(可固定影像格式,方便處理)及計算像素值出現的次數。(圖一)、(圖二)、(圖三)、(圖四)分別是輸入的原始影像及程式繪製的R、G、B直方圖,由藍色成分的直方圖可發現此張影像的藍色成分很少。還有,程式沒有作repaint處理,直方圖視窗請移到沒有被蓋住的地方,最小化後再最大化即會顯示。

//plot histogram
//constructor parameters as below:
//1.title
//2.H--histogram array;data type is int; H.length= 256
//3.max is the max value of H;data type is float
// only plot one band for each instance
import javax.swing.*;
import java.awt.*;
class PlotHistogram extends JFrame
{
int[] H=new int[256];
int max;
PlotHistogram(String title, int[] H, int max)
{
super(title);
this.H=H;
this.max=max;
setSize(600,450);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
super.paint(g); //call superclass's paint method
g.setColor(Color.white);
g.fillRect(0,0,600,450);
g.setColor(Color.black);
g.drawLine(50,50,50,400);
g.drawLine(50,400,550,400);
int width=600;
int height=450;
float stepX = (float) (width-100)/ (float)H.length;
float stepY = (float) (height-100 )/ (float) max;
for ( int i = 0; i < H.length; i++ )
{
int x = (int) ((float)i*stepX);
int y = (int) ((float)H[i]*stepY);
g.drawLine(x+50,400,x+50,height-50-y);
if(i%50==0)
{
String s=Integer.toString(i);
g.drawString(s,x+50,420);
}
}
}
}



import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import javax.media.jai.*;
import javax.media.jai.operator.*;
import javax.media.jai.widget.ScrollingImagePanel;
class PlotHistogramTest
{
public static void main(String args[])
{
int[][] histogram=new int[3][256];
int[] max={0,0,0};
// input original image
Scanner scanner= new Scanner(System.in);
System.out.println("Plase input the path and file name of the image file.");
String fileName = scanner.next();
RenderedOp img0 = JAI.create("fileload",fileName);
//display original image
JFrame f=new JFrame();
f.setTitle(fileName+"(original image)");
f.add(new ScrollingImagePanel(img0,img0.getWidth(),img0.getHeight()));
f.pack();
f.show();
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
//reformat image----image0 to image1
//BandedSampleModel, DataBuffer.TYPE_BYTE
int img1Width=img0.getWidth();
int img1Height=img0.getHeight();
ImageLayout layout=new ImageLayout();
layout.setSampleModel(new BandedSampleModel(DataBuffer.TYPE_BYTE,img1Width,img1Height,3));
RenderingHints rh=new RenderingHints(JAI.KEY_IMAGE_LAYOUT,layout);
RenderedOp img1=FormatDescriptor.create(img0,DataBuffer.TYPE_BYTE,rh);

//get pixel data from image 1
Raster ras1=img1.getData();
DataBuffer daBuf1=ras1.getDataBuffer();

//computing histogram
for(int i=0;i<3;i++) //initialize histogrsm with 0
for(int j=0;j<256;j++)
histogram[i][j]=0;

for(int bank=0; bank<3; bank++)
for(int i=0;i for(int j=0;j {
int g=daBuf1.getElem(bank,i*img1Width+j);
histogram[bank][g]=histogram[bank][g]+1;
}
//plot histogram---using "plotHistogram class"
//looking for the max value
for(int bank=0; bank<3; bank++)
for (int g=0; g< 256; g++)
{
if (histogram[bank][g]>max[bank])
max[bank]=histogram[bank][g];
}
//building R, G, B plotHistogram instance
PlotHistogram plotRed=new PlotHistogram("histogram-RED",histogram[0],max[0]);
PlotHistogram plotGREEN=new PlotHistogram("histogram-GREEN",histogram[1],max[1]);
PlotHistogram plotBLUE=new PlotHistogram("histogram-BLUE",histogram[2],max[2]);
}
}













沒有留言:

張貼留言