Java实现基于内容的数字图像处理代码

这个Java程序实现了基于内容的图像处理,包括读取图像、颜色特征提取和图像检索。用户界面允许用户打开单个图像文件或者整个目录,将图像添加到颜色、纹理和形状数据库中,并进行相应的图像检索。程序使用了颜色直方图、纹理和形状特征进行匹配,以找到最相似的图像。
摘要由CSDN通过智能技术生成

package CBIR;

import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;


public class myFile {
String filepath;
File imagefile;
BufferedImage image;
int []rgbdata;

/**
* 构造函数只需要图片地址*/
myFile(String fp){
filepath = fp;
imagefile = new File(fp);
try {
image = ImageIO.read(imagefile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
rgbdata = new int [image.getWidth()*image.getHeight()];
GetRGB(image,0,0,image.getWidth(),image.getHeight(),rgbdata);//将RGB值存储进rgbdata
}


int [] GetRGB(BufferedImage image,int x,int y,int width,int height,int [] pixels)
{
int type = image.getType();  
        if ( BufferedImage.TYPE_INT_ARGB == type  || BufferedImage.TYPE_INT_RGB == type )  
            return (int [])image.getRaster().getDataElements( x, y, width, height, pixels );  
        return image.getRGB( x, y, width, height, pixels, 0, width );  
}

int getWidth(){
return image.getWidth();
}
int getHeight(){
return image.getHeight();
}
int []getRgb(){
return rgbdata;
}
}


可视化界面:

package CBIR;
import java.awt.*;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.math.*;
import java.util.*;


import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;


@SuppressWarnings("unused")
public class MyFrame {
/**
* 定义窗口结构
* */
JFrame jf = new JFrame();
JPanel panel = new JPanel();
Container content = jf.getContentPane();
JMenuBar mymb = new JMenuBar();
JMenu file = new JMenu("文件");
JMenuItem fofile = new JMenuItem("打开文件夹");
JMenuItem foone = new JMenuItem("打开单个图像文件");
JMenu prep = new JMenu("图像预处理");
JMenuItem fafile = new JMenuItem("添加颜色数据库-目录");
JMenuItem faone = new JMenuItem("添加颜色数据库-文件");
JMenuItem tfafile = new JMenuItem("添加纹理数据库-目录");
JMenuItem tfaone = new JMenuItem("添加纹理数据库-文件");
JMenuItem sfafile = new JMenuItem("添加形状数据库-目录");
JMenuItem sfaone = new JMenuItem("添加形状数据库-文件");
JMenu myretrieval = new JMenu("图像检索");
JMenuItem bocr = new JMenuItem("基于颜色图像检索");
JMenuItem bosr = new JMenuItem("基于形状图像检索");
JMenuItem tbr = new JMenuItem("基于纹理图像检索");
JMenuItem TEST = new JMenuItem("TEST");

String top_imgpath=null;//用于判断是否有待检测图案

public MyFrame(){

//jf.setLayout(null);
panel.setLayout(null);
//panel.setBounds(100, 100, 800, 800);

file.add(fofile);
file.add(foone);
foone.addActionListener(new OpenOneGActionListener());

prep.add(fafile);
prep.add(faone);
prep.add(tfafile);
prep.add(tfaone);
prep.add(sfafile);
prep.add(sfaone);
sfafile.addActionListener(new PreShapeActionListener());
tfafile.addActionListener(new PreTextureFileActionListener());
faone.addActionListener(new PreOneFActionListener());
fafile.addActionListener(new PreFileActionListener());
myretrieval.add(bocr);
myretrieval.add(bosr);
myretrieval.add(tbr);
bosr.addActionListener(new BasedOnShapeActionListener());
bocr.addActionListener(new BasedOnColorActionListrener());
tbr.addActionListener(new BasedOnTextureActionListener());



mymb.add(file);
mymb.add(prep);
mymb.add(myretrieval);
//mymb.add(TEST);
//TEST.addActionListener(new test());

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(new Dimension(800,700));
jf.setTitle("MyCBIR-yishSun");


jf.setJMenuBar(mymb);
jf.setContentPane(panel);
jf.setVisible(true);
}

/**
* 打开单幅图片
* */
class OpenOneGActionListener implements ActionListener{
@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent arg0) {
JFileChooser fCh = new JFileChooser("F:\\2015ComprehensiveTraining\\ObjectCategories");
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPEG","jpg");
fCh.setFileFilter(filter);
JLabel label = new JLabel();
int n = fCh.showOpenDialog(fCh);
if(n == fCh.APPROVE_OPTION){
ImageIcon img = new ImageIcon(fCh.getSelectedFile().getPath());
img.setImage(img.getImage().getScaledInstance(180, 180, Image.SCALE_DEFAULT));
label.setText("待查询图像");
label.setVerticalTextPosition(JLabel.BOTTOM);
label.setHorizontalTextPosition(JLabel.CENTER);
label.setToolTipText(fCh.getSelectedFile().getPath());
label.setIcon(img);
top_imgpath =  fCh.getSelectedFile().getPath();
label.setBounds(10, 150, 180, 200);
panel.removeAll();
panel.add(label);
panel.updateUI();
jf.repaint();
}
else{
}
}

}


/**
* 鸡肋功能,并没实现*/
class OpenFileActionListener implements ActionListener{
@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent arg0) {
JFileChooser fCh = new JFileChooser("C://");
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPEG","BMP","jpg","bmp");
fCh.setFileFilter(filter);
JLabel label = new JLabel();
int n = fCh.showOpenDialog(fCh);
if(n == fCh.APPROVE_OPTION){
label.setText("SHow");
label.setVerticalTextPosition(JLabel.CENTER);
label.setHorizontalAlignment(JLabel.CENTER);
label.setToolTipText(fCh.getSelectedFile().getPath());
label.setIcon(new ImageIcon(fCh.getSelectedFile().getPath()));
}
else
label.setText("DK");
  panel.removeAll();
  panel.add(label);
  content.add(panel);
  panel.updateUI();
  jf.repaint();
}

}

/**
* 初始单幅图片,早先测试用
* */
class PreOneFActionListener implements ActionListener{
@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent arg0) {
String imgfpath;
JFileChooser fCh = new JFileChooser("F:\\2015ComprehensiveTraining");
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPEG","jpg");
fCh.setFileFilter(filter);
JLabel label = new JLabel();
 
int n = fCh.showOpenDialog(fCh);

if(n == fCh.APPROVE_OPTION){
imgfpath = fCh.getSelectedFile().getPath();

myFile m = new myFile(imgfpath);
CBIRColor M1 = new CBIRColor(m.getWidth(),m.getHeight(),m.getRgb());
connectMysql insert = new connectMysql();
insert.insertHis(imgfpath,M1.getEx(),M1.getDx());
M1.show();
}
else
  panel.removeAll();
  panel.add(label);
  content.add(panel);
  panel.updateUI();
  jf.repaint();
// TODO Auto-generated method stub
}




}


