最近遇到如题的问题,语义分割数据集格式是原始图像、标注图像、色号与类别对应的标签。
java实现思路就是读入标注图像,将像素转为类别的二维矩阵,然后对二维矩阵进行同类别的连通域检测,将检测结果保存为ArrayList格式:label,point1(x1,y1),point2(x2,y2)
首先得到文件夹下所有的文件
File file=new File("data/img/");
File[] tempList = file.listFiles();
for (int filei = 0; filei < tempList.length; filei++) {
if (tempList[filei].isFile()) {
}
}
读取文件,获取类别对应标签的hashmap,其中类别与色号用#隔开,由于opencv中无法显示中文,这里用数字代替。之后会有一个数字与类别的hashmap与此类似,不一一列出。
public Map Readtxt(String filename) throws Exception{
//读入标签文件,保存成hashmap的格式
Map labelmap = new HashMap();
File file = new File(filename);
FileReader fr = new FileReader(file);
BufferedReader bf = new BufferedReader(fr);
String line = " ";
int i = 1;
while ((line = bf.readLine()) !=null){
String label[] = line.split(" #");
//labelmap.put(label[1], label[0]);
labelmap.put(label[1], i);
i++;
}
bf.close();
return labelmap;
}
然后将图像像素保存为像素矩阵,有像素值的就用类别代替,没有的置零。
//获取图片的像素矩阵
GetImagePixel gip = new GetImagePixel( );
String colormap [][] = gip.getImagePixel("imgname");
int[][] colorintmap = new int[colormap.length][colormap[0].length];
for(int i=0;i<colormap.length;i++){
for(int j=0;j<colormap[i].length;j++){
if(labelmap.get(colormap[i][j])!=null){
colorintmap[i][j] = (int) ( labelmap.get(colormap[i][j]));}else{
colorintmap[i][j] =