Exercise 1 2 Funda Spatial

Embed Size (px)

Citation preview

REport - EE440

Dang Tran Chi Toan 10ECE DUTReport EE440

1. Read image with Matlab[footnoteRef:1] [1: The students are required to submit the Matlab codes for these exercises.]

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

2. Color image and manipulationa. Load and show the image LightHouse_color.pnglighthouse = imread('kodim19.png');imshow(lighthouse);

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

REDBLUEGREEN

c. Convert this color image to a grayscale image using rgb2gray. Display the resultGray_lighthouse = rgb2gray(lighthouse);figure;imshow(Gray_lighthouse);

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,:));

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

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); endendfigure;subplot(121);imshow(lighthouse);subplot(122);imshow(newlighthouse);

3. Write an imagea. 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[footnoteRef:2] [2: For bonus points]

a. Choose a video (for example an .avi file) from your computer or Internetb. 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.

c. Show this video and report some framessubplot(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 rotationa. Load a grayscale image image = imread(lena512.bmp);

b. Use imrotate to rotate this image with 45, and then with 90image45 = 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);

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);%midpointsmidx=ceil((size(imagepad,1)+1)/2);midy=ceil((size(imagepad,2)+1)/2); imagerot=zeros(size(imagepad)); %rotationfor 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