Skip to content

Commit d1367b2

Browse files
committed
IJ-OpenCV plugins
1 parent 38eff76 commit d1367b2

21 files changed

Lines changed: 1644 additions & 0 deletions

Plugins/IJ-OpenCV.zip

18.9 MB
Binary file not shown.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
import ij.IJ;
3+
import ij.ImagePlus;
4+
import ij.gui.GenericDialog;
5+
import ij.plugin.PlugIn;
6+
import ijopencv.ImageConverter;
7+
import org.bytedeco.javacpp.opencv_core;
8+
import static org.bytedeco.javacpp.opencv_imgproc.ADAPTIVE_THRESH_GAUSSIAN_C;
9+
import static org.bytedeco.javacpp.opencv_imgproc.ADAPTIVE_THRESH_MEAN_C;
10+
import static org.bytedeco.javacpp.opencv_imgproc.THRESH_BINARY;
11+
import static org.bytedeco.javacpp.opencv_imgproc.THRESH_BINARY_INV;
12+
import static org.bytedeco.javacpp.opencv_imgproc.adaptiveThreshold;
13+
14+
/*
15+
* To change this license header, choose License Headers in Project Properties.
16+
* To change this template file, choose Tools | Templates
17+
* and open the template in the editor.
18+
*/
19+
20+
/**
21+
*
22+
* @author jonathan
23+
*/
24+
public class Adaptive_ThresholdJ_ implements PlugIn {
25+
26+
String method;
27+
String thresholdmethod;
28+
int maxValue=255;
29+
int blockSize=3;
30+
31+
@Override
32+
public void run(String arg) {
33+
ImagePlus imp = IJ.getImage();
34+
// Converter
35+
ImageConverter ic = new ImageConverter();
36+
37+
opencv_core.Mat m = ic.convertTo(imp);
38+
// opencv_imgproc.cvtColor(m, m, opencv_imgproc.COLOR_BGR2GRAY);
39+
opencv_core.Mat res = new opencv_core.Mat();
40+
41+
if (!showDialog()) {
42+
return;
43+
}
44+
45+
int adaptiveMethod;
46+
if(method=="Mean"){
47+
adaptiveMethod = ADAPTIVE_THRESH_MEAN_C;
48+
}else{
49+
adaptiveMethod = ADAPTIVE_THRESH_GAUSSIAN_C;
50+
}
51+
52+
int thresType;
53+
if(thresholdmethod=="Binary"){
54+
thresType = THRESH_BINARY;
55+
}else{
56+
thresType = THRESH_BINARY_INV;
57+
}
58+
59+
60+
adaptiveThreshold(m, res, maxValue, adaptiveMethod, thresType, blockSize, 2);
61+
62+
ImagePlus imp2 = ic.convertFrom(res);
63+
imp2.show();
64+
65+
}
66+
67+
private boolean showDialog() {
68+
69+
GenericDialog gd = new GenericDialog("Adaptive Threshold");
70+
String[] items = {"Mean","Gaussian"};
71+
String[] itemsThreshold = {"Binary","Binary Inv"};
72+
gd.addChoice("Method", items, items[0]);
73+
gd.addChoice("Threshold Type", itemsThreshold, itemsThreshold[0]);
74+
gd.addNumericField("Max Value", maxValue, 0);
75+
gd.addNumericField("Block size", blockSize, 0);
76+
//gd.addNumericField("Gaussian Kernel width:", gaussianKernelWidth, 0);
77+
78+
gd.showDialog();
79+
if (gd.wasCanceled()) {
80+
return false;
81+
}
82+
83+
method = gd.getNextChoice();
84+
thresholdmethod = gd.getNextChoice();
85+
maxValue = (int) gd.getNextNumber();
86+
blockSize = (int) gd.getNextNumber();
87+
return true;
88+
}
89+
90+
91+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
}

Plugins/src/BlackHatJ_.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
8+
import ij.IJ;
9+
import ij.ImagePlus;
10+
import ij.Prefs;
11+
import ij.gui.GenericDialog;
12+
import ij.gui.PointRoi;
13+
import ij.plugin.PlugIn;
14+
import static ij.plugin.Thresholder.backgrounds;
15+
import static ij.plugin.Thresholder.methods;
16+
import ij.process.AutoThresholder;
17+
import ijopencv.ImageConverter;
18+
import ijopencv.KeyPointsConverter;
19+
import java.awt.Choice;
20+
import java.awt.event.ItemEvent;
21+
import java.awt.event.ItemListener;
22+
import java.util.Vector;
23+
import org.bytedeco.javacpp.opencv_core;
24+
import org.bytedeco.javacpp.opencv_core.KeyPointVector;
25+
import org.bytedeco.javacpp.opencv_core.Mat;
26+
import org.bytedeco.javacpp.opencv_features2d;
27+
import static org.bytedeco.javacpp.opencv_features2d.AGAST;
28+
import static org.bytedeco.javacpp.opencv_features2d.AGAST;
29+
import static org.bytedeco.javacpp.opencv_features2d.AGAST;
30+
import org.bytedeco.javacpp.opencv_features2d.Feature2D;
31+
import org.bytedeco.javacpp.opencv_features2d.KAZE;
32+
import org.bytedeco.javacpp.opencv_imgproc;
33+
import static org.bytedeco.javacpp.opencv_imgproc.MORPH_BLACKHAT;
34+
import static org.bytedeco.javacpp.opencv_imgproc.MORPH_RECT;
35+
import static org.bytedeco.javacpp.opencv_imgproc.getStructuringElement;
36+
import static org.bytedeco.javacpp.opencv_imgproc.morphologyEx;
37+
import org.bytedeco.javacpp.opencv_xfeatures2d;
38+
39+
/**
40+
*
41+
* @author jonathan
42+
*/
43+
public class BlackHatJ_ implements PlugIn {
44+
int xSize, ySize;
45+
46+
@Override
47+
public void run(String arg) {
48+
ImagePlus imp = IJ.getImage();
49+
// Converter
50+
ImageConverter ic = new ImageConverter();
51+
52+
opencv_core.Mat m = ic.convertTo(imp);
53+
opencv_imgproc.cvtColor(m, m, opencv_imgproc.COLOR_BGR2GRAY);
54+
Mat res = new opencv_core.Mat();
55+
56+
if (!showDialog()) {
57+
return;
58+
}
59+
60+
Mat element = getStructuringElement(MORPH_RECT, new opencv_core.Size(xSize, ySize));
61+
62+
morphologyEx(m, res, MORPH_BLACKHAT, element);
63+
64+
ImagePlus imp2 = ic.convertFrom(res);
65+
imp2.show();
66+
67+
}
68+
69+
private boolean showDialog() {
70+
71+
GenericDialog gd = new GenericDialog("Black Hat Kernel");
72+
gd.addNumericField("X size", xSize, 1);
73+
gd.addNumericField("Y size", ySize, 1);
74+
//gd.addNumericField("Gaussian Kernel width:", gaussianKernelWidth, 0);
75+
76+
gd.showDialog();
77+
if (gd.wasCanceled()) {
78+
return false;
79+
}
80+
81+
xSize = (int) gd.getNextNumber();
82+
if (xSize < 1) {
83+
xSize = 1;
84+
}
85+
ySize = (int) gd.getNextNumber();
86+
if (ySize < 1) {
87+
ySize = 1;
88+
}
89+
90+
return true;
91+
}
92+
93+
94+
}

0 commit comments

Comments
 (0)