/**
* 利用颜色的特征提取初始化一个文件夹*/
class PreFileActionListener implements ActionListener{
int count = 0;
int all = 0;


@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent arg0) {
connectMysql insert = new connectMysql();
String imgfpath;
JFileChooser fCh = new JFileChooser("F:\\2015ComprehensiveTraining");
fCh.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int n=fCh.showOpenDialog(fCh);
File file = fCh.getSelectedFile();
File[] tempList = file.listFiles();



if(n == fCh.APPROVE_OPTION){
for(int i=0;i<tempList.length;i++){
it(tempList[i]);
       }


for(int i=0;i<tempList.length;i++){
itratorFile(tempList[i],insert);
}
}
}
/**
* 遍历文件夹*/
private void it(File file){
int a=0;
if(file.isDirectory()){
File []fa = file.listFiles();
if(0<fa.length){
for(int i=0;i<fa.length;i++){
it(fa[i]);
}
}
}
else{
if(file.getPath().endsWith("jpg")||file.getPath().endsWith("JPG"))
all++;
}


}//本来想用此做个进度条,最后没时间做了

private void itratorFile(File file,connectMysql a){
String imgpath;
if(file.isDirectory()){
File []fa = file.listFiles();
if(0<fa.length){
for(int i=0;i<fa.length;i++){
itratorFile(fa[i],a);
}
}
}
else{
//这里应该设置寻找Jpeg文件。后加//已加
if(file.getPath().endsWith("jpg")||file.getPath().endsWith("JPG")){
myFile m = new myFile(file.getPath());
CBIRColor M1 = new CBIRColor(m.getWidth(),m.getHeight(),m.getRgb());
a.insertHis(file.getPath(),M1.getEx(),M1.getDx());
count++;
System.out.println(count);
}
}
}
}

class PreShapeActionListener implements ActionListener{
int count = 0;
int all = 0;


@SuppressWarnings("static-access")
@Override
public void actionPerformed(ActionEvent arg0) {
connectMysql insert = new connectMysql();
String imgfpath;
JFileChooser fCh = new JFileChooser("F:\\2015ComprehensiveTraining");
fCh.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int n=fCh.showOpenDialog(fCh);
File file = fCh.getSelectedFile();
File[] tempList = file.listFiles();

if(n == fCh.APPROVE_OPTION){
for(int i=0;i<tempList.length;i++){
it(tempList[i]);
       }


for(int i=0;i<tempList.length;i++){
itratorFile(tempList[i],insert);
}
}
}
/**
* 遍历文件夹*/

private void it(File file){
int a=0;
if(file.isDirectory()){
File []fa = file.listFiles();
if(0<fa.length){
for(int i=0;i<fa.length;i++){
it(fa[i]);
}
}
}
else{
if(file.getPath().endsWith("jpg")||file.getPath().endsWith("JPG"))
all++;
}


}//同上



private void itratorFile(File file,connectMysql a){
String imgpath;
if(file.isDirectory()){
File []fa = file.listFiles();
if(0<fa.length){
for(int i=0;i<fa.l

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值