Stitching sub images to reconstruct full image

8 次查看(过去 30 天)
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

采纳的回答

Mohammad Abouali
Mohammad Abouali 2015-1-14
编辑:Mohammad Abouali 2015-1-14
Again, why don't you use blockproc()? :D
what you are asking, i.e. stitching the sub images into one big one, can be done in a single command using blockproc() as follows:
Binary=blockproc( reshape(1:16,4,4)', [1,1], @(x) BI(:,:,x.data) );
where Binary contains all the sub images placed in the order you show in the figure. 16 sub images are not much but if there were more you can even do this in parallel as easily as:
Binary =blockproc( reshape(1:16,4,4)', [1,1], @(x) BI(:,:,x.data), 'UseParallel',true);
  5 个评论
Mohammad Abouali
Mohammad Abouali 2015-1-14
编辑:Mohammad Abouali 2015-1-14
If you are just thresholding using graythresh you can write your blockproc like this
Binary=blockproc(OrigImage,[25,25],@(x) (im2bw(x.data,graythresh(x.data(:)))) )
That would threshold your image using Otsu's method and it is not global, the threshold is decided on each 25x25 tile separately. You don't need to divide the image into tiles, you don't need to spend time stitching them back.
Another approach is to use the function that ImageAnalyst told you.

请先登录,再进行评论。

更多回答(6 个)

Iain
Iain 2015-1-13
depending how you've done it....
BInew = permute(BI,[1 3 2]);
BInew = reshape(BInew,16*size(BI,1),[]);
ought to work.
  9 个评论
Iain
Iain 2015-1-13
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, [])

请先登录,再进行评论。


Image Analyst
Image Analyst 2015-1-13
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 个评论
Image Analyst
Image Analyst 2015-1-14
See attached blockproc demos. And don't forget to look at Mohammad's last comment under his answer.

请先登录,再进行评论。


Jason
Jason 2015-1-13
Why does this approach not work:
ct=0;
for ii=1:4
for jj=1:4
ct=ct+1
Binary(startingCols(jj):endingCols(jj), startingRows(ii):endingRows(ii))=BI(:,:,ct);
end
end
imshow(Binary,[0,1])
  2 个评论
Iain
Iain 2015-1-13
That should work provided you calculate startingcols etc. correctly.
Image Analyst
Image Analyst 2015-1-13
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
Alessandro Masullo 2015-1-13
What about using mat2cell and cell2mat? It should be much easier.

Jason
Jason 2015-1-14
I've managed to get a solution where I first have to crop the image to ensure there number of pixels corresponding to integer tiling: (100 in my case). its not ideal, but it seems a big struggle to get the non integer tiles as well.
  3 个评论
Jason
Jason 2015-1-14
local thresholding worked a lot better than global!
Image Analyst
Image Analyst 2015-1-14
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
KHOR WEI KOK 2016-9-2
Hi, I would like to ask, how you manage to show the line segmentation and numbering on your image axes?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by