Programming error?
1 次查看(过去 30 天)
显示 更早的评论
My problem is that I have an image which I uploads. I want to obtain the line profile (intensity) for a certain leaning line of my choice.
My error arises when I am obtaining the intensity profile (mode_vecX). See code below..., also I have to round of my y-vector. Is that needed?
% Uploads the Image to a vector:
image_vec = imread(image_filename);
% Creates the coordinate vectors of the image
pixlength_vecX = 1:length(image_vec(:,1));
pixlength_vecY = 1:length(image_vec(1,:));
% Obtains the points which you want the slope between:
disp('Please pick two points along the line profile you want to investigate: ');
imshow(image_vec);
title('Please pick two points along the line profile you want to investigate: ');
xlabel('x pixel');
ylabel('y pixel');
[x,y] = ginput(2);
close all
% Finds the line's equation y = m*x + k
p = polyfit(x,y,1);
yPixel_vec = polyval(p,pixlength_vecY); %
% Obtains the intensity at coordinate (x,y)
mode_vecX = image_vec(pixlength_vecY,round(yPixel_vec));
The error message I get when I run this is:
??? Index exceeds matrix dimensions.
Error in ==> imageProfile at 69 mode_vecX = image_vec(pixlength_vecY,round(yPixel_vec));
Something is wrong here and I need some other thoughts and eyes. Many thanks for the assistance.
0 个评论
采纳的回答
Sean de Wolski
2011-8-2
In reply to your deleted answer from yesterday, your error was, as Jan suggested, that you were confusing x/y with row/col. For image processing in general I (and others) highly recommend getting in the habit of using the first and second dimension - naming your variables r/c instead of x/y etc.
This is a working solution to your problem yesterday:
% Obtains the points which you want the slope between:
disp('Please pick the two points of the line profile (pick upper point first): ');
imshow(image_vec);
title('Please pick the two points of the line profile (pick upper point first): ');
xlabel('x pixel');
ylabel('y pixel');
[x,y] = ginput(2);
close all
% slope of the line between point A and B
m = diff(x)/diff(y); %note switch
b = x(1)-m*y(1);
% Function coordinates f(x) = y
fline = @(pix)pix.*m+b;
xPixel = 1:size(image_vec,2);
yPixel = fline(xPixel);
figure
imshow(image_vec);
hold on;
plot(yPixel,xPixel,'g'); %note switch!
title('Uploaded Image');
xlabel('x-pixel'); ylabel('y-pixel');
If you want the value on the line - use round (or fix / ceil) and sub2ind to get the values on the image.
更多回答(1 个)
Jan
2011-8-2
I assume you have confused the 1st and 2nd dimension and the 1st argument must be "pixlength_vecX" in this line:
% Buggy:
mode_vecX = image_vec(pixlength_vecY, round(yPixel_vec))
To solve such problems by your own, let the debugger stop MATLAB, when the error occurs:
dbstop if error
Then inspect, why either "pixlength_vecY" or "round(yPixel_vec)" exceeds the dimensions of image_vec.
This forum cannot replace a debugger and it is up to you to fix your programs as far as possible.
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!