
function imageProcess(bitmap,threshold1,var1,threshold2,var2)
%Cornell University
%Spring 2010 ECE 4760 Final Project
%Image Scan using Phototransistor Array
%Yunchi Luo yl477 Mengliang Yu my288
%Copyright 2010
%
%This image process function will load a target image.
%It will first increase the contrast of the target image by calling a
%sub-function BW(bitmap,threshold1,var1). Threshold value is set by the
%user. Gray scale value in the target image bigger than the threshold will
%be set to 255 while gray scale value smaller than the thrshold will be
%further reduced by (20+Variation).Then it will enlarge the target
%image by calling the iir function written by Franciso de Castro. After
%that, it will deconvolve the target image by using MATLAB built-in
%function deconvblind. Finally, it will call BW again to further increase
%the contrast of the target image and display it.

%%convert initial value to uint8 for gray scale display
image = uint8(bitmap); 

%%show initial image
figure();  
imshow(image);

%%adjust contrast step 1
image = BW(image,threshold1,var1); 
%%image = BW(160,image,10);

%%enlarge the initial picture by using function iir
image1=iir(image,20,'Display','off'); 

%%convert from rgb to gray scale
figure();
image2=double(rgb2gray(image1));
%%adjust contrast 
image2=uint8(image2/max(max(image2))*255); 
imshow(image2); 

%%estimated PSF function
H = ones(15,15);
%%avoid ringing effect
t = edgetaper(image2,H);  
%%use matlab builtin function to do deconvolution
[t1 PSF1]=deconvblind(t,H,20 ); 
figure();
imshow(t1); 

%%image3 = iir(t1,2,'Display','off');
%%image3=double(rgb2gray(image3));
%%image4=uint8(image3/max(max(image3))*255);
%%figure();
%%imshow(image4);

%%adjust contrast step 2
t2 = BW(t1,threshold2,var2);
%%t2 = BW(140,t1,10);
end

function pic = BW(picture,threshold,var)
%%This function is used to increase the contrast of an image under given
%%parameters threshold and variation.

%%get image size
[h l]=size(picture);

%%convert to gray scale
temp = uint8(picture);

for i=1:h
    for j=1:l
        %%adjust contrast
        if (temp(i,j)<threshold)
            temp(i,j)=temp(i,j)-var-20;
        else
            temp(i,j)=255;
        end
    end
end

%%show image
figure();
imshow(temp);
pic = temp;
end