Programming error?

1 view (last 30 days)
Lizan
Lizan on 2 Aug 2011
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.

Accepted Answer

Sean de Wolski
Sean de Wolski on 2 Aug 2011
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.
  2 Comments
Lizan
Lizan on 3 Aug 2011
Thank you. I rewrote my code yesterday, using col and row instead of x and y and I got the same result as above (although I am using polyfit), however...
I have one remaining problem with this code. When I try to obtain the values of my image for these lines (row,col) I keep getting the message "??? Subscript indices must either be real positive integers or logicals.". I have looked into this and I noticed that some of the values of the row coordinate is negative. So I tried to fix this by zero those that are below zero,.. but the problem still remains. I am using round. Then I tried using sub2ind but this is not working. The vectors are in the same size as the image size.
Got any idea what the problem could be?
Lizan
Lizan on 3 Aug 2011
Instead of using zero I replaced the negative values with value one.
Also, I replaced all values above the image size with the max. image lengths.
This fixed the problem.
Now when I am plotting image_vec(ypixel, xpixel), from above, I get all values for the whole image. I just want the values along this line.
What is wrong here?

Sign in to comment.

More Answers (1)

Jan
Jan on 2 Aug 2011
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.

Categories

Find more on Images in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!