Matlab实现图像处理并以xml格式存入文件

xiaoxiao2021-07-04  235

题目:给出一组图片(图片上框有不同颜色的方框,同一颜色的方框可能有多个,不同颜色的方框代表不同的类别) 例如这样的图片: 要求: ①在一个文件夹里循环读入图片 ②输出图片的宽、高、框的类别、框的左下角坐标(X,Y),框右上角坐标(X,Y),标注的难易程度(0:不难) ③将输出的结果保存成xml类型(根节点、子节点、二级节点)的文件中,保存的文件名与读入的图片名相匹配。

实现过程: ①实现循环读文件夹中的图片:

% 图像文件夹路径 file_path = 'F:\标注图\'; %获取该文件夹中所有jpg格式的图像 img_path_list = dir(strcat(file_path,'*.jpg')); %获取图像总数量 img_num = length(img_path_list); %有满足条件的图像 if img_num > 0 %逐一读取图像 for j = 1:img_num % 图像名 image_name = img_path_list(j).name; rgb = imread(strcat(file_path,image_name)); 进行图像相关处理 end end

②实现图像处理并达到输出要求:

r = rgb(:,:,1); %提取彩色图像的红色分量。 g = rgb(:,:,2); %提取彩色图像的绿色分量。 b = rgb(:,:,3); %提取彩色图像的蓝色分量。 [w,h,s]=size(rgb); %提取图像的高(行)、宽(列),维数(没有用到) red=r>=175&g<36&b<36; %提取符合图片中红色的像素范围 blue=r<=60&g<135&b>70; %提取符合图片中蓝色的像素范围 green=(20>r)&(g>80)&(g<100)&(b>20)&(b<=35); %提取符合图片中绿色的像素范围 orange=(r>200)&(g<118)&(g>36)&b<50; %提取符合图片中橙色的像素范围 purple=(175>r)&(r>97)&g<35&(b>52)&(b<=67); %提取符合图片中紫色的像素范围 a{1}=red; a{2}=blue; a{3}=green; %将颜色存入矩阵 a{4}=orange; a{5}=purple; yz=300; %作为bwareaopen函数的参数 for i=1:5 if i==1 str='车厢'; elseif i==2 str='车门'; elseif i==3 %不同颜色所代表的类别 str='异物'; elseif i==4 str='插销'; elseif i==5 str='文字'; end %删除二值图像BW中面积小于P对象 colorb = bwareaopen(a{i},yz); %一般默认8邻域 connect_size = 8; %创建由指定形状shape对应的结构元素,半径为8的圆形 se = strel('disk',connect_size); %膨胀灰度,二值,压缩二值图像colorb。参数SE为由strel函数返回的结构元素或者结构元素对象组。 colorb = imdilate(colorb,se); %找联通区域的数量 L = bwlabel(colorb); num = max(max(L)); B=cell(num,8); %依次取每个框的每个角的坐标和长款 for i = 1:num [x,y] = find(L == i); minx=min(y); maxx=max(y); miny=min(x); maxy=max(x); Width=maxx-minx; Hight=maxy-miny; x2= minx+Width; y1=miny+Hight;

③将说出数据存入XML文件中

