Scale a mask in an image

20 views (last 30 days)
Carine
Carine on 26 Nov 2015
Answered: Carine on 20 Jan 2016
Dear all,
I have a mask which is convex (ellipse like although not a real ellipse). I want to scale it to obtain a mask bigger with the same shape and centered at the same place. Would anybody know how to do that?
I attach an example of the mask.
Cheers.

Accepted Answer

Carine
Carine on 20 Jan 2016
Thanks for your answers Stefan and Image Analyst.
What I wanted in fact was a disk around the "ellipse like blob". So, at the end, what I've done, using Imresize, is designing two such shapes (ellipse like bigger than the original one) and combining them:
agr = 0.03;
Ims1 = Im(floor(1-agr+agr*centre(k,2)):ceil(rows*1-agr)+agr*centre(k,2)),...
floor(1-agr+agr*centre(k,1)):ceil(columns*(1-agr)+agr*centre(k,1)));
Ims1 = imresize(Ims1,[rows columns]);
agr = 0.25;
Ims2 = Im(floor(1-agr+agr*centre(k,2)):ceil(rows*(1-agr)+agr*centre(k,2)),...
floor(1-agr+agr*centre(k,1)):ceil(columns*(1-agr)+agr*centre(k,1)));
Ims2 = imresize(Ims2,[rows columns]);
mask = xor(Ims2,Ims1) % Equivalent
where Im is the image with the original blob, "centre" is the centre of the original blob and [rows,colums] the size of the first image.
The result is in the image included.
Cheers

More Answers (2)

Stefan Karlsson
Stefan Karlsson on 26 Dec 2015
use imresize. If you use it with interpolation 'nearest', you have your functionality in a single line. If you use another interpolation method (like bilinear for example) you will need to transform the result back to binary through thresholding. It will generate pixel values inbetween 0 and 1.

Image Analyst
Image Analyst on 26 Dec 2015
Depends on what you mean by "the same shape". Does the image have to be the same size and shape? If you want approximately the same shape of the white blob, but are willing to have a larger image, then you can use imresize() like Stefan suggested.
If you want the same size image but just have the white blob larger, then you could use imdilate():
newMask = imdilate(mask, true(11)); % Whatever number gets you the size you want.
However this will round out the shape (smooth it) the larger it gets. If you want a larger white blob but the same size image, you can do it in two ways.
One is to call imresize with the nearest option and then crop the image. You'll have to figure out the rows and columns to crop it at.
The second way is to use bwboundaries() to get the x and y coordinates of the perimeter, and use regionprops() to get the centroid. Then calculate the distance of each point from the centroid and extrapolate that outwards by some scale factor. Now you have a new set of expanded x,y coordinates and you can use poly2mask() to turn them into a binary image. This should be perhaps the most accurate way to retain the exact shape.

Categories

Find more on Images 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!