% histogram stretching script

f = imread('people.png');

%computer in the class
%[m,n,l] = size(f); fr = reshape(f,m*n*l,1); % reshape image for hist
%figure(1); hist(fr,[0:255]); % histogram of image. [0:255] indicates bin locations

figure(1); histogram(f); % histogram image

% same for both verions
figure(2); image(f); title('low contrast image');

f = double(f); % cast f as a double to allow fractions

mf = min(min(min(f))); Mf = max(max(max(f)));

%A = [mf 1; Mf 1]; % matrix of values
A = [mf 1; 30 1]; % manually adjust max value
b = [0; 255]; % create matrix for linear system y = c1*x + c2
c = A\b; % solve the system for c1 and c2
y = @(x) c(1)*x + c(2); % y = c1*x + c2, linear transformation
fp = y(f); % processed image. i.e. stretching histogram

figure(3);
subplot(1,2,1); histogram(f); title('OG image')
subplot(1,2,2); histogram(fp); title('Processed image')

fp_double = fp; fp = uint8(fp); f = uint8(f); % uint8 rounds values above 255

figure(4);
subplot(1,2,1); image(f); title('OG image')
subplot(1,2,2); image(fp); title('Processed image')

figure(5);
subplot(1,2,1); histogram(f,'BinLimits',[0 255]); title('OG image')
subplot(1,2,2); histogram(fp,'BinLimits',[0 255]); title('Processed image')




  



