function [TH] = threshold(IMG, level)
    isColor = size(IMG,3) == 3;
    if isColor
     IMG = rgb2gray(IMG);
     warning('please supply gray img');
    end
    M = IMG>=level;
    TH = IMG;
    TH(M)=255;
    TH(~M)=0;
end