Error in my code.....
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
is a function which is calling another function- Histo.m inside it. But its showing the below mentioned error.
function FebEuclid()
I1 = imread('F:\Project-VP(new)\1.tif');
I1=rgb2gray(I1);
h1 = Histo(I1);
for i=1:5
img_pack = sprintf('%d.tif',i);
if ~exist(img_pack, 'file')
fprintf('%s not found.\n', img_pack);
continue;
end
I2=imread(img_pack);
I2=rgb2gray(I2);
h2 = Histo(I2);
E_distance = sqrt(sum((h1-h2).^2));
display(E_distance);
end
Histo.m
function[h]=Histo(I)
I = double(I);
h = zeros(256);
[r,c] = size(I);
for i= 1 : r
for j =1 : c
v = I(i,j);
h(v)=h(v)+1;
end
end
end
Error: Attempted to access h(0);
What might be the problem??
0 Kommentare
Akzeptierte Antwort
Wayne King
am 4 Aug. 2013
This:
v = I(i,j);
h(v)=h(v)+1;
If an element of I is zero, which is quite possible because I is a gray-scale image, then how can you access the 0-th element of a vector in MATLAB?
3 Kommentare
Weitere Antworten (2)
Roger Stafford
am 4 Aug. 2013
The error message is telling you what the problem is. The 'histo' function has received an input 'I' with zeros in it, and is attempting to use these zeros as indices, 'v', in the 'h' array. You can't have zero values for matlab indices.
0 Kommentare
dpb
am 4 Aug. 2013
function FebEuclid()
...
I1=rgb2gray(I1);
h1 = Histo(I1);
...
function[h]=Histo(I)
...
for i= 1 : r
for j =1 : c
v = I(i,j);
h(v)=h(v)+1;
You've taken the actual value of the image in grayscale and used it as an index into an array..._NOT_ what you intended, undoubtedly.
Matlab will take a non-integer value of an index and convert it to an integer (albeit w/ a warning unless you've defeated them) but since grayscale can also include the value 0 identically, that doesn't generate the rounding warning of non-integer value but does generate the error that "you can't do that!" since Matlab arrays are one-based.
If you're trying to do a histogram, why don't you use one of the Matlab builtin functions to do it? Or, explain what your function Histo is supposed to be doing.
2 Kommentare
dpb
am 4 Aug. 2013
I don't have image processing where I guess imhist must reside so don't know it precisely, but like any of the histogram functions, you've got to start w/ a number of bins (1:N) and then determine which bin each value falls within its boundaries and then increment that bin. (If it's more than one dimensional histogram that's intended then the number of bins has to range in that many dimensions as well, of course).
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!