function windows = extract_window_furkan(Ilow,maskI,fore_ratio,mag)

% extract a window from an image and store the location

% thresh_var = 0.1*var_cal(Ilow);
% thresh_var = -1;
% variance = 0;
foreground = 0;

while foreground <= fore_ratio %(variance <= thresh_var || foreground <= fore_ratio)
    % select a random subwindow
    windows = randwindow2_furkan(Ilow,mag);

    % check if the region is background
%     Inew = Ilow(windows(1):windows(1)+windows(3)-1,windows(2):windows(2)+windows(3)-1);
    Inew_mask = maskI(windows(1):windows(1)+windows(3)-1,windows(2):windows(2)+windows(3)-1);
%     variance = var_cal(Inew);
    %foreground = fore_cal(Inew_mask);
    %foreground = length(find(Inew_mask==1));
    foreground = sum(Inew_mask(:))/windows(3)^2;
end

% function windows = extract_window(Ilow,maskI,fore_ratio)
% 
% % extract a window from an image and store the location
% 
% thresh_var = var_cal(Ilow);
% variance = 0;
% foreground = 0;
% 
% while(variance <= thresh_var || foreground <= fore_ratio)
%     % select a random subwindow
%     windows = randwindow(Ilow,1);
% 
%     % check if the region is background
%     Inew = Ilow(windows(1):windows(1)+windows(3)-1,windows(2):windows(2)+windows(3)-1);
%     Inew_mask = maskI(windows(1):windows(1)+windows(3)-1,windows(2):windows(2)+windows(3)-1);
%     variance = var_cal(Inew);
%     foreground = fore_cal(Inew_mask);
% end