How to interpolate the coordinates of a vector on an image using MATLAB?
image, interpolation, matlab
Solution
If you have the image processing toolbox, you can do this by creating an image of the polygon followed by finding the contour:
xy=[165 88; 401 88; 401 278; 165 278];
%# create the image - check the help for impolygon for how to make sure that
%# your line is inside the pixel
img = poly2mask(xy(:,1),xy(:,2),max(xy(:,1))+3,max(xy(:,2))+3);
figure,imshow(img) %# show the image
%# extract the perimeter. Note that you have to inverse x and y, and that I had to
%# add 1 to hit the rectangle - this shows one has to be careful with rectangular
%# polygons
boundary = bwtraceboundary(logical(img),xy(1,[2,1])+1,'n',8,inf,'clockwise');
%# overlay extracted boundary
hold on, plot(boundary(:,2),boundary(:,1),'.r')
Edited to show how to use bwtraceboundary and to warn of pixel offset with rectangles.
Problem
For instance, if I have got a vector describing a rectangle ``` xy=[165 88; 401 88; 401 278; 165 278]; ``` on an image. How can I obtain the following vector ``` [165 88; % increase X - hold y 166 88; 167 88; ... ; 399 88; 400 88; 401 88; % hold x - increase y 401 89; 401 90; 401 91; ... ; 401 276; 401 277; 401 278; % decrease X - hold y 400 278; 399 278; 398 278; ... ; 167 278; 166 278; 165 278; % hold x - decrease y 165 277; 165 276; ... ; 165 87]; ``` using a MATLAB inbuilt function or do I need to write it using FOR LOOPS? The algorithm must work for a generic vector with n-points and xy coordinates.