img = imread('spatial_res.bmp');

f_img = imread('fotki-spatial009.tif');
%w_img = rgb2gray(w_img_color);
%w_img = imrotate(w_img_flipped,180);

w_img = imread('walmart-spatial.tif');
%w_img = rgb2gray(w_img_color);
%w_img = imrotate(w_img_flipped,180);

c_img = imread('costco-spatial.tif');
%c_img = rgb2gray(c_img_color);
%c_img = imrotate(c_img_flipped,180);

%** Analize Original Test Image *******************************************
scanline = 1500;
thresh = 128;

zero_crossings = ones(2998,1);

for i = 1:2999
    if img(scanline,i+1) <= thresh & img(scanline,i) > thresh
        zero_crossings(i) = 0;
    end
end


last = 0;
x = 1;
for i = 1:2998
    if zero_crossings(i) == 0
        if i - last > 3
            period(x) = i - last;
            last = i;
            x = x + 1;
        end
    end
end
x

period(1)=0;
period = period/max(period(:));
o_period = period;
%**************************************************************************


%*** Analize Fotki Image ************************************************
[height,width] = size(f_img)
scanline = round(height*3/4);

zero_crossings = ones(width,1);
thresh = 90;

for i = 1:width-1
    if f_img(scanline,i+1) <= thresh & f_img(scanline,i) > thresh
        zero_crossings(i) = 0;
    end
end

last = 0;
x = 1;
for i = 1:width-1
    if zero_crossings(i) == 0
        if i - last > 3
            period(x) = i - last;
            last = i;
            x = x + 1;
        end
    end
end
x

period(1)=0;
period = period/max(period(:));

figure();
subplot(3,1,1), plot(o_period);
title('Original Image Period Plot');
xlabel('horizontal space');
ylabel('normalized period');
subplot(3,1,2), plot(period);
title('Fotki Image Period Plot');
xlabel('horizontal space');
ylabel('normalized period');
subplot(3,1,3), plot((period - o_period).^2);
title('Squared Error: original vs. walmart');
xlabel('horizontal space');
ylabel('squared error');
axis([0 300 0 .4]);

%**************************************************************************

%*** Analize Walmart Image ************************************************
[height,width] = size(w_img)
scanline = round(height*3/4);

zero_crossings = ones(width,1);
thresh = 90;

for i = 1:width-1
    if w_img(scanline,i+1) <= thresh & w_img(scanline,i) > thresh
        zero_crossings(i) = 0;
    end
end

last = 0;
x = 1;
for i = 1:width-1
    if zero_crossings(i) == 0
        if i - last > 3
            period(x) = i - last;
            last = i;
            x = x + 1;
        end
    end
end
x

period(1)=0;
period = period/max(period(:));

figure();
subplot(3,1,1), plot(o_period);
title('Original Image Period Plot');
xlabel('horizontal space');
ylabel('normalized period');
subplot(3,1,2), plot(period);
title('Walmart Image Period Plot');
xlabel('horizontal space');
ylabel('normalized period');
subplot(3,1,3), plot((period - o_period).^2);
title('Squared Error: original vs. walmart');
xlabel('horizontal space');
ylabel('squared error');
axis([0 300 0 .4]);

%**************************************************************************



%*** Analize Costco Image *************************************************
[height,width] = size(c_img)
scanline = round(height*3/4);

scanline = round(height*3/4);

zero_crossings = ones(width,1);
thresh = 90;

for i = 1:width-1
    if c_img(scanline,i+1) <= thresh & c_img(scanline,i) > thresh
        zero_crossings(i) = 0;
    end
end

last = 0;
x = 1;
for i = 1:width-1
    if zero_crossings(i) == 0
        if i - last > 3
            period(x) = i - last;
            last = i;
            x = x + 1;
        end
    end
end
x

period(1)=0;
period = period/max(period(:));

figure();
subplot(3,1,1), plot(o_period);
title('Original Image Period Plot');
xlabel('horizontal space');
ylabel('normalized period');
subplot(3,1,2), plot(period);
title('Costco Image Period Plot');
xlabel('horizontal space');
ylabel('normalized period');
subplot(3,1,3), plot((period - o_period).^2);
title('Squared Error: original vs. costco');
xlabel('horizontal space');
ylabel('squared error');
axis([0 300 0 .4]);

%**************************************************************************

