Skip to content

Instantly share code, notes, and snippets.

@satlavida
Created October 23, 2019 05:58
Show Gist options
  • Save satlavida/c3b8cffbd2b0a0fb919381c1a6862e76 to your computer and use it in GitHub Desktop.
Save satlavida/c3b8cffbd2b0a0fb919381c1a6862e76 to your computer and use it in GitHub Desktop.
clc;
clear all;
close all;
img = (imread('deer1.jpg'));
T = input('Input Threshold');
T1 = uint32(0);
T2 = uint32(0);
T1n = 0;
T2n = 0;
T1mean = 0;
T2mean = 0;
Tdiff = 21;
[x y] = size(img);
val = uint64(0);
while(~(Tdiff < 5 && Tdiff > -5))
for i = 1:x
for j = 1:y
val = uint32(img(i,j));
if val < T
T1 = T1 + val;
T1n = T1n + 1;
else
T2 = T2 + val;
T2n = T2n + 1;
end
end
end
T1mean = (T1/T1n);
T2mean = (T2/T2n);
Tnew = (T1mean + T2mean)/2;
Tdiff = abs(int32(Tnew) - int32(T));
disp(sprintf('Tdiff = %d', Tdiff));
if Tdiff > 5
T = Tnew;
disp(sprintf('New value of T = %d', T));
end
T1 = 0;T1n = 0;T2 = 0;T2n = 0;
end
im2 = img;
for i= 1:x
for j= 1:y
if(img(i,j) > T)
im2(i,j) = 255;
else
im2(i,j) = 0;
end
end
end
[level EM] = graythresh(img);
img2 = im2bw(img,level);
subplot(3,1,1);
imshow(img), title('Orignal image');
subplot(3,1,2);
imshow(img2), title('Threshold image inbuilt function');
subplot(3,1,3);
imshow(im2),title('Thr');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment