How can I convert grayscale image to a binary image without using a toolbox function?

I want to create a binary image from a gray scale image, using a specific threshold value of 0.2, but without using im2bw(), which is in the Image Processing Toolbox. How do I do it?

 Risposta accettata

Just threshold:
binaryImage = grayImage > 0.2; % or < or <= or >= (whatever you want).
No toolbox needed.

11 Commenti

function y = myrgb2gray(x);
x = imread('rgb.TIF');
R=x;G=x;B=x;
y = 0.299*R + 0.59*G + 0.111*B;
y = rgb2gray(x); %
This how i convert rgb to gray but how can i convert it to binary. i didn't get what you wrote.
This will convert (the badly-named) y to binary:
y = y > 0.2;
Just try it and see.
when i run the program y=y>0.2; it's giving me wrong answer, it showed me the output of 0.1 not 0.2,also when i tried to change the threshold it's giving me wrong output... sorry for disturbing.
If the original image has a range of uint8, meaning 0-255, then you need to use the gray level corresponding to that. In other words, your threshold needs to be 0.2 * 255 = 51 rather than 0.2
y = y > 51;
im2bw() uses normalized threshold levels, so if you're not using a normalized image you have to not use a normalized threshold value and use a value in the range of your original image, like in the range 0-255.
I converted my gray image to binary using following statement.
Binary = gray >= 51;
Now i want the gray image back. how to restore it? can any one please tell me.
Thank you in advance
Don't use gray as the name of your variable. It is the name of a built-in function. What do you mean by "back"? What happened to it. You still have (the badly named) "gray" in your functions workspace, right? So just use it. You didn't say "clear gray" did you? So it should still be there.
To be clear, let's assume an empty workspace:
inpict = imread('cameraman.tif'); % a uint8-class grayscale image
mask = inpict >= 128; % a logical-class binary image
At this point there are two variables in the workspace: inpict and mask. You can't back-calculate inpict from mask, but you don't have to, since it's still in memory. Just use inpict.

Accedi per commentare.

Più risposte (1)

x = imread('cameraman.tif');
figure,imshow(x);
[r,c] = size (x);
output=zeros(r,c);
for i = 1 : r
for j = 1 : c
if x(i,j) > 128
output(i,j)=1;
else
output(i,j)=0;
end
end
end
figure,imshow(output);

1 Commento

There's no need for the loops. The appropriate answer was given years prior.
inpict = imread('cameraman.tif'); % a uint8-class grayscale image
mask = inpict >= 128; % a logical-class binary image
... or if inpict is not needed for anything, you can avoid the intermediate result:
mask = imread('cameraman.tif') >= 128; % a logical-class binary image
Display them however you choose.

Accedi per commentare.

Richiesto:

il 14 Ott 2016

Commentato:

DGM
il 24 Ago 2023

Community Treasure Hunt

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

Start Hunting!

Translated by