Image processing 如何测量背景上彩色区域的百分比?ImageJ?
我想测量一下这张灰色地图上紫色区域的百分比 我可以粗略地说,这代表了10%的面积,但我需要一个精确的值 我知道有一个叫做ImageJ的免费工具,但我不知道如何使用它 也许你可以建议我另一个工具或如何执行这样的任务Image processing 如何测量背景上彩色区域的百分比?ImageJ?,image-processing,imagej,Image Processing,Imagej,我想测量一下这张灰色地图上紫色区域的百分比 我可以粗略地说,这代表了10%的面积,但我需要一个精确的值 我知道有一个叫做ImageJ的免费工具,但我不知道如何使用它 也许你可以建议我另一个工具或如何执行这样的任务 提前感谢。您可以为该任务编写一个小型ImageJ插件: import ij.gui.DialogListener; import ij.gui.GenericDialog; import ij.plugin.filter.ExtendedPlugInFilter; import ij
提前感谢。您可以为该任务编写一个小型ImageJ插件:
import ij.gui.DialogListener;
import ij.gui.GenericDialog;
import ij.plugin.filter.ExtendedPlugInFilter;
import ij.plugin.filter.PlugInFilterRunner;
import ij.process.ImageProcessor;
import ij.IJ;
import ij.ImagePlus;
import java.awt.AWTEvent;
import java.awt.Label;
import java.lang.Math;
public class Purple_Counter implements ExtendedPlugInFilter, DialogListener{
private final int FLAGS = DOES_RGB | PARALLELIZE_STACKS;
private boolean preview = true;
private ImagePlus im;
private int count;
private double total;
private int color = 0xff64318a;
private int background = 0xffd7d7d7;
private double maxDistanceColor;
private double maxDistanceBckg ;
public int setup(String args, ImagePlus im){
this.im = im;
maxDistanceColor = 60;
maxDistanceBckg = 60;
return FLAGS;
}
public void run(ImageProcessor ip){
int height = ip.getHeight();
int width = ip.getWidth();
count = 0;
total = 0;
for(int a = 0; a < height; a++){
for(int b = 0; b < height; b++){
int p = ip.getPixel(a,b);
if ( calculateDistance(p,color) < maxDistanceColor){
count++;
ip.putPixel(a,b,0xffff0000);
}
if ( calculateDistance(p,background) < maxDistanceBckg){
total++;
ip.putPixel(a,b,0xff000000);
}
}
}
IJ.log("Counted: " + count + "| Total: " + total);
IJ.log("Percentage: " + (count/total) * 100f);
IJ.log("");
}
private double calculateDistance(int color1, int color2){
int red1 = (color1 >> 16)&0xff;
int red2 = (color2 >> 16)&0xff;
int green1 = (color1 >> 8)&0xff;
int green2 = (color2 >> 8)&0xff;
int blue1 = color1 & 0xff;
int blue2 = color2 & 0xff;
double distance = Math.sqrt((red1 * red1 - 2*red1*red2 + red2*red2) + (green1 * green1 - 2*green1*green2 + green2*green2) + (blue1 * blue1 - 2*blue1*blue2 + blue2*blue2));
return distance;
}
public int showDialog(ImagePlus imp, String Command, PlugInFilterRunner pfr){
//Dialog to control the used color distances
GenericDialog gd = new GenericDialog("Distance Settings");
gd.addMessage("Please chose maximal distances to identify POI and background pixels: ");
gd.addSlider("POI Red: ", 0,255,0x64);
gd.addSlider("POI Green: ", 0,255,0x31);
gd.addSlider("POI Blue: ", 0,255,0x8a);
gd.addSlider("BCKG Red: ", 0,255,0xd7);
gd.addSlider("BCKG Green: ", 0,255,0xd7);
gd.addSlider("BCKG Blue: ", 0,255,0xd7);
gd.addSlider("POI max. Distance: ", 0.001, 1000, maxDistanceColor);
gd.addSlider("BCKG max. Distance: ", 0.001, 1000, maxDistanceBckg);
gd.addPreviewCheckbox(pfr, "Preview");
gd.addDialogListener(this);
gd.showDialog();
if(gd.wasCanceled() ||!dialogItemChanged(gd, null)){
return DONE;
}
return IJ.setupDialog(imp, FLAGS);
}
public boolean dialogItemChanged(GenericDialog gd, AWTEvent e){
int alpha = 0xff;
int colorRed = (int)gd.getNextNumber();
int colorGreen = (int)gd.getNextNumber();
int colorBlue = (int)gd.getNextNumber();
int bckgRed = (int)gd.getNextNumber();
int bckgGreen = (int)gd.getNextNumber();
int bckgBlue = (int)gd.getNextNumber();
color = (alpha << 24 | colorRed << 16 | colorGreen << 8 | colorBlue);
background = (alpha << 24 | bckgRed << 16 | bckgGreen << 8 | bckgBlue);
maxDistanceColor = gd.getNextNumber();
maxDistanceBckg = gd.getNextNumber();
return true;
}
public void setNPasses(int nPasses){
//Has to be implemented for ExtendedPlugInFilter
}
}
导入ij.gui.DialogListener;
导入ij.gui.GenericDialog;
导入ij.plugin.filter.ExtendedPlugInFilter;
导入ij.plugin.filter.PlugInFilterRunner;
导入ij.process.ImageProcessor;
输入ij.ij;
导入ij.ImagePlus;
导入java.awt.AWTEvent;
导入java.awt.Label;
导入java.lang.Math;
公共类Purple_计数器实现ExtendedPlugInFilter、DialogListener{
private final int FLAGS=是否并行化堆栈;
私有布尔预览=真;
私人ImagePlus im;
私人整数计数;
私人双总;
专用int color=0xff64318a;
私有整数背景=0xffd7d7d7;
专用双maxDistanceColor;
私人双maxDistanceBckg;
公共整数设置(字符串参数、ImagePlus im){
this.im=im;
maxDistanceColor=60;
最大距离bckg=60;
返回旗;
}
公共无效运行(图像处理器ip){
int height=ip.getHeight();
int width=ip.getWidth();
计数=0;
总数=0;
对于(int a=0;a>16)和0xff;
int red2=(color2>>16)和0xff;
int green1=(颜色1>>8)和0xff;
int green2=(color2>>8)&0xff;
int blue1=color1&0xff;
int blue2=color2&0xff;
双距离=Math.sqrt((红色1*red1-2*red1*red2+red2*red2)+(绿色1*green1-2*green1*green2+green2*green2)+(蓝色1*blue1-2*blue1*blue2+blue2*blue2));
返回距离;
}
public int showDialog(ImagePlus imp、字符串命令、PlugInFilterRunner pfr){
//对话框来控制使用的颜色距离
GenericDialog gd=新建GenericDialog(“距离设置”);
gd.addMessage(“请选择识别POI和背景像素的最大距离:”);
gd.addSlider(“POI红色:”,0255,0x64);
gd.addSlider(“POI绿色:”,0255,0x31);
gd.addSlider(“POI蓝色:,0255,0x8a”);
gd.添加滑块(“BCKG红色:”,0255,0xd7);
gd.addSlider(“BCKG绿色:”,0255,0xd7);
gd.addSlider(“BCKG蓝色:”,0255,0xd7);
gd.addSlider(“POI最大距离:”,0.001,1000,maxDistanceColor);
gd.添加滑块(“BCKG最大距离:”,0.001,1000,最大距离BCKG);
gd.addPreviewCheckbox(pfr,“预览”);
gd.addDialogListener(this);
gd.showDialog();
if(gd.wascanced()| |!dialogItemChanged(gd,null)){
已完成的返回;
}
返回IJ.setupDialog(imp,FLAGS);
}
公共布尔对话框项已更改(GenericDialog gd,AWTEvent e){
int alpha=0xff;
int colorRed=(int)gd.getNextNumber();
int colorGreen=(int)gd.getNextNumber();
int colorBlue=(int)gd.getNextNumber();
int bckgRed=(int)gd.getNextNumber();
int bckgGreen=(int)gd.getNextNumber();
int bckgBlue=(int)gd.getNextNumber();
color=(alpha您可以为该任务编写一个小型ImageJ插件:
import ij.gui.DialogListener;
import ij.gui.GenericDialog;
import ij.plugin.filter.ExtendedPlugInFilter;
import ij.plugin.filter.PlugInFilterRunner;
import ij.process.ImageProcessor;
import ij.IJ;
import ij.ImagePlus;
import java.awt.AWTEvent;
import java.awt.Label;
import java.lang.Math;
public class Purple_Counter implements ExtendedPlugInFilter, DialogListener{
private final int FLAGS = DOES_RGB | PARALLELIZE_STACKS;
private boolean preview = true;
private ImagePlus im;
private int count;
private double total;
private int color = 0xff64318a;
private int background = 0xffd7d7d7;
private double maxDistanceColor;
private double maxDistanceBckg ;
public int setup(String args, ImagePlus im){
this.im = im;
maxDistanceColor = 60;
maxDistanceBckg = 60;
return FLAGS;
}
public void run(ImageProcessor ip){
int height = ip.getHeight();
int width = ip.getWidth();
count = 0;
total = 0;
for(int a = 0; a < height; a++){
for(int b = 0; b < height; b++){
int p = ip.getPixel(a,b);
if ( calculateDistance(p,color) < maxDistanceColor){
count++;
ip.putPixel(a,b,0xffff0000);
}
if ( calculateDistance(p,background) < maxDistanceBckg){
total++;
ip.putPixel(a,b,0xff000000);
}
}
}
IJ.log("Counted: " + count + "| Total: " + total);
IJ.log("Percentage: " + (count/total) * 100f);
IJ.log("");
}
private double calculateDistance(int color1, int color2){
int red1 = (color1 >> 16)&0xff;
int red2 = (color2 >> 16)&0xff;
int green1 = (color1 >> 8)&0xff;
int green2 = (color2 >> 8)&0xff;
int blue1 = color1 & 0xff;
int blue2 = color2 & 0xff;
double distance = Math.sqrt((red1 * red1 - 2*red1*red2 + red2*red2) + (green1 * green1 - 2*green1*green2 + green2*green2) + (blue1 * blue1 - 2*blue1*blue2 + blue2*blue2));
return distance;
}
public int showDialog(ImagePlus imp, String Command, PlugInFilterRunner pfr){
//Dialog to control the used color distances
GenericDialog gd = new GenericDialog("Distance Settings");
gd.addMessage("Please chose maximal distances to identify POI and background pixels: ");
gd.addSlider("POI Red: ", 0,255,0x64);
gd.addSlider("POI Green: ", 0,255,0x31);
gd.addSlider("POI Blue: ", 0,255,0x8a);
gd.addSlider("BCKG Red: ", 0,255,0xd7);
gd.addSlider("BCKG Green: ", 0,255,0xd7);
gd.addSlider("BCKG Blue: ", 0,255,0xd7);
gd.addSlider("POI max. Distance: ", 0.001, 1000, maxDistanceColor);
gd.addSlider("BCKG max. Distance: ", 0.001, 1000, maxDistanceBckg);
gd.addPreviewCheckbox(pfr, "Preview");
gd.addDialogListener(this);
gd.showDialog();
if(gd.wasCanceled() ||!dialogItemChanged(gd, null)){
return DONE;
}
return IJ.setupDialog(imp, FLAGS);
}
public boolean dialogItemChanged(GenericDialog gd, AWTEvent e){
int alpha = 0xff;
int colorRed = (int)gd.getNextNumber();
int colorGreen = (int)gd.getNextNumber();
int colorBlue = (int)gd.getNextNumber();
int bckgRed = (int)gd.getNextNumber();
int bckgGreen = (int)gd.getNextNumber();
int bckgBlue = (int)gd.getNextNumber();
color = (alpha << 24 | colorRed << 16 | colorGreen << 8 | colorBlue);
background = (alpha << 24 | bckgRed << 16 | bckgGreen << 8 | bckgBlue);
maxDistanceColor = gd.getNextNumber();
maxDistanceBckg = gd.getNextNumber();
return true;
}
public void setNPasses(int nPasses){
//Has to be implemented for ExtendedPlugInFilter
}
}
导入ij.gui.DialogListener;
导入ij.gui.GenericDialog;
导入ij.plugin.filter.ExtendedPlugInFilter;
导入ij.plugin.filter.PlugInFilterRunner;
导入ij.process.ImageProcessor;
输入ij.ij;
导入ij.ImagePlus;
导入java.awt.AWTEvent;
导入java.awt.Label;
导入java.lang.Math;
公共类Purple_计数器实现ExtendedPlugInFilter、DialogListener{
private final int FLAGS=是否并行化堆栈;
私有布尔预览=真;
私人ImagePlus im;
私人整数计数;
私人双总;
专用int color=0xff64318a;
私有整数背景=0xffd7d7d7;
专用双maxDistanceColor;
私人双maxDistanceBckg;
公共整数设置(字符串参数、ImagePlus im){
this.im=im;
maxDistanceColor=60;
最大距离bckg=60;
返回旗;
}
公共无效运行(图像处理器ip){
int height=ip.getHeight();
int width=ip.getWidth();
计数=0;
总数=0;
对于(int a=0;a>16)和0xff;
int red2=(color2>>16)和0xff;
int green1=(颜色1>>8)和0xff;
int green2=(color2>>8)&0xff;
int blue1=color1&0xff;
int b