Sei sulla pagina 1di 15

REPORT - EE440

EXERCISE 1 & 2
Fundamentals and Spatial Transformation



Dang Tran Chi Toan
Chitoan.1992@gmail.com

Dang Tran Chi Toan 10ECE DUT Report EE440
1. Read image with Matlab
1

a. Load the image lena512.bmp, using imread(), and show it using imshow().
lena512 = imread('lena512.bmp');
imshow(lena512);



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(lena512) % uint8
max(max(lena512)) % 245
min(min(lena512,[],1)) % 25

c. Convert the data to double type (use MATLAB function double()), show the
double-typed image using imshow()
D_lena512 = double(lena512);
figure;
imshow(D_lena512);

d. Is the result from c correct? If not, how do you think you can correct it?
D_lena512 = double(lena512);
figure;
imshow(D_lena512,[]);




1
The students are required to submit the Matlab codes for these exercises.
Dang Tran Chi Toan 10ECE DUT Report EE440
2. Color image and manipulation
a. Load and show the image LightHouse_color.png
lighthouse = imread('kodim19.png');
imshow(lighthouse);



b. Show three plans R, G, B of this image
R = lighthouse(:,:,1);
B = lighthouse(:,:,2);
G = lighthouse(:,:,3);
figure;
subplot(141);imshow(R);
subplot(142);imshow(B);
subplot(143);imshow(G);


RED BLUE GREEN

c. Convert this color image to a grayscale image using rgb2gray. Display the
result
Gray_lighthouse = rgb2gray(lighthouse);
figure;
imshow(Gray_lighthouse);
Dang Tran Chi Toan 10ECE DUT Report EE440



d. Crop and show a patch (subimage) from this color image (Hint: get a submatrix
using M(row1:row2, col1:col2))

imshow(lighthouse(3:200,3:200,:));



Dang Tran Chi Toan 10ECE DUT Report EE440

e. Divide this color image into 16 equal blocks. Place the blocks in the reverse
order as below. Show the resulting color image



h = size(lighthouse,1);
w = size(lighthouse,2);
d = size(lighthouse,3);
hi = h/4;wi = w/4;
newlighthouse = zeros (h,w,d,'uint8');
for i = 1:4
for k = 1:4
x = ((i-1)*hi+1):i*hi;
y = ((k-1)*wi+1):k*wi;
a = ((4-i)*hi+1):(5-i)*hi;
b = ((4-k)*wi+1):(5-k)*wi;
newlighthouse(x,y,1:d) = lighthouse(a ,b ,1:d);
end
end
figure;
subplot(121);imshow(lighthouse);
subplot(122);imshow(newlighthouse);



Before After
Dang Tran Chi Toan 10ECE DUT Report EE440

Dang Tran Chi Toan 10ECE DUT Report EE440
3. Write an image
a. Reload the image LightHouse_color.png from exercise 2

RGBlighthouse = imread('kodim19.png');

b. Exchange the plans R and G of this image, show the resulting image

h = size(RGBlighthouse,1);
w = size(RGBlighthouse,2);
d = size(RGBlighthouse,3);
GRBlighthouse = zeros(h,w,d,'uint8');
GRBlighthouse(1:h,1:w,1) = RGBlighthouse(1:h,1:w,2);
GRBlighthouse(1:h,1:w,2) = RGBlighthouse(1:h,1:w,1);
GRBlighthouse(1:h,1:w,3) = RGBlighthouse(1:h,1:w,3);
subplot(121);imshow(RGBlighthouse);
subplot(122);imshow(GRBlighthouse);



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.
imwrite(GRBlighthouse,'GRBlighthouse.bmp');

4. Read a video with Matlab
2

a. Choose a video (for example an .avi file) from your computer or Internet
b. Using VideoReader to load this video

video =
VideoReader('Monsters.University.2013.720p.BluRay.x264.YIFY.mp4')
% Summary of Multimedia Reader Object for
'Monsters.University.2013.720p.BluRay.x264.YIFY.mp4'.
% Video Parameters: 23.98 frames per second, RGB24 1280x720.
% 149314 total video frames available.

2
For bonus points
Dang Tran Chi Toan 10ECE DUT Report EE440

Dang Tran Chi Toan 10ECE DUT Report EE440
c. Show this video and report some frames
subplot(331);imshow(read(video,100));
subplot(332);imshow(read(video,200));
subplot(333);imshow(read(video,300));
subplot(334);imshow(read(video,400));
subplot(335);imshow(read(video,500));
subplot(336);imshow(read(video,600));
subplot(337);imshow(read(video,700));
subplot(338);imshow(read(video,800));
subplot(339);imshow(read(video,900));


