在Java中绘制缓冲图像的直方图

问题描述:

我有负载缓冲图像,需要绘制直方图吗?请建议我下一步绘图 RGB直方图.如果可以使用Jai来完成,请建议我这样做的方法.我已经尝试了很多,也用谷歌搜索了很多,但是dint找到了任何正确的解决方案. 这是我如何加载图像的方法,请提供后续步骤

I had load buffered image and need to plot histogram? please suggest me next steps to to plot RGB histogram. if it can done using jai please suggest me the way to do it. I hadtried alot and also googled alot but dint found any right solution. here is how i had load my image please provide me next steps

    BufferedImage   image= ImageIO.read(new File("C:\\Images\\Sunset.jpg"));             

    ParameterBlock pb = new ParameterBlock();

    int[] bins = { 256 };
    double[] low = { 0.0D };
    double[] high = { 256.0D };

    pb.addSource(image);
    pb.add(null);
    pb.add(1);
    pb.add(1);
    pb.add(bins);
    pb.add(low);
    pb.add(high);

    RenderedOp op = JAI.create("histogram", pb, null);
    Histogram histogram = (Histogram) op.getProperty("histogram");

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.IOException;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.*;

public class FinalHistogram extends JPanel {

    int[] bins = new int[256];
    FinalHistogram(int[] pbins) {
        bins = pbins;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {        
        for (int i = 0; i < 256; i++) {
            System.out.println("bin[" + i + "]===" + bins[i]);
            g.drawLine(200 + i, 300, 200 + i, 300 - (bins[i])/1000);
        }
    }


    public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame();
        frame.setSize(500, 500);
        int[] pbins = new int[256];
        int[] sbins = new int[256];
        PlanarImage image = JAI.create("fileload", "image12.tiff");
        BufferedImage bi = image.getAsBufferedImage();    
        System.out.println("tipe is          " + bi.getType());
        int[] pixel = new int[3];

        int k = 0;
        Color c = new Color(k);
        Double d = 0.0;
        Double d1;
        for (int x = 0; x < bi.getWidth(); x++) {
            for (int y = 0; y < bi.getHeight(); y++) {
                pixel = bi.getRaster().getPixel(x, y, new int[3]);
                d=(0.2125*pixel[0])+(0.7154*pixel[1])+(0.072*pixel[2]);
                k=(int) (d/256);
                sbins[k]++;
            }
        }
        System.out.println("copleted" + d + "--" + k);
        JTabbedPane jtp=new JTabbedPane();
        ImageIcon im= new ImageIcon(bi);
        jtp.addTab("Histogram",new FinalHistogram(sbins));
        frame.add(jtp);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}