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

R Programming File

This R program demonstrates point-to-point image transformations, including histogram equalization, geometric transformations like rotation, scaling and translation, linear filtering using convolution with Gaussian and Sobel filters, and edge detection using ideal filters in the frequency domain and non-linear filtering with a Laplacian mask. Key steps include loading an image, converting to grayscale, computing histograms and Fourier transforms, applying various filters, and displaying the original and transformed images.

Uploaded by

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

R Programming File

This R program demonstrates point-to-point image transformations, including histogram equalization, geometric transformations like rotation, scaling and translation, linear filtering using convolution with Gaussian and Sobel filters, and edge detection using ideal filters in the frequency domain and non-linear filtering with a Laplacian mask. Key steps include loading an image, converting to grayscale, computing histograms and Fourier transforms, applying various filters, and displaying the original and transformed images.

Uploaded by

Krishna Soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Q1. Write a R program for Point-to-point transformation.

This laboratory
experiment provides for thresholding an image and the evaluation of its histogram.
Histogram equalization. This experiment illustrates the relationship among the
intensities (gray levels) of an image and its histogram.

# Program:

# Load necessary packages


library(imager)
library(ggplot2)

# Load an image
img <- load.image("path/to/your/image.jpg")

# Convert the image to grayscale


gray_img <- grayscale(img)

# Apply histogram equalization


equalized_img <- equalize(gray_img)

# Compute histograms
hist_original <- hist(as.vector(as.matrix(gray_img)), breaks = seq(0, 1, by = 1/256),
plot = FALSE)
hist_equalized <- hist(as.vector(as.matrix(equalized_img)), breaks = seq(0, 1, by =
1/256), plot = FALSE)

# Plot the histograms


ggplot() +
geom_line(aes(x = hist_original$mids, y = hist_original$counts), color = "blue") +
labs(title = "Original Image Histogram") +
theme_minimal()

ggplot() +
geom_line(aes(x = hist_equalized$mids, y = hist_equalized$counts), color = "blue") +
labs(title = "Equalized Image Histogram") +
theme_minimal()

# Display the original and equalized images


par(mfrow = c(1, 2))
plot(gray_img, main = "Original Image", col = grey.colors(256))
plot(equalized_img, main = "Equalized Image", col = grey.colors(256))
Q2. Write a R program for Geometric transformations. This experiment shows
image rotation, scaling, and translation. Two-dimensional Fourier transform.

# Program:

1 Image Rotation, Scaling and Translation:

library(imager)
library(ggplot2)

# Load an image
image <- load.image('path/to/your/image.jpg')

# Rotation
rotation_matrix <- matrix(c(cos(pi/4), -sin(pi/4), sin(pi/4), cos(pi/4)), nrow = 2)
rotated_image <- imager::rotate(image, matrix = rotation_matrix)

# Scaling
scaling_factor <- 1.5
scaled_image <- resize(image, scale = scaling_factor)

# Translation
translation_matrix <- matrix(c(1, 0, 50, 0, 1, 30), nrow = 2)
translated_image <- translate(image, matrix = translation_matrix)

# Display the original and transformed images


par(mfrow=c(2, 2))
plot(image, main = 'Original Image')
plot(rotated_image, main = 'Rotated Image')
plot(scaled_image, main = 'Scaled Image')
plot(translated_image, main = 'Translated Image')

2 Two-dimensional Fourier Trasnsform:

# Load necessary packages


library(imager)
library(ggplot2)

# Load an image
image <- load.image('path/to/your/image.jpg')

# Convert the image to grayscale


gray_image <- grayscale(image)

# Compute the 2D Fourier Transform


fourier_transform <- fft(as.cimg(gray_image))

# Compute the magnitude spectrum


magnitude_spectrum <- log(abs(fourier_transform) + 1)

# Display the original image and its magnitude spectrum


par(mfrow=c(1, 2))
plot(gray_image, main = 'Original Image', col = grey.colors(256))
plot(magnitude_spectrum, main = 'Magnitude Spectrum', col = grey.colors(256))
Q3. Write a R program for Linear filtering using convolution. Highly selective filters.

