# Error in my code.....

9 visualizzazioni (ultimi 30 giorni)
Febin Benjamin il 4 Ago 2013
is a function which is calling another function- Histo.m inside it. But its showing the below mentioned error.
function FebEuclid()
I1=rgb2gray(I1);
h1 = Histo(I1);
for i=1:5
img_pack = sprintf('%d.tif',i);
if ~exist(img_pack, 'file')
continue;
end
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 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Wayne King il 4 Ago 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 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Wayne King il 4 Ago 2013
I think we need to know more about what Histo() is intended to do.
Febin Benjamin il 4 Ago 2013
Modificato: Febin Benjamin il 4 Ago 2013
Histo is giving me the histogram of the intensity values that's all..... If in the above code i add plot(h)..... then it'l plot me the graph of those intensity values.... Instead of Histo i could have used imhist but I am intending to use my own functions here.... i hope u have a clear picture now....

Accedi per commentare.

### Più risposte (2)

Roger Stafford il 4 Ago 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 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

dpb il 4 Ago 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 CommentiMostra NessunoNascondi Nessuno
Febin Benjamin il 4 Ago 2013
I am just experimenting(its part of my personal project).... trying to use my own functions instead of in-built ones.... So Histo is doing the exact job that imhist would be doing in my code(except that error) :)
dpb il 4 Ago 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).

Accedi per commentare.

### Categorie

Scopri di più su Image Processing Toolbox in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by