I am getting an error index must be a positive integer or logical. Please Help

Actually I am doing a project on "automatic detection of diabetic retinopathy using digital non dilated rgb fundus images"
my complete code is:-
clc;
clear all;
close all;
%seperating the RED GREEN BLUE components of a rgb image
X=imread('C:\MJCET\91280612.jpg');
imshow(X)
R = X(:,:,1);
image(R), colormap([[0:1/255:1]', zeros(256,1), zeros(256,1)]), colorbar;
%Green Component
G = X(:,:,2);
figure;
image(G), colormap([zeros(256,1),[0:1/255:1]', zeros(256,1)]), colorbar;
%Blue component
B = X(:,:,3);
figure;
image(B), colormap([zeros(256,1), zeros(256,1), [0:1/255:1]']), colorbar;
Z=im2double(G);
%converting GREEN SCALE image to GRAY SCALE image
gray = 0.5870 * X(:,:,2);
figure,imshow(gray);
Y=im2double(gray);
%Median Filtering
A=im2double(Y);
H=medfilt2(A, [30 30]);
figure,imshow(H);
L=im2double(H);
%Normalisation
zz = imsubtract(L,Z);
figure,imshow(zz),title('norm');
%Histogram Equalisation
%%HISTOGRAM EQULAIZER
figure,subplot(1,2,1),imshow(zz), title('original image')
subplot(1,2,2),imhist(zz),title('original image histogram')
%%Calculating the CDF
hst=imhist(zz);
j=1;
cdff(1,1)=hst(1,1);
for i=2:256
cdff(i)=hst(i)+cdff(i-j);
end
cdff1=cdff';
cdf_min=min(cdff);
[row col]=size(zz);
mn=row*col;
figure, plot(cdff), title('CDF of Image')
%%calcuting new intensity
for indx=1:length(cdff)
h(indx)=round((cdff(indx)-cdf_min)/(mn-cdf_min)*255);
end
h1=h';
figure,plot(h1), title('New value for General Histogram')
%%EQULIZED IMAGE
HIm=double(zeros(size(zz,1),size(zz,2)));
for i=1:row;
for j=1:col;
HIm(i,j) = h((round(zz(i,j)+1)));
end
end
figure,subplot(1,2,1),imshow(HIm), title('Equlized Image')
subplot(1,2,2),imhist(HIm) ,title('Equlized image histogram')
Alhamdulillah! except the last part where the histogram is being equalized rest of the program have no error's.
so at last zz comes to be the subtracted image. this image comes when the gray image is subtracted from green plane of rgb fundus image. so I hope the output is also gray image.
I am new to matlab. please help me with errors
output is:
??? Attempted to access h(0); index must be a positive integer or logical.
Error in ==> burr at 74
HIm(i,j) = h((round(zz(i,j)+1)));
>>

 Accepted Answer

Evidently zz is not a grayscale image - it's a floating point image because one value of it is (0.945098 - 1). And you can't have the 0.945098'th element of h. It must be 1, 2, 3, etc. - whole numbers greater than or equal to 1, or true false values (a logical vector).

12 Comments

By the way, you probably don't need or want a histogram equalized image. I never have in well over 30 years of image processing.
I have changed the question. please check. the output is a gray image. and the first element to be addresses is 0. so I think comes the error. please check my modified question.
in the paper i refer it says to perform histogram equalization for contrast enhancement. so I did that part
You are right that zero is not an allowed index.
Histogram equalization can be used to "enhance," or increase the contrast, though it's only optional if you want it to look like it has a wider dynamic range. It's not needed for image analysis and you can do the same operations, for example thresholding, on the original image, just with a different threshold. Moreover, the "enhanced" image often doesn't look very natural/realistic if you use histogram equalization because of the non-linear, not very smooth transform it often uses. Almost always a simple linear stretch will give a better appearance, but like I said, it is just for appearance only and not needed for analysis. So I never do histogram equalization and simply use [] as the second argument to imshow() to do a simple linear stretch, which looks great (unlike histogram equalization).
Contrast-limited Locally Adaptive Histogram Equalization (CLAHE) though can be useful for getting rid of a background gradient. This (CLAHE) is done by the function adapthisteq() in the Image Processing Toolbox.
Thank you! I will use and see adapthisteq (). But still I got the problem of negative value after subtraction. And I got to subtract as there's no other way to subtract two images I think.
Yes, you can get negative values with subtraction. Why is that a problem?
Because it can't be indexed as that's my error Even if I try adapthist I think the same problem may come please assist!
I don't understand why a difference in intensities needs to be an index of an array.
I have another problem. I am getting an error. I have got to perform multi level thresholding on an image. my code:
W=im2double(HIm);
thresh = multithresh(W);
my error:
Undefined function or method 'multithresh' for input arguments of type 'double'.
please help
You may have an older version, before that function was introduced. What is your release year?
Check the release notes over the past 3 years to see if it was introduced since then. http://www.mathworks.com/help/relnotes/index.html

Sign in to comment.

More Answers (1)

You have not defined "h". Is it an array or a function?

3 Comments

could you please again check the modified question. please assist!
You have two images in the range 0 to 1. You subtract one from the other. The maximum possible output for any location is 1 (if the first image had 1 and the second had 0) and the minimum is -1 (if the first image had 0 and the second had 1). So your zz values are in the range -1 to +1. You add 1 to this value, getting a result in the range +0 to +2. You then round that, which is going to give you 0, 1, or 2. You then attempt to use that as an index. The 1 and 2 are plausible indexes but the 0 is not.
I suspect that when you constructed your logic you forgot the subtraction could end up negative.
So, what do you suggest me to do such that not to get negative value or any other way. Thanks for the information.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!