Binary image? Only black and white? [closed]

By name, a binary image is one that has two levels. But is it essential that those two colors are black and white? Or any other combination also allowed as shown in attached snap. Snap is output image, updated image, that is generated as a result of below code.

My code

[X,map]= imread('lena.png');
subplot 121
imshow(X)
R=X(:,:,1);
G=X(:,:,2);
B=X(:,:,3);
title('Orginal Image');
[r c]=size(R);
for i=1:r
for j=1:c
if R(i,j)<128
X1(i,j,1)=0;
else
X1(i,j,1)=1;
end
end
end
for i=1:r
for j=1:c
if G(i,j)<128
X1(i,j,2)=0;
else
X1(i,j,2)=1;
end
end
end
for i=1:r
for j=1:c
if B(i,j)<128
X1(i,j,3)=0;
else
X1(i,j,3)=1;
end
end
end
subplot 122
imshow(X1);
title('Updated Image');

• so, what's the question? you've answered it yourself. Also, this image is not 1 bit. Otherwise it could only have two colors. This has at least five colors that I can count. Jun 6, 2020 at 18:34
• Two color??is it must that those two colors are black and white??or any other color combination will also be considered binary image?? Jun 6, 2020 at 19:46
• "binary" means something. And that something has nothing to do with color. engr, a lot of your question are "look this up in a dictionary". Jun 7, 2020 at 8:14

Your image is binary or two-tone per color channel. Thus you have 2x2x2=8 possible tones, or 3 bits.

In a bitmapped image, you can encode each pixel using any number of bits, then use a global table to define what color should be mapped to each code.

Edit: Your code could be written as:

[X,map]= imread('lena.png');
X1 = round(X/255);


Each channel can take two possible values, thus one bit is sufficient to encode it. Each pixel contains three channels, thus three bits are sufficient to encode it. I would not call X1 a «binary image».

• Despite the fact that image is using colors other than the two basic colors of black and white,still it will be considered binary image?? Jun 7, 2020 at 4:59
• "Thus you have 2x2x2=8 possible tones, or 3 bits." one bit for each channel? Jun 7, 2020 at 5:07
• Two-liner, well-spotted Jun 7, 2020 at 8:40
• @LaurentDuval what do you mean by Two liner? Jun 8, 2020 at 1:06
• The answer provides you with codes, in two lines, that simplifies your code, by using the vector/matrix features of Matlab Jun 8, 2020 at 9:30

A binary code is a relatively abstract representation of some quantized information on two levels. As such (being an abstract think), it often requires to be interpreted to turn into a real-word representation. The unitless 0 and 1 shall be concerted into quantized with dimensions, using:

• ancillary data, like contained in the header of the PNG image file (number of rows, columns, channels, min and max values, etc.); they are inherited in the size of X and the nature of map
• accepted standards or "default".

In other words, 0 is not black by nature, and neither is 1 white. For instance if you generate in Matlab for a 2-level image and display it:

imagesc(floor(2*rand(16,16)))

it often appears in colors other than black or white. Here is how I see it without specifying a colormap:

You can specificity a map if you want a more adapted behavior. For instance, suppose that your image comes from in infrared device, it might be wiser to display it in a different colormap, to avoid interpretation in the RGB case.

Then, as well-answered by @Knut Inge, you are building an image with three binarily encoded channels. So you can have up to $$2^3$$ different color indices. To become interpretable, they should be a associated to specific color mapping. Here, you are using the default from Matlab function image.