MATLAB Answers

Stitching sub images to reconstruct full image

조회 수: 46(최근 30일)
Jason
Jason 13 Jan 2015
답변: KHOR WEI KOK 2 Sep 2016
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

  댓글 수: 0

Sign in to comment.

채택된 답변

Mohammad Abouali
Mohammad Abouali 14 Jan 2015
편집: Mohammad Abouali 14 Jan 2015
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

표시 이전 댓글 수: 2
Mohammad Abouali
Mohammad Abouali 14 Jan 2015
If your sub images, i.e. BI, is of size 25x25x16 then of course when you put them back together in a 4x4 tiles you would get 100x100 pixels and not 101x101. If your original image before processing was 101x101 and after tiling it and processing it you ended up with 16 tiles of 25x25 then pretty much you have dropped one row and column.
I can suggest two approaches:
Approach 1 based on what you have and the codes that you posted I think you are dropping the last raw and column of the original image. Therefore, that row and column is not being processed as part of your tile. then you have two options:
Approach1, Option 1 Let say OrigImage is your original image with size 101x101. Then you tile it into BI with size 25x25x16. You can stitch BI back using the command I gave you above:
Binary=blockproc( reshape(1:16,4,4)', [1,1], @(x) BI(:,:,x.data) );
Binary would be 100x100. Then to compare it with original image resize your original image as follow
OrigImage=OrigImage(1:end-1,1:end=1);
The above command drops the last row and column from your original image and makes it 100x100, now both Binary and OrigImage have the same size.
Approach1, Option 2 This is the same as previous option, but instead you resize the binary to make it 101x101. Here is what you should do:
% stitch the tiles
Binary=blockproc( reshape(1:16,4,4)', [1,1], @(x) BI(:,:,x.data) );
% resize Binary
Binary=imresize(Binary,[101,101],'nearest');
Now both Binary and OrigImage are 101x101.
Approach 2 The second approach is to go back and fix the tiling; so that you don't drop a row and a column from the image. I really suggest that you use blockproc and then you won't have any of these problems.
Let's say you have combined all the processing that you need to do on a tile in a function called myOperation.m. Therefore you have
BI(:,:,1)=myOperation(Tile1);
BI(:,:,2)=myOperation(Tile2);
...
BI(:,:,16)=myOperation(Tile16); of course you do this in a loop.
This means that you need to divide the original image into Tiles (tile 1 to tile 16. then you need to stitch BI back into one image.
If you have such function then you can use blockprock as follow:
Binary=blockproc(OrigImage,[tileRowSize, tileColSize],@(x) myOperation(x.data));
Note that the above command does not requires the origimage to be divided in tile. and note that you won't get the BIs but you would get directly the stitched image.
In this approach you just have to make sure that myOperation should be able to work on variable sized tiles.
Mohammad Abouali
Mohammad Abouali 14 Jan 2015
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.
Jason
Jason 14 Jan 2015
Thankyou very much

Sign in to comment.

추가 답변(6개)

Iain
Iain 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

표시 이전 댓글 수: 6
Jason
Jason 13 Jan 2015
Ah OK. How would I change this line to cater for an arbitrary number of tiles (so far I had tiles=4)?
BInew = permute(BInew,[1 3 2 4]);
Iain
Iain 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
Jason 14 Jan 2015
Thankyou

Sign in to comment.


Image Analyst
Image Analyst 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

표시 이전 댓글 수: 12
Jason
Jason 14 Jan 2015
Thanks for your help, it seems like the Blockproc approach is easier and caters for all posiblities, although I understand the above apporach better, just can't get it to work.
You asked what
size(BI) was:
ans =
25 25 16
Image Analyst
Image Analyst 14 Jan 2015
See attached blockproc demos. And don't forget to look at Mohammad's last comment under his answer.
Jason
Jason 14 Jan 2015
Thankyou

Sign in to comment.


Jason
Jason 13 Jan 2015
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 13 Jan 2015
That should work provided you calculate startingcols etc. correctly.
Image Analyst
Image Analyst 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.

Sign in to comment.


Alessandro Masullo
Alessandro Masullo 13 Jan 2015
What about using mat2cell and cell2mat? It should be much easier.

  댓글 수: 1

Jason
Jason 13 Jan 2015
Hi, I don't see how this can reconstruct a full image from the subimages

Sign in to comment.


Jason
Jason 14 Jan 2015
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

Image Analyst
Image Analyst 14 Jan 2015
Why did you even split up your image in the first place?
Jason
Jason 14 Jan 2015
local thresholding worked a lot better than global!
Image Analyst
Image Analyst 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.

Sign in to comment.


KHOR  WEI KOK
KHOR WEI KOK 2 Sep 2016
Hi, I would like to ask, how you manage to show the line segmentation and numbering on your image axes?

  댓글 수: 0

Sign in to comment.


Translated by