如何使用RGB像素值绘制直方图? [英] how to plot histogram using RGB pixel value?

查看:103
本文介绍了如何使用RGB像素值绘制直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在netbeans平台上制作了App.我想绘制直方图.我有红色,绿色和蓝色的图像像素.因此,请任何人对我提出建议,如何使用此像素值绘制直方图?我的代码在下面,其中我使用图像的红色,绿色和蓝色像素值.

i m make App in netbeans platform. i want to draw hisrogram. i have Red, Green ,and Blue color's pixel of image. so, please any one suugest to me that how can i plot histogram using this pixels value? my code is below ,in which i take RED,GREEN, and BLUE pixel value of image.

 enter code here



    import java.awt.Component;
     import java.awt.image.BufferedImage;
     import java.io.File;
     import java.io.IOException;
     import javax.imageio.ImageIO;

      public class WalkImageTest10 extends Component {

      public static void main(String[] foo) throws IOException {
      WalkImageTest10 wa= new WalkImageTest10();
     }

      public void printPixelARGB(int pixel) {
      int alpha = (pixel >> 24) & 0xff;
      int red = (pixel >> 16) & 0xff;
      int green = (pixel >> 8) & 0xff;
      int blue = (pixel) & 0xff;

      System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);
      //System.out.println(pixel);
     }

     private void marchThroughImage(BufferedImage image) {
     int w = image.getWidth();
      int h = image.getHeight();
      int pixel;
      System.out.println("width, height: " + w + ", " + h);

     for (int i = 0; i < h; i++) {
     for (int j = 0; j < w; j++) {
     //System.out.println("x,y: " + j + ", " + i);
     pixel = image.getRGB(j, i);
     printPixelARGB(pixel);
      //System.out.println("value of K:"+k+ " value of  pixel: " + pixel[j][i]);
     }
     }
System.out.println("loop is completed");
}

public WalkImageTest10() throws IOException {
// this is an image of a white spot on a black background.
// with the smoothing in the image it's of course not all black
// and white
BufferedImage image = 
ImageIO.read(new File("F:\\java\\aimages\\003.jpg"));
marchThroughImage(image);

}
}

推荐答案

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) {
    //g.drawLine();

    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);
        //g.drawLine(200 + i, 200, 200 + i, 200-(bins[i])/1500);

        //  System.out.println("bin["+i+"]==="+bins[i]);
     }

   }


  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.add("New image", new JLabel((im)));
        jtp.addTab("Histogram",new FinalHistogram(sbins));
        frame.add(jtp);
     frame.setVisible(true);
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
   }
 }

这篇关于如何使用RGB像素值绘制直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