# Program:

1 Linear filtering using convulation:

library(imager)
library(ggplot2)

# Load an image
image <- load.image('path/to/your/image.jpg')

# Convert the image to grayscale


gray_image <- grayscale(image)

# Define a Gaussian filter (Example: Highly selective filter for blurring)


gaussian_filter <- matrix(c(1, 4, 6, 4, 1,
4, 16, 24, 16, 4,
6, 24, 36, 24, 6,
4, 16, 24, 16, 4,
1, 4, 6, 4, 1), nrow = 5, byrow = TRUE) / 256

# Apply convolution with the Gaussian filter


filtered_image <- convolve(gray_image, gaussian_filter, boundary = "wrap", normalize
= TRUE)

# Display the original and filtered images


par(mfrow=c(1, 2))
plot(gray_image, main = 'Original Image', col = grey.colors(256))
plot(filtered_image, main = 'Filtered Image', col = grey.colors(256))

2 Highly selective filter example: Edge Detection (Sobel filter)

# Load necessary packages


library(imager)
library(ggplot2)

# Load an image
image <- load.image('path/to/your/image.jpg')

# Convert the image to grayscale


gray_image <- grayscale(image)
# Apply Sobel filter for edge detection
sobel_x <- imager::sobel(gray_image, direction = "x")
sobel_y <- imager::sobel(gray_image, direction = "y")
magnitude <- sqrt(sobel_x^2 + sobel_y^2)

# Display the original and edge-detected images


par(mfrow=c(1, 2))
plot(gray_image, main = 'Original Image', col = grey.colors(256))
plot(magnitude, main = 'Edge-Detected Image', col = grey.colors(256))
Q4. Write a R program for Ideal filters in the frequency domain. Non Linear filtering
using convolutional masks. Edge detection. This experiment enables students to
understand the concept of edge detectors and their operation in noisy images.

# Program:

1 Ideal Filters in Frequency domain

library(imager)
library(ggplot2)

# Load an image
image <- load.image('path/to/your/image.jpg')

# Convert the image to grayscale


gray_image <- grayscale(image)

# Compute the 2D Fourier Transform


fourier_transform <- fft(as.cimg(gray_image))
frequencies <- fftshift(fourier_transform)

# Create an ideal high-pass filter


rows <- nrow(gray_image)
cols <- ncol(gray_image)
center_row <- rows / 2
center_col <- cols / 2
radius <- 30 # Adjust the radius based on your requirements
mask <- matrix(1, nrow = rows, ncol = cols)
mask <- cv2.circle(mask, c(center_col, center_row), radius, 0, -1)

# Apply the ideal high-pass filter


frequencies_high_pass <- frequencies * mask
filtered_image_high_pass <- Re(fft(as.cimg(frequencies_high_pass), inverse = TRUE))

# Display the original and filtered images


par(mfrow=c(1, 2))
plot(gray_image, main = 'Original Image', col = grey.colors(256))
plot(filtered_image_high_pass, main = 'Filtered Image (High Pass)', col =
grey.colors(256))
2 Non-linear Filtering for Edge Detection (Using Laplacian Mask)

# Load necessary packages


library(imager)
library(ggplot2)

# Load an image
image <- load.image('path/to/your/image.jpg')

# Convert the image to grayscale


gray_image <- grayscale(image)

# Apply a Laplacian mask for edge detection


laplacian_mask <- matrix(c(0, 1, 0,
1, -4, 1,
0, 1, 0), nrow = 3, byrow = TRUE)
filtered_image_laplacian <- convolve(gray_image, laplacian_mask, boundary =
"wrap", normalize = TRUE)

# Display the original and filtered images


par(mfrow=c(1, 2))
plot(gray_image, main = 'Original Image', col = grey.colors(256))
plot(filtered_image_laplacian, main = 'Edge-Detected Image (Laplacian)', col =
grey.colors(256))

You might also like