import ij.*; import ij.process.*; import ij.gui.*; import java.awt.*; import ij.plugin.*; import ij.plugin.frame.*; public class Japan_Flag implements PlugIn { boolean dialogCanceled = false; int width = 0; private void doDialog() { GenericDialog gd = new GenericDialog("幅の指定"); gd.addNumericField("国旗の幅:", width, 0); gd.showDialog(); if (gd.wasCanceled()) { dialogCanceled = true; } else { width = (int)gd.getNextNumber(); } } public void run(String arg) { doDialog(); if (dialogCanceled) return; int height = width * 2 / 3; ImageProcessor ip = new ColorProcessor(width, height); ip.setColor(Color.white); ip.fill(); int diameter = height * 3 / 5; int cx = width / 2, cy = height / 2; ip.setColor(Color.red); ip.fillOval(cx - diameter / 2, cy - diameter / 2, diameter, diameter); ImagePlus imp = new ImagePlus("Japan Flag", ip); imp.show(); } }
Test_4.javaを参考に、Adaptive_Bin.javaの関数runを完成させてください。
import ij.*; import ij.process.*; import ij.gui.*; import java.awt.*; import ij.plugin.filter.*; public class Test_4 implements PlugInFilter { boolean dialogCanceled = false; ImagePlus imp; int th = 0; private void doDialog() { GenericDialog gd = new GenericDialog("⼆値化の閾値の指定"); gd.addNumericField("閾値:", th, 0); gd.showDialog(); if (gd.wasCanceled()) { dialogCanceled = true; } else { th = (int)gd.getNextNumber(); } } @Override public int setup(String arg, ImagePlus imp) { this.imp = imp; return DOES_8G; } @Override public void run(ImageProcessor ip) { doDialog(); if (dialogCanceled) return; int height = ip.getHeight(); int width = ip.getWidth(); ImageProcessor ip2 = new ByteProcessor(width, height); ip2.setColor(Color.black); ip2.fill(); for(int x = 0; x < width; x++) { for(int y = 0; y < height; y++) { int val = ip.getPixel(x, y); if (val > th) { ip2.putPixel(x, y, 255); } } } ImagePlus imp2 = new ImagePlus("⼆値化(" + th + ")", ip2); imp2.show(); } }
import ij.*; import ij.process.*; import ij.gui.*; import java.awt.*; import ij.plugin.filter.*; public class Adaptive_Bin implements PlugInFilter { boolean dialogCanceled = false; int n = 3; int c = 0; private float calcLocalMean(int x, int y, int n, ImageProcessor ip) { int height = ip.getHeight(); int width = ip.getWidth(); if (x >= 0 && x < width && y >= 0 && y < height) { float sum = (float)0; int total = (2 * n + 1) * (2 * n + 1); for (int yy = -n; yy <= n; yy++) { for (int xx = -n; xx <= n; xx++) { if ((y + yy) >= 0 && (y + yy) < height && (x + xx) >= 0 && (x + xx) < width) { sum = sum + ip.getPixel(x + xx, y + yy); } } } return sum / total; } return (float)0.0; } private void doDialog() { GenericDialog gd = new GenericDialog("適応的二値化のパラメータ設定"); gd.addNumericField("局所(片側) n:", n, 3); gd.addNumericField("局所的平均値から差し引く定数 c:", c, 0); gd.showDialog(); if (gd.wasCanceled()) { dialogCanceled = true; } else { n = (int)gd.getNextNumber(); c = (int)gd.getNextNumber(); } } @Override public void run(ImageProcessor ip) { doDialog(); if (dialogCanceled) return; int height = ip.getHeight(); int width = ip.getWidth(); ImageProcessor ip2 = new ByteProcessor(width, height); // 適応的二値化の結果を格納する変数 // ip2 を黒色で塗りつぶす ip2.setColor(Color.black); ip2.fill(); for(int x = 0; x < width; x++) { for(int y = 0; y < height; y++) { int val = ip.getPixel(x, y); // calcLocalMean関数を呼び出す時のパラメータを考えてください。 float th = calcLocalMean( ? ) - c; if (val > th) { ip2.putPixel(x, y, 255); } } } ImagePlus imp2 = new ImagePlus("適応的二値化(n=" + n + ",c=" + c + ")", ip2); imp2.show(); } @Override public int setup(String arg0, ImagePlus imp) { return DOES_8G; } }
![]() |
![]() |
![]() |
n=3, c=0 | n=3, c=10 | n=3, c=20 |
---|---|---|
![]() |
![]() |
![]() |
n=10, c=0 | n=10, c=10 | n=10, c=20 |
![]() |
![]() |
![]() |
n=40, c=0 | n=40, c=10 | n=40, c=20 |
お世辞にもImageJのエディタは使いやすいとは言えません。 そこで、VSCodeによるプラグイン開発の方法をここで紹介したいと思います。
準備として、ドキュメントの下に「java」フォルダを作ります。 このフォルダの下に、プロジェクトごとにフォルダを作成してプログラムを格納していくことになります。 今回は、「kisounit1」というプロジェクトを1つ作成します。 「kisounit1」プロジェクトに、3週目で作成するプラグインのプログラムすべてを格納することができます。 また、ここではテキストの最初に出てくるサンプルのプラグインTest_1.javaを題材として用いることにします。
import ij.*; public class RunTest_1 { public static void main(String[] args) { ImagePlus imp = IJ.openImage("C:/Users/yama/Desktop/dave.png"); IJ.runPlugIn(imp, "Test_1", ""); } }