my own imhist function error

1 view (last 30 days)
Ana
Ana on 10 Dec 2012
Hello, iam new in matlab, and i have some questions about this function i made. my teacher wants us to do our own imhist, and i did this. my question is, iam using a double image and it gives an error, what i should change in this function to works fine with double image.
( Attempted to access vetor(1.01519); index must be a positive integer or logical.)
function img2 = HistOnMyOwn(image)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
img2=image;
im2double(img2);
vetor=zeros(256,1);
for i=1:size(img2,1)
for j =1:size(img2,2)
vetor(img2(i,j)+1) = vetor(img2(i,j)+1)+1;
end
end
plot(vetor);
end

Accepted Answer

Sean de Wolski
Sean de Wolski on 10 Dec 2012
This oughtta make it pretty clear:
im2double(uint8(1:10:255))

More Answers (1)

Image Analyst
Image Analyst on 10 Dec 2012
DO NOT use image as the name of your input argument - you'd be blowing away the important built-in image() function.
Your
im2double(img2);
line does nothing since it's not assigned to anything. Even if it did, it would do nothing because your image is apparently already a double in the range 0-1. You should do this instead:
img2 = uint8(255 * inputImage); % Using inputImage here instead of image.
Note: if your double image has values outside the 0-1 range, then you'll have to do things a little differently.
  3 Comments
Image Analyst
Image Analyst on 10 Dec 2012
So img2(i,j)+1 is 1.05098???? I just don't see how img2(i,j) can possibly have the value 0.05098 when you multiplied it by 255 and rounded it to the nearest integer. It just doesn't make sense. It must be an integer in the range 0-255. Is there some code you're not sharing? Put this line inside your inner most for loop
fprintf('(i, j) = (%d, %d), img2(%d, %d) = %d\n',...
i, j, i, j, img2(i, j);
Tell me what you discover.
Image Analyst
Image Analyst on 10 Dec 2012
Hmmmm. You just accepted Sean's answer. Ok, I guess whatever the problem was, you finally figured it out.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!