Stitching sub images to reconstruct full image
Show older comments
I have subdivided an image into 4x4 tiles and produced a subimage and obtained a binary imaged. I add each of these binary images to a binary stack using a loop.

After the loop, how do I reconstruct/stitch the sub binary images back to the full image size, so I can create a binary image representing the while raw image so to use as a mask:
My binary sub images are expressed as :
BI(:,:,i) %where i is 1:16 as I'm using 4x4 tiles
This is my approach that isn't working:
%Now combine binary images so to create regions to act as mask on original
%image
size(BI) %Confirm there are 16 planes of images in the binarystack
Binary=[]; %Create empty Binary Image that will hold reconstructed sub images
ct=0; %counter
for jj=1:tiles
for ii=1:tiles
ii
jj
ct=ct+1
startingCols(jj)
startingRows(ii)
Binary(startingCols(ii):endingCols(ii), startingRows(jj):endingRows(jj))=BI(:,:,ct);
figure(2)
subplot(4,4,ct)
imshow(BI(:,:,ct),[0,1])
Binary=BI;
end
end
Accepted Answer
More Answers (6)
Iain
on 13 Jan 2015
depending how you've done it....
BInew = permute(BI,[1 3 2]);
BInew = reshape(BInew,16*size(BI,1),[]);
ought to work.
9 Comments
Jason
on 13 Jan 2015
Iain
on 13 Jan 2015
I wanted to swap the 2nd and 3rd dimensions of your 3D array around, so that the pixels would be stored down a the first column of the first segment, then down the first column of the 2nd segment... etc.
You might want:
BInew = reshape(BI,[size(BI,[1 2]) 4 4]);
BInew = permute(BInew,[1 3 2 4]);
BInew = reshape(BInew,size(BI,1)*4,[]);
Iain
on 13 Jan 2015
BInew = reshape(BI,[size(BI,[1 2]) 4 4]);
needs to be:
BInew = reshape(BI,[25 25 4 4]);
Jason
on 13 Jan 2015
Iain
on 13 Jan 2015
You forgot the last reshape
BInew = reshape(BInew,size(BI,1)*4,[]);
BInew = reshape(BInew,100,[]);
Iain
on 13 Jan 2015
You'd need to change the reshapes, not the permute. You've got 16 tiles though.... (4 x 4)
BInew = reshape(BI,[25 25 tileshigh tileswide]);
BInew = permute(BInew,[1 3 2 4]);
BInew = reshape(BInew, 25*tileshigh, [])
Jason
on 14 Jan 2015
Image Analyst
on 13 Jan 2015
Why bother saying this:
Binary(startingCols(ii):endingCols(ii), startingRows(jj):endingRows(jj))=BI(:,:,ct);
if you're just going to say this
Binary=BI;
three lines later? Not only that, but you switched rows and columns. The first index should be rows, not columns as you have it, and the second index should be columns, not rows as you have it.
15 Comments
Image Analyst
on 13 Jan 2015
Well, what are startingRows(jj),endingRows(jj),startingCols(ii), endingCols(ii), and size(BI)? Chances are they aren't the full size of a plane of BI. For example if BI is 100 by 200 then the distance between rows must be 100 inclusive, and between columns 200 inclusive. It any one of them is not, then something is wrong. You can't stuff a 100 by 200 image into a space that's 99 by 199 or whatever.
Jason
on 13 Jan 2015
Jason
on 13 Jan 2015
Image Analyst
on 13 Jan 2015
Jason's reply moved from an Answer to here:
I think I have identified the problem (not sur ehow to fix it): the definition of the start and end points for the grid to split up the image (in this case into 4x4 subimages , as per I.A help is:
[rows,cols]=size(ROI) % ROI is my Image
tiles = 4 % Number of tiles or chunks you want it divide up into.
% Find out where the dividing lines are. This may be a fractional value.
startingRows = linspace(1, rows, tiles+1);
startingCols = linspace(1, cols, tiles+1);
% Now get the starting integer rows / cols.
startingRows = int32(startingRows(1:end-1))
startingCols = int32(startingCols(1:end-1))
% Get the ending rows.
endingRows = [startingRows(2:end)-1, rows]
endingCols = [startingCols(2:end)-1, cols]
%Determine number of pixels in each tile;
nx=endingRows(1)-startingRows(1);
ny=endingCols(1)-startingCols(1);
If I look at the output of the end points, the last entry should be 100 and not 101? (My raw image size is 101 x 101.
endingRows =
25 50 75 101
endingCols =
25 50 75 101
Image Analyst
on 13 Jan 2015
If your image is 101-by-101, then why do you want to process only out to 100-by-100? Why don't you want to include the last row and column?
Jason
on 13 Jan 2015
Image Analyst
on 13 Jan 2015
Your 4th line has 26 columns instead of 25 like the others.
What does this say:
size(BI)
Jason
on 13 Jan 2015
Image Analyst
on 13 Jan 2015
Edited: Image Analyst
on 13 Jan 2015
This will happen if the number of rows or columns is not an integer multiple of the number of tiles you want to divide it up into. So for 4 tiles, you can have 100 or 104 or 108, but not 101 or 102 or 103, etc. You can still chop it up and stitch them together you just can't do it like you're doing. You should do variations of this
tallMatrix = [m1; m2]; % m1 and m2 have the same number of columns.
wideMatrix = [m1, m2]; % m1 and m2 have the same number of rows.
Jason
on 14 Jan 2015
Image Analyst
on 14 Jan 2015
See attached blockproc demos. And don't forget to look at Mohammad's last comment under his answer.
Jason
on 14 Jan 2015
Jason
on 13 Jan 2015
2 Comments
Iain
on 13 Jan 2015
That should work provided you calculate startingcols etc. correctly.
Image Analyst
on 13 Jan 2015
The first index should be rows, not columns as you have it, and the second index should be columns, not rows as you have it.
Alessandro Masullo
on 13 Jan 2015
0 votes
What about using mat2cell and cell2mat? It should be much easier.
Jason
on 14 Jan 2015
0 votes
3 Comments
Image Analyst
on 14 Jan 2015
Why did you even split up your image in the first place?
Jason
on 14 Jan 2015
Image Analyst
on 14 Jan 2015
OK but you don't need to split apart your image to do that. You can get a better local thresholding using adapthisteq() to flatten your image and then use a global threshold, or use blockproc like Mohammad suggested. adapthisteq is like splitting your image apart into tons of tiles that are only a pixel apart and will be much better and more accurate than splitting your image into 4 tiles and then computing the threshold for the whole tile. You should really look into these methods.
KHOR WEI KOK
on 2 Sep 2016
0 votes
Hi, I would like to ask, how you manage to show the line segmentation and numbering on your image axes?
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!