-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyBinary.m
59 lines (53 loc) · 1.72 KB
/
myBinary.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function [J] = myBinary(I,T)
%
% The function binarizes the image with different thresholds,
% one for each channel.
% Use the fuction myBinary(I,T), where 'I' is the input image
% and 'T' is a 3 point vector with the thresholds for the RGB layers.
% 'T' is a vector of the form [r g b]. You can set a single value to
% 'T', if the image is in grayscale it will take this value as the
% threshold, but if the image is RGB this value will be used for each
% of the thresholds ([r g b])
%
% ---- Oscar Castro, 09 05 2021
%
% ---------- Example ----------
%
% image = "img\lenna.tif";
% I = imread(image);
%
% T = [150 255 255];
% J = myBinary(I, T);
%
% figure,
% subplot(121), imshow(I),
% title('Original Image'); axis on;
% subplot(122), imshow(J),
% title('Image with threshold'); axis on;
% ===================================================================== %
% Get dimensions and channels of 'I'
[row,col,chan]= size(I);
% Gets dimensions of 'T'
[~, ts] = size(T);
% 'T' is a single value and is repeated for the other channels
if (chan == 3 && ts == 1)
T(2) = T(1); T(3) = T(1);
end
% Creates an array of zeros with the dimensions of the input image
J = zeros(row, col, class(I));
% Each pixel of image 'I' is evaluated
for k = 1:chan
for i = 1:row
for j = 1:col
% If the pixel value is greater than or equal to 'T' on
% their respective channel, pixel value is set to 255
if I(i,j,k) >= T(k)
J(i,j,k) = 255;
% but if it's lower, pixel value is set to 0
else
J(i,j,k) = 0;
end
end
end
end
end