Matlab imshow function border tight not working under small size image

조회 수: 40 (최근 30일)
Jihang Wang
Jihang Wang 2015년 3월 3일
댓글: Walter Roberson 2019년 1월 9일
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?

답변 (1개)

Jihang Wang
Jihang Wang 2015년 3월 4일
편집: Jihang Wang 2015년 3월 4일
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
Cg Gc
Cg Gc 2019년 1월 9일
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 2019년 1월 9일
Example of use:
img = imread('cameraman.tif');
outsize = 100;
outputimg = removeBorder(img, outsize);

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by