5. Image rotation
a. Load a grayscale image
image = imread(lena512.bmp);
b. Use imrotate to rotate this image with 45, and then with 90
image45 = imrotate(image,45);
image90 = imrotate(image,90);
c. Show the original image and the rotated image in the same figure (using
subplot)
subplot(131);imshow(image);
subplot(132);imshow(image45);
subplot(133);imshow(image90);

Dang Tran Chi Toan 10ECE DUT Report EE440


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.
image=imread(lena512.bmp);

%image padding
[Rows, Cols, Dim] = size(image);
Diagonal = sqrt(Rows^2 + Cols^2);
RowPad = ceil(Diagonal Rows) + 2;
ColPad = ceil(Diagonal Cols) + 2;
imagepad = zeros(Rows+RowPad, Cols+ColPad);
imagepad(ceil(RowPad/2)ceil(RowPad/2)+Rows-1),ceil(ColPad/2)
ceil(ColPad/2)+Cols-1)) = image;

degree=45;
rads = pi*(degree/180);
%midpoints
midx=ceil((size(imagepad,1)+1)/2);
midy=ceil((size(imagepad,2)+1)/2);

imagerot=zeros(size(imagepad));

%rotation
for i=1:size(imagerot,1)
for j=1:size(imagerot,2)
x= (i-midx)*cos(rads)+(j-midy)*sin(rads);
y=-(i-midx)*sin(rads)+(j-midy)*cos(rads);
x=round(x)+midx;
y=round(y)+midy;

if (x>=1 && y>=1 && x<=size(imagepad,2) && y<=size(imagepad,1))
imagerot(I,j)=imagepad(x,y); % k degrees rotated image
end

end
end

figure,imagesc(imagerot);
colormap(gray(256));



Dang Tran Chi Toan 10ECE DUT Report EE440

Dang Tran Chi Toan 10ECE DUT Report EE440
6. Image downsampling
a. Load the grayscale image Lena
image = imread('lena512.bmp');

b. Use imresize to downsample this image by factor of 2 (in each dimension).
Compare the options nearest and bilinear.

image = imread('lena512.bmp');
% b)
image2 = imresize (image,0.5);
image2n = imresize (image,0.5,'nearest');
image2b = imresize (image,0.5,'bilinear');
figure;
subplot(141);imshow(image);
subplot(142);imshow(image2);
subplot(143);imshow(image2n);
subplot(144);imshow(image2b);
The image with option bilinear is more blurred then the one with nearest option.

Original Resize by factor of 2
Resize by factor of 2
nearest
Resize by factor of 2
'bilinear'
Dang Tran Chi Toan 10ECE DUT Report EE440


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)
k = 2;
arr =[];
for i=1:k:size(image,2)
for j=1:k:size(image,1)
arr = [arr image(i,j)];
end
end
newimage =
reshape(arr,ceil(size(image,2)/k),ceil(size(image,1)/k))';
figure,imshow(image);
figure,imshow(newimage);



7. Quantization
a. Load an grayscale image
image = imread('lena512.bmp');

b. Quantize this image if 6 bits are used to represent an intensity value. Show the
result
d = double(image)/255;
i6bits = uint8(round(d*63)*floor(255/63));
i4bits = uint8(round(d*15)*floor(255/15));
i1bit = uint8(round(d*1)*floor(255/1));
subplot(2,2,1);imshow(image);
subplot(2,2,2);imshow(i4bits);
subplot(2,2,3);imshow(i6bits);
subplot(2,2,4);imshow(i1bit);
Dang Tran Chi Toan 10ECE DUT Report EE440



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.




Original
4 bits image
1 bits image 6 bits image
Original 4 bits image
Dang Tran Chi Toan 10ECE DUT Report EE440

8. Statistics
a. Load the grayscale image Lena
image = imread('lena512.bmp');

b. Find the maximum and minimum intensity values of the image
max = max(image(:)) % max = 245
min = min(image(:)) % min = 25

c. Find the mean, standard deviation and variance of the intensity values
Mean = mean(image(:)) % Mean = 124.0505
STD = std(double(image(:))) % STD = 47.8538
Var = var(double(image(:))) % Var = 2.2900e+03

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

image = imread('lena512.bmp');
for i = 0:63
for k = 0:63
block{i+1,k+1} = image(i*8+1 : (i+1)*8, k*8+1 :
(k+1)*8);
newimage{i+1,k+1} = mean(block{i+1,k+1}(:));
end
end
newimage = uint8(cell2mat(newimage));
subplot(1,2,1);imshow(image);
subplot(1,2,2);imshow(newimage);axis([-123 123 -123 123]);

Potrebbero piacerti anche