Vous êtes sur la page 1sur 16

# NAME : H XUN CNG

CLASS : 10ECCE
Exercise 1 & 2
Fundamentals and Spatial Transformation
a. Load the image lena512.bmp, using imread(), and show it using imshow().
imshow(im);

b. Get the type of the loaded image data (Use MATLAB function class()), and
get the maximum and minimum data value for this image
class(im)
ans =
uint8
max(im(:))
ans =
245

min(im(:))
ans =
25
c. Convert the data to double type (use MATLAB function double()), show the
double-typed image using imshow()
newim = double(im);
imshow(newim);

d. Is the result from c correct? If not, how do you think you can correct it?
The result from c is not correct. We can correct it in this way:
imshow(newim, [25 245]);

## 2. Color image and manipulation

a. Load and show the image LightHouse_color.png
imshow(im);

## b. Show three plans R, G, B of this image

R=im;
R(:,:,2:3)=0;
imshow(R);
title('Red plane')

G=im;
G(:,:,1)=0;
G(:,:,3)=0;
imshow(G);
title('Green plane');

imshow(im);
B=im;
B(:,:,1:2)=0;
imshow(B);
title('Blue plane');

c. Convert this color image to a grayscale image using rgb2gray. Display the
result
gray=rgb2gray(im);
imshow(gray);

d. Crop and show a patch (subimage) from this color image (Hint: get a
submatrix using M(row1:row2, col1:col2))
subim=im(100:400,100:400,:);
imshow(subim);

e. Divide this color image into 16 equal blocks. Place the blocks in the reverse
order as below. Show the resulting color image
% Get the dimensions of the image.
[row, col, numberOfColorBands] = size(im);
% Size of each block in rows.
rowDist = [row/4 * ones(1, 4)];
% Size of each block in columns.
colDist = [col/4 * ones(1, 4)];
% Divide the image into 16 blocks.
divided = mat2cell(im, rowDist, colDist, numberOfColorBands);
reversed = divided; %Prepare to reverse.
% Reverse the order.
for r = 1 : 4
for c = 1 : 4
reversed{r,c} = divided{5-r,5-c};
end
end
% Compare the original and resulting image.
Re = cell2mat(reversed);
subplot(121);
imshow(im); title('Original Image');
subplot(122);
imshow(Re); title('Resulting Image');

## The original image and the resulting color image:

3. Write an image
a. Reload the image LightHouse_color.png from exercise 2
b. Exchange the plans R and G of this image, show the resulting image
exchange=im;
exchange(:,:,1) = im(:,:,3);
exchange(:,:,3) = im(:,:,1);
imshow(exchange);
imwrite(exchange, 'exchangeImage.bmp');

c. Using imwrite to generate an image file for this new color image (for example
bmp file). Check on your computer whether it is correct.
d.
Here is the image from my computer, it is correct:

## 4. Read a video with Matlab

n = vid.NumberOfFrames;
frame = 1;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
title('Video frames display');
for i = 1:4;
subplot(2, 2, i);
index = randi(n);
while (index < frame | index == frame)
index = randi(n);
end
frame = index;
caption = sprintf('Frame #%d of %d Frames', ...
frame,n);
title(caption);
drawnow;
end

## Here are some frames:

5. Image rotation
b. Use imrotate to rotate this image with 45, and then with 90
c. Show the original image and the rotated image in the same figure (using
subplot)

im1 = imrotate(im,45);
im2 = imrotate(im,90);
subplot(131);
imshow(im);title('Original');
subplot(132);
imshow(im1);title('45 Degrees');
subplot(133);
imshow(im2);title('90 Degrees');

d. Write a program to carry out the rotation of an image, i.e. do not use the
function imrotate. Compare with the results from question b.
Here is the function carrying out the rotating:
function ImRotated = RotateFunction(im, degree);
switch mod(degree, 360)
% Special cases
case 0
ImRotated = im;
case 90
ImRotated = rot90(im);
case 180
ImRotated = im(end:-1:1, end:-1:1);
case 270
ImRotated = rot90(im(end:-1:1, end:-1:1));
% General rotations
otherwise
% Convert to radians and create transformation matrix
a = degree*pi/180;
R = [+cos(a) +sin(a); -sin(a) +cos(a)];
% Figure out the size of the transformed image
[m,n,p] = size(im);
dest = round( [1 1; 1 n; m 1; m n]*R );
dest = bsxfun(@minus, dest, min(dest)) + 1;
ImRotated = zeros([max(dest) p],class(im));

image

## % Map all pixels of the transformed image to the original

for i = 1:size(ImRotated,1)
for j = 1:size(ImRotated,2)
source = ([i j]-dest(1,:))*R.';
if all(source >= 1) && all(source <= [m n])
% Get all 4 surrounding pixels
C = ceil(source);
F = floor(source);
% Compute the relative areas
A = [...
((C(2)-source(2))*(C(1)-source(1))),...
((source(2)-F(2))*(source(1)-F(1)));
((C(2)-source(2))*(source(1)-F(1))),...
((source(2)-F(2))*(C(1)-source(1)))];
% Extract colors and re-scale them relative

to area

cols = bsxfun(@times, A,
double(im(F(1):C(1),F(2):C(2),:)));
% Assign
ImRotated(i,j,:) = sum(sum(cols),2);
end
end

end

end

## And use it to rotate image:

im1 = RotateFunction(im,45);
im2 = RotateFunction(im,90);
subplot(131);
imshow(im);title('Original');
subplot(132);
imshow(im1);title('45 Degrees');
subplot(133);
imshow(im2);title('90 Degrees');

The figure below is the result of RotateFunction. As we see, this result is the same as the
result from question b.

6. Image downsampling
a. Load the grayscale image Lena
b. Use imresize to downsample this image by factor of 2 (in each dimension).
Compare the options nearest and bilinear.
N = imresize(im,0.5,'nearest');
B = imresize(im,0.5,'bilinear');
subplot(121);
imshow(N);title('Nearest');
subplot(122);
imshow(B);title('Bilinear');

With Nearest-neighbor interpolation, the output pixel is assigned the value of the pixel that
the point falls within. No other pixels are considered. Meanwhile with Bilinear interpolation,
the output pixel value is a weighted average of pixels in the nearest 2-by-2 neighborhood.
This explains why the image with Bilinear interpolation seem to be smoother
c. Write a simple program to downsample the Lena image by factor of 2, i.e. do
not use imresize. Generalize for factor of k. (Hint: in this question we omit a
lowpass filtering step before downsampling)

.
A simple program to downsample the Lena image by factor of 2:
oldsize=size(im);
newsize=ceil(oldsize./2);
rowindex =(((1:newsize(1))-1).*2)+1;
colindex =(((1:newsize(2))-1).*2)+1;
newim = im(rowindex, colindex, :);
subplot(121);
imshow(im);title('Original');
subplot(122);
imshow(newim);title('Downsampling');

## Generalize for factor of k:

function newim = downsampling(im, k);
oldsize=size(im);
newsize=ceil(oldsize./k);
rowindex =(((1:newsize(1))-1).*k)+1;
colindex =(((1:newsize(2))-1).*k)+1;
newim = im(rowindex, colindex, :);
subplot(121);
imshow(im);title('Original');
subplot(122);
imshow(newim);title('Downsampling');

7. Quantization
b. Quantize this image if 6 bits are used to represent an intensity value. Show the
result

quantizedIm=im;
quantizedIm= floor(quantizedIm/2^3)*2^3;

subplot(121);
imshow(im);title('Original');
subplot(122);
imshow(quantizedIm);title('Quantized Image if 6bits are used');

c. Repeat question b with 4 bits and 1 bit. Show the original image and all the
obtained results (from questions b and c) in the same figure.
quantizedIm6=im;
quantizedIm4=im;
quantizedIm1=im;
quantizedIm6= floor(quantizedIm6/2^3)*2^3;
quantizedIm4= floor(quantizedIm4/2^5)*2^5;
quantizedIm1= floor(quantizedIm1/2^8)*2^8;
subplot(221);
imshow(im);title('Original');
subplot(222);
imshow(quantizedIm6);title('Quantized Image if 6bits are used');
subplot(223);
imshow(quantizedIm4);title('Quantized Image if 4bits are used');
subplot(224);
imshow(quantizedIm1);title('Quantized Image if 1bits are used');

8. Statistics
a. Load the grayscale image Lena
b. Find the maximum and minimum intensity values of the image
im = lena;
max = max(im(:))
min = min(im(:))

max =
245
min =
25
c. Find the mean, standard deviation and variance of the intensity values
im_double = double(lena);
% get number of rows and columns value
[r,c] = size(lena);
% mean of gray scale image
mean = sum(im_double(:))/(r*c)
% variance of gray scale image
% Compute E{x^2}
SquareValue = im_double.^2;
meanOfSquare = sum(SquareValue(:))/(r*c);
% Compute [E{x}]^2
SquareOfMean = mean^2;
% Var{x} = E{x^2} - [E{x}]^2
variance = meanOfSquare - SquareOfMean

mean =
124.0505
variance =
2.2900e+003
d. Divide the image into non-overlapping blocks of 8 x 8 pixels. The mean of the
intensity values of each block is then used to represent this block, i.e. each
block becomes one pixel. Show this resulting image (with smaller size) and
the original one in the same figure
[rows, cols] = size(im);
numBlocksR = rows/8;
numBlocksC = cols/8;
% Size of each block in rows.
blockVectorR = [8 * ones(1, numBlocksR)];
% Size of each block in columns.
blockVectorC = [8 * ones(1, numBlocksC)];
% Divide the image into 16 blocks.
divided = mat2cell(im, blockVectorR, blockVectorC);
newim = ones(64); %Prepare to create new image.
for r = 1 : numBlocksR
for c = 1 : numBlocksC
newim(r,c) = mean(mean(divided{r,c}));
end
end
subplot(1,2,1);
imshow(im);
title('Original Image');
subplot(1,2,2);
imshow(uint8(newim));
title('Resulting Image');