I have written a Matlab script to reduce the number of intensity levels of each pixel of a grayscale image from 256 to some power of 2.
img_color = imread('photo.jpg');
img_gray = rgb2gray(img_color);
imshow(img_gray);
[rows, cols] = size(img_gray);
noOfDesiredIntensityLevels = 2; // test data. will check for 4,8,16,32,etc.
bitsNeededToRepresentIntensityLevels = log2(noOfDesiredIntensityLevels);
new_img = img_gray;
for i = 1 : rows
for j = 1 : cols
new_img(i,j) = floor(img_gray(i,j)/(2^(8-bitsNeededToRepresentIntensityLevels)));
end
end
figure
imshow(new_img);
On execution, the script returns a black image. My expectation was that the image will be turned into black-and-white (intensity value for each pixel will be either a 0 or a 1).
What am I missing here?
P.S: I am a novice in Matlab and Image Processing. So, please ignore any mistakes in my understanding.