0% found this document useful (0 votes)
10 views

Image Processing in MATLAB Part 2

This document describes various image processing techniques in MATLAB, including taking the complement of an image, converting an image to binary, changing the binary threshold, applying logarithmic, exponential, and power law transformations, adding and removing salt and pepper noise using mean and median filters. Code examples are provided for each technique.

Uploaded by

MUHAMMAD ARSLAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Image Processing in MATLAB Part 2

This document describes various image processing techniques in MATLAB, including taking the complement of an image, converting an image to binary, changing the binary threshold, applying logarithmic, exponential, and power law transformations, adding and removing salt and pepper noise using mean and median filters. Code examples are provided for each technique.

Uploaded by

MUHAMMAD ARSLAN
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment

Strategies for Enhancing Visual Impact, Distortion Minimization, and Image Enhancement

1. Objective:
The objective of this lab report is to provide with a foundational understanding of image processing in
MATLAB, focusing on enhancing visual quality, reducing distortions, and applying transformative
modifications.

2. Introduction

2.1 Image Complement using MATLAB


Binary image is a digital image that has only two possible values for each pixel – either 1 or 0, where 0
represents white and 1 represents black. In the complement of a binary image, the image pixel having
value zeros become ones and the image pixel having value ones become zeros; i.e white and black color
of the image is reversed. In the nut-shell, complementary command converts the brighter part into darker
and visa versa.

imcomplement: Command for taking complement of image

Code
%Image Complement
original_image = imread('img1.jpeg');
gray_image = rgb2gray(original_image); % convert RGB to Gray Scale

complemented_image = imcomplement(gray_image); %Complement of the image

subplot(2,2,1), imshow(original_image); title('Original Image');


subplot(2,2,2), imshow(gray_image); title('Gray Image');
subplot(2,2,3), imshow(complemented_image); title('Complemented Image');

2.2 Conversion of Image into Binary Image :


A binary image is a digital image that has only two possible values for each pixel. Typically, these values
are 0 and 1, representing black and white, respectively. Binary images are often used in digital image
processing for tasks such as object detection, image segmentation, and pattern recognition.

im2bw: This command is used to convert the image into binary image in Matlab.

Code
%Conversion of image into binary image
image_variable = imread("img1.jpeg");
binary1 = im2bw(image_variable); %Convert into binary image

subplot(1,2,1), imshow(image_variable); title('Original Image');


subplot(1,2,2), imshow(binary1); title('Binary Image');
2.3 Changing Binary Threshold using MATLAB
Binary thresholding is a technique used in image processing to create a binary image from a grayscale
image. The process involves setting a threshold value, and then each pixel in the grayscale image is
compared to this threshold. If the pixel's intensity is greater than or equal to the threshold, it is set to the
maximum intensity value (typically 255 for an 8-bit image), representing white. Otherwise, it is set to the
minimum intensity value (typically 0), representing black.
Code
% Binary Image Threshold
image = imread("img1.jpeg");
binary_image = im2bw(image);

threshold_01 = im2bw(image, 0.1); %set threshold value of 0.1


threshold_04 = im2bw(image, 0.4); %set threshold value of 0.4
threshold_08 = im2bw(image, 0.8); %set threshold value of 0.8

subplot(2,2,1), imshow(binary_image); title('Binary Image');


subplot(2,2,2), imshow(threshold_01); title('Threshold 0.1');
subplot(2,2,3), imshow(threshold_04); title('Threshold 0.4');
subplot(2,2,4), imshow(threshold_08); title('Threshold 0.8');

2.4 Logarithmic Transformation of Image using MATLAB


• Improving Contrast by putting Scaling Factor
• Logarithmic transformation modifies the darker part of an image.

Code
%Logarithmic Transformation
image=imread("img1.jpeg");
gray_image = rgb2gray(image);

double_value=im2double(gray_image);
Output1=2*log(1+double_value); %set the scaling factor of 2
Output2=2.5*log(1+double_value); %set the scaling factor of 2.5
Output3=3*log(1+double_value); %set the scaling factor of 3

subplot(2,2,1), imshow(gray_image); title('Original Image');


subplot(2,2,2), imshow(Output1); title('Output Scaling Factor 2');
subplot(2,2,3), imshow(Output2); title('Output Scaling Factor 2.5');
subplot(2,2,4), imshow(Output3); title('Output Scaling Factor 3');
2.5 Exponential Transformation of Image using MATLAB
Exponential image transformation is opposite of logarithmic image transformation. Logarithmic
transformation modifies the darker part of an image, while exponential transformation modifies the
brighter part of an image.
Code

I = imread('img1.jpeg');
Id = im2double(I);

output1 = 4*(((1+0.3).^(Id))-1);
output2 = 4*(((1+0.4).^(Id))-1);
output3 = 4*(((1+0.6).^(Id))-1);

subplot(2,2,1), imshow(I); title('Original Image');


subplot(2,2,2), imshow(output1); title('for 0.3');
subplot(2,2,3), imshow(output2); title('for 0.4');
subplot(2,2,4), imshow(output3); title('for 0.6');

2.6 Power Law Transformation of Image Using MATLAB


Power -law transformation enables us having both logarithmic and exponential transformation in using
simply using the value of the power. If the power is less than one, then it behaves like logarithmic
transformation. If the power is more than one, it behaves like exponential transformation.

Code
I = imread('image.png');
Id = im2double(I);

output1 = 2*(Id.^0.5);
output2 = 2*(Id.^1.5);
output3 = 2*(Id.^3.0);

subplot(2,2,1), imshow(I);
subplot(2,2,2), imshow(output1);
subplot(2,2,3), imshow(output2);
subplot(2,2,4), imshow(output3);;

2.7 Adding Salt and Pepper Noise using MATLAB


It is called salt and pepper noise because after adding this noise, it looks like that someone has spread
some salt and pepper on top of the image.
Code

I = imread('sky.jpg');
N=imnoise(I,'salt & pepper', 0.08);
imshow(N)
2.8 Removing Salt and Pepper Noise using Mean Filter in MATLAB
To remove noise, we will use a built-in function of MATLAB named “imfilter()”. This function is used to
apply various types of filters to images. First load an image. Then added salt and pepper noise to the
image. After that, create a 3 x 3 convolutional kernel and convert the kernel into a mean filter by replacing
the values of the matrix by 1/9. Finally using the “imfilter()” function, apply the mean filter to the image.

Code

I = imread('img1.jpg');
N=imnoise(I,'salt & pepper', 0.03);
mf = ones(3, 3)/9;
noise_free = imfilter(N,mf);

subplot(2,2,1),imshow(I), title('Original Image');


subplot(2,2,2),imshow(N), title('Noisy Image');
subplot(2,2,3),imshow(noise_free), title('After Removing Noise');

2.9 Median Filter to Remove Noise from Image


Removing noises from image is one of the most frequently performed tasks in image processing. It
significantly improve the accuracy of different tasks. For example - if you want to perform object
detection, removing the noises from the image can help you having better accuracy.

The first step of applying median filter to remove noises from images in MATLAB is to read the image
using 'imread()' function. Then using 'medfilt2()' function to remove the noises. The 'medfilt2()' function
requires two input arguments. One is the noise image and second is the size of filter.

First generate a noisy image using 'imnoise()' function. Then used the noisy image in the filter. Define the
size of the filter [3 3]. That means it is square area containing 9 pixels. Before passing the image, split it
into three channels - red, green and blue. It is because the filter can handle one channel at a time. Passing
an image with all three channels together will trigger an error.

Once all three channels are filtered by 'midfilt2' filter, using 'cat' function, the channels are combined
together to form the same RGB image with less noise.
Code

I = imread('img1.jpg');
N = imnoise(I, 'salt & pepper', 0.3);

red_channel = N(:, :, 1);


green_channel = N(:, :, 2);
blue_channel = N(:, :, 3);

red_channel = medfilt2(red_channel, [3 3]);


green_channel = medfilt2(green_channel, [3 3]);
blue_channel = medfilt2(blue_channel, [3 3]);

F = cat(3, red_channel, green_channel, blue_channel);

subplot(2, 1, 1); imshow(N); title('Noisy Image');


subplot(2, 1, 2); imshow(F); title('Image After Noise Removal');

You might also like