|
| 1 | + |
| 2 | +import ij.IJ; |
| 3 | +import ij.ImagePlus; |
| 4 | +import ij.gui.GenericDialog; |
| 5 | +import ij.plugin.PlugIn; |
| 6 | +import ij.text.TextWindow; |
| 7 | +import ijopencv.ImageStackConverter; |
| 8 | +import java.util.ArrayList; |
| 9 | +import org.bytedeco.javacpp.FloatPointer; |
| 10 | +import org.bytedeco.javacpp.IntPointer; |
| 11 | +import org.bytedeco.javacpp.PointerPointer; |
| 12 | +import org.bytedeco.javacpp.opencv_core; |
| 13 | +import org.bytedeco.javacpp.opencv_imgproc; |
| 14 | +import static org.bytedeco.javacpp.opencv_imgproc.calcHist; |
| 15 | + |
| 16 | + |
| 17 | +/* |
| 18 | + * To change this license header, choose License Headers in Project Properties. |
| 19 | + * To change this template file, choose Tools | Templates |
| 20 | + * and open the template in the editor. |
| 21 | + */ |
| 22 | +/** |
| 23 | + * |
| 24 | + * @author jonathan |
| 25 | + */ |
| 26 | +public class BGR_Histogram_ComparisonJ_ implements PlugIn { |
| 27 | + |
| 28 | + int blueBins = 8; |
| 29 | + int greenBins = 8; |
| 30 | + int redBins = 8; |
| 31 | + |
| 32 | + @Override |
| 33 | + public void run(String arg) { |
| 34 | + |
| 35 | + ImagePlus imp = IJ.getImage(); |
| 36 | + int stacksize = imp.getStack().getSize(); |
| 37 | + |
| 38 | + if (imp.getStack().getSize() == 1) { |
| 39 | + IJ.error("You need a stack of images"); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Converter |
| 44 | + ImageStackConverter isc = new ImageStackConverter(); |
| 45 | + opencv_core.MatVector mvec = isc.convertTo(imp); |
| 46 | + |
| 47 | + if (!showDialog()) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + double[][] comparison = new double[4][stacksize * (stacksize - 1) / 2]; |
| 52 | + |
| 53 | + opencv_core.Mat mask = new opencv_core.Mat(); |
| 54 | + IntPointer intPtrChannels = new IntPointer(0, 1, 2); |
| 55 | + IntPointer intPtrHistSize = new IntPointer(blueBins, greenBins, redBins); |
| 56 | + FloatPointer fltPtrRanges = new FloatPointer(0.0f, 255.0f, 0.0f, 255.0f, 0.0f, 255.0f); |
| 57 | + PointerPointer ptptranges = new PointerPointer(fltPtrRanges, fltPtrRanges, fltPtrRanges); |
| 58 | + |
| 59 | + opencv_core.Mat hist1 = new opencv_core.Mat(); |
| 60 | + opencv_core.Mat hist2 = new opencv_core.Mat(); |
| 61 | + int n = 0; |
| 62 | + for (int i = 0; i < mvec.size() - 1; i++) { |
| 63 | + calcHist(mvec.get(i), 1, intPtrChannels, mask, hist1, 3, intPtrHistSize, ptptranges, true, false); |
| 64 | + opencv_core.normalize(hist1, hist1); |
| 65 | + for (int j = i + 1; j < mvec.size(); j++) { |
| 66 | + calcHist(mvec.get(j), 1, intPtrChannels, mask, hist2, 3, intPtrHistSize, ptptranges, true, false); |
| 67 | + opencv_core.normalize(hist2, hist2); |
| 68 | + comparison[0][n] = opencv_imgproc.compareHist(hist1, hist2, opencv_imgproc.CV_COMP_CORREL); |
| 69 | + comparison[1][n] = opencv_imgproc.compareHist(hist1, hist2, opencv_imgproc.CV_COMP_CHISQR); |
| 70 | + comparison[2][n] = opencv_imgproc.compareHist(hist1, hist2, opencv_imgproc.CV_COMP_INTERSECT); |
| 71 | + comparison[3][n] = opencv_imgproc.compareHist(hist1, hist2, opencv_imgproc.CV_COMP_BHATTACHARYYA); |
| 72 | + n++; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + String headings = "Method\t"; |
| 77 | + for (int i = 0; i < mvec.size() - 1; i++) { |
| 78 | + for (int j = i + 1; j < mvec.size(); j++) { |
| 79 | + headings = headings + (i + 1) + "-" + (j + 1) + "\t"; |
| 80 | + } |
| 81 | + } |
| 82 | + headings = headings.substring(0, headings.lastIndexOf("\t")); |
| 83 | + ArrayList list = new ArrayList(); |
| 84 | + |
| 85 | + String row1 = "Correlation\t"; |
| 86 | + String row2 = "CHI Square\t"; |
| 87 | + String row3 = "Intersection\t"; |
| 88 | + String row4 = "BHATTACHARYYA\t"; |
| 89 | + for (int i = 0; i < comparison[0].length - 1; i++) { |
| 90 | + row1 = row1 + comparison[0][i] + "\t"; |
| 91 | + row2 = row2 + comparison[1][i] + "\t"; |
| 92 | + row3 = row3 + comparison[2][i] + "\t"; |
| 93 | + row4 = row4 + comparison[3][i] + "\t"; |
| 94 | + |
| 95 | + } |
| 96 | + row1 = row1 + comparison[0][comparison[0].length - 1] ; |
| 97 | + row2 = row2 + comparison[1][comparison[0].length - 1]; |
| 98 | + row3 = row3 + comparison[2][comparison[0].length - 1]; |
| 99 | + row4 = row4 + comparison[3][comparison[0].length - 1]; |
| 100 | + |
| 101 | + list.add(row1); |
| 102 | + list.add(row2); |
| 103 | + list.add(row3); |
| 104 | + list.add(row4); |
| 105 | + |
| 106 | + TextWindow textWindow = new TextWindow("Similarity Table", headings, list, 600, 400); |
| 107 | + textWindow.setVisible(true); |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + private boolean showDialog() { |
| 112 | + |
| 113 | + GenericDialog gd = new GenericDialog("BGR Histogram Comparison"); |
| 114 | + gd.addNumericField("Blue bins", blueBins, 0); |
| 115 | + gd.addNumericField("Green bins", greenBins, 0); |
| 116 | + gd.addNumericField("Red bins", redBins, 0); |
| 117 | + //gd.addNumericField("Gaussian Kernel width:", gaussianKernelWidth, 0); |
| 118 | + |
| 119 | + gd.showDialog(); |
| 120 | + if (gd.wasCanceled()) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + blueBins = (int) gd.getNextNumber(); |
| 125 | + if (blueBins < 1) { |
| 126 | + blueBins = 1; |
| 127 | + } |
| 128 | + if (blueBins > 255) { |
| 129 | + blueBins = 255; |
| 130 | + } |
| 131 | + |
| 132 | + greenBins = (int) gd.getNextNumber(); |
| 133 | + if (greenBins < 1) { |
| 134 | + greenBins = 1; |
| 135 | + } |
| 136 | + if (greenBins > 255) { |
| 137 | + greenBins = 255; |
| 138 | + } |
| 139 | + redBins = (int) gd.getNextNumber(); |
| 140 | + if (redBins < 1) { |
| 141 | + redBins = 1; |
| 142 | + } |
| 143 | + if (redBins > 255) { |
| 144 | + redBins = 255; |
| 145 | + } |
| 146 | + |
| 147 | + return true; |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +} |
0 commit comments