Error in my code.....

1 Ansicht (letzte 30 Tage)
Febin Benjamin
Febin Benjamin am 4 Aug. 2013
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??

Akzeptierte Antwort

Wayne King
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
Wayne King
Wayne King am 4 Aug. 2013
I think we need to know more about what Histo() is intended to do.
Febin Benjamin
Febin Benjamin am 4 Aug. 2013
Bearbeitet: Febin Benjamin am 4 Aug. 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....

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Roger Stafford
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.

dpb
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
Febin Benjamin
Febin Benjamin am 4 Aug. 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
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).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by