Sie sind auf Seite 1von 19

DA NANG UNIVERSITY OF SCIENCE AND TECHNOLOGY

FACULTY OF ADVANCED SCIENCE AND TECHNOLOGY

EE 440
HOMEWORK 1
REPORT
Class: 15ECE-CE

Student: Vo Hoang Chuong

Le Quang Dao
Exercise 1 & 2
Fundamentals and Spatial Transformation
1. Read image with Matlab
a. Load the image lena512.bmp, using imread(), and show it using
imshow().
I = imread('lena512.bmp')
imshow(I)

b. Get the type of the loaded image data (Use MATLAB function class()),
and get the maximum and minimum data value for this image
I = imread('lena512.bmp')
a = max(max(I))
b = min(min(I,[],1))
ClassName = class(I)
Result:
a = 245
b = 25
ClassName = uint8
c. Convert the data to double type (use MATLAB function double()),
show the double-typed image using imshow()
I = imread('lena512.bmp')
double_I = double(I)
imshow(double_I)
The result is blank:
d. Is the result from c correct? If not, how do you think you can correct
it?
The result from c is not correct.
I = imread('lena512.bmp')
double_I = double(I)
imshow(uint8(double_I))

2. Color image and manipulation


a. Load and show the image LightHouse_color.png
I = imread('kodim19.png');
imshow(I);
b. Show three plans R, G, B of this image
I = imread('kodim19.png');
R = I(:,:,1);
G = I(:,:,2);
B = I(:,:,3);
figure;
subplot(1,3,1);imshow(R);
subplot(1,3,2);imshow(G);
subplot(1,3,3);imshow(B);
RED GREEN BLUE
c. Convert this color image to a grayscale image using rgb2gray. Display the
result
I = imread('kodim19.png');
Gray_I = rgb2gray(I);
imshow(Gray_I);
d. Crop and show a patch (subimage) from this color image (Hint: get a
submatrix using M(row1:row2, col1:col2))
I = imread('kodim19.png');
imshow(I(100:300,200:400,:));
e. Divide this color image into 16 equal blocks. Place the blocks in the
reverse order as below. Show the resulting color image

Before After

I = imread('kodim19.png');
[h,w,d] = size(I);
hi = h/4;wi = w/4;
new_I = 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;
new_I(x,y,1:d) = I(a ,b ,1:d);
end
end
figure;
subplot(1,2,1);imshow(I);
subplot(1,2,2);imshow(new_I);

Before After

3. Write an image
a. Reload the image LightHouse_color.png from exercise 2
RGB = imread('kodim19.png');

b. Exchange the plans R and G of this image, show the resulting image
[h,w,d] = size(RGB);
GRB = zeros(h,w,d,'uint8');
GRB(1:h,1:w,1) = RGB(1:h,1:w,2);
GRB(1:h,1:w,2) = RGB(1:h,1:w,1);
GRB(1:h,1:w,3) = RGB(1:h,1:w,3);
subplot(1,2,1);imshow(RGB);
subplot(1,2,2);imshow(GRB);
Before After

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(GRB,'GRBlighthouse.bmp');
4. Read a video with Matlab
a. Choose a video (for example an .avi file) from your computer or Internet
b. Using VideoReader to load this video
video = VideoReader('abc.mp4')

c. Show this video and report some frames


video = VideoReader('abc.mp4')
subplot(3,3,1);imshow(read(video,100));
subplot(3,3,2);imshow(read(video,200));
subplot(3,3,3);imshow(read(video,300));
subplot(3,3,4);imshow(read(video,400));
subplot(3,3,5);imshow(read(video,500));
subplot(3,3,6);imshow(read(video,600));
subplot(3,3,7);imshow(read(video,700));
subplot(3,3,8);imshow(read(video,800));
subplot(3,3,9);imshow(read(video,900));

5. Image rotation
a. Load a grayscale image
I = imread('lena512.bmp');
b. Use imrotate to rotate this image with 45°, and then with 90°
I45 = imrotate(I,45);
I90 = imrotate(I,90);
c. Show the original image and the rotated image in the same figure (using
subplot)
I = imread('lena512.bmp');
I45 = imrotate(I,45);
I90 = imrotate(I,90);
subplot(1,3,1);imshow(I);
subplot(1,3,2);imshow(I45);
subplot(1,3,3);imshow(I90);
e. 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.
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));

% Map all pixels of the transformed image to


the original image
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

The program to apply the function:


I = imread('lena512.bmp');
I1 = RotateFunction(I,45);
I2 = RotateFunction(I,90);
subplot(1,3,1);
imshow(I);title('Original');
subplot(1,3,2);
imshow(I1);title('45 Degrees');
subplot(1,3,3);
imshow(I2);title('90 Degrees');
6. Image downsampling
a. Load the grayscale image Lena
I = imread('lena512.bmp');
b. Use imresize to downsample this image by factor of 2 (in each
dimension). Compare the options nearest and bilinear.
I = imread('lena512.bmp');
I2 = imresize (I,0.5);
I2n = imresize (I,0.5,'nearest');
I2b = imresize (I,0.5,'bilinear');
subplot(2,2,1);imshow(I); title('Original');
subplot(2,2,2);imshow(I2); title('Resize by factor of
2');
subplot(2,2,3);imshow(I2n); title('Resize by factor
of 2 "nearest"');
subplot(2,2,4);imshow(I2b); title('Resize by factor
of 2 "bilinear"');
Analysis: The ‘bilinear’ result seems to be more decent than the ‘nearest’ result
because the ‘nearest’ result’s pixels are more recognizable.
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)
I = imread('lena512.bmp');
[row,column,color] = size(I);
k = 2;

Y = I(1:k:row - 1,1:k:column - 1,:);

subplot(1,2,1);imshow(I);
subplot(1,2,2);imshow(Y);
Before After

7. Quantization
a. Load an grayscale image.
I = imread('lena512.bmp');
b. Quantize this image if 6 bits are used to represent an intensity value.
Show the result
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.
I = imread('lena512.bmp');
Idouble = double(I)/255;
I6bits = uint8(round(Idouble*63)*floor(255/63));
I4bits = uint8(round(Idouble*15)*floor(255/15));
I1bit = uint8(round(Idouble*1)*floor(255/1));
subplot(2,2,1);imshow(I); title('Original');
subplot(2,2,2);imshow(I4bits); title('6-bit image');
subplot(2,2,3);imshow(I6bits); title('4-bit image');
subplot(2,2,4);imshow(I1bit); title('1-bit image');
8. Statistics
a. Load the grayscale image Lena
I = imread('lena512.bmp');
b. Find the maximum and minimum intensity values of the image
max = max(I(:))
min = min(I(:))
c. Find the mean, standard deviation and variance of the intensity values
Mean = mean(I(:))
STD = std(double(I(:)))
Var = var(double(I(:)))
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
I = imread('lena512.bmp');
for i = 0:63
for k = 0:63
block{i+1,k+1} = I(i*8+1 : (i+1)*8, k*8+1 :
(k+1)*8);
Y{i+1,k+1} = mean(block{i+1,k+1}(:));
end
end
Y = uint8(cell2mat(Y));
subplot(1,2,1);imshow(I);
subplot(1,2,2);imshow(Y);axis([-123 123 -123 123]);

Das könnte Ihnen auch gefallen