i trying write simple program allows user select reference image , images in same directory, normalize them same size (300x300), extract features, calculate distance features of , show them in order of less distant distant.
as beginner in java, having trouble code, code has no error, , able run program, when run application, application crashes, console log :
> exception in thread "main" java.lang.noclassdeffounderror: com.sun.media.jai.codec.seekablestream @ javax.media.jai.operator.bmpdescriptor.class$(bmpdescriptor.java:95) @ javax.media.jai.operator.bmpdescriptor.<clinit>(bmpdescriptor.java:94) @ java.lang.class.forname0(native method) @ java.lang.class.forname(unknown source) @ javax.media.jai.registryfileparser.getinstance(registryfileparser.java:224) @ javax.media.jai.registryfileparser.registerdescriptor(registryfileparser.java:360) @ javax.media.jai.registryfileparser.parsefile(registryfileparser.java:295) @ javax.media.jai.registryfileparser.loadoperationregistry(registryfileparser.java:55) @ javax.media.jai.operationregistry.initializeregistry(operationregistry.java:371) @ javax.media.jai.jai.<clinit>(jai.java:566) @ imagepr.naivesimilarityfinder.rescale(naivesimilarityfinder.java:116) @ imagepr.naivesimilarityfinder.<init>(naivesimilarityfinder.java:46) @ imagepr.naivesimilarityfinder.main(naivesimilarityfinder.java:223) here code of java file:
9 import java.awt.borderlayout; 10 import java.awt.color; 11 import java.awt.container; 12 import java.awt.font; 13 import java.awt.gridlayout; 14 import java.awt.image.renderedimage; 15 import java.awt.image.renderable.parameterblock; 16 import java.io.file; 17 import java.io.ioexception; 18 19 import javax.imageio.imageio; 20 import javax.media.jai.interpolationnearest; 21 import javax.media.jai.jai; 22 import javax.media.jai.iterator.randomiter; 23 import javax.media.jai.iterator.randomiterfactory; 24 import javax.swing.jfilechooser; 25 import javax.swing.jframe; 26 import javax.swing.jlabel; 27 import javax.swing.joptionpane; 28 import javax.swing.jpanel; 29 import javax.swing.jscrollpane; 30 31 import com.sun.media.jai.widget.displayjai; 32 /** 33 * class uses simple, naive similarity algorithm compare image 34 * others in same directory. 35 */ 36 public class naivesimilarityfinder extends jframe 37 { 38 // reference image "signature" (25 representative pixels, each in r,g,b). 39 // use instances of color make things simpler. 40 private color[][] signature; 41 // base size of images. 42 private static final int basesize = 300; 43 // files? 44 private static final string basepath = 45 "c:\\imagecmp"; 46 47 /* 48 * constructor, creates gui , start image processing task. 49 */ 50 public naivesimilarityfinder(file reference) throws ioexception 51 { 52 // create gui 53 super("naive similarity finder"); 54 container cp = getcontentpane(); 55 cp.setlayout(new borderlayout()); 56 // put reference, scaled, in left part of ui. 57 renderedimage ref = rescale(imageio.read(reference)); 58 cp.add(new displayjai(ref), borderlayout.west); 59 // calculate signature vector reference. 60 signature = calcsignature(ref); 61 // need component store x images in stack, x 62 // number of images in same directory original one. 63 file[] others = getotherimagefiles(reference); 64 jpanel otherpanel = new jpanel(new gridlayout(others.length, 2)); 65 cp.add(new jscrollpane(otherpanel), borderlayout.center); 66 // each image, calculate signature , distance 67 // reference signature. 68 renderedimage[] rothers = new renderedimage[others.length]; 69 double[] distances = new double[others.length]; 70 (int o = 0; o < others.length; o++) 71 { 72 rothers[o] = rescale(imageio.read(others[o])); 73 distances[o] = calcdistance(rothers[o]); 74 } 75 // sort vectors *together*. 76 (int p1 = 0; p1 < others.length - 1; p1++) 77 (int p2 = p1 + 1; p2 < others.length; p2++) 78 { 79 if (distances[p1] > distances[p2]) 80 { 81 double tempdist = distances[p1]; 82 distances[p1] = distances[p2]; 83 distances[p2] = tempdist; 84 renderedimage tempr = rothers[p1]; 85 rothers[p1] = rothers[p2]; 86 rothers[p2] = tempr; 87 file tempf = others[p1]; 88 others[p1] = others[p2]; 89 others[p2] = tempf; 90 } 91 } 92 // add them ui. 93 (int o = 0; o < others.length; o++) 94 { 95 otherpanel.add(new displayjai(rothers[o])); 96 jlabel ldist = new jlabel("<html>" + others[o].getname() + "<br>" 97 + string.format("% 13.3f", distances[o]) + "</html>"); 98 ldist.setfont(new font(font.monospaced, font.plain, 36)); 99 system.out.printf("<td class=\"simpletable legend\"> "+ 100 "<img src=\"miscresources/imagesimilarity/icons/miniicon_%s\" "+ 101 "alt=\"similarity result\"><br>% 13.3f</td>\n", others[o].getname(),distances[o]); 102 otherpanel.add(ldist); 103 } 104 // more gui details. 105 pack(); 106 setvisible(true); 107 setdefaultcloseoperation(jframe.exit_on_close); 108 } 109 110 /* 111 * method rescales image 300,300 pixels using jai scale 112 * operator. 113 */ 114 private renderedimage rescale(renderedimage i) 115 { 116 float scalew = ((float) basesize) / i.getwidth(); 117 float scaleh = ((float) basesize) / i.getheight(); 118 // scales original image 119 parameterblock pb = new parameterblock(); 120 pb.addsource(i); 121 pb.add(scalew); 122 pb.add(scaleh); 123 pb.add(0.0f); 124 pb.add(0.0f); 125 pb.add(new interpolationnearest()); 126 // creates new, scaled image , uses on displayjai component 127 return jai.create("scale", pb); 128 } 129 130 /* 131 * method calculates , returns signature vectors input image. 132 */ 133 private color[][] calcsignature(renderedimage i) 134 { 135 // memory signature. 136 color[][] sig = new color[5][5]; 137 // each of 25 signature values average pixels around it. 138 // note coordinate of central pixel in proportions. 139 float[] prop = new float[] 140 {1f / 10f, 3f / 10f, 5f / 10f, 7f / 10f, 9f / 10f}; 141 (int x = 0; x < 5; x++) 142 (int y = 0; y < 5; y++) 143 sig[x][y] = averagearound(i, prop[x], prop[y]); 144 return sig; 145 } 146 147 /* 148 * method averages pixel values around central point , return 149 * average instance of color. point coordinates proportional 150 * image. 151 */ 152 private color averagearound(renderedimage i, double px, double py) 153 { 154 // iterator image. 155 randomiter iterator = randomiterfactory.create(i, null); 156 // memory pixel , accumulator. 157 double[] pixel = new double[3]; 158 double[] accum = new double[3]; 159 // size of sampling area. 160 int samplesize = 15; 161 int numpixels = 0; 162 // sample pixels. 163 (double x = px * basesize - samplesize; x < px * basesize + samplesize; x++) 164 { 165 (double y = py * basesize - samplesize; y < py * basesize + samplesize; y++) 166 { 167 iterator.getpixel((int) x, (int) y, pixel); 168 accum[0] += pixel[0]; 169 accum[1] += pixel[1]; 170 accum[2] += pixel[2]; 171 numpixels++; 172 } 173 } 174 // average accumulated values. 175 accum[0] /= numpixels; 176 accum[1] /= numpixels; 177 accum[2] /= numpixels; 178 return new color((int) accum[0], (int) accum[1], (int) accum[2]); 179 } 180 181 /* 182 * method calculates distance between signatures of image , 183 * reference one. signatures image passed parameter 184 * calculated inside method. 185 */ 186 private double calcdistance(renderedimage other) 187 { 188 // calculate signature image. 189 color[][] sigother = calcsignature(other); 190 // there several ways calculate distances between 2 vectors, 191 // calculate sum of distances between rgb values of 192 // pixels in same positions. 193 double dist = 0; 194 (int x = 0; x < 5; x++) 195 (int y = 0; y < 5; y++) 196 { 197 int r1 = signature[x][y].getred(); 198 int g1 = signature[x][y].getgreen(); 199 int b1 = signature[x][y].getblue(); 200 int r2 = sigother[x][y].getred(); 201 int g2 = sigother[x][y].getgreen(); 202 int b2 = sigother[x][y].getblue(); 203 double tempdist = math.sqrt((r1 - r2) * (r1 - r2) + (g1 - g2) 204 * (g1 - g2) + (b1 - b2) * (b1 - b2)); 205 dist += tempdist; 206 } 207 return dist; 208 } 209 210 /* 211 * method image files in same directory reference. 212 * kicks include reference image. 213 */ 214 private file[] getotherimagefiles(file reference) 215 { 216 file dir = new file(reference.getparent()); 217 // list image files in directory. 218 file[] others = dir.listfiles(new jpegimagefilefilter()); 219 return others; 220 } 221 222 /* 223 * entry point application, opens file image 224 * used reference , starts application. 225 */ 226 public static void main(string[] args) throws ioexception 227 { 228 jfilechooser fc = new jfilechooser(basepath); 229 fc.setfilefilter(new jpegimagefilefilter()); 230 int res = fc.showopendialog(null); 231 // have image! 232 if (res == jfilechooser.approve_option) 233 { 234 file file = fc.getselectedfile(); 235 new naivesimilarityfinder(file); 236 } 237 // oops! 238 else 239 { 240 joptionpane.showmessagedialog(null, 241 "you must select 1 image reference.", "aborting...", 242 joptionpane.warning_message); 243 } 244 } 245 246 } any quick guess might doing wrong? coding in eclipse in windows machine , appreciated :)
Comments
Post a Comment