Vous êtes sur la page 1sur 6

DIP Lab 3

Nishat Ahmed

24 September 2019
Question 1
Write a function that takes an image and flips it left and down using matrix
indexing (NOT pixel wise operation), and gives the resulting image as output.
Read the image ‘cameraman.tif’. Use it as input to the above function and
display both the original and resulting image.

Code
function [img_out] = Flip(img)
img1 = zeros(size(img));
img1(1:1:end,1:1:end) = img(end:-1:1,end:-1:1);
img_out = uint8(img1);
end

Output

Figure 1: Original vs. Flipped

1
Question 2
Read the image ‘cameraman.tif’. Prompt the user to enter threshold value for
conversion to black and white image. Convert the image to black and white
using the im2bw command and entered threshold value. Display the original
and converted image.

Code
img1 = imread(’cameraman.tif’);
th = input(’Enter Threshold Value(0-1): ’);
img_bw = im2bw(img1,th);

Output

Figure 2: Original vs. Converted

2
Question 3
Explore the imresize function. Resize an image to 3/7 or its original size and
display the original and resized images. Find whether the size of the new image
is exactly 3/7 of the original. Why or why not?

Code
img = imread(’cameraman.tif’);
img1 = imresize(img,(3/7));

Output

Figure 3: Original vs. Resized

Explanation
When we resize the original image it gives a value of (109.71 x 109.71), but pixel
cannot be in float. So the image size is 110x110. Thus resized image is not the
exact 3/7 of original image.

3
Question 4
Write a MATLAB function that takes an image (grayscale) and quantization
levels as input, converts the image gray levels according to the quantization
levels and returns the resulting image.

Code
function [Qimg] = Quantize(img,lvl)
Qimg = (255/lvl)*round(img*(lvl/255));
end

Question 5
Write a MATLAB m-file that reads the ‘cameraman.tif’ image and prompts the
user to enter whether they want to change the size or gray levels of the image.
Depending on the user input, ask for the factor for resampling or quantization
levels, and use this information, and Exercise 3 and 4 to compute the desired
result. Display the resulting images.

Code
img = imread(’cameraman.tif’);
choice = input(’Enter Choice 1-Sampling | 2-Quantization(0-8):’);
if choice==1
S_F = input(’Sampling Factor? :’);
out = imresize(img,S_F);
method = "Sampling"; %Just to help in plotting figures
value = S_F; %Represent name & val of choice
elseif choice == 2
Q_L = input(’Quantization Levels? :’);
out = Quantize(img,Q_L);
method = "Quantization"; %Just to help in plotting figures
value = Q_L; %Represent name & val of choice
else
fprint(’Enter Valid choice’);
end

4
Output Figures

(a) Quantization : Level = 1 (b) Quantization : Level = 6

(a) Sampling : Level = 0.756 (b) Sampling : Level = 0.35

Vous aimerez peut-être aussi