a = imread('D:\lh.jpg');
a =rgb2gray(a);
r1 = input('Enter lower gray level:');
r2 = input('Enter upper gray level:');
b = a;
s = size(a);
for i=1:s(1)
for j=1:s(2)
if a(i,j)>r1
if a(i,j)>r2
b(i,j)=255;
end;
end;
end;
end;
c = a;
for i=1:s(1)
for j=1:s(2)
if a(i,j)>r1
if a(i,j)>r2
c(i,j) = 255;
else
c(i,j) = 0;
end;
end;
end;
end;
subplot(1,3,1);
imshow(a);
subplot(1,3,2);
imshow(b)
subplot(1,3,3);
imshow(c)

Output

Enter lower gray level:20
Enter upper gray level:50