Error in my code.....
조회 수: 7 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
추가 답변 (2개)
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.
댓글 수: 0
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
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 Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!