Replace values to create an image with binary values (0,1)

Dear Researchers,
I have a matrix (2D image) which I need to replace values in such a way to obtain binary image.
I used Max function which replace values for one condition. However, In this case, I need to replace values greater and less than zero with zero (0) and replace all values = zero with 1.
I have uploaded image, vaues are ranging from (-3.8 to 3.8), can you please guide me on how to obtain binary image.
Many thanks in advance.

 采纳的回答

OK, now that you've added the .mat file, there are no values that are exactly 0. You need to define a range.
s = load('image.mat')
V = s.V;
subplot(2, 2, 1);
imshow(V, []);
colorbar
subplot(2, 2, 3:4);
histogram(V, 256);
grid on;
title('Histogram of V')
xlabel('Value')
ylabel('Count')
% There are no values that are exactly zero so let's pick a tolerance
tolerance = 0.1;
binaryImage = abs(V) < tolerance;
subplot(2, 2, 2);
imshow(binaryImage, []);

1 个评论

Perfect, It works, with what i required, now it's all about siffting and get more accurate results.
Many thanks dear @Image Analys

请先登录,再进行评论。

更多回答(2 个)

Sounds like a logical result would work.
a = [-3.8 -1 0 1 3.8];
c = a~=1
c = 1×5 logical array
1 1 1 0 1

7 个评论

Thanks for propmt reply, since this replace values accroding to the condition, but give a logical matrix, thus I need to obtain binary image which I can't obtain by logical matrix.
You can then convert it to double or int32, for example double( c) or int32( c).
After applying above operation and using double(c) or int32, the image obtained is not binary and unable to get same signature given in primary image (but with 0, 1)
I have to generate a binary image (e.g., 1 is land and 0 is sea)
In the primary image green color (with zero values) is land, and rest of image with yellow, red, blue is water with values < and > then 1.
Consider sharing your data. You can attach it to your post using the paperclip icon.
Dear @Cris LaPierre I have attached data file, since size is larger, I clipped small size.
The challenge I see is that none of your data values exactly equal 1. You'll need to look at your data and decide what range of values you want to consider to represent land.

请先登录,再进行评论。

" I need to replace values greater and less than zero with zero (0) and replace all values = zero with 1. "
So why don't you just do
outputMatrix = inputMatrix == 0;

类别

帮助中心File Exchange 中查找有关 Images 的更多信息

产品

版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by