
function value=blockvalue_size4_quar(img,threshold,blocksize);
%img=imread(['C:\Documents and Settings\ruigangfang\My Documents\MATLAB\no reference verify\perfect image\im (',num2str(17),').bmp']);
warning('off','comm:obsolete:randint');
%threshold=20;
%blocksize=4;
testnum=10;
block=zeros(1,testnum);

[m,n,t]=size(img);
if t>1
    img=rgb2gray(img);
end

[height,width]=size(img);
img=img(5:height-4,5:width-4);
[height,width]=size(img);

blockheight=floor(height/blocksize);
blockwidth=floor(width/blocksize);
blocknum=blockheight*blockwidth;


for i=1:testnum
    %i
    x=img;
    x=double(x);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%add blockiness
    if i>1
        temp=floor(blocknum*i/100);
        coheight=randint(1,temp,[0,blockheight-1])*blocksize;%%random select 2*i blocks (in height)
        cowidth=randint(1,temp,[0,blockwidth-1])*blocksize;%%random select 2*i blocks (in width)
        
        for j=1:temp
            for p=1:blocksize
                for q=1:blocksize
                    x(coheight(j)+p,cowidth(j)+q)=128;
                end
            end
        end
    end
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    h_blockin_num=0;
    h_blockout_num=0;
    w_blockin_num=0;
    w_blockout_num=0;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for m=1:blockwidth-1
        for n=1:height
            if (x(n,blocksize*(m-1)+blocksize)-x(n,blocksize*(m-1)+blocksize-1))>threshold
                w_blockin_num=w_blockin_num+1;
            end
            if (x(n,blocksize*(m-1)+blocksize+1)-x(n,blocksize*(m-1)+blocksize))>threshold
                w_blockout_num=w_blockout_num+1;
            end
        end
    end

    for m=1:blockheight-1
        for n=1:width
            if (x(blocksize*(m-1)+blocksize,n)-x(blocksize*(m-1)+blocksize-1,n))>threshold
                h_blockin_num=h_blockin_num+1;
            end
            if (x(blocksize*(m-1)+blocksize+1,n)-x(blocksize*(m-1)+blocksize,n))>threshold
                h_blockout_num=h_blockout_num+1;
            end
        end
    end
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    block(i)=((1+h_blockout_num)/(1+h_blockin_num)+(1+w_blockout_num)/(1+w_blockin_num))/2;
    %block(i)=(1+h_blockout_num)/(1+h_blockin_num);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
x=[0:testnum-1];
y=block;
fun=@(beta,x)(beta(1)*(beta(3)+x).^2+beta(2)*(beta(3)+x)+1);
beta0=[-0.1 100 20];
beta=nlinfit(x,y,fun,beta0);
value=beta(3);

end

























