0% found this document useful (0 votes)
3 views9 pages

Assignment2 Solution Report

This report details the MATLAB solutions for an image-processing assignment involving grayscale and color images. It includes four scripts (q1.m, q2.m, q3.m, q4.m) that perform various operations such as reading, writing, intensity transformations, channel separations, and conversions to grayscale. The report outlines the purpose of each script and provides a brief description of the code used.

Uploaded by

f2023266796
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Assignment2 Solution Report

This report details the MATLAB solutions for an image-processing assignment involving grayscale and color images. It includes four scripts (q1.m, q2.m, q3.m, q4.m) that perform various operations such as reading, writing, intensity transformations, channel separations, and conversions to grayscale. The report outlines the purpose of each script and provides a brief description of the code used.

Uploaded by

f2023266796
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment 2 Solution Report

cc371: Artificial Intelligence


SST, UMT
Prepared MATLAB solution for the given PGM and PPM image-processing assignment

Student Name Bisma Pari Memon

Student ID __________________

Assignment Assignment 2

Discussion Partner None / __________________

Prepared Files q1.m, q2.m, q3.m, q4.m, output images

This report provides a complete MATLAB solution for the assignment. The code reads grayscale and color
images, performs intensity and logarithm transformations, separates RGB channels, generates grayscale
versions of color images, and saves all required outputs in the same folder.

Assignment 2 Solution Report


Overview
The solution is divided into four MATLAB files exactly according to the assignment requirements: q1.m for
basic grayscale read/write, q2.m for grayscale processing, q3.m for color read/write, and q4.m for color image
processing.

File Purpose Main Outputs

Read [Link] and [Link]


q1.m owl_Out.pgm, mecca06_Out.pgm
and write them with new names

Perform grayscale intensity Intensity outputs, log outputs, no-


q2.m
changes and log transform loop outputs

Read [Link] and [Link]


q3.m owl_Out.ppm, mecca06_Out.ppm
into R, G and B matrices

Perform RGB channel operations, Red/Green/Blue outputs,


q4.m log transform and grayscale combined channels, grayscale
conversion outputs

Question 1
Read the grayscale files [Link] and [Link] and save them as owl_Out.pgm and mecca06_Out.pgm.

Description of code
The script reads both grayscale images using imread and immediately writes them back using imwrite with the
exact output names requested in the assignment.

MATLAB code (q1.m)


clc;
clear;
close all;

% Q1 - Read the grayscale images and write them with new names.

if isempty(mfilename('fullpath'))
scriptDir = pwd;
else
scriptDir = fileparts(mfilename('fullpath'));
end

m1 = imread(fullfile(scriptDir, '[Link]'));
m2 = imread(fullfile(scriptDir, '[Link]'));

imwrite(m1, fullfile(scriptDir, 'owl_Out.pgm'));


imwrite(m2, fullfile(scriptDir, 'mecca06_Out.pgm'));

disp('Q1 completed successfully.');

Assignment 2 Solution Report


Question 2
Perform grayscale processing using loops and also repeat part of the work without loops.

Description of code
The script first reads [Link] and [Link] into matrices. Then it adds intensity values using for loops
with clipping at 255, applies a base-10 logarithm transform, creates versions with +20, +40, +60, +80 and +100
intensity, and finally repeats the +50 and log steps without loops using vectorized MATLAB statements.

Input and output image summary

Assignment 2 Solution Report


MATLAB code (q2.m)
clc;
clear;
close all;

% Q2 - Grayscale image processing.

if isempty(mfilename('fullpath'))
scriptDir = pwd;
else
scriptDir = fileparts(mfilename('fullpath'));
end

% Part (a): read the images in matrices.


m1 = imread(fullfile(scriptDir, '[Link]'));
m2 = imread(fullfile(scriptDir, '[Link]'));

% Part (b): add 50 using for loop with clipping at 255.


m1_inc50_loop = addIntensityLoop(m1, 50);
m2_inc50_loop = addIntensityLoop(m2, 50);

imwrite(m1_inc50_loop, fullfile(scriptDir, 'owl_IntensityInc50_Out.pgm'));


imwrite(m2_inc50_loop, fullfile(scriptDir, 'mecca06_IntensityInc50_Out.pgm'));

% Part (c): base-10 logarithm of each pixel, then normalize to save.


m1_log_loop = logTransformImage(m1);
m2_log_loop = logTransformImage(m2);

imwrite(m1_log_loop, fullfile(scriptDir, 'owl_Log_Out.pgm'));


imwrite(m2_log_loop, fullfile(scriptDir, 'mecca06_Log_Out.pgm'));

% Part (d): add 20, 40, 60, 80 and 100 using for loop.

Assignment 2 Solution Report


levels = [20 40 60 80 100];

for k = 1:length(levels)
value = levels(k);

owlOut = addIntensityLoop(m1, value);


meccaOut = addIntensityLoop(m2, value);

imwrite(owlOut, fullfile(scriptDir, sprintf('owl_Intensity%d_Out.pgm', value)));

% The assignment uses both mecca and mecca06 in different places.


% To stay safe, both versions are saved.
imwrite(meccaOut, fullfile(scriptDir, sprintf('mecca_Intensity%d_Out.pgm', value)));
imwrite(meccaOut, fullfile(scriptDir, sprintf('mecca06_Intensity%d_Out.pgm', value)));
end

% Part (e): repeat part (b) and part (c) without using for loop.
m1_inc50_noloop = addIntensityNoLoop(m1, 50);
m2_inc50_noloop = addIntensityNoLoop(m2, 50);

m1_log_noloop = logTransformImage(m1);
m2_log_noloop = logTransformImage(m2);

imwrite(m1_inc50_noloop, fullfile(scriptDir, 'owl_IntensityInc50_NoLoop_Out.pgm'));


imwrite(m2_inc50_noloop, fullfile(scriptDir, 'mecca06_IntensityInc50_NoLoop_Out.pgm'));

imwrite(m1_log_noloop, fullfile(scriptDir, 'owl_Log_NoLoop_Out.pgm'));


imwrite(m2_log_noloop, fullfile(scriptDir, 'mecca06_Log_NoLoop_Out.pgm'));

disp('Q2 completed successfully.');

function outImage = addIntensityLoop(inImage, value)


outImage = uint8(zeros(size(inImage)));

for i = 1:size(inImage, 1)
for j = 1:size(inImage, 2)
newValue = double(inImage(i, j)) + value;
if newValue > 255
newValue = 255;
end
outImage(i, j) = uint8(newValue);
end
end
end

function outImage = addIntensityNoLoop(inImage, value)


outImage = uint8(min(double(inImage) + value, 255));
end

function outImage = logTransformImage(inImage)


logImage = log10(double(inImage) + 1);
maxValue = max(logImage(:));

if maxValue == 0
outImage = uint8(logImage);
else
outImage = uint8((logImage ./ maxValue) * 255);
end
end

Assignment 2 Solution Report


Question 3
Read [Link] and [Link] into separate red, green and blue matrices and save them back as
owl_Out.ppm and mecca06_Out.ppm.

Description of code
The script reads each color image, extracts the red, green and blue layers using indexing, and reconstructs the
original image by combining the three matrices back into a single RGB image.

MATLAB code (q3.m)


clc;
clear;
close all;

% Q3 - Read the color images into R, G and B matrices and write them again.

if isempty(mfilename('fullpath'))
scriptDir = pwd;
else
scriptDir = fileparts(mfilename('fullpath'));
end

img1 = imread(fullfile(scriptDir, '[Link]'));


img2 = imread(fullfile(scriptDir, '[Link]'));

mR1 = img1(:, :, 1);


mG1 = img1(:, :, 2);
mB1 = img1(:, :, 3);

mR2 = img2(:, :, 1);


mG2 = img2(:, :, 2);
mB2 = img2(:, :, 3);

owlOut = cat(3, mR1, mG1, mB1);


meccaOut = cat(3, mR2, mG2, mB2);

imwrite(owlOut, fullfile(scriptDir, 'owl_Out.ppm'));


imwrite(meccaOut, fullfile(scriptDir, 'mecca06_Out.ppm'));

disp('Q3 completed successfully.');

Question 4
Perform color channel manipulations, log transform and grayscale conversion on [Link] and [Link].

Description of code
The script creates red-only, green-only and blue-only versions of both color images. It also creates Green+Blue,
Red+Blue and Red+Green combinations by setting one channel to zero. After that, it applies a log transform to
all three channels and finally converts each RGB image to grayscale by averaging the red, green and blue pixel
values.

Assignment 2 Solution Report


Input and output image summary

Assignment 2 Solution Report


MATLAB code (q4.m)
clc;
clear;
close all;

% Q4 - Color image processing.

