求颜色直方图的java代码,C语言写的也可以,多谢多谢 可以发到我的邮箱:sadwxqezc@163.com
发布网友
发布时间:2022-05-18 03:01
我来回答
共2个回答
热心网友
时间:2023-10-08 15:14
使用“java ColorHistogram 图片路径”命令运行就能看到对该图片的RGB颜色分析
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class ColorHistogram {
int R[]=new int[256];
int G[]=new int[256];
int B[]=new int[256];
int RGB[]=new int[256];
int max=0;
boolean finished=false;
public ColorHistogram(File f){
for(int i=0;i<256;i++){
R[i]=0;
G[i]=0;
B[i]=0;
RGB[i]=0;
}
if(f==null) return;
try {
BufferedImage bufIm=ImageIO.read(f);
int w=bufIm.getWidth();
int h=bufIm.getHeight();
if(w<=0||h<=0) return;
for(int i=0;i<h;i++)
for(int j=0;j<w;j++){
int rgb=bufIm.getRGB(j, i);
int r=(rgb>>16)&0xff;
int g=(rgb>>8)&0xff;
int b=(rgb)&0xff;
R[r]++;
G[g]++;
B[b]++;
}
for(int i=0;i<256;i++){
RGB[i]=(R[i]+G[i]+B[i])/3;
max=max<R[i]?R[i]:max;
max=max<G[i]?G[i]:max;
max=max<B[i]?B[i]:max;
}
finished=true;
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("max="+max);
}
public void paintHistogram(Graphics g,int w,int h,int flag,boolean smoothly,int mx,int my,JLabel detail){
if(!finished) return;
int src[]=null;
if(flag==1){
g.setColor(Color.RED);
src=R;
}else if(flag==2){
g.setColor(Color.GREEN);
src=G;
}else if(flag==3){
g.setColor(Color.BLUE);
src=B;
}else if(flag==4){
g.setColor(Color.WHITE);
src=RGB;
}
int lineH=0;
int x=0;
int lastX=0;
int lastH=0;
int tmpH=0;
boolean painMouseLine=false;
if(mouseX<0||!detail.isEnabled()) painMouseLine=true;
int mouseI=-1;
int mouseH=-1;
for(int i=0;i<src.length;i++){
lineH=src[i]*h/max;
x=(i*w)>>8;
if(smoothly)
for(int j=0;j<x-lastX;j++){
tmpH=lastH+(lineH-lastH)*j/(x-lastX);
g.drawLine(lastX+j,h-tmpH,lastX+j,h);
}
if(!painMouseLine&&x>mouseX){
Color c=g.getColor();
g.setColor(Color.yellow);
g.drawLine(x,0,x,h-lineH);
mouseH=lineH;
mouseI=i;
g.setColor(c);
painMouseLine=true;
}
g.drawLine(x,h-lineH,x,h);
lastX=x;
lastH=lineH;
}
g.setColor(Color.yellow);
g.drawLine(0,h-mouseH-1,w,h-mouseH-1);
if(detail.isEnabled()&&mouseI>0){
detail.setText(""+mouseI+","+(src[mouseI]*100/max)+"%");
}
}
static ColorHistogram colorHistogram=null;
static JPanel histogram=null;
static int rgbFlag=1;
static JCheckBox multilayerBox=null;
static JCheckBox smoothlyBox=null;
static JCheckBox valueBox=null;
static JLabel valueL=null;
static int mouseX=-1;
static int mouseY=-1;
static boolean lastIsMultilayer=false;
public static void main(String args[]){
JFrame jf=new JFrame();
jf.setBounds(200,100, 600, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jp=new JPanel();
JRadioButton Rb=new JRadioButton("R",true);
JRadioButton Gb=new JRadioButton("G");
JRadioButton Bb=new JRadioButton("B");
JRadioButton RGBb=new JRadioButton("RGB");
ButtonGroup group=new ButtonGroup();
group.add(Rb);
group.add(Gb);
group.add(Bb);
group.add(RGBb);
multilayerBox=new JCheckBox("叠放");
jp.add(multilayerBox);
jp.add(Rb);
jp.add(Gb);
jp.add(Bb);
jp.add(RGBb);
smoothlyBox=new JCheckBox("平滑");
jp.add(smoothlyBox);
valueBox=new JCheckBox("详细:",true);
valueL=new JLabel("null,null");
jp.add(valueBox);
jp.add(valueL);
jf.add(jp,"South");
if(args.length>0)
colorHistogram=new ColorHistogram(new File(args[0]));
else
colorHistogram=new ColorHistogram(null);
histogram=new JPanel(){
public void paint(Graphics g){
if(!multilayerBox.isSelected()||!lastIsMultilayer){
super.paint(g);
lastIsMultilayer=multilayerBox.isSelected();
}
colorHistogram.paintHistogram(g,getWidth(),getHeight(),rgbFlag,smoothlyBox.isSelected(),mouseX,mouseY,valueL);
}
};
histogram.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if(valueBox.isSelected()){
mouseX=e.getX();
mouseY=e.getY();
}
histogram.repaint();
}
});
histogram.setBackground(Color.BLACK);
jf.add(histogram);
Rb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
rgbFlag=1;
histogram.repaint();
}});
Gb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
rgbFlag=2;
histogram.repaint();
}});
Bb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
rgbFlag=3;
histogram.repaint();
}});
RGBb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
rgbFlag=4;
histogram.repaint();
}});
smoothlyBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(!smoothlyBox.isSelected()){
multilayerBox.setSelected(false);
}
histogram.repaint();
}});
multilayerBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(multilayerBox.isSelected()){
valueBox.setSelected(false);
valueL.setEnabled(false);
}
valueBox.setEnabled(!multilayerBox.isSelected());
histogram.repaint();
}});
valueBox.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
valueL.setEnabled(valueBox.isSelected());
histogram.repaint();
}});
jf.setVisible(true);
jf.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent arg0) {
multilayerBox.setSelected(false);
histogram.repaint();
}
});
jf.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent arg0) {
multilayerBox.setSelected(false);
}
});
}
}
热心网友
时间:2023-10-08 15:15
开源项目CxImage中有现成的功能和代码。