Matlab imshow function border tight not working under small size image

17 views (last 30 days)
By default, when imshow displays an image in a figure, it surrounds the image with a gray border. You can change this default and suppress the border using the 'border' parameter.
But, I recently found that border tight will stop working when the matrix is smaller than approximately 125-130 thresholding
Please try these two Matlab command and you will see the difference
imshow(rand(130,130),'Border','tight') % works! no grey boundary at all
imshow(rand(120,120),'Border','tight') % not working! grey boundary appears
I carefully read the doc and found the sentence below When set to ‘tight', the figure window does not include any space around the image in the figure. If the image is very small or if the figure contains other objects besides an image and its axes, imshow might use a border regardless of how this parameter is set.
That is very disappointing. What I am trying to do is to capture image after imshow and store it to the matrix using command like
f=getframe(gcf);
resultMatr=f.cdata;
so any border will influence the result.
Any solution for that?

Answers (1)

Jihang Wang
Jihang Wang on 4 Mar 2015
Edited: Jihang Wang on 4 Mar 2015
I just solved the problem by writing the function below
function outputimg = removeBorder(inputimg, size)
outputimg = zeros(size,size,3);
grayscale = rgb2gray(inputimg);
%Find upper left element of the foreground
[x,y] = find(grayscale~=204,1);
outputimg = inputimg(x:x+size-1,y:y+size-1,:);
Observing that the grays region (border) pixel intensity is always 204, basically, it detects the first element position that is not equal to 204, that will be the upper left point of the square. Then, since we know the square size of efficient region, we can finally crop the matrix to remove all the border.
The ONLY drawback of the code that may fail is that the foreground cannot contain pixel value that is exactly 204 where in my case, it is acceptable. I also tried to count border size in each side, but found the size is not balanced for some reason, not sure why.
Thank you for @ Benoit_11's help.
  2 Comments
Cg Gc
Cg Gc on 9 Jan 2019
I know this is a little odd, but can you provide an example of how to use your function? I am trying to group a few images together and I really would like to remove the border, but I keep receiving an error message because I don't have enough input arguments. I am not sure what that means. An example would be lovely.
Thank you.
Walter Roberson
Walter Roberson on 9 Jan 2019
Example of use:
img = imread('cameraman.tif');
outsize = 100;
outputimg = removeBorder(img, outsize);

Sign in to comment.

Categories

Find more on Image Processing Toolbox 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!