if isempty(mfilename('fullpath'))
scriptDir = pwd;
else
scriptDir = fileparts(mfilename('fullpath'));
end

% Part (a): read the color images into channel matrices.


img1 = imread(fullfile(scriptDir, '[Link]'));
img2 = imread(fullfile(scriptDir, '[Link]'));

mR1 = img1(:, :, 1);


mG1 = img1(:, :, 2);
mB1 = img1(:, :, 3);

mR2 = img2(:, :, 1);


mG2 = img2(:, :, 2);
mB2 = img2(:, :, 3);

zero1 = uint8(zeros(size(mR1)));
zero2 = uint8(zeros(size(mR2)));

% Part (b): keep only Red channel.


owlRed = cat(3, mR1, zero1, zero1);
meccaRed = cat(3, mR2, zero2, zero2);
imwrite(owlRed, fullfile(scriptDir, 'owl_Red_out.ppm'));
imwrite(meccaRed, fullfile(scriptDir, 'mecca06_Red_out.ppm'));

% Part (c): keep only Green channel.


owlGreen = cat(3, zero1, mG1, zero1);
meccaGreen = cat(3, zero2, mG2, zero2);
imwrite(owlGreen, fullfile(scriptDir, 'owl_Green_out.ppm'));
imwrite(meccaGreen, fullfile(scriptDir, 'mecca06_Green_out.ppm'));

% Part (d): keep only Blue channel.


owlBlue = cat(3, zero1, zero1, mB1);
meccaBlue = cat(3, zero2, zero2, mB2);
imwrite(owlBlue, fullfile(scriptDir, 'owl_Blue_out.ppm'));
imwrite(meccaBlue, fullfile(scriptDir, 'mecca06_Blue_out.ppm'));

% Part (e): make Red channel zero -> Green + Blue.


owlGreenBlue = cat(3, zero1, mG1, mB1);
meccaGreenBlue = cat(3, zero2, mG2, mB2);
imwrite(owlGreenBlue, fullfile(scriptDir, 'owl_GreenBlue_out.ppm'));
imwrite(meccaGreenBlue, fullfile(scriptDir, 'mecca06_GreenBlue_out.ppm'));

% Part (f): make Green channel zero -> Red + Blue.


owlRedBlue = cat(3, mR1, zero1, mB1);
meccaRedBlue = cat(3, mR2, zero2, mB2);
imwrite(owlRedBlue, fullfile(scriptDir, 'owl_RedBlue_out.ppm'));
imwrite(meccaRedBlue, fullfile(scriptDir, 'mecca06_RedBlue_out.ppm'));

% Part (g): make Blue channel zero -> Red + Green.


owlRedGreen = cat(3, mR1, mG1, zero1);
meccaRedGreen = cat(3, mR2, mG2, zero2);

Assignment 2 Solution Report


imwrite(owlRedGreen, fullfile(scriptDir, 'owl_RedGreen_out.ppm'));
imwrite(meccaRedGreen, fullfile(scriptDir, 'mecca06_RedGreen_out.ppm'));

% Part (h): base-10 logarithm of each RGB channel.


owlLog = logTransformColor(img1);
meccaLog = logTransformColor(img2);
imwrite(owlLog, fullfile(scriptDir, 'owl_Log_Out.ppm'));
imwrite(meccaLog, fullfile(scriptDir, 'mecca06_Log_Out.ppm'));

% Part (i): convert to grayscale by average of R, G and B channels.


owlGray = uint8((double(mR1) + double(mG1) + double(mB1)) ./ 3);
meccaGray = uint8((double(mR2) + double(mG2) + double(mB2)) ./ 3);
imwrite(owlGray, fullfile(scriptDir, 'owl_grayScale_Out.pgm'));
imwrite(meccaGray, fullfile(scriptDir, 'mecca06_grayScale_Out.pgm'));

disp('Q4 completed successfully.');

function outImage = logTransformColor(inImage)


logImage = log10(double(inImage) + 1);
maxValue = max(logImage(:));

if maxValue == 0
outImage = uint8(logImage);
else
outImage = uint8((logImage ./ maxValue) * 255);
end
end

Submission notes
 Keep the four MATLAB files and the four original input images in the same folder.
 Run q1, q2, q3 and q4 separately in MATLAB.
 The output files will be created in the same folder.
 If your teacher requires your own student ID in the folder name, rename the folder before submission.

Assignment 2 Solution Report

You might also like