docNode = com.mathworks.xml.XMLUtils.createDocument('annotation'); docRootNode = docNode.getDocumentElement; thisElement = docNode.createElement('size'); docRootNode.appendChild(thisElement); dataNode = docNode.createElement('width'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',h))); thisElement.appendChild(dataNode); dataNode = docNode.createElement('height'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',w))); thisElement.appendChild(dataNode); thisElement = docNode.createElement('object'); docRootNode.appendChild(thisElement); dataNode = docNode.createElement('name'); dataNode.appendChild(docNode.createTextNode(sprintf('%s',str))); thisElement.appendChild(dataNode); dataNode = docNode.createElement('difficult'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',0))); thisElement.appendChild(dataNode); CElement = docNode.createElement('bndbox'); thisElement.appendChild(CElement); dataNode = docNode.createElement('xmin'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',minx))); CElement.appendChild(dataNode); dataNode = docNode.createElement('ymin'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',y1))); CElement.appendChild(dataNode); dataNode = docNode.createElement('xmax'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',x2))); CElement.appendChild(dataNode); dataNode = docNode.createElement('ymax'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',miny))); CElement.appendChild(dataNode); docNode.appendChild(docNode.createComment('this is a comment')); xmlwrite(filename,docNode); type(filename);

整体代码:

file_path = 'C:\Users\Administrator\Desktop\pic\';% 图像文件夹路径 img_path_list = dir(strcat(file_path,'*.jpg'));%获取该文件夹中所有jpg格式的图像 img_num = length(img_path_list);%获取图像总数量 if img_num > 0 %有满足条件的图像 for j = 1:img_num %逐一读取图像 image_name = img_path_list(j).name;% 图像名 rgb = imread(strcat(file_path,image_name)); r = rgb(:,:,1); %提取彩色图像的红色分量。 g = rgb(:,:,2); %提取彩色图像的绿色分量。 b = rgb(:,:,3); [w,h,s]=size(rgb); i = find('.'==image_name); a = image_name(1:i-1); imname = strrep(a, ' ', ''); filename=[imname,'.xml']; fid=fopen(filename,'a'); red=r>=175&g<36&b<36; blue=r<=60&g<135&b>70; green=(20>r)&(g>80)&(g<100)&(b>20)&(b<=35); orange=(r>200)&(g<118)&(g>36)&b<50; purple=(175>r)&(r>97)&g<35&(b>52)&(b<=67); a=cell(5,1); a{1}=red; a{2}=blue; a{3}=green; a{4}=orange; a{5}=purple; yz=300; %作为bwareaopen函数的参数 docNode = com.mathworks.xml.XMLUtils.createDocument('annotation'); docRootNode = docNode.getDocumentElement; thisElement = docNode.createElement('size'); docRootNode.appendChild(thisElement); dataNode = docNode.createElement('width'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',h))); thisElement.appendChild(dataNode); dataNode = docNode.createElement('height'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',w))); thisElement.appendChild(dataNode); for i=1:5 if i==1 str='车厢'; elseif i==2 str='车门'; elseif i==3 str='异物'; elseif i==4 str='插销'; elseif i==5 str='文字'; end colorb = bwareaopen(a{i},yz); %删除二值图像BW中面积小于P对象 connect_size = 8; se = strel('disk',connect_size); colorb = imdilate(colorb,se); L = bwlabel(colorb); num = max(max(L)); B=cell(num,8); for i = 1:num [x,y] = find(L == i); minx=min(y); maxx=max(y); miny=min(x); maxy=max(x); Width=maxx-minx; Hight=maxy-miny; x2= minx+Width; y1=miny+Hight; [m,n]=size(B); thisElement = docNode.createElement('object'); docRootNode.appendChild(thisElement); dataNode = docNode.createElement('name'); dataNode.appendChild(docNode.createTextNode(sprintf('%s',str))); thisElement.appendChild(dataNode); dataNode = docNode.createElement('difficult'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',0))); thisElement.appendChild(dataNode); CElement = docNode.createElement('bndbox'); thisElement.appendChild(CElement); dataNode = docNode.createElement('xmin'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',minx))); CElement.appendChild(dataNode); dataNode = docNode.createElement('ymin'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',y1))); CElement.appendChild(dataNode); dataNode = docNode.createElement('xmax'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',x2))); CElement.appendChild(dataNode); dataNode = docNode.createElement('ymax'); dataNode.appendChild(docNode.createTextNode(sprintf('%g',miny))); CElement.appendChild(dataNode); end docNode.appendChild(docNode.createComment('this is a comment')); xmlwrite(filename,docNode); type(filename); end fprintf('%d %d %s\n',i,j,strcat(file_path,image_name));% 显示正在处理的图像名 %图像处理过程 省略 sta=fclose(fid); end end

结果文件内容截图:

转载请注明原文地址: https://www.6miu.com/read-4821274.html

最新回复(0)