• No results found

SOURCE CODE FOR SEGMENTATION METHOD

// im port the libraries using in the program im portjava.aw t. T ransparency;

im portjava.aw t.im age.*; im port java.io.*;

im portjavax.m edia.jai.*; im portjavax.sw ing.*; im portjava.aw t.event.*;

im portcom .sun.m edia.jai.codec.*; // m ain class o f im age segem entation public class Im ageSeg

{

// get a file from input

public static File getF ileF orF ilenam e(S tring filenam e) throw sIO E xception {

File fi = new File(filenam e); if (!fi.exists())

throw new IO E xception(fi.getN am e() + " not found"); if (Ifi.isFileQ )

throw new' IO E xception(fi.getN am e() + " is not a file"); return fi;

i

i

// m ain m ethod

public static void m ain(String[] args) {

String filenam e=null;

// Prom pt for user to choose the file

JF ileC hooser chooser = new JFileC hooserQ ;

chooser.setF ileS electio nM od e(JF ileC h oo ser.F IL E S O N L Y ); intreturnVal = chooser.show O penD ialog(null);

// Confirm the user choosing file.

if(returnVal == JF ileC hooser.A PP R O V E _O P T IO N ) { //System .out.println("Y ou chose to open this file: " + // chooser.getS electedF ile().getN am e());

filenam e=chooser.getS electedF ile().getP ath(); *

f

intxpels=0 . ypels=0 ; //initialize the variables double [] intensity ;

try (

// get the input file

File fil = getFileF orF ilenam e(filenam e); //System , out. println("Segm enting...");

122

F ilelnputS tream fin l = new F ile In p u tS tream (fil);

B ufferedR eader b in l = new B ufferedR eader(new In p u tS tream R ead er(fin l)); String lin el = bin l .readLineQ ; //read in each line o f the input file

w hile (lin el != null )

)

I

String[] resu ltl = line 1 .split("\\t");

String filenam eI= nu ll,filenam e2= n ull,filenam e3 = nu ll,filenam e4= nu ll; for (int z= 0 ; z< resu ltl .length; z++)

i

i

file n a m e l= re su ltl [0]; filenam e2= resu ltl [ 1];

}

11 create output files nam e

filenam e3= filenam el .re p la c e A ll(" .tif,"_ se g m en t.tif'); filenam e4= filenam el ,re p la c e A ll(" .tif',"_output.txt");

11 create output im age file

R enderedlm age pi = JA I.create("fileload", filenam e 1); Raster im agedata=pi.getD ata();

W ritableR asterw r=null ;

W ritableR aster im ag edatal = pi.copyD ata(w r); intensity = new double [100];

// create an new object from class segm ent segm entm yseg;

m yseg = new segm ent(); try {

intzzz= 1;

File fi = getFileF orF ilenam e(filenam e2); F ilelnputS tream fin = new F ilelnputS tream (fi);

B ufferedR eader bin = new B ufferedR eader(new InputS tream R eader(fin)); String line = bin.readL ine();

B ufferedW riter out = new B ufferedW riter(new FileW riter(filenam e4)); // using progress bar

P rogressM onitorlnputS tream pin

= new P rogressM onitorInputS tream (null, fi.getN am e(), fin); P rogressM onitor pm = pin.getProgressM onitorQ ;

File file = new F ile(filenam e2); FileR eaderfr = new FileR eader(file);

LineN um berR eaderln = new LineN um berR eader(fr); int count = 0 ;

w hile (ln.readL ine() != null){ count++;

i

pm .setM axim um (count); w hile (lin e != null )

123

// initialize the variables String[] result = line.split("V\t"); intstartx=0 ;

intstartv=0 ; intlastx=0 ; intlasty=0 ;

for (int z=0 ; z< result.length; z++) (

i

// get the location from the gridding file starty=Integer.parseInt(result[O J); startx=:rlnteger.parselnt(result[ 1 ]); lasty = lnteger.parselnt(result[2 ]); lastx=lnteger.parselnt(result[3]); i i

for (int y=starty;y< = lasty;y+ + ) for (int x=startx;x< = lastx ;x++)

{

i

inti.j;

x p e ls= la stx -sta rtx + l; 11 pixels in X -axis y p e ls= la sty -sta rty + l; 11 pixels in Y -axis intensity= new double[xpels*ypels];

for (j= 0 ;j<ypels;j++) for (i= 0 ;i<xpels;i++)

intensity [i+ xpels*j]= im agedata.getS am ple(i+ startx,j+ starty,0);

11 get the intensity value

}

}

// segm enting using the m ethods from class seg m en t

m yseg.create(xpels, ypels, startx, starty, lastx, lasty, intensity); m yseg.initialize(xpels, ypels, startx, starty, lastx, lasty, intensity); if (x pels* yp els> = 100)

{

m y seg .set_d t_e_w (0.1,1,0.0251); m yseg.set_init_curve(3); i else i m yseg.set_dt_e_\v(0 . 1, 1.0 .0 1 ); m yseg.set_init_curve( 1); i i m yseg.segm ent();

out. w rite(D ouble.toS tring(m yseg.areainfo(startx+ 1 ,sta rty + 1,la stx -1,la sty -1))); out.vvrite("\t");

if (m yseg.areainfo(startx,starty,lastx,lasty)!= 0)

124

else

out. \vrite(D ouble.toString( m yseg.background_intensitym edian(startx. starty, lastx .lasty))); out.vvrite("\t");

if (m y seg .areainfo(startx + l .starty+1 .la stx -1.la sty -1)!= 0 )

out.w rite(D ou ble.to S trin g(m y seg .area_jntensity _75 pv alu e(startx + l, starty+1, la stx -1, la sty -1)));

else

out.w rite(D ou ble.toS tring(m yseg.background_intensity_75pvalue(startx, starty, lastx. lasty)));

out.w rite("\t");

out. vvrite(D ouble.toS tring(m yseg.background_intensitym edian(startx,starty,lastx,lasty))); out.w rite("\n");

// output to the output text file pm .setProgress(zzz); z z z = zzz + 1; f i inti.j; for(j=0 ;j<m yseg.ypels;j++ )

for (i= 0 ;i< m yseg .xp els- 1 ;i++)

{

if

(m yseg.sign (m y seg .area_m apping[i+ m yseg.xpels*j])!= m yseg.sign(m yseg.area_m apping[ i+ 1 + m yseg.xpels*j]))

{

if (m yseg .sign (m yseg.area_m apping[i+ m yseg.xpels*j])< 0 ) im agedatal.setS am ple(i+ m yseg .startx,j+ m yseg .starty ,0,60000); else

im ag ed atal.setS am p le(i+ l+ m y seg .startx ,j+ m y seg .starty ,0,60000); t f I ) } { inti j ; for(j=0 ;j< m yseg.ypels- 1 ;j++) for (i= 0 ;i<m yseg.xpels;i++ ) t

if

(m yseg.sign (m yseg.area_m apping[i+ m yseg.xpels*j])!= m yseg.sign(m yseg.area_m apping[ i+ m y seg .x p els* (j+ l)]))

i i

if (m y seg .sig n(m yseg.area_m apping[i+ m yseg.xpels*j])< 0) im aged atal.setS am ple(i+ m yseg .startx,j+ m y seg .starty.0.6 00 00 ); else

im ag ed atal.setS am p le(i+ m y seg .startx ,j+ l+ m y seg .starty ,0 ,60000); i

i i

125

int j;

for(j=0 ;j<m yseg.ypels;j+-O

it

if (m yseg .sign (m y seg.area_ m app in g[0+ m yseg .x pels* j])> = 0 ) im agedatal .setS am ple(0+ m yseg.startx,j+ m yseg.starty,0,60000);

if (m y seg .sign (m y seg .area_ m appin g[m yseg.x pels- 1 + m y seg .x p els* j])> = 0 ) im agedatal .setS am p le(m y seg .x p els-1 + m yseg.startx ,j+ m y seg .starty,0,60000); }

i { inti;

for(i=0 ;i<m vseg.xpels;i++ )

if (m yseg .sign (m y seg.area_ m app in g[i+ m yseg.xp els*0 ])>=0 ) im agedatal .setSam ple(i+m yseg. startx, 0+m yseg. starty,0,60000);

if (m yseg .sign(m yseg.area_m apping[i+ m yseg.xpels*(m yseg. y p els- 1 )])> = 0) im a g e d a ta l. setS am ple(i+ m yseg. startx, m yseg. ypels- 1+m yseg. starty,0,60000);

\

II draw the boundary o f each cell in the output im age file

line = bin.readLineQ ;

}

out.closeQ ; }

catch (IO E xception e) { // Trap exception S ystem .err.println(e.toString()); // D isplay error

)

B ufferedlm age bi = new B ufferedIm age(pi.getC olorM odel(), im a g e d a ta l, true, null); R enderedlm age op =JA I.create("filestore",bi,filenam e3,"T IF F ");

lin el = b in l.rea d L in e();

}

» )

catch (IO E xception e) { // Trap exception System .err.println(e.toStringO ); // D isplay error

f

//S ystem .out.println("F inished!");

int response = JO ptionP ane.show O ptionD ialog( null // C enter in window.

, "The w hole segm entation process has finished" // M essage , null // Title in titlebar

. JO ptionPane.D E FA U LT O PT IO N // O ption type , JO ptionP ane,P L A IN _M E S S A G E // m essageT ype

126 . null . null , null ); if (response==0 ) i i System. exit(O); i i if (resp o n se= = -l) t i System.exit(O); ( //System . exit(O); i i i // Icon (none)

// Button text as above.

Related documents