1

I am new to matlab and am working on Image processing. I am using the roipoly function to create a mask. As I understand I can use it like this :

I = imread('eight.tif');
c = [222 272 300 270 221 194];
r = [21 21 75 121 121 75];
BW = roipoly(I,c,r);
figure, imshow(I)
figure, imshow(BW)

The image is shown below : image1

One observation I had was that the interpolation between the adjacent points as specified by the c & r matrix is done by 'Linear Interpolation', in other sense always a straight line is drawn between the points. Can it be possible that somehow other types of interpolation are incorporated , like quadratic or cubic spline interpolation ?

Suppose what I really wanted was to do this as shown in the picture below. [Pardon my drawing, its not very good].

image2

Here the circles show the points on the contour. I wanted the figure that is extracted or created to be in the shape as shown by the lines. As you can see it is possible only if we do interpolation by using splines or quadratic equations and not by linear interpolation which is done by roipoly.

How can I do this ? Can you guys help me ?

roni
  • 1,443
  • 3
  • 28
  • 49

1 Answers1

2

You can use imellipse:

I = imread('eight.tif');
% roughly estimating ellipse values from your given c/r
c = [222 272 300 270 221 194];
r = [21 21 75 121 121 75];
xmin = min(c);
ymin = min(r);
width = range(c);
height = range(r);

h_im = imshow(I);
e = imellipse(gca,[xmin ymin width height]);
BW = createMask(e,h_im);

figure, imshow(I)
figure, imshow(BW)

If you don't want to use an eclipse, you can use interp1 or other interpolation functions on c and r :

% editing r and c so the shape closes - just take first value, append to end:
c = [222 272 300 270 221 194 222];
r = [21 21 75 121 121 75 21];
% adjust interpolation to suit
c2 = interp1(1:7,c,1:0.2:7,'pchip');
r2 = interp1(1:7,r,1:0.2:7,'pchip');
BW2 = roipoly(I,c2,r2);

example interpolated roipoly

nkjt
  • 7,825
  • 9
  • 22
  • 28
  • No this will not work actually. There is no guarantee that the points on the contour will be spaced out so evenly. What I wanted was to draw a figure even if the points are not placed in a 'good' geometric pattern. Hence I specifically asked my question regarding interpolation. – roni Aug 02 '13 at 11:37
  • 1
    Do you mean that they're unevenly spaced around the object, or that the desired shape cannot be roughly defined by an eclipse? An example would be good but in the meantime see the edit for using interp1 or similar. – nkjt Aug 02 '13 at 12:33
  • Thank you. This may work. I will be trying it out. And Yes you got it correct! It works brilliantly. I accepted your answer! Could you tell me if I can write my own customized function to integrate it within the roipoly function ? Can u tell me about the guidelines... – roni Aug 03 '13 at 05:41
  • Another question @nkjt . Since we are dealing with two dimensional data sets, should not it be better that I use interp2 ? – roni Aug 03 '13 at 06:13
  • For interp2 you'd need an X, Y, and Z - Z is a function of X and Y, interp2 interpolates it over some new values XI and YI. – nkjt Aug 03 '13 at 09:30