You should consider vectorizing your code to make it more legible and less obfuscated. You can completely avoid for
loops by doing the following:
function finalImage = nicholson11(image,range)
imDouble = double(image);
intensityImage = 0.3*imDouble(:,:,1) + 0.59*imDouble(:,:,2) + 0.11*imDouble(:,:,3);
finalImage = zeros(size(intensityImage));
finalImage(intensityImage >= range(1) | intensityImage <= range(2)) = 255;
% Assuming your original image was uint8
finalImage = im2uint8(finalImage);
Try that and see if you get the same results. Also, going with the comments that have already been said, when you create finalImage
, this is a double
array, which assumes that the intensities are between [0,1]
. As such, you will either need to normalize your intensities so that they fall in this range, or use a conversion routine that will get it to the type that you want. Assuming that your image was originally uint8
, you can use the im2uint8
command.