public void run(ImageProcessor ip) { String[] imageNames = getOpenImageNames(); if (imageNames[0] == "None") { IJ.error("need at least 2 binary open images"); return; } double previousMinOverlap = Prefs.get("BVTB.BinaryFeatureExtractor.minOverlap", 0); boolean previousCombine = Prefs.get("BVTB.BinaryFeatureExtractor.combine", false); GenericDialog gd = new GenericDialog("Binary Feature Extractor"); gd.addChoice("Objects image", imageNames, imageNames[0]); gd.addChoice("Selector image", imageNames, imageNames[1]); gd.addNumericField("Object_overlap in % (0=off)", previousMinOverlap, 0, 9, ""); gd.addCheckbox("Combine objects and selectors", previousCombine); gd.addCheckbox("Count output", true); gd.addCheckbox("Analysis tables", false); gd.showDialog(); if (gd.wasCanceled()) { return; } String objectsImgTitle = gd.getNextChoice(); String selectorsImgTitle = gd.getNextChoice(); double minOverlap = gd.getNextNumber(); boolean combineImages = gd.getNextBoolean(); boolean showCountOutput = gd.getNextBoolean(); boolean showAnalysis = gd.getNextBoolean(); if (gd.invalidNumber() || minOverlap < 0 || minOverlap > 100) { IJ.error("invalid number"); return; } Prefs.set("BVTB.BinaryFeatureExtractor.minOverlap", minOverlap); Prefs.set("BVTB.BinaryFeatureExtractor.combine", combineImages); if (objectsImgTitle.equals(selectorsImgTitle)) { IJ.error("images need to be different"); return; } ImagePlus objectsImp = WindowManager.getImage(objectsImgTitle); ImageProcessor objectsIP = objectsImp.getProcessor(); ImagePlus selectorsImp = WindowManager.getImage(selectorsImgTitle); ImageProcessor selectorsIP = selectorsImp.getProcessor(); if (!objectsIP.isBinary() || !selectorsIP.isBinary()) { IJ.error("works with 8-bit binary images only"); return; } if ((objectsImp.getWidth() != selectorsImp.getWidth()) || objectsImp.getHeight() != selectorsImp.getHeight()) { IJ.error("images need to be of the same size"); return; } // close any existing RoiManager before instantiating a new one for this analysis RoiManager oldRM = RoiManager.getInstance2(); if (oldRM != null) { oldRM.close(); } RoiManager objectsRM = new RoiManager(true); ResultsTable objectsRT = new ResultsTable(); ParticleAnalyzer analyzeObjects = new ParticleAnalyzer(analyzerOptions, measurementFlags, objectsRT, 0.0, 999999999.9); analyzeObjects.setRoiManager(objectsRM); analyzeObjects.analyze(objectsImp); objectsRM.runCommand("Show None"); int objectNumber = objectsRT.getCounter(); Roi[] objectRoi = objectsRM.getRoisAsArray(); ResultsTable measureSelectorsRT = new ResultsTable(); Analyzer overlapAnalyzer = new Analyzer(selectorsImp, measurementFlags, measureSelectorsRT); ImagePlus outputImp = IJ.createImage("output", "8-bit black", objectsImp.getWidth(), objectsImp.getHeight(), 1); ImageProcessor outputIP = outputImp.getProcessor(); double[] measuredOverlap = new double[objectNumber]; outputIP.setValue(255.0); for (int o = 0; o < objectNumber; o++) { selectorsImp.killRoi(); selectorsImp.setRoi(objectRoi[o]); overlapAnalyzer.measure(); measuredOverlap[o] = measureSelectorsRT.getValue("%Area", o); if (minOverlap != 0.0 && measuredOverlap[o] >= minOverlap) { outputIP.fill(objectRoi[o]); finalCount++; } else if (minOverlap == 0.0 && measuredOverlap[o] > 0.0) { outputIP.fill(objectRoi[o]); finalCount++; } } // measureSelectorsRT.show("Objects"); selectorsImp.killRoi(); RoiManager selectorRM = new RoiManager(true); ResultsTable selectorRT = new ResultsTable(); ParticleAnalyzer.setRoiManager(selectorRM); ParticleAnalyzer analyzeSelectors = new ParticleAnalyzer(analyzerOptions, measurementFlags, selectorRT, 0.0, 999999999.9); analyzeSelectors.analyze(selectorsImp); selectorRM.runCommand("Show None"); int selectorNumber = selectorRT.getCounter(); if (combineImages) { outputImp.updateAndDraw(); Roi[] selectorRoi = selectorRM.getRoisAsArray(); ResultsTable measureObjectsRT = new ResultsTable(); Analyzer selectorAnalyzer = new Analyzer(outputImp, measurementFlags, measureObjectsRT); double[] selectorOverlap = new double[selectorNumber]; outputIP.setValue(255.0); for (int s = 0; s < selectorNumber; s++) { outputImp.killRoi(); outputImp.setRoi(selectorRoi[s]); selectorAnalyzer.measure(); selectorOverlap[s] = measureObjectsRT.getValue("%Area", s); if (selectorOverlap[s] > 0.0d) { outputIP.fill(selectorRoi[s]); } } selectorRoi = null; selectorAnalyzer = null; measureObjectsRT = null; } // selectorRT.show("Selectors"); outputImp.killRoi(); String outputImageTitle = WindowManager.getUniqueName("Extracted_" + objectsImgTitle); outputImp.setTitle(outputImageTitle); outputImp.show(); outputImp.changes = true; if (showCountOutput) { String[] openTextWindows = WindowManager.getNonImageTitles(); boolean makeNewTable = true; for (int w = 0; w < openTextWindows.length; w++) { if (openTextWindows[w].equals("BFE_Results")) { makeNewTable = false; } } TextWindow existingCountTable = ResultsTable.getResultsWindow(); if (makeNewTable) { countTable = new ResultsTable(); countTable.setPrecision(0); countTable.setValue("Image", 0, outputImageTitle); countTable.setValue("Objects", 0, objectNumber); countTable.setValue("Selectors", 0, selectorNumber); countTable.setValue("Extracted", 0, finalCount); countTable.show("BFE_Results"); } else { IJ.renameResults("BFE_Results", "Results"); countTable = ResultsTable.getResultsTable(); countTable.setPrecision(0); countTable.incrementCounter(); countTable.addValue("Image", outputImageTitle); countTable.addValue("Objects", objectNumber); countTable.addValue("Selectors", selectorNumber); countTable.addValue("Extracted", finalCount); IJ.renameResults("Results", "BFE_Results"); countTable.show("BFE_Results"); } } if (showAnalysis) { ResultsTable extractedRT = new ResultsTable(); ParticleAnalyzer analyzeExtracted = new ParticleAnalyzer( ParticleAnalyzer.CLEAR_WORKSHEET | ParticleAnalyzer.RECORD_STARTS, measurementFlags, extractedRT, 0.0, 999999999.9); analyzeExtracted.analyze(outputImp); objectsRT.show("Objects"); selectorRT.show("Selectors"); extractedRT.show("Extracted"); } else { objectsRT = null; selectorRT = null; } objectsRM = null; measureSelectorsRT = null; analyzeObjects = null; overlapAnalyzer = null; objectRoi = null; selectorRM = null; objectsImp.killRoi(); objectsImp.changes = false; selectorsImp.changes = false; }
public class imageProcessing extends ImagePlus implements PlugIn { private candidateTextDet ctd; private textRegLoca trl; private characExtract ce; private ImagePlus Im = IJ.createImage("new_image", "8-bit black", 640, 480, 1); int[] c; public void set(String name) { Im = IJ.openImage(name); run(""); } @Override public void run(String arg) { java.io.File file = new java.io.File("").getAbsoluteFile(); String name = file.getPath().replace('\\', '/') + '/'; ctd = new candidateTextDet(); trl = new textRegLoca(); ce = new characExtract(); int chardata[] = new int[35]; ImagePlus im = Im.duplicate(); ImagePlus finish = Im.duplicate(); IJ.run(im, "8-bit", ""); ImagePlus featuremap = ctd.candidateTextDet(im); int l = trl.textRegLoca(featuremap, finish); ce.spaceset(l); c = new int[l]; c = ce.characExtract(l); // mn.load_actionPerformed(null); // mn.train_actionPerformed(null); int total = 0; for (int i = 0; i < l; i++) { total += c[i]; } int i = 0; for (int count = 0; count < total; count++) { if (c[i] == count) { outputTextarea.setText(outputTextarea.getText() + " "); i++; c[i] += c[i - 1]; } String path = name + "src/cache/ocr/" + (count + 1) + ".jpg"; chardata = extractBytes(path); int flag = 0; int m = 0; for (m = 0; m < position; m++) { for (int j = 0; j < 35; j++) { if (data[m][j] != chardata[j]) { flag--; break; } else { flag++; } } if (flag == 35) { break; } else { flag = 0; } } char recognized = ' '; if (flag == 35) { recognized = ch[m]; } else { recognized = '?'; } outputTextarea.setText(outputTextarea.getText() + "" + recognized); // java.io.File fileocr = new java.io.File(name + "src/cache/ocr/" + (count + 1) + ".jpg"); // fileocr.delete(); } } public int[] extractBytes(String ImageName) { // open image int n = 0; int chardata[] = new int[35]; ImagePlus I3 = IJ.openImage(ImageName); ImageProcessor ip1 = I3.getProcessor(); for (int j = 0; j < 7; j++) { for (int k = 0; k < 5; k++) { int temp = ip1.getPixel(k, j); if (temp == 255) { chardata[n] = 1; } else { chardata[n] = 0; } // System.out.println("getpixel = "+temp); n++; } } return chardata; } }