Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use help command in Matlab to learn more about the following commands. Use “figu

ID: 3940214 • Letter: U

Question

Use help command in Matlab to learn more about the following commands.
Use “figure” command to open up placeholder for a specific figure, otherwise, new figure will overwrite the old figure
Also use “subplot” command to display multiple figures/plots/images  
Label each figures by using command such as “title”, “xlabel”, and “ylabel”

1. Load image lighthouse.jpg and display % use matlab command imread a.

A = imgread(‘lighthouse.jpg’);

b. figure; c. Imshow(A);

2. Convert image to grayscale image and display % use matlab command rgb2gray

a. B=rgb2gray(A);

b. figure;

c. Imshow(B);

3. Find the size of the image  

% use matlab command size

a. [a,b]=size(B);

4. Change the contrast of the image and display

% raise each image pixel values by a power of 0.5, 1, 1.5

a. C = B.^0.5 % .^ is raising by 0.5 for each array value

b. D=B.^1

c. E=B.^1.5

d. F=B.^2

e. G=B.^4

f. Imshow(C)

g. Imshow(D)

h. Imshow(E)

i. Imshow(F)

j. Imshow(G)

5. Discuss what happened to the images

6. Write the images to a file such as jpg, tif, png % use matlab command imwrite

7. Other interesting image analysis is observing the histogram of the image. Since most of the grayscale image pixels are represented in 8 bits, ie. 256 levels, you can use imhist command with 256 bins observe statistics of the image. Also observe how histogram changes for different images obtained in step 4 above.

8. Create binary images i.e. black and white image based on thresholding

% use matlab command im2bw

H=im2bw(B, 0.5), try for different threshold

9. Reduce the size of the image by ½ (in x and y direction) and display

10. Reconstruct the original size by repeating the pixel values

Explanation / Answer


1 Load image lighthouse.jpg and display


im1 = imread('Lighthouse.jpg'); % READ IMAGE
imshow(im1); % SHOW IMAGE
size(im1); % SIZE BY [ROW COLUM COLORMATRIX]
% COLORMATRIX USUALLY 3 WHICH IS FOR
% RGB, AND SHOWS 3 MATRIXES OF
% [ROW COLUM] DIMENTIONS.

imr = im1(:,:,1); % SHOW TRUE COLOR IMAGING FOR RGB IN SUBPLOTS
img = im1(:,:,2);
imb = im1(:,:,3);
immr = mean(mean(imr));
immg = mean(mean(img));
immb = mean(mean(imb));
imsr = std(imr(:));
imsg = std(std(img));
imsb = std(std(imb));
subplot(3,3,1);
imshow(im1);
subplot(3,3,2);
imshow(immr);
subplot(3,3,3);
imshow(immg);
subplot(3,3,4);
imshow(immb);
subplot(3,3,5);

2


I = rgb2gray(RGB);
figure
imshow(I)

Read an indexed image with an RGB colormap. Then, convert the colormap to grayscale.

Read the sample file, chegg.tif, which is an indexed image with an RGB colormap.

[X,map] = imread('chegg.tif');

Display the image.

imshow(X,map)

3

a. [a,b]=size(B);

b=imread(image_file1);

[a,b] = size(b);


6


Write the images to a file such as jpg, tif, png % use matlab command imwrite

leyt us consider an array of grayscale values to a PNG file in the current folder.

png

A = rand(50);
imwrite(A,'myGray.png')


jpeg

array of random RGB values.

A = rand(49,49);
A(:,:,2) = rand(49,49);
A(:,:,3) = rand(49,49);


tif

Create two sets of random image data, im1 and im2.

im1 = rand(50,40,3);
im2 = rand(50,50,3);

Write the first image to a new TIFF file. Then, append the second image to the same file.

imwrite(im1,'myMultipageFile.tif')
imwrite(im2,'myMultipageFile.tif','WriteMode','append')

8

BW = im2bw(I, level) converts the grayscale image I to a binary image.
If you do not specify level, im2bw uses the value 0.5

load trees
BW = im2bw(X,map,0.4);
imshow(X,map), figure, imshow(BW)