How can I use interp1 for a figure created using imread?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I'll preface this by saying that I'm very new to MATLAB, so I apologize if I sound a bit incoherent.
I'm trying to figure out what the width of the border of an image would be. I have used imread to do so, and I did this for several different images, creating a figure like so (in the actual code there are more than two lines, but I only used two here for simplicity's sake):
function compare_image_profile
I(:,:,1) = imread('filename_1');
I(:,:,2) = imread('filename_2');
trace_color(1,1) = 'b';
trace_color(2,1) = 'g';
figure
hold on
for k = 1:2
plot(I(700,:,k), trace_color(k,1));
end
Doing this allows me to create the figure that I attached (I zoomed into the area that I want to focus on).
Essentially, I want to be able to find the distance between the two points on the x-axis with a fixed y value (for example, 6000) for both of these lines respectively--basically finding the x for a given y when the line dips, and again for when it increases.
I have tried to use the interp1 function, but I can't seem to figure out how to do so with my imread-created figure. Whenever I try doing something like interp1(y,x,6000) for example I get an error saying that the function or variables are undefined. I'm not sure how I can refer to the x or y axes in this case.
Sorry for the long-winded question, and hope it made sense! Thank you so much for the help and have a lovely day :)
Edit: Just wanted to include that I'm fine with using something other than (or in addition to) interp1, that was just the only thing I could think of using at the moment.
1 commentaire
Graeme L
le 3 Août 2017
Hey I just have a quick comment.
I don't think interp1 will work as you expect it to when your data has multiple points at your search value (6000 in your example).
To demonstrate this I wrote this:
%%First Data
x = -1:1/(3*pi):1;
y = x.^2;
searchVal = 0.4;
xq = interp1(y,x,searchVal);
fig = figure();
plot(x,y)
hold on
stem(xq,searchVal);
pause()
clf(fig)
%%Second Data
% x = 0:pi/32:4*pi;
% y = sin(x);
% searchVal = 0.877;
% try
% xq = interp1(y,x,searchVal);
% catch ME
% disp(ME.message)
% end
% CODE to fix issue
% logical indices where y values cross searchVal
yUpIndex = y>searchVal;
yDownIndex = y<=searchVal;
% shifting to determine which two indices will be needed
% form interpolation (ie. [x1,y1], [x2,y2])
shift1 = yUpIndex & circshift(yDownIndex,1);
shift2 = yUpIndex & circshift(yDownIndex,-1);
shift3 = yDownIndex & circshift(shift1,-1);
shift4 = yDownIndex & circshift(shift2,1);
y1 = [];
y2 = [];
x1 = [];
x2 = [];
% combine all transitions into one vector
% finds transitions <searchVal to >searchVal
if any(shift3)
y1 = [y1 y(shift3)];
y2 = [y2 y(circshift(shift3,1))];
x1 = [x1 x(shift3)];
x2 = [x2 x(circshift(shift3,1))];
end
% finds transitions >searchVal to <searchVal
if any(shift4)
y1 = [y1 y(shift4)];
y2 = [y2 y(circshift(shift4,-1))];
x1 = [x1 x(shift4)];
x2 = [x2 x(circshift(shift4,-1))];
end
% linear interpolation
xq = ((x2-x1)./(y2-y1)).*(searchVal-y1) + x1;
plot(x,y);
hold on
stem(xq,searchVal*ones(size(xq)));
The first set of data shows a quadratic data set, which has a similar trend to what your image shows. When you try to use interp1 the interpolated x value you get out is way off. This is because interp1 will sort the y values into increasing order (along with their associated x value) which completely changes the original data.
The second data set shows that if you have any two values that are equal value (quite likely in your case with integer image data), interp1 will throw an error. Because you can't have two "y-values" (x-values in your case) for one "x-value". Just like regular y=f(x) functions.
Below is what I think could work for you. It will find all the times your function crosses the search value (your 6000 value). and linearly interpolate between the values to return all appropriate x values. You will have to decide what to do with all possible points.
Réponse acceptée
KSSV
le 3 Août 2017
You are plotting the boundary using:
plot(I(700,:,k), trace_color(k,1));
This means, you have the coordinates i.e (x,y) in hand. The values I(700,:,k) are y values. You are plotting the curves w.r.t their positions. Knowing the dimensions if image i.e I..these positions can be generated and then call interp1.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Calendar dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!