% Scripts to filter Bleeeender images wwwhich are .pngs
% This version places a bounding box around the object (red in figure(1)),
% enlarges it by enlargement (blue in figure(1) to allow for some views being larger; 
% centres the image, scale it to 256x256, and adjusts its contrast.
% It is important that the same enlargement and centering is used for all views of an object

clear all; close all hidden;

rotList = [10:20:355];
rotlist = [000 022 045 068 090];
rotlist = [000];
%matlabpool(8);

%dataPath = '/home/leigh/Desktop/PhDProject/ProcessedImages/ALOI2';
%dataPath = '/home/erolls/imagedata/ALOI';
dataPath = '/home/erolls/imagedata/BlenderIm';

dirs = dir(dataPath);
catNames = {dirs([dirs.isdir]).name};
catNames = setdiff(catNames, {'.', '..'});

%for i = 1:numel(catNames)
for i = 1:1
    files = dir(fullfile(dataPath, catNames{i}, '*.png'))
    files = {files.name}    
%     file = [ fullfile(dataPath, catNames{i}) '/' catNames{i} '_r' int2str(rotList(1)) '.png' ]
%     tmp = imread(file);
%     size(tmp)
%     st = regionprops(double(tmp > 30), 'BoundingBox');
%     rect = st.BoundingBox % xmin ymin width height
%     figure(1);
%     imshow(tmp); hold on;
%     rectangle('Position', rect, 'EdgeColor', 'r');
%     enlargement = 20; % aim for this as some views may be larger than the first, but clip as necessary
%     rect(1) = rect(1) - enlargement; % xmin
%     if rect(1) < 1; rect(1) = 1; end
%     rect(3) = rect(3) + (enlargement * 2); % width
%     if rect(3) + rect(1) > size(tmp, 2); rect(3) = size(tmp, 2) - rect(1); end
%     rect(2) = rect(2) - enlargement; % ymin
%     if rect(2) < 1; rect(2) = 1; end % y = 1 is the top of the image
%     rect(4) = rect(4) + (enlargement * 2); % width
%     if rect(4) + rect(2) > size(tmp, 1); rect(4) = size(tmp, 1) - rect(2); end
%     rect    
%     rectangle('Position', rect, 'EdgeColor', 'b');
%     tmp1 = imcrop(tmp, rect);
%     size(tmp1)
%     figure(10);
%     imshow(tmp1);
%     % now resize to a max dim of 256
%     m = max(rect(3),rect(4))
%     tmp2 = imresize(tmp1, [(rect(4) * 256 / m) (rect(3) * 256 / m)]);
%     size(tmp2)
%     figure(2);
%     imshow(tmp2);
%     x = size(tmp2,2)
%     c1 = floor(128 - x/2)
%     if c1 < 1; c1 = 1; end
%     if c1 > 256; c1 = 256; end
%     y = size(tmp2,1)
%     r1 = floor(128 - y/2)
%     if r1 < 1; r1 = 1; end
%     if r1 > 256; r1 = 256; end
%     tmp3 = uint8(zeros(256, 256));
%     %tmp3(r1 : r2, c1 : c2) = tmp2;
%     tmp3(r1 : (r1 + size(tmp2, 1) -1), c1 : (c1 + size(tmp2, 2) - 1)) = tmp2;
%     figure(3);
%     imshow(tmp3);
%     %pause
%     tmp3 = imadjust(tmp3, stretchlim(tmp3)); % set min=0 max=255 with 1% saturation at each end
%     figure(4);
%     imshow(tmp3);
%     drawnow
    for j = 1:numel(rotList) % a check on the image cropping etc       
        file = [ fullfile(dataPath, catNames{i}) '/' catNames{i} '_r' int2str(rotList(j)) '.png' ]
        tmp = imread(file);
%         tmp1 = imcrop(tmp, rect);
%         tmp2 = imresize(tmp1, [(rect(4) * 256 / m) (rect(3) * 256 / m)]);
%         tmp3 = uint8(zeros(256, 256));
%         tmp3(r1 : (r1 + size(tmp2, 1) -1), c1 : (c1 + size(tmp2, 2) - 1)) = tmp2;
%         tmp = imadjust(tmp3, stretchlim(tmp3)); % set min=0 max=255 with 1% saturation at each end
%         figure(10+j);
        size(tmp)
        imshow(tmp);
        drawnow;
    end
    pause
    parfor j = 1:numel(rotList)
        file = [ fullfile(dataPath, catNames{i}) '/' catNames{i} '_r' int2str(rotList(j)) '.png' ]
        tmp = imread(file);    
        tmp1 = imcrop(tmp, rect);
        tmp2 = imresize(tmp1, [(rect(4) * 256 / m) (rect(3) * 256 / m)]);
        tmp3 = uint8(zeros(256, 256));
        tmp3(r1 : (r1 + size(tmp2, 1) -1), c1 : (c1 + size(tmp2, 2) - 1)) = tmp2;
        tmp = imadjust(tmp3, stretchlim(tmp3)); % set min=0 max=255 with 1% saturation at each end
        [a b c] = fileparts(fullfile(file));
        % fid = fopen(fullfile(a,[ b '.dat' ]), 'w');
        fid = fopen(fullfile(a,[b]), 'w');
        fwrite(fid, tmp', '*uint8');
        fclose(fid);
        %FilterConv4([b '.dat'], [ fullfile(dataPath, catNames{i}) '/' ], 0);
        FilterConv4([b], [ fullfile(dataPath, catNames{i}) '/' ], 0);

    end
end

matlabpool close;
