⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getimagefeature.java

📁 动画素材图像语义标注系统:该系统实现对图片的语义标注
💻 JAVA
字号:
package searchjet.annotation;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.HashMap;



/**
 * 输入图片路径,从指定的配置文件中读取图片物理特征信息,返回一个HashMap feature,
 * 在这个表中,key为应填入的选项框的名称,value为一个字符串,包括了选项值
 * @author mikil
 *
 */
public class GetImageFeature {
	/**
	 * get the feature of image
	 * @param mediaUriget
	 * @return  feature of image
	 */
	public HashMap getImageFeature(String mediaUri){
		HashMap feature=new HashMap();
		boolean writepath=writeFilePath(mediaUri);
		if(writepath){
			boolean extract=extraFeature();
			if(extract){
				putFeatureToHashMap(feature);
			}
		}
		return feature;
	}
	
	
	/**
	 * write the filepath to the docment
	 * @param filepath
	 * @return whether write succeed
	 */
	private boolean writeFilePath(String filepath){
		try{
		    FileOutputStream out = new FileOutputStream("D:\\Input.txt");
		    byte[] buf = filepath.getBytes();
		    out.write(buf);
		    out.flush();
		    out.close();
		    return true;
		    }catch (Exception e) {
				e.printStackTrace();
			}
		    return false;
	}	
	
	/**
	 * extract the feature of image ,through use the ExtraImage.dll
	 * @return whether extract succeed
	 */
	private boolean extraFeature(){
		try{
		   ExtraImageFeature ex=new ExtraImageFeature();
		   ex.extra();
		   return true;
		}
	    catch (Exception e) {
		e.printStackTrace();
	    }
        return false;
	}
	
	/**
	 * read the feature of image from the file output.txt
	 * @return the string[] of feature
	 */
	private String[] readImageFeature(){
		String[] f=new String[9];
		String str=readFile("D:\\Output.txt");
		String[] s=splitString(",",str);
		for(int i=0;i<8;i++){
			f[i]=s[i];
		}
		String texture=s[8];
		for(int i=9;i<s.length;i++){
		    if(s[i]!=""){
		       texture+=" ";
		       texture+=s[i];
		   }
		}
		f[8]=texture;
		return f;
	}
	
	/**
	 * read  file named fileName 
	 * @param fileName the name of  file
	 * @return the string from  file
	 */
	private String readFile(String filePath) {
		String result = new String("");
		int length = 0 ;
		char s [] = new char[8048];
		try{
            FileReader f = new FileReader(new File(filePath));
    	    length = f.read(s);
    		f.close();
           }
		catch 
		(FileNotFoundException ex)			   
		   {System.out.println("找不到文件");}
		  catch (Exception e)			   
		   {System.out.println("字符数组长度不够");}
        result = new String(s,0,length);
        return result;
	}
	
	/**
	 * write feature to a hashmap
	 * @param feature hashmap,feature of image
	 */
	private void putFeatureToHashMap(HashMap feature){
		String[] fstr=readImageFeature();
		feature.put("moment",fstr[0]);//力矩
		feature.put("barycenterx", fstr[1]);//重心矩坐标X
		feature.put("barycentery", fstr[2]);//重心矩坐标Y
		feature.put("barycentervalue", fstr[3]);//重心矩值
		feature.put("energy",fstr[4]);//能量
		feature.put("entropy", fstr[5]);//熵
		feature.put("inertia", fstr[6]);//惯性矩
		feature.put("placidity", fstr[7]);//局部平稳性
		feature.put("colorhistogram", fstr[8]);//颜色直方图
	}
	
	/**
	   * divide the string to string array
	   * @param str   the string which will be divided
	   * @param s     list separator
	   * @return      as a string array
	   */
	private String[] splitString(String sdelimiter,String str){
      String[] array=str.split(sdelimiter);
      return array;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -