advanced usage of matlab roipoly command

2019-04-15 07:16发布

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 ?

1条回答
我只想做你的唯一
2楼-- · 2019-04-15 07:39

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

查看更多
登录 后发表回答