Error in my code.....

1 回表示 (過去 30 日間)
Febin Benjamin
Febin Benjamin 2013 年 8 月 4 日
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??

採用された回答

Wayne King
Wayne King 2013 年 8 月 4 日
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 件のコメント
Wayne King
Wayne King 2013 年 8 月 4 日
I think we need to know more about what Histo() is intended to do.
Febin Benjamin
Febin Benjamin 2013 年 8 月 4 日
編集済み: Febin Benjamin 2013 年 8 月 4 日
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....

サインインしてコメントする。

その他の回答 (2 件)

Roger Stafford
Roger Stafford 2013 年 8 月 4 日
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 2013 年 8 月 4 日
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 件のコメント
Febin Benjamin
Febin Benjamin 2013 年 8 月 4 日
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 2013 年 8 月 4 日
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).

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by