Code shows"Integer operands are required for colon operator when used as index "

2 views (last 30 days)
Here is an code which i've written but it shows a warning and stops executing. I tried using round for 'noise' variable,still the code didn't work.
% Load image img = imread('gel.png'); img = rgb2gray(img);
% Identify lanes
imshow(img)
[x,y] = ginput;
% Invert image
img = max(img(:)) - img;
% Subtract background
[xn,yn] = ginput(1);
noise = img((yn-2):(yn+2), (xn-2):(xn+2));
noise = mean(noise(:));
img = img - noise;
% Calculate means
means = (1:size(img,1)) * double(img(:,round(x))) ./ sum(double(img(:,round(x))), 1);
% Plot
hold on
plot(x, means, 'r.')
  4 Comments

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 22 Nov 2013
gniput returns fractional floating point values. But then you try to use it as an index (yn-2):(yn+2). That won't work if yn = 42.34567. Try casting to int32 first before using as an index.
yn = int32(in);
  2 Comments
IPlover
IPlover on 22 Nov 2013
Edited: Walter Roberson on 22 Nov 2013
I tried again and it is still showing error
% Load image
img = imread('c.jpg');
img = rgb2gray(img);
% Identify lanes
imshow(img)
[x,y] = ginput;
% Invert image
img = max(img(:)) - img;
% Subtract background
yn=int32(in);
[xn,yn] = ginput(1);
noise = img((yn-2):(yn+2), (xn-2):(xn+2));
noise = mean(noise(:));
img = img - noise;
% Calculate means
means = (1:size(img,1)) * double(img(:,round(x))) ./ sum(double(img(:,round(x))), 1);
% Plot
hold on
plot(x, means, 'r.')
Walter Roberson
Walter Roberson on 22 Nov 2013
You do the ginput, and then you calculate yn as integral valued from it, and then you immediately write over yn and use the floating value from it. You should do this:
[x,y] = ginput;
xnl = max(ceil(x)-2, 1);
xnh = min(floor(x)+2, size(img,2));
ynl = max(ceil(y)-2, 1);
ynh = min(floor(x)+2, size(img,1));
noise = imag(ynl:ynh, xnl:xnh);
noise = mean(noise(:));
means = (1:size(img,1)) * mean(double(img(:,ceil(x))));
I don't know why you are weighting the means by the row number?
The choice of floor() and ceil() that I used was to maximize the likelihood that the calculation would stay inside the array. I then added min() and max() to force it to be inside on the boundaries. Except that I didn't bother to code it for the means: really you should make sure it is in the array, but as you have seen the pattern now you can modify the code yourself.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!