2016年2月29日星期一

Week 4 Reconstruction with Large Image with Dividing the Image

This week our group tried to focus on 3 aspects related to this project:

1. How to divide the large image, whose pixels exceed 1000*1000 approximately. For this part, our group use MATLAB to reads the image and get the data, then compressed the image to achieve the equal length and width. Then divide the image into small blocks which are 160*160, then for each block, we do the reconstruction, at last, we combined the image. In this process, we met some dilemma. 

2. The sampling rate ([0,1]) in reconstructed image. We tried to find the relationship between the sampling rate and the reconstructed quality. The relationship can be described on 2 following for a certain image:



  • If the number of masks is fixed, the higher sampling rate, the better reconstructed quality will be attained.
  • If the sampling rate is fixed, the higher number of masks (which does not exceed the pixels of the original image), the the better reconstructed quality will be attained.


3. How to compare the original image and the reconstructed image. We used the function called ssim(image, new_image), it would return a value which is between 0 and 1, if the value is nearer to 1, the reconstructed quality would be better.


First Part

For the first part, dealing with large image. Our group modify the reconstruction part into one function, linear_reconstruction(new_image), which pass the image as the parameter.  The flowchart of this week  is on the following:




Our group tried different ways to implement the reconstruction.

First situation result can be seen on the following:


   The part in red line was cut and reconstructed.  The image with title new image was the reconstructed part.

   When our group tried to do the reconstruction for one specific part for the first time, MATLAB returns the area with black color.



The program takes a very long time, we did not figure out what is wrong with the program. When we tried to reconstruct the whole image, it returns the image with all black.

The Failure of the reconstruction

Our group tried the divide, reconstruction and combine the small image, and successful.  Which means the algorithm of the divide has no problem.  





Original Image

New Image


The code is under the following:

1. The reconstruction function

function [ newimg ] = linear_reconstruction( o_ima )
%UNTITLED4 Summary of this function goes here
%   Detailed explanation goes here


[a,b] = size( o_ima );
if a>b
   c=b;
else c=a;
end


H=im2double(o_ima);
ima=imresize(H,[c c]);

NM = 1000;
NP=c;
MaskData=zeros(NM,NP*NP);


for i=1:NM                                               
    temp=rand(NP);
    temp=temp>0.5;
    MaskData(i,:)= temp(:);
end
   
    temp7=ima(:,:,1);
    THzData=double(MaskData)*double(temp7(:));
    newimg(:,:,1)=linear_rec(THzData, MaskData);
    temp7=ima(:,:,2);
    THzData=double(MaskData)*double(temp7(:));
    newimg(:,:,2)=linear_rec(THzData, MaskData);
    temp7=ima(:,:,3);
    THzData=double(MaskData)*double(temp7(:));
    newimg(:,:,3)=linear_rec(THzData, MaskData);

end

2. The main function code
[fn,pn,~]=uigetfile('*.jpg','Choose Image');
Img = imread([pn fn]);
figure(1),subplot(2,2,1),imagesc(Img), title('original image')
tic
[a,b] = size(Img);
 b=b/3;
 yue = 1;
 for i=1:1:a
     c = mod(a, i);
     d = mod(b, i);
     if c==0 && d==0
         yue = i;
     end
 end
   if yue>160
      yue=160;
  else yue;
    end

b=b/3;
c=yue;
c=(c-mod(c,yue));
H=im2double(Img);
Im=imresize(H,[c c]);


%imshow(Im)
hold on
L = size(Im);
height=yue;
width=yue;
max_row = floor(L(1) /height);  
max_col = floor(L(2) /width);
seg = cell(max_row,max_col);
%??
for row = 1:max_row   
    for col = 1:max_col        
    seg(row,col)= {Img((row-1)*height+1:row*height,(col-1)*width+1:col*width,:)};  
    end
end 


for i=1:max_row*max_col
    seg{i} = linear_reconstruction(seg{i});
    
end

