KEMBAR78
IMAGEPROCESSING | PDF | Digital Signal Processing | Algorithms
0% found this document useful (0 votes)
20 views14 pages

IMAGEPROCESSING

Image processing lab notes
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)
20 views14 pages

IMAGEPROCESSING

Image processing lab notes
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
You are on page 1/ 14

LAB 1

% Read an image % Convert image to grayscale


img = imread('image.png'); gray_img = rgb2gray(img);
% Display the image % Display the grayscale image
imshow(img);title('Original Image'); figure;
imshow(gray_img);
title('Grayscale Image');

% Resize the image to half its original size % Rotate the image by 45 degrees
resized_img = imresize(img, 0.5); rotated_img = imrotate(img, 45);
% Display the resized image % Display the rotated image
figure; figure;
imshow(resized_img); imshow(rotated_img);
title('Resized Image (50%)'); title('Rotated Image by 45 degrees');
% Open a new figure
figure; % Crop the image (x, y, width, height)
plot(1:10, rand(1, 10), '-o'); cropped_img = imcrop(img, [50 50 200 200]);
xlabel('X-axis'); % Display the cropped image
ylabel('Y-axis'); figure;
title('Random Data Plot'); imshow(cropped_img);
grid on; title('Cropped Image');

LAB 2
% Apply Sobel edge detection % Create a random RGB image of size 256x256
edge_img = edge(rgb2gray(img), 'sobel'); random_rgb_img = uint8(randi([0, 255], 256, 256,
% Display the edge-detected image 3));
figure; % Display the RGB image
imshow(edge_img); title('Edge Detection'); imshow(random_rgb_img);
title('Random RGB Image');
% Save the image imwrite(random_rgb_img,
'rand_RGB_image.png’);
Title(‘radom RBG image’);
% Perform histogram equalization % Separate the red, green, and blue channels
equalized_img = histeq(rgb2gray(img)); red_channel = img(:, :, 1);
% Display the histogram-equalized image green_channel = img(:, :, 2);
figure; imshow(equalized_img); blue_channel = img(:, :, 3);
title('Histogram Equalization'); % Display the channels
figure;
subplot(1, 3, 1), imshow(red_channel),
title('Red Channel');
subplot(1, 3, 2),
imshow(green_channel),title('Green Channel');
subplot(1, 3, 3), imshow(blue_channel),
title('Blue Channel');

% Convert image to binary using a threshold % Create a random grayscale image of size 256x256
binary_img = imbinarize(rgb2gray(img), 0.5); random_gray_img = uint8(randi([0, 255], 256, 256));
% Display the binary image % Display the grayscale image
figure;
imshow(binary_img); imshow(random_gray_img);
title('Binary Image'); title('Random Grayscale Image');
LAB 3

%Image Resize % Example Shrink Image By Factor of Two


Imresize () Using Default Interpolation Method
% Resize image to specified dimensions I = imread('img.JPEG');
B2 = imresize(A, [200 300]); J = imresize(I, 0.5);
% Resize to 200 rows and 300 columns % Shrink the image by a factor of two.
figure; subplot (2,1,1);
imshow(B2); imshow(I);
title('Resized Image (200x300)'); title('original image');
% display original image subplot(2,1,2);
imshow(J);
% display shrinked image
title('shrinked image');

%Perform Shrinking of an image %Example Shrink Image Using bilinear-


% Read the input image Interpolation I = imread(‘img.JPEG’); J =
I = imread(‘Image.JPEG’); imresize(I, 0.2, ‘bilinear’) subplot (2,1,1);
% Shrink the image to 40% of its original imshow(I); title(‘original image’);
size using nearest-neighbor interpolation subplot(2,1,2); imshow(J); title(‘shrinked
J = imresize(I, 0.4, ‘nearest’); image’);
% Display the original and shrinked images
side by side
subplot(2,1,1);
imshow(I);
title(‘Original Image’);
subplot(2,1,2);
imshow(J);
title(‘Shrinked Image’);
%Example Shrink Image Using bicubic- Perform Zooming of an image
Neighbor Interpolation % Example Enlarge image using Nearest
% Read the input image Interpolation
I = imread(‘img.JPEG’); I = imread(‘img.JPEG’);
% Shrink the image to 60% of its original J = imresize(I, 2, ‘nearest’)
size using bicubic interpolation %Shrink the image to 40% of the original
J = imresize(I, 0.6, ‘bicubic’); size using nearest-neighbor interpolation.
% Display the original and shrinked images This is the fastest method, but it has the
side by side lowest quality.
subplot(2,1,1); Subplot (2,1,1);
imshow(I); imshow(I);
title(‘Original Image’); title(‘original image’);
subplot(2,1,2);
subplot(2,1,2); imshow(J);
imshow(J); title(‘shrinked image’);
title(‘Shrinked Image’);
%Example Enlarge Image Using Bilinear- %Example Enlarge Image Using Cubic-
Interpolation Interpolation
I = imread(‘img.JPEG’); I = imread(‘img.JPEG’);
J = imresize(I, 5, ‘bilinear’) J = imresize(I, 5, ‘cubic’);
subplot (2,1,1); %Shrink the image to 40% of the original
imshow(I); size using nearest-neighbor interpolation.
title(‘original image’); This is the fastest method, but it has the
subplot(2,1,2); lowest quality.
imshow(J); Subplot (2,1,1);
title(‘shrinked image’); imshow(I);
title(‘original image’);
subplot(2,1,2);
imshow(J);
title(‘shrinked image’);

Perform Cropping of an image % Example Crop Indexed Image Specifying


% Read the input image Crop Rectangle
I = imread(‘image.png’); I = imread(‘img.tif’);
I2 = imcrop(I,[75 68 130 112]);
% Example 1: Interactive cropping % Crop image, specifying crop rectangle.
figure; Subplot(1,2,1)
imshow(I); imshow(I)
title(‘Interactive Cropping – Select title(‘Original Image’)
Region’); % Display original image
Icropped1 = imcrop(I); % Allows user to subplot(1,2,2)
crop interactively imshow(I2)
figure; title(‘Cropped Image’) % Display cropped
imshow(Icropped1); image.
title(‘Cropped Image (Interactive)’);
% Example 2: Cropping using a specified
rectangle
rect = [50 50 200 150]; % [x, y, width,
height]
Icropped2 = imcrop(I, rect); % Cropping
the image
figure;
imshow(Icropped2);
title(‘Cropped Image (Rectangle)’);

LAB 4
% Read and display an image originalImage %Read and display an image
= imread('img.JPEG'); % Repalce originalImage = imread('image.jpg');
'your_image.jpg' with the actual iamge file %%Perform the 2D FFT on the image
path imshow(originalImage); title('original fftImage
Image'); =fft2(double(originalImage));
%% Define Parameters for Gaussian filter
sigma = 2; %%Define filter parameters
% Standard deviation of the guassian D0 = 40; %Cutoff frequency
distribution
Filtersize = 11; % size of the filter kernel % %%Create the frequency domain filter
Create a Gaussain kernel using fspecial (high-pass)
gaussianKernal = fspecial('gaussian', [x, y] =
filtersize, sigma); %Apply the Gaussian meshgrid(1:size(originalImage, 2),
filter to the image using 1:size(originalImage, 1));
centerX = ceil (size(originalImage,
imfilter filteredImage =
2) /2);
imfilter(originalImage, centerY = ceil (size(originalImage,
gaussianKernal, 'conv');
%Display the filtered image figure; 1) /2);
imshow(filteredImage);
title('Low-Pass Filtered Image (Gaussian)'); %%High-Pass filter
H = double(sqrt((x - centerX) .^2 +
(y - centerY) .^2) > D0);

%% Apply the filter in frequency


domain
filteredImage = real(ifft2(fftImage
.* H));

%% Display the original and filtered


image
figure;
subplot( 1, 2, 1);
imshow(originalImage);
title('Original Image');
subplot(1,2,2);
imshow(filteredImage, []);
title('High-pass Fltered Image');

LAB 5
I = checkerboard(8);
Deblur image using Wiener filter PSF = fspecial('gaussian',7,10);
I = im2double(imread('image.jpg')); V = .01;
imshow(I); BlurredNoisy =
title('Original Image'); imnoise(imfilter(I,PSF),'gaussian',0,V);
LEN = 21; NOISEPOWER = V*prod(size(I));
THETA = 11; [J LAGRA] =
PSF = fspecial('motion', LEN, THETA); deconvreg(BlurredNoisy,PSF,NOISEPOWER);
blurred = imfilter(I, PSF, 'conv', subplot(221); imshow(BlurredNoisy);
'circular'); title('A = Blurred and Noisy');
figure, imshow(blurred) subplot(222); imshow(J);
noise_mean = 0; title('[J LAGRA] = deconvreg(A,PSF,NP)');
noise_var = 0.0001; subplot(223);
blurred_noisy = imnoise(blurred, imshow(deconvreg(BlurredNoisy,PSF,[],LAGRA/
'gaussian', noise_mean, noise_var); 10));
figure, imshow(blurred_noisy) title('deconvreg(A,PSF,[],0.1*LAGRA)');
title('Simulate Blur and Noise') subplot(224);
estimated_nsr = 0; imshow(deconvreg(BlurredNoisy,PSF,[],LAGRA
wnr2 = deconvwnr(blurred_noisy, PSF, *10));
estimated_nsr); title('deconvreg(A,PSF,[],10*LAGRA)');
figure, imshow(wnr2)
title('Restoration of Blurred, Noisy
Image Using NSR = 0')
estimated_nsr = noise_var / var(I(:));
wnr3 = deconvwnr(blurred_noisy, PSF,
estimated_nsr);
figure, imshow(wnr3)
title('Restoration of Blurred, Noisy
Image Using Estimated NSR');

I = imread("board.tif");
I = imcrop(I,[2 50 255 255]); rng default;
imshow(I) I = checkerboard(8);
title("Original Image") PSF = fspecial('gaussian',7,10);
by-5. V = .0001;
PSF = fspecial("gaussian",5,5); BlurredNoisy =
blurred = imnoise(imfilter(I,PSF),'gaussian',0,V);
imfilter(I,PSF,"symmetric","conv"); WT = zeros(size(I));
V = 0.002; WT(5:end-4,5:end-4) = 1;
blurred_noisy = INITPSF = ones(size(PSF));
imnoise(blurred,"gaussian",0,V); [J P] =
imshow(blurred_noisy) deconvblind(BlurredNoisy,INITPSF,20,10*sqrt(
title("Blurred and Noisy Image") V),WT);
blur and decrease the number of subplot(221);imshow(BlurredNoisy);
iterations to 5. title('A = Blurred and Noisy');
luc1 = subplot(222);imshow(PSF,[]);
deconvlucy(blurred_noisy,PSF,5); title('True PSF');
imshow(luc1) subplot(223);imshow(J);
title("Restored Image") title('Deblurred Image');
subplot(224);imshow(P,[]);
title('Recovered PSF');

LAB 6
%1. Edge Detection Code: %. Line Detection using Hough Transform:
% Read, rotate, and display the image
% Importing the image and converting it to I = imread('image.tif');
grayscale rotI = imrotate(I, 33, 'crop');
I = rgb2gray(imread("flowers.jpg")); imshow(rotI);

% Displaying the original grayscale image


subplot(2, 4, 1), % Find the edges in the image using the
imshow(I); edge function
title("Gray Scale Image"); BW = edge(rotI, 'canny');
imshow(BW);
% Sobel Edge Detection
J = edge(I, 'Sobel'); % Compute the Hough transform of the
subplot(2, 4, 2), binary image returned by edge
imshow(J); [H, theta, rho] = hough(BW);
title("Sobel");
% Display the Hough transform
% Prewitt Edge detection figure;
K = edge(I, 'Prewitt'); imshow(imadjust(rescale(H)), [], 'XData',
subplot(2, 4, 3), theta, 'YData', rho, ...
imshow(K); 'InitialMagnification', 'fit');
title("Prewitt"); xlabel('\theta (degrees)');
ylabel('\rho');
% Robert Edge Detection axis on;
L = edge(I, 'Roberts'); axis normal;
subplot(2, 4, 4), hold on;
imshow(L); colormap(gca, hot);
title("Robert");
% Find the peaks in the Hough transform
% Log Edge Detection matrix using houghpeaks
M = edge(I, 'log'); P = houghpeaks(H, 5, 'threshold', ceil(0.3 *
subplot(2, 4, 5), max(H(:))));
imshow(M);
title("Log"); % Superimpose a plot on the image of the
transform that identifies the peaks
% Zerocross Edge Detection x = theta(P(:, 2));
M = edge(I, 'zerocross'); y = rho(P(:, 1));
subplot(2, 4, 6), plot(x, y, 's', 'color', 'black');
imshow(M);
title("Zerocross"); % Find lines in the image using houghlines
lines = houghlines(BW, theta, rho, P,
% Canny Edge Detection 'FillGap', 5, 'MinLength', 7);
N = edge(I, 'Canny');
subplot(2, 4, 7), % Create a plot that displays the original
imshow(N); image with the lines superimposed
title("Canny"); figure, imshow(rotI), hold on;

max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:, 1), xy(:, 2), 'LineWidth', 2,
'Color', 'green');

% Plot beginnings and ends of lines


plot(xy(1, 1), xy(1, 2), 'x', 'LineWidth', 2,
'Color', 'yellow');
plot(xy(2, 1), xy(2, 2), 'x', 'LineWidth', 2,
'Color', 'red');

% Determine the endpoints of the


longest line segment
len = norm(lines(k).point1 -
lines(k).point2);
if len > max_len
max_len = len;
xy_long = xy;
end
end

% Highlight the longest line segment


plot(xy_long(:, 1), xy_long(:, 2),
'LineWidth', 2, 'Color', 'red');

LAB 7

% Read and Display the image % Load the image


RGB = imread("image.jpg"); rgb = imread('image.jpg');
imshow(RGB); imshow(rgb);

% Convert the RGB image to a grayscale % Compute the complement of the RGB
image and display the result image
I = rgb2gray(RGB); c = imcomplement(rgb);
imshow(I); imshow(c);

% Read the RGB Image % Read the input image


A = imread('image.jpg'); img = imread('image.jpg');
figure, imshow(A);
title('RGB Image'); % Convert the input image to grayscale
gray_img = rgb2gray(img);
% Represent the RGB image in [0 1] range
% Create an empty matrix for the RGB output
I = double(A) / 255; image
R = I(:,:,1); out_img = zeros(size(gray_img, 1),
G = I(:,:,2); size(gray_img, 2), 3);
B = I(:,:,3);
% Specify a colormap (e.g., jet)
% Hue Calculation colormap_name = 'jet';
numi = 1/2 * ((R - G) + (R - B)); color_map = colormap(colormap_name);
denom = sqrt((R - G).^2 + ((R - B) .* (G -
B))); % Extract RGB color channels from the
colormap
H = acosd(numi ./ (denom + 0.000001)); %
red = color_map(:, 1);
Avoid divide by zero
green = color_map(:, 2);
H(B > G) = 360 - H(B > G); % Adjust if B > G blue = color_map(:, 3);
H = H / 360; % Normalize to range [0 1]
% Map grayscale intensity values to RGB colors
% Saturation Calculation out_img(:, :, 1) = red(gray_img);
S = 1 - (3 ./ (sum(I, 3) + 0.000001)) .* min(I, out_img(:, :, 2) = green(gray_img);
[], 3); out_img(:, :, 3) = blue(gray_img);

% Intensity Calculation % Convert the output image to 8-bit unsigned


I = sum(I, 3) / 3; integer format
out_img = im2uint8(out_img);
% Combine HSI
HSI = zeros(size(A)); % Display the input image, grayscale image,
HSI(:,:,1) = H; and pseudo-color image
HSI(:,:,2) = S; subplot(1, 3, 1); imshow(img); title('Original
HSI(:,:,3) = I; Image');
subplot(1, 3, 2); imshow(gray_img);
figure, imshow(HSI);
title('Grayscale Image');
title('HSI Image');
subplot(1, 3, 3); imshow(out_img);
title('Pseudo-Color Image');

You might also like