ImageJ - 累积直方图和直方图

ImageJ - Cumulative histograms and Histograms

我的程序 运行 遇到了一个小问题,因为它似乎无法在直方图中找到最高值来计算直方图应该是的比例,所以现在整个直方图都超出了界限

我真的希望有人能帮助我,因为它快把我逼疯了

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.awt.*;
import ij.plugin.filter.*;

public class Oblig3_Oppg2 implements PlugInFilter {

    public int setup(String arg, ImagePlus im) {;
        return DOES_8G + NO_CHANGES;
    }

    public void run(ImageProcessor ip) {
        final int W = 256;
        final int H = 100;
        final int H1 = 140;
        int[] hist = ip.getHistogram();
        int[] KH = new int[W]; //Cumulative Histogram Array


        int maxVal;

        //Calculates the highest pixel count in the Histogram

        for (int i = 0; i < W; i++){
            if (hist[i] > maxVal){
                maxVal = i;
            }
        }

        KH[0] = hist[0];

        for(int i = 1; i < W; i++) {
            KH[i] = KH[i-1] + hist[i];
        }

        ImageProcessor histIp = new ByteProcessor(W, H1);
        histIp.setValue(255);
        histIp.fill();

        int max = KH[255];


        for(int j = 0; j < W; j++){
            KH[j] = (KH[j]*100)/max;  //Scales the Cumulative Histogram
            hist[j] = (hist[j]*100)/maxVal; // Scales the Histogram
        }

        for (int k = 0; k < W; k++){
            histIp.setValue(0);
            histIp.drawLine(k, H, k, H-KH[k]);
        }

        for (int k = 0; k < W; k++){
            histIp.setValue(0);
            histIp.drawLine(k, H, k, H-hist[k]);
        }

        for (int l = 0; l < W; l++){
            histIp.setValue(l);
            histIp.drawLine(l, 140, l, 102);
        }
        histIp.setValue(0);
        histIp.drawLine(W, H, W, 0);

        // Display the histogram image:

        String hTitle = "Histogram";
        ImagePlus histIm = new ImagePlus(hTitle, histIp);
        histIm.show();

    }

}

您应该将 maxVal 设置为实际的 value,而不是循环中的当前 index

for (int i = 0; i < W; i++){
    if (hist[i] > maxVal){
        maxVal = hist[i]; // <-- here
    }
}

此外,最好将循环限制为 hist.length 而不是 W。如果您将 W 设置为不同于 ip.getHistogram() returns.

的数组长度的某个值,这将防止错误

因为你没有提供一个可运行的例子(即整个Java class;我假设你实现了ij.plugin.filter.PlugInFilter),我没有测试代码,它是我不太清楚你想要实现什么。