for row = 1:max_row      
   for col = 1:max_col        
    new_image((row-1)*height+1:row*height,(col-1)*width+1:col*width,:) = seg{row,col} ;  
    end
end 


for i=1:max_row*max_col
imwrite(seg{i},strcat('m',int2str(i),'.bmp'));   %??i??????'mi.bmp'??
end



for row = 1:max_row       
    
    for col = 1:max_col
 rectangle('Position',[yue*(col-1),yue*(row-1),yue,yue],...
         'LineWidth',2,'LineStyle','-','EdgeColor','r');
    end
end
hold off

figure(1),subplot(2,2,2),imshow(new_image);title('new image');

Second Part

Our group changed the sampling rate in the reconstruction process. 
Here are examples of fixed number of masks, different sampling rate with the similarity between the original image and reconstructed image:

160*160 pixels with 0.2 sampling rate


The similarity is 0.7850.



160*160 pixels with 0.4 sampling rate

The similarity is 0.8594.


160*160 pixels with 0.6 sampling rate


The similarity is 0.9099.

160*160 pixels with 0.8 sampling rate


The similarity is 0.9583.


160*160 pixels with 1.0 sampling rate


The similarity is approximately 1. 

Under this situation, if number of masks is fixed, the higher sampling rate, the better reconstructed quality.



Under this situation, if sampling rate is fixed, the higher number of masks, the better reconstructed quality.


Third Part 

Our group used the function ssim(original_image, new_image) to compare the similarity. The principle of this image is on the following [1]:

 ssim Structural Similarity Index for measuring image quality

  •     SSIMVAL = ssim(A, REF) calculates the Structural Similarity Index

             (ssim) value for image A, with the image REF as the reference. A and
             REF can be 2D grayscale or 3D volume images, and must be of the same
             size and class. 
  

  •     [SSIMVAL, SSIMMAP] = ssim(A, REF) also returns the local ssim value for

              each pixel in SSIMMAP. SSIMMAP has the same size as A.


  •     [SSIMVAL, SSIMMAP] = ssim(A, REF, NAME1, VAL1,...) calculates the ssim

              value using name-value pairs to control aspects of the computation.
              Parameter names can be abbreviated.


    References:

    [1] Z. Wang, A. C. Bovik, H. R. Sheikh, and E. P. Simoncelli, "Image Quality Assessment: From Error Visibility to Structural Similarity," IEEE Transactions on Image Processing, Volume 13, Issue 4, pp. 600- 612, 2004.

2016年2月21日星期日

Week 3 Reconstruction of Real Image

This week we tried to reconstruct a real image with different colors. The function imread() was used. The process was to reconstruct the image directly.

1.Read the image and store the data of each point in a matrix.
2.Set the numbers of the mask pattern using the numbers of pixels, this has been proved that the quality of reconstruction is the best.
3.Obtain the THz data and combine them to do the linear reconstruction. 
4.Use a simple function to compare the reconstructed image with the original image. The principle of this function is to compare the pixels of 2 different images.

We meet some problem in reconstructing the image. They are listed on the following:

1.The image should have equal length and width, the original image should be "square", the number of the pixels should be n × n.
2.MATLAB has its own limit on the size of the matrix. If the image is too large, the work can not be done. 

The flowchart is on the following: 



The matlab code of this part is on the following:

% test linear_rec(THzData, MaskData)
clear all;

o_ima=imread('5.jpg');

figure(1),subplot(2,3,1),imagesc(o_ima), title('original image')
% simulate mask set data

[a,b] = size(o_ima);
if a>b
   c=b;
else c=a;
end


H=im2double(o_ima);
ima=imresize(H,[c c]);
[m,n,l]=size(ima);



