Question

I am making a function that takes an image and a luminance range and then returns an intensity sliced image. So essentially it detects if a certain pixel's intensity is in the range and then sets the intensity to 255 so it's bright white, or otherwise leaves it alone. The equation for luminance I'm given is L = 0.3R + 0.59G + 0.11B. I think everything is executed correctly, but when I try to use the built in image function to turn the intensities into an image I get the error in the title. Here is my code so far.

function finalImage = nicholson11(image,range)

originalImage = imread(image);
[W,H,clrs] = size(originalImage);
finalImage = zeros(W,H,3);

for i=1:1:W
for j=1:1:H
if ((((originalImage(i,j,1))*0.3) +((originalImage(i,j,2))*0.59) +((originalImage(i,j,3))*0.11)) >=range(1) && ((((originalImage(i,j,1))*0.3) +((originalImage(i,j,2))*0.59) + ((originalImage(i,j,3))*0.11))<=range(2)))
finalImage(i,j,:)=255;

end
end
end
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top