function collage=image2D load map.txt; A=spconvert(floor(map(:,[2 1 3]))+1); figure spy(A); ids=unique(map(:,3)); docNode = com.mathworks.xml.XMLUtils.createDocument('root'); docRootNode = docNode.getDocumentElement; [A,mapi,alpha]=imread('map.png'); if isempty(alpha) alpha=uint8(zeros(size(A,1),size(A,2))); end image.A=A;image.map=mapi;image.alpha=alpha; hlimage=[]; c=1; hold all for id=ids' area=docNode.createElement('area'); docRootNode.appendChild(area); area.setAttribute('id',num2str(id)); [row,col]=find(map==id); x=floor(map(row,1)); y=map(row,2); k = convhull(x,y); plot(x(k),y(k),'r-'); bbox=docNode.createElement('bbox'); area.appendChild(bbox); top=min(y(k));height=max(y(k))-min(y(k)); left=min(x(k));width=max(x(k))-min(x(k)); bbox.setAttribute('top',num2str(top)); bbox.setAttribute('height',num2str(height)); bbox.setAttribute('left',num2str(left)); bbox.setAttribute('width',num2str(width)); hlmap=docNode.createElement('highlightmap'); area.appendChild(hlmap); roi=roipoly(image.A,x(k),y(k)); %J = roifilt2(image.A,roi,@(color)color/2); J=image.A; J(roi)=J(roi)/2; hlimage(c).A=J(1+top:height+top,1+left:width+left,:); J=image.alpha; J(roi)=255; hlimage(c).alpha=J(1+top:height+top,1+left:width+left); %if not(isempty(image.alpha)) % hlimage(c).alpha=image.alpha(top:height+top,left:width+left); %else % hlimage(c).alpha=[]; %end hlimage(c).node=hlmap; hlimage(c).dim=[width;height]; for i=k' point=docNode.createElement('point'); area.appendChild(point); point.setAttribute('x',num2str(x(i))); point.setAttribute('y',num2str(y(i))); end c=c+1; end n=ceil(sqrt(max(size(hlimage)))); dim=[n n]; collage.A=cell(dim); collage.dim=cell(dim); collage.alpha=cell(dim); collage.node=cell(dim); collage.map=image.map; for i=1:max(size(hlimage)) collage.dim{i}=hlimage(i).dim; collage.A{i}=hlimage(i).A; collage.alpha{i}=hlimage(i).alpha; collage.node{i}=hlimage(i).node; end width=[];height=[]; for rows=1:dim(1) m=max([collage.dim{rows,:}],[],2); height(rows)=m(2); end for cols=1:dim(2) m=max([collage.dim{:,cols}],[],2); width(cols)=m(1); end for rows=1:dim(1) for cols=1:dim(2) node=collage.node{rows,cols}; if not(isempty(node)) node.setAttribute('left',num2str(sum(width(1:cols-1)))); node.setAttribute('top',num2str(sum(height(1:rows-1)))); end collage.A{rows,cols}=pad(collage.A{rows,cols},[height(rows) width(cols) 3]); collage.alpha{rows,cols}=pad(collage.alpha{rows,cols},[height(rows) width(cols)]); end end imwrite(cell2mat(collage.A),'collage.png','Alpha',cell2mat(collage.alpha)); xmlwrite('map.xml',docNode); xslt('output.xml', 'merge.xsl', 'merged.xml'); system('del map.xml'); system('rename merged.xml map.xml'); end function B=pad(A,dim) if isempty(A) B=uint8(zeros(dim)); else B=uint8(padarray(A,dim-size(A),0,'post')); end end