The four-element position vector using getPosition returns double values rather than integers.

3 views (last 30 days)
Hi everyone,
The four-element position vector returned after selecting rectangle or ellipse or other shapes in an image as a result of imrect, imellipse functions using getPosition function is of type double. When the first two elements represent the coordinates of the top left corner of the rectangle and the next two elements represent the width and height of the rectangle, all the four values are expected to be integers because the image is discrete and the values are in units of 'number of pixels' (I assume).
It does not make sense for the coordinates or width and height to be double. Can anyone explain the reason for the vector to have double values, which means that it is giving us sub-pixel accuracy ?
-- Thanks in advance, Ram.

Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 25 Aug 2012
imshow('pout.tif');
h = imrect;
position = wait(h);
% the result is double but without decimals. if you want the result to be integer
position = uint32(position);

Ramaprasad Kulkarni
Ramaprasad Kulkarni on 27 Aug 2012
Hi Azzi,
Thanks for the quick reply.
The position vector returned for imrect function is of type double but the values are integers, which is expected and makes sense. However in my case where I use the following code
h = imellipse(gca,[10 10 width/10 height/10]);
addNewPositionCallback(h,@(p) title(mat2str(p,3)));
fcn = makeConstrainToRectFcn('imellipse',get(gca,'XLim'),get(gca,'YLim'));
setPositionConstraintFcn(h,fcn);
wait(h);
pos = getPosition(h)
the 'pos' is a four-element vector of type double and also the values are real and not integers. The output looks like
pos = [62.9228 63.0031 568.4113 567.0534]
I expect the output (after manually finding the four values from the image) to be [63 64 568 566] even if the vector is of type double. The fractions after decimal point does not make sense. If can cast the data type to integer using int32, but the vector I get is [63 63 568 567].
I would like to know why the output is a vector with real values and not integer values. Also, what function can be used to get integer values from the 'pos' vector to be same as what I measure from the image.
-- Thanks, Ram.
  2 Comments
Walter Roberson
Walter Roberson on 27 Aug 2012
What Units are you using? The Position is possibly being stored in a different unit than you are using, resulting in a non-integral value when you examine it.
Ramaprasad Kulkarni
Ramaprasad Kulkarni on 28 Aug 2012
Thanks Walter for your reply.
Here are the three lines of code which precede the above code snipped.
options.Resize='on';
s = inputdlg('Enter a 1-by-4 position vector of a square region bounding the desired circular mask','Mask position',1,{'0 0 100 100'},options);
pos = [str2num(s{1})];
So, I am not defining any units for 'pos' variable.
Moreover, another strange thing I found out a while ago is when I run following code:
h_im = imshow(I);
h = imellipse(gca,[63 64 567 567]);
Pos = getPosition(h);
Mask = createMask(h,h_im);
invMask = xor(Mask,ones(size(Mask)));
ImData(invMask) = 255;
Essentially, I am setting all the pixels outside the ellipse (with position [63 64 567 567]) with 255. I expect 63rd column and 64th row to be part of the circle and hence at least few circumference pixels not set to 255, but strangely I find that the circumference starts at 64th column and 65th row.
Am I understanding anything wrong here?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!