




已阅读5页,还剩13页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/导入必要的包import java.awt.image.BufferedImage;import javax.imageio.ImageIO;import java.io.*;import java.util.Scanner;import java.util.Arrays;import javax.swing.*;import java.awt.*;/* * 基于k-means的简单图像检索 * author xuweibin * */class CBIR private File srcFile; /图像路径 private boolean bFile; private File file; /读入图片文件 private BufferedImage bi; private int imNum; /图像个数 private int width; /图像宽度 private int height; /图像高度 private int minx; /图像最小横坐标 private int miny; /图像最小纵坐标 public int rgb; /图像rgb颜色值 private double rate; /图像频率数组 private static final int N=65536; /图像像素个数,256*256,可以智能化 private static final int M=8; /颜色级数,可以智能化 private static final int IN=6; /图像个数,可以改变 private static boolean flag; /标记数组 private int It1; /初始化簇1 private int It2; /初始化簇2 private double center1; /簇1中心 private double center2; /簇2中心 private double dis1; /到簇1中心的距离 private double dis2; /到簇2中心的距离 /* * 在构造函数中初始化 * */ public CBIR() srcFile=null;bFile=false;file=null;imNum=0;width=0;height=0;minx=0;miny=0;rgb=null;rate=null;flag=null;It1=It2=-1;center1=center2=null;dis1=dis2=0; /* * 读入图像文件流 * */ public void readImage() srcFile=new File(image/); bFile=srcFile.exists(); if(!bFile|!srcFile.isDirectory()|!srcFile.canRead() try srcFile.createNewFile(); catch(IOException e)e.printStackTrace(); else file=srcFile.listFiles(); imNum=file.length; /* * 获得图像的rgb,并转化为亮度值 * */ public void getRGB() readImage();bi=new BufferedImageimNum;for(int i=0;iimNum;i+)trybii=ImageIO.read(filei); catch(IOException e)System.out.println(e); rgb=new intimNum; for(int k=0;kimNum;k+) width=bik.getWidth();height=bik.getHeight();minx=bik.getMinX();miny=bik.getMinY(); rgbk=new intwidth*height; int count=0; for(int i=minx;iwidth;i+) for(int j=miny;j16; int g=(pixel&0xff00)8; int b=(pixel&0xff); rgbkcount+=(int)(r*0.299+g*0.586+b*0.114); /转化为亮度值 /for(int i=0;iimNum;i+)/ for(int j=0;jN;j+)/ / System.out.println(rgbij);/ /* * 获得图像的频率数组 * */public void getRATE()getRGB();rate=new doubleimNumM;for(int j=0;jimNum;j+)int temp=new intM;for(int i=0;iM;i+) tempi=0;int sum=0;for(int i=0;iN;i+) if(0=rgbji&rgbji32) temp0+; else if(32=rgbji&rgbji64) temp1+; else if(64=rgbji&rgbji96) temp2+; else if(96=rgbji&rgbji128) temp3+; else if(128=rgbji&rgbji160) temp4+; else if(160=rgbji&rgbji192) temp5+; else if(192=rgbji&rgbji234) temp6+; else temp7+; sum+;for(int i=0;iM;i+)rateji=tempi/(sum*1.0);/for(int i=0;iimNum;i+)/ for(int j=0;jM;j+)/ System.out.println(rateij);/ System.out.println();/ /* * 获得两个频率数组的距离 * */public double getDistance(double r1,double r2)double pe=0;for(int i=0;iM;i+)pe+=Math.abs(r1i-r2i); /使用的是曼哈顿距离,也即城市街区距离return pe; /* * 获得初始化的簇中心 * */ public void getIntial() getRATE(); Scanner input=new Scanner(System.in); System.out.print(请输入初始化簇1:); It1=input.nextInt(); System.out.print(请输入初始化簇2:); It2=input.nextInt(); /* * 获得更新后的簇中心 * */ public void getCenter(double rt,boolean fl,boolean fg,double center) int temp=0; double centerTemp=new doubleM; for(int i=0;iM;i+) centerTempi=0; if(fg) for(int i=0;iIN;i+) if(fli) temp+; for(int j=0;jM;j+) centerTempj+=rtij; else for(int i=0;iIN;i+) if(!fli) temp+; for(int j=0;jM;j+) centerTempj+=rtij; for(int i=0;iM;i+) centeri=centerTempi/temp; /* * 对图像进行k-means聚类 * */public boolean cluster()getIntial();flag=new booleanIN;for(int i=0;iIN;i+)flagi=false;flagIt1-1=false;flagIt2-1=true;center1=new doubleM;center1=(double)rateIt1-1;center2=new doubleM;center2=(double)rateIt2-1;/for(int i=0;iM;i+)/System.out.println(center1i);boolean tflag=new booleanflag.length;tflag=(boolean)flag.clone(); while(true) for(int i=0;idis2) tflagi=true; else tflagi=false;if(Arrays.equals(flag,tflag) break;else flag=(boolean)tflag.clone(); getCenter(rate,flag,false,center1); getCenter(rate,flag,true,center2); System.out.println();System.out.println(聚类结果簇1:);for(int i=0;iIN;i+)if(!flagi)System.out.println(i+1);System.out.println(聚类结果簇2:);for(int i=0;iIN;i+)if(flagi)System.out.println(i+1);return flag; /* * 一个简单的UI,使用Gridlayout布局、jLabel容器 * */class ImageShowprivate Frame f;private JLabel bt1;private ImageIcon icon1; public void show(boolean fg,boolean fa) if(fa) f = new Frame(簇2:); f.setLayout (new GridLayout (5, 1); for(int i=0;ifg.length;i+) if(fgi) bt1=new JLabel(,JLabel.CENTER); bt1.setBorder(BorderFactory.createLineBorder(Color.white,3); icon1=new ImageIcon(image/+(i+1)+.gif); bt1.setIcon(icon1); f.add(bt1); f.setLocation(1000,10); else f = new Frame(簇1:); f.setLayout (new GridLayout (5, 1); for(int i=0;ifg.length;i+) if(!fgi) bt1=new JLabel(,JLabel.CENTER); bt1.setBorder(BorderFactory.createLineBorder(Color.white,3); icon1=new ImageIcon(image/+(i+1)+.gif); bt1.setIcon(icon1); f.add(bt1); f.setLocation(10,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年中职高考对口升学(理论考试)真题卷【生物与化工大类】模拟练习
- IMGN388-Antibody-生命科学试剂-MCE
- Human-TNFSF8-mRNA-生命科学试剂-MCE
- 2025年芜湖安徽工程大学高层次人才招聘60人模拟试卷及一套答案详解
- Golgi-laurdan-生命科学试剂-MCE
- 广平县安全培训课件
- 2025春季海南五指山市校园招聘教师15人模拟试卷附答案详解(模拟题)
- 2025年南京鼓楼医院集团安庆市石化医院招聘19人模拟试卷及答案详解(考点梳理)
- 2025内蒙古自治区农牧业科学院招聘48人考前自测高频考点模拟试题及答案详解(各地真题)
- 2025年中心供应室项目发展计划
- 关于公布2016年度中国电力优质工程奖评审结果的通知
- 送达地址确认书(诉讼类范本)
- 商务礼仪情景剧剧本范文(通用5篇)
- 幼教培训课件:《家园共育体系建构与实施策略》
- 《电子制造技术-电子封装》配套教学课件
- 三坐标测量基础知识(基础教育)
- 机关档案管理工作培训PPT课件
- 厦华验厂不良整改计划表
- (高清正版)T_CAGHP 054—2019 地质灾害治理工程质量检验评定标准(试行)
- 新速腾保险丝对照说明(12款1.4T手豪)
- 设备管理流程
评论
0/150
提交评论