您的位置:首页 > 编程语言 > MATLAB

[置顶] 制作VOC2007 数据 (matlab版) + 抠图工具

2017-04-03 10:19 204 查看
抠图工具见链接:http://download.csdn.net/detail/yeyang911/9802496

下面主要有两方面组成

1、由扣取的图像转换成txt , 存成格式如下:

%000002.jpg dog 44 28 132 121

%000002.jpg car 50 27 140 110

将扣的图像放入自己对应的文件夹如下:left,right 为两类

data文件夹内的内容如下:



matlab 代码

fildall = fopen('output1.txt','wt');%要存的数据txt

fileName = 'data\';%数据文件夹
matt = [];
filenames = dir(fileName);
filenames = filenames(3:end);
for i = 1:size(filenames,1);
file = dir([fileName filenames(i).name]);
file = file(3:end);
for j = 1:size(file,1)
name = file(j).name;
name = strrep(name,'.png','');
splitName = strsplit(name,'_');
SN = cell2mat(splitName(1));
matt = [matt; str2num(SN)];
end

end
[a b] = sort(matt);%排序
count = 1;
for i = 1:size(filenames,1);
file = dir([fileName filenames(i).name]);
file = file(3:end);
for j = 1:size(file,1)
name = file(j).name;
name = strrep(name,'.png','');
splitName = strsplit(name,'_');
N1 = str2num(cell2mat(splitName(3)));
N2 = str2num(cell2mat(splitName(4)));
N3 = str2num(cell2mat(splitName(5)));
N4 = str2num(cell2mat(splitName(6)));
SN = cell2mat(splitName(1));
outPutName = [SN '.png ' filenames(i).name ' ' num2str(N1) ' ' num2str(N2) ' ' num2str(N3) ' ' num2str(N4)];
outPut(count).line = outPutName;
count = count + 1;
end
end
outPut = outPut(b);
for i = 1:size(outPut,2)
fprintf(fildall, outPut(i).line);
fprintf(fildall, '\n');
end
fclose(fildall);


2、由图像转变成xml 文件

%%
%该代码可以做voc2007数据集中的xml文件,
%%
clc;
clear;
%注意修改下面五个变量
imgpath='E:\video\all\';%图像存放文件夹
txtpath='output1.txt';%txt文件
xmlpath_new='Annotations/';%修改后的xml保存文件夹
foldername='VOC2007';%xml的folder字段名
file_suffix = '.png'; %% 注意修改后缀名

fidin=fopen(txtpath,'r');
lastname='begin';

while ~feof(fidin)
tline=fgetl(fidin);
str = regexp(tline, ' ','split');
filepath=[imgpath,str{1}];
img=imread(filepath);
[h,w,d]=size(img);
imshow(img);
rectangle('Position',[str2double(str{3}),str2double(str{4}),str2double(str{5}),str2double(str{6})],'LineWidth',4,'EdgeColor','r');
pause(0.1);

if strcmp(str{1},lastname)%如果文件名相等,只需增加object
object_node=Createnode.createElement('object');
Root.appendChild(object_node);
node=Createnode.createElement('name');
node.appendChild(Createnode.createTextNode(sprintf('%s',str{2})));
object_node.appendChild(node);

node=Createnode.createElement('pose');
node.appendChild(Createnode.createTextNode(sprintf('%s','Unspecified')));
object_node.appendChild(node);

node=Createnode.createElement('truncated');
node.appendChild(Createnode.createTextNode(sprintf('%s','0')));
object_node.appendChild(node);

node=Createnode.createElement('difficult');
node.appendChild(Createnode.createTextNode(sprintf('%s','0')));
object_node.appendChild(node);

bndbox_node=Createnode.createElement('bndbox');
object_node.appendChild(bndbox_node);

node=Createnode.createElement('xmin');
node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{3}))));
bndbox_node.appendChild(node);

node=Createnode.createElement('ymin');
node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{4}))));
bndbox_node.appendChild(node);

node=Createnode.createElement('xmax');
node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str2num(str{5})+str2num(str{3})))));
bndbox_node.appendChild(node);

node=Createnode.createElement('ymax');
node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str2num(str{6})+str2num(str{4})))));
bndbox_node.appendChild(node);
else %如果文件名不等,则需要新建xml
copyfile(filepath, 'JPEGImages');
%先保存上一次的xml
if exist('Createnode','var')
tempname=lastname;
tempname=strrep(tempname,file_suffix,'.xml');
xmlwrite(tempname,Createnode);
end

Createnode=com.mathworks.xml.XMLUtils.createDocument('annotation');
Root=Createnode.getDocumentElement;%根节点
node=Createnode.createElement('folder');
node.appendChild(Createnode.createTextNode(sprintf('%s',foldername)));
Root.appendChild(node);
node=Createnode.createElement('filename');
node.appendChild(Createnode.createTextNode(sprintf('%s',str{1})));
Root.appendChild(node);
source_node=Createnode.createElement('source');
Root.appendChild(source_node);
node=Createnode.createElement('database');
node.appendChild(Createnode.createTextNode(sprintf('My Database')));
source_node.appendChild(node);
node=Createnode.createElement('annotation');
node.appendChild(Createnode.createTextNode(sprintf('VOC2007')));
source_node.appendChild(node);

node=Createnode.createElement('image');
node.appendChild(Createnode.createTextNode(sprintf('flickr')));
source_node.appendChild(node);

node=Createnode.createElement('flickrid');
node.appendChild(Createnode.createTextNode(sprintf('NULL')));
source_node.appendChild(node);
owner_node=Createnode.createElement('owner');
Root.appendChild(owner_node);
node=Createnode.createElement('flickrid');
node.appendChild(Createnode.createTextNode(sprintf('NULL')));
owner_node.appendChild(node);

node=Createnode.createElement('name');
node.appendChild(Createnode.createTextNode(sprintf('yy')));
owner_node.appendChild(node);
size_node=Createnode.createElement('size');
Root.appendChild(size_node);

node=Createnode.createElement('width');
node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(w))));
size_node.appendChild(node);

node=Createnode.createElement('height');
node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(h))));
size_node.appendChild(node);

node=Createnode.createElement('depth');
node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(d))));
size_node.appendChild(node);

node=Createnode.createElement('segmented');
node.appendChild(Createnode.createTextNode(sprintf('%s','0')));
Root.appendChild(node);
object_node=Createnode.createElement('object');
Root.appendChild(object_node);
node=Createnode.createElement('name');
node.appendChild(Createnode.createTextNode(sprintf('%s',str{2})));
object_node.appendChild(node);

node=Createnode.createElement('pose');
node.appendChild(Createnode.createTextNode(sprintf('%s','Unspecified')));
object_node.appendChild(node);

node=Createnode.createElement('truncated');
node.appendChild(Createnode.createTextNode(sprintf('%s','0')));
object_node.appendChild(node);

node=Createnode.createElement('difficult');
node.appendChild(Createnode.createTextNode(sprintf('%s','0')));
object_node.appendChild(node);

bndbox_node=Createnode.createElement('bndbox');
object_node.appendChild(bndbox_node);

node=Createnode.createElement('xmin');
node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{3}))));
bndbox_node.appendChild(node);

node=Createnode.createElement('ymin');
node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str{4}))));
bndbox_node.appendChild(node);

node=Createnode.createElement('xmax');
node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str2num(str{5})+str2num(str{3})))));
bndbox_node.appendChild(node);

node=Createnode.createElement('ymax');
node.appendChild(Createnode.createTextNode(sprintf('%s',num2str(str2num(str{6})+str2num(str{4})))));
bndbox_node.appendChild(node);

lastname=str{1};
end
%处理最后一行
if feof(fidin)
tempname=lastname;
tempname=strrep(tempname,file_suffix,'.xml');
xmlwrite(tempname,Createnode);
end
end
fclose(fidin);

file=dir(pwd);
for i=1:length(file)
if length(file(i).name)>=4 && strcmp(file(i).name(end-3:end),'.xml')
fold=fopen(file(i).name,'r');
fnew=fopen([xmlpath_new file(i).name],'w');
line=1;
while ~feof(fold)
tline=fgetl(fold);
if line==1
line=2;
continue;
end
expression = '   ';
replace=char(9);
newStr=regexprep(tline,expression,replace);
fprintf(fnew,'%s\n',newStr);
end
fprintf('已处理%s\n',file(i).name);
fclose(fold);
fclose(fnew);
delete(file(i).name);
end
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