Created
November 29, 2023 16:03
-
-
Save 27Cobalter/59303827fbf621606bbd47a9f706c3a2 to your computer and use it in GitHub Desktop.
imagejで一定範囲のmin, maxを求めるplugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ij.*; | |
import ij.gui.*; | |
import ij.measure.*; | |
import ij.plugin.*; | |
import ij.plugin.filter.*; | |
import ij.process.*; | |
import java.awt.*; | |
import java.util.Arrays; | |
public class Custom_Min_Max implements PlugIn { | |
public void run(String arg) { | |
ResultsTable rt = Analyzer.getResultsTable(); | |
if (rt == null) { | |
rt = new ResultsTable(); | |
Analyzer.setResultsTable(rt); | |
} | |
ImagePlus imp = IJ.getImage(); | |
if (imp == null) { | |
IJ.showMessage("Required to Select Image Window!"); | |
return; | |
} | |
ImageProcessor ip = imp.getProcessor(); | |
int width = ip.getWidth(); | |
int height = ip.getHeight(); | |
Rectangle rect = imp.getRoi().getBoundingRect(); | |
if (rect == null) { | |
IJ.showMessage("Roi not select!"); | |
return; | |
} | |
int type = imp.getType(); | |
int area = rect.height * rect.width; | |
int[] grayArr = new int[area]; | |
for (int i = 0; i < rect.height; i++) { | |
for (int j = 0; j < rect.width; j++) { | |
int val = 0; | |
if (type == ImagePlus.COLOR_RGB) { | |
int[] pixels = (int[])ip.getPixels(); | |
val = (int)pixels[(j + rect.x) + (i + rect.y) * width]; | |
int r = (val >> 16) & 0xff; | |
int g = (val >> 8) & 0xff; | |
int b = (val >> 0) & 0xff; | |
val = (r + g + b) / 3; | |
} else if (type == ImagePlus.GRAY16){ | |
short[] pixels = (short[])ip.getPixels(); | |
val = pixels[(j + rect.x) + (i + rect.y) * width] & 0xffff; | |
} | |
grayArr[i * rect.width + j] = val; | |
} | |
} | |
Arrays.sort(grayArr); | |
rt.incrementCounter(); | |
// raw min_max | |
rt.addValue("min_raw", grayArr[0]); | |
rt.addValue("max_raw", grayArr[area - 1]); | |
double ratio = 0.95; | |
int half_ex = (int)(((1.0 - ratio) / 2.0) * area); | |
rt.addValue("min_95", grayArr[half_ex]); | |
rt.addValue("max_95", grayArr[(area - 1) - half_ex]); | |
ratio = 0.9; | |
half_ex = (int)(((1.0 - ratio) / 2.0) * area); | |
rt.addValue("min_90", grayArr[half_ex]); | |
rt.addValue("max_90", grayArr[(area - 1) - half_ex]); | |
ratio = 0.8; | |
half_ex = (int)(((1.0 - ratio) / 2.0) * area); | |
rt.addValue("min_80", grayArr[half_ex]); | |
rt.addValue("max_80", grayArr[(area - 1) - half_ex]); | |
rt.show("Results"); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment