stitching an image together from others

Asked by John on 22 Jun 2012
Latest activity Commented on by John on 22 Jun 2012

Hi,

I am calling two for loops to load a set of map png images into matlab. I then want to stitch them together. 5x5 at the moment.

I was previously setting each image loaded to, img1 img2 etc, and then stitching them together using

str1 = sprintf('F:\\Mapping\\%g\\%g,x,y);
A1 = exist(str1);
if A1 == 2;
img1 = imread(str1);
else
img1 = imread('F:\Mapping\No_File.png')l
end

and then stitching them all together as so:

map = [img1,img2,img3,img4,img5;img6... etc];

In order to simplify, I would like to run a for loop and do the following:

   for m = x-2:1:x+2
    for n = y - 2:1:y+2
        str = sprintf('F:\\Mapping\\%s\\%g\\%g\\%g\\%g\\%g.png',mstring,z,xi,m,yi,n);
        A = exist(str);
        if A == 2;
            img = imread(str);
        else
            img = imread('F:\Mapping\No_File.jpg');
        end
        %%%%%%%%%%%%%%%
    end
end

except I'm not sure now how to write the map = [img1,img2... etc part as obviously I am changing the value of img each loop.

How would I write the img to the appropriate part of the map image? Is there a way?

I've tried:

        stitchxstart = (m-x+2)*256 + 1;
        stitchxend = (m+1-x+2)*256;
        stitchystart = (n-y+2)*256 + 1;
        stitchyend = (n+1-y+2)*256;

and then write as:

map(stitchxstart:stitchxend,stitchystart:stitchyend,:) = img;

as I know the outcome will be a 1280*1280*3... but it obviously doesn't work... not quite sure how to write it to the map variable in the correct manner.

Thanks in advance....

0 Comments

John

Products

No products are associated with this question.

2 Answers

Answer by Image Analyst on 22 Jun 2012
Accepted answer

Try this code:

c1 = rand(5,5,3)
cStitched = c1; % Initialize with the first one.
subplot(2,2,1);
imshow(c1);
% Now tack on the others.
for k = 1 : 10
	c2 = rand(5,5,3);
	subplot(2,2, 2);
	imshow(c2);
	cStitched = [cStitched, c2];
	subplot(2,1, 2);
	imshow(cStitched);
	% Prompt user if they want to continue.
	promptMessage = sprintf('Do you want to Continue processing,\nor Cancel to abort processing?');
	button = questdlg(promptMessage, 'Continue', 'Continue', 'Cancel', 'Continue');
	if strcmp(button, 'Cancel')
		return;
	end
end

That should be enough to give you a start. You can use a semicolon instead of a comma to tack on vertically. So what I'd do is use the above code to make a full length "row" of 5 by 1280 pixels, then use the semicolon with the same concept

cStitched = [cStitched; oneRow];

To tack on that row vertically.

For the stitching to be general and robust, it's not easy. For example to handle different kinds of images (color, monochrome), different sizes, where they're placed if they different sizes, etc. This File Exchange submission handles all that very well with maximum flexibility: http://www.mathworks.com/matlabcentral/fileexchange/25797-stitch

1 Comment

John on 22 Jun 2012

Cheers for this, see my modified code... could probably be neater but meh... it does the job

Image Analyst
Answer by John on 22 Jun 2012
stitch = zeros(1280,1280,3);
tempstitch = zeros(1280,1280,3);
j = 0;
for m = x-2:1:x+2
    i = 0;
    for n = y-2:1:y+2
        fprintf('M: %g N: %g\ni: %g j: %g\n',m,n,i,j);
        str = sprintf('F:\\Mapping\\%s\\%g\\%g\\%g\\%g\\%g.png',mstring,z,xi,m,yi,n);
        A = exist(str);
        if A == 2;
            img = imread(str);
        else
            img = imread('F:\Mapping\No_File.jpg');
        end
        if i == 0
            tempstitch = img;
        elseif i > 0
            tempstitch = [tempstitch;img];    
        end
        i = i + 1;
    end
    if j == 0
        stitch = tempstitch;
    elseif j > 0;
        stitch = [stitch,tempstitch];
    end
    j = j + 1;
end

0 Comments

John

Contact us