NP=c;
figure(1),subplot(2,4,1),imagesc(ima), title('original image')
% simulate mask set data
NM=200; % nunmber of masks
MaskData=zeros(NM,c*c);
subplot(2,4,2),im1=imagesc(ima);title('mask pattern')
subplot(2,4,3),im2=imagesc(ima);title('R combined')
subplot(2,4,4),im3=imagesc(ima);title('G combined')
subplot(2,4,5),im4=imagesc(ima);title('B combined')

for i=1:NM                                               
    temp=rand(c); temp=temp>0.5;
    MaskData(i,:)= temp(:);
    temp1=reshape(temp,c,c);%mask pattern
    set(im1,'CData',temp1);
    temp2=temp.*double(ima(:,:,1));%combined patten
    set(im2,'CData',temp2);
    temp3=temp.*double(ima(:,:,2));
    set(im3,'CData',temp3);
    temp4=temp.*double(ima(:,:,3));
    set(im4,'CData',temp4);


end
    temp5(:,:,1)=temp2;
    temp5(:,:,2)=temp3;
    temp5(:,:,3)=temp4;
    temp6=double(temp5);
    subplot(2,4,6),imagesc(temp6),title('combined');
   
    temp7=ima(:,:,1);
    THzData=double(MaskData)*double(temp7(:));
    newimg(:,:,1)=linear_rec(THzData, MaskData);
    temp7=ima(:,:,2);
    THzData=double(MaskData)*double(temp7(:));
    newimg(:,:,2)=linear_rec(THzData, MaskData);
    temp7=ima(:,:,3);
    THzData=double(MaskData)*double(temp7(:));
    newimg(:,:,3)=linear_rec(THzData, MaskData);
  
  
figure(1);subplot(2,4,7),imshow(newimg,[]);simliarity( ima, newimg );% display the reconstructed image
title('Reconstruction');
similarity(ima,newimg);

The code of the compare function is on the following:
function simliarity( ima, newimg )
%UNTITLED4 Summary of this function goes here
%   Detailed explanation goes here
I1=ima;
I2=newimg;
ssimval = ssim(I1,I2);

  
    fprintf('The ssim value is %0.4f.\n',ssimval);
  
    title(sprintf('Reconstruction - simliarity is %0.4f',ssimval));

end

And the result is on the following:

It was obviously this 2 pictures was different. The direct reconstruction illustrates failure. 

From the blog of last year, we learnt that there is one way, which was to decoration the axises with different value of the image, then combine them. Which is that we take the Red color, Green color and blue color out and to obtain the THz data, combing them separately. The flowchart is on the following:


And the matlab code:

% test linear_rec(THzData, MaskData)
clear all;

o_ima=imread('5.jpg');

figure(1),subplot(2,3,1),imagesc(o_ima), title('original image')
% simulate mask set data

[a,b] = size(o_ima);
if a>b
   c=b;
else c=a;
end


H=im2double(o_ima);
ima=imresize(H,[c c]);
[m,n,l]=size(ima);



NP=c;
figure(1),subplot(2,4,1),imagesc(ima), title('original image')
% simulate mask set data
NM=200; % nunmber of masks
MaskData=zeros(NM,c*c);
subplot(2,4,2),im1=imagesc(ima);title('mask pattern')
subplot(2,4,3),im2=imagesc(ima);title('R combined')
subplot(2,4,4),im3=imagesc(ima);title('G combined')
subplot(2,4,5),im4=imagesc(ima);title('B combined')

for i=1:NM                                               
    temp=rand(c); temp=temp>0.5;
    MaskData(i,:)= temp(:);
    temp1=reshape(temp,c,c);%mask pattern
    set(im1,'CData',temp1);
    temp2=temp.*double(ima(:,:,1));%combined patten
    set(im2,'CData',temp2);
    temp3=temp.*double(ima(:,:,2));
    set(im3,'CData',temp3);
    temp4=temp.*double(ima(:,:,3));
    set(im4,'CData',temp4);


end
    temp5(:,:,1)=temp2;
    temp5(:,:,2)=temp3;
    temp5(:,:,3)=temp4;
    temp6=double(temp5);
    subplot(2,4,6),imagesc(temp6),title('combined');
   
    temp7=ima(:,:,1);
    THzData=double(MaskData)*double(temp7(:));
    newimg(:,:,1)=linear_rec(THzData, MaskData);
    temp7=ima(:,:,2);
    THzData=double(MaskData)*double(temp7(:));
    newimg(:,:,2)=linear_rec(THzData, MaskData);
    temp7=ima(:,:,3);
    THzData=double(MaskData)*double(temp7(:));
    newimg(:,:,3)=linear_rec(THzData, MaskData);
  
  
figure(1);subplot(2,4,7),imshow(newimg,[]);simliarity( ima, newimg );% display the reconstructed image
title('Reconstruction');
similarity(ima,newimg);



In next week, we will try some other solutions to deal with large images. This week we meet the dilemma here.








2016年2月15日星期一

Week 2 Common Image with Its Reconstruction

On week 2, our group researched on the effect of the numbers of the masks on the linear reconstructed image and RGB combine of a real colorful image. 

We used matlab to create a simple image containing 1600 pixels. The masks of the image increased from 100 to 1650 and the quality of the linear reconstruction varied with the masks increasing. The flowchart of this week's progress can be seen on the following:




Our group writes a function to pass the parameter of the mask numbers so the function can call the parameter to do different reconstruction of different mask patterns. When the mask number is less than the total pixels of the image, the quality of linear reconstructed image becomes better as the mask number increased. If the mask number exceeds the pixels, the linear reconstruction can not complete. 
The result can be seen on the following:

For the first part focusing on the reconstructed image and the pixels, the code can is attached on the following, it is divided into 2 parts, the main function and the combined function.

% test linear_rec(THzData, MaskData)
clear all;

% sample pattern
NP=40; % number of pixels
ima=zeros(NP);ima(:,1:3)=1; ima(:,14:16)=1; ima(:,24:26)=1; ima(:,38:40)=1; ima(1:3,:)=1;ima(14:16,:)=1;ima(24:26,:)=1; ima(38:40,:)=1; %establish a 32*32 zero matrix,sample pattern
figure(1),subplot(6,3,1),imagesc(ima), title('original image(NM)')
% simulate mask set data
NM=100; % nunmber of masks
x=1;
test(ima, NM, NP, x );

NM=600; % nunmber of masks
x=2;
 test(ima, NM, NP, x );
NM=1200; % nunmber of masks
x=3;
test(ima, NM, NP, x );
NM=1600; % nunmber of masks
x=4;
 test(ima, NM, NP, x );
NM=1650; % nunmber of masks
x=5;
test(ima, NM, NP, x );


Function

function test(ima, NM, NP, x ) %[ ima, imga, imgb, imgc ] = 
subplot(6,3,3*x+1),imga=imagesc(ima);title('mask pattern')
subplot(6,3,3*x+2),imgb=imagesc(ima);title('combined')
a=3*x+1;
b=3*x+2;
c=3*x+3;
MaskData=zeros(NM,NP*NP);

for i=1:NM
    temp=rand(NP); temp=temp>0.5;
    MaskData(i,:)= temp(:);
    %pause(0.1)
    tempa=reshape(temp,NP,NP);%mask pattern
    set(imga,'CData',tempa);
    tempb=tempa.*ima; %combined patten
    set(imgb,'CData',tempb);
end
THzData=MaskData*ima(:); % simulate terahertz measurement data
% reconstruction
newimg=linear_rec(THzData, MaskData); % call the reconstruciton function
% reifne and plot
newimg=squeeze(newimg);
newimg=newimg.*(newimg>0);
subplot(6,3,3*x+3),imgc=imagesc(newimg);title(['Reconstruction(NM=' , num2str(NM), ')']); 


end

Our group meet the problem this week. We tried to solve the RGB combination but meet some problem with the code. The problem of the result can be seen on the following:


Problem 1


Problem 2

The error window in matlab is on the following: