制作VOC2007 数据 (matlab版) + 抠图工具

抠图工具见链接: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



### 回答1: KNN(K最近邻算法)是一种常用的分类算法,可以应用于图像处理中的抠图操作。在MATLAB中实现KNN算法进行图像抠图,需要按照以下步骤进行: 1. 获取待抠图的图像,并将其转换为合适的数据格式。可以使用MATLAB中的imread函数读取图像,并将其转为灰度图像以简化处理。 2. 对图像进行预处理,例如去噪和平滑等操作。可以通过应用滤波器(如高斯滤波器)对图像进行平滑处理,消除一些噪声影响。 3. 对每个像素进行特征提取,以构建特征向量。可以选择使用像素的颜色值和位置等特征作为输入特征。 4. 将数据集分为训练集和测试集。将一部分图像作为训练集,剩余的图像作为测试集用于验证算法的准确性。 5. 对每个测试像素,计算其与训练集中各像素的距离,可选择欧式距离或曼哈顿距离等作为距离度量。 6. 选取K个最近邻,根据它们的类别(即训练集中所属的像素类别),确定测试像素的类别。可以通过计算最频繁的类别来确定。 7. 将测试像素与训练集中的像素重新组合,即将其分类为前景或背景像素。 8. 重复以上步骤,直到对所有测试像素进行分类。 9. 最后通过图像的重建操作,将抠图后的结果输出。可以创建一个与原始图像大小相同的新图像,并将抠图后的像素重新填充到对应的位置。 通过以上步骤,就可以在MATLAB中实现KNN算法进行图像抠图操作。需要注意的是,KNN算法的性能很大程度上取决于特征的选择和距离度量的方法,因此在具体应用中需根据实际情况进行调整和优化。 ### 回答2: Matlab可以使用KNN算法来实现图像的抠图。首先,我们需要加载图像,并将其转换为特征向量表示。常用的图像特征表示方法有颜色直方图、纹理特征和形状特征等。 接下来,我们需要加载训练集和测试集的图像,并提取其特征向量表示。然后,我们使用KNN算法来找到与测试图像最相似的训练图像。 首先,我们计算测试图像与所有训练图像之间的距离。常用的距离度量方法包括欧氏距离、曼哈顿距离和余弦相似度等。我们选择适合我们问题的距离度量方法。 然后,我们根据距离的大小将训练图像进行排序。选择K个距离最近的训练图像作为测试图像的最邻近邻居。通常情况下,K的取值是一个较小的正整数。 最后,我们将测试图像与K个最邻近的训练图像进行比较,并根据它们的标签来确定测试图像的类别。可以使用多数表决的方法来决定测试图像的类别。即,选择K个最邻近的训练图像中出现次数最多的标签作为测试图像的类别。 通过这样的方法,我们可以实现KNN算法进行图像的抠图。根据测试图像的特征向量和K个最邻近的训练图像的标签,我们可以获得测试图像的抠图结果。 需要注意的是,KNN算法有其局限性。在应用KNN算法进行图像抠图时,我们需要选择合适的特征表示方法和距离度量方法,以及合适的K值。此外,KNN算法对特征表示的质量和样本分布的均匀性也很敏感。因此,在实际应用中,我们可能需要对图像进行预处理和优化,以提高抠图的准确性和效果。 ### 回答3: k近邻算法(k-nearest neighbors,简称knn)是一种常用的分类算法,可以用于图像抠图。下面将简要介绍如何使用MATLAB实现knn图像抠图。 1. 首先,将需要抠图的图像导入MATLAB的工作环境中。可以使用imread函数读取图像,将其转换为RGB格式。例如:image = imread('image.jpg'); 2. 将图像进行预处理,包括去噪、灰度化和归一化等操作,以便提高knn算法的准确性。 3. 根据需要抠图的区域,选择一系列样本点作为训练集。可以手动选择样本点,也可以通过自动划定区域选择样本点。 4. 使用knn算法对图像进行分类。将图像中的每个像素点作为测试样本,计算其与训练集中各样本点的欧几里得距离,选择距离最近的k个样本点。根据这k个样本点的类别,决定该像素点的类别。 5. 根据分类结果,将目标区域从原图像中分割出来,形成抠图效果。可以使用imwrite函数将分割结果保存为新的图像文件。例如:imwrite(result,'result.png'); 值得注意的是,knn图像抠图算法可能受到一些因素的影响,如训练集样本点的选择、k值的确定等。因此,建议根据具体情况进行调整和优化,以获得更好的抠图效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值