0% found this document useful (0 votes)
4 views24 pages

GS321 - 3 - Image Preprocessing in R

This document outlines a practical tutorial for advanced image processing using R, specifically focusing on preprocessing techniques necessary for computer vision models. It covers various topics including installation of required libraries, data loading, brightness manipulation, contrast adjustment, gamma correction, color changes, cropping, and filtering techniques. The tutorial concludes by encouraging the application of these preprocessing techniques in building machine learning models for computer vision projects.

Uploaded by

kazimbayagrace
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)
4 views24 pages

GS321 - 3 - Image Preprocessing in R

This document outlines a practical tutorial for advanced image processing using R, specifically focusing on preprocessing techniques necessary for computer vision models. It covers various topics including installation of required libraries, data loading, brightness manipulation, contrast adjustment, gamma correction, color changes, cropping, and filtering techniques. The tutorial concludes by encouraging the application of these preprocessing techniques in building machine learning models for computer vision projects.

Uploaded by

kazimbayagrace
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/ 24

SCHOOL OF EARTH SCIENCES, REAL ESTATE,

BUSINESS AND INFORMATICS

DEPARTMENT OF GEOSPATIAL SCIENCES AND TECHNOLOGY


(GST)

BSc. in GIS and RS


2024/2025

GS 321 Advanced Image Processing


Practical 1a: Getting Started with Image Pre-
processing in R

Instructor:
Dr. Msusa
Mr. Wangabo
Introduction
A few problems associated with image data include complexity, inaccuracy,
and inadequacy. This is why before building a computer vision model, it is
essential that the data is preprocessed (cleaned and processed to the desired
format) to achieve the desired results.

We will go through within the framework of the following headings:

Prerequisites
To follow through the tutorial, one needs:

• RStudio

• Kaggle R

Data: Use the image provided in the data folder

We will go through within the framework of the following headings.

1. Installation and Libraries

2. Load and Check Data

3. Manipulation Brightness & Darkness

4. Combine

5. Manipulating Contrast

6. Gamma Correction
7. Colour Change

8. Cropping

9. Saving

10. Flip, Flop, Rotate, Resize

11. Low Pass Filter

12. High Pass Filter

1. Installation and Libraries

First, we need to install the “EBImage” and “jpeg” packages.


# Installation
if (!require("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install()BiocManager::install()
BiocManager::install("EBImage")library(EBImage)
install.packages("jpeg")

library(EBImage)
library(jpeg)

2. Load and Check Data


You must write the path of your own files.
image_path_1 <- "E:/land/Geita.jpg"
image_path_2 <- "E:/land/Nyarugusu.jpg"

image_1 <- readImage(image_path_1)


image_2 <- readImage(image_path_2)

# Check data
display(image_1)
print(image_1)
# check data
display(image_2)
print(image_2)

When we check the below array. This is a multi-dimensional array


This small table shows us 1:5 for the x-axis, 1:6 for the y-axis, 1 for the z-
axis. Dim is multidimensional, it indicates 600, 600, 3
Image
colorMode : Color
storage.mode : double
dim : 4250 5500 3
frames.total : 3
frames.render: 1
imageData(object)[1:5,1:6,1]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 1 1 1 1
[2,] 0 0 0 0 0 0
[3,] 0 0 0 0 0 0
[4,] 0 0 0 0 0 0
[5,] 0 0 0 0 0 0
Let’s draw the plot of image_1. Plot data gives us an intensity value between
0 to 1. We also see three colours (blue, green, red), we see their values on
the y-axis. x-axis, 0 value reflects darker color, 1 reflects lighter color.
hist(image_1)

Plot for david_bowie. When we compare two plots (frank_zappa and


david_bowie) David Bowie histogram is closer to 1.0, there are two peaks in
frank_zappa histogram, and the blue colour is closer to 0.0, meaning is it has
darker colours. We can say david_bowie is lighter than frank_zappa photos.
# Plot data
hist(image_2)
# Manipulating brightness
# We are putting some lightness on image_1 pic.
# When we compare the firt table of image_1 and the last one, we see the
numbers are different. The first table arrays are around 0.90 and the last one
around 1.30

3. Manipulation Brightness & Darkness


Let’s manipulate brightness. We are putting some lightness on frank_zappa
photo with + 0.4, you can adjust whatever brightness you want.
brightened_image <- image_1 + 0.4
print(brightened_image)

When we compare the first table of image_1 and the last one, we see the
numbers are different. The first table arrays are around 0 and the last one
around -0.4
Image
colorMode : Color
storage.mode : double
dim : 4250 5500 3
frames.total : 3
frames.render: 1

imageData(object)[1:5,1:6,1]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.6 0.6 0.6 0.6 0.6 0.6
[2,] -0.4 -0.4 -0.4 -0.4 -0.4 -0.4
[3,] -0.4 -0.4 -0.4 -0.4 -0.4 -0.4
[4,] -0.4 -0.4 -0.4 -0.4 -0.4 -0.4
[5,] -0.4 -0.4 -0.4 -0.4 -0.4 -0.4

As we can see the new image is brighter than the original one.
display(brightened_image)

Let’s make it darker.


# Dark
darkened_image <- frank_zappa - 0.4
display(darkened_image)

Let’s plot a darker frank_zappa histogram.


hist(darkened_image)
4. Combine
Image_1 and image_2 files were put into one file. When it is scrolled, the
two photos will be shown in the same file.
combined_image<- combine(image_1, image_2)
display(combined_image, all = TRUE)

We add two pictures into one image.


The arrays can be divided, multiplication or other mathematical operation.
image_2 is divided by 2 for combining pictures better.
combined_image2 <- image_1+ image_2/2
display(combined_image2)
Histogram of the combined photos. As we can see, we manipulated the
colours of image_2 (it was divided by 2). Therefore we can see the difference
in the histogram of this manipulation too.
# histogram of the combined pic.
hist(combined_image2)
5. Manipulating Contrast
# Manipulating Contrast
low_contrast_image <- image_1 * 0.5
display(low_contrast_image)
high_contrast_image <- image_1 * 3
display(high_contrast_image)
6. Gamma Correction
gamma_1 <- image_1^0.5
gamma_2 <- image_1^3

# let's see the gamma difference


display(gamma_1)
display(gamma_2)
7. Color Change
# Color Change
# As we can see colormode changed
colorMode(frank_zappa) <- Grayscale
print(image_1)
Image
colorMode : Grayscale
storage.mode : double
dim : 4250 5500 3
frames.total : 3
frames.render: 3

imageData(object)[1:5,1:6,1]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 1 1 1 1 1
[2,] 0 0 0 0 0 0
[3,] 0 0 0 0 0 0
[4,] 0 0 0 0 0 0
[5,] 0 0 0 0 0 0
To display all frames use 'all = TRUE'.

8. Cropping
# cropping
cropped_image <- image_1[3102:2023, 2014:1252, ] # we just select x, y axis, z is blank
display(cropped_image)

9. Saving
# let's save the crop file cropped_image
# new image file
writeImage(cropped_image, "E:/land/NewImage.jpg")

10. Flip, Flop, Rotate, Resize


# flip, flop, rotate, resize
flipped_image <- flip(image_1)
display(flipped_image)
rotated_image <- rotate(image_1, 45)
display(rotated_image)
flopped_image <- flop(image_1)
display(flopped_image)
resized_image <- resize(image_1, 200)
display(resized_image)
11. Low Pass Filter
# low pass filter
low_pass_filter <- makeBrush(41, shape = "disc", step = FALSE)^2
low_pass_filter <- low_pass_filter / sum(low_pass_filter)
Image.low <- filter2(image_1, low_pass_filter)
display(Image.low)
12. High Pass Filter
# high-pass filter
high_pass_filter <- matrix(1, nc = 3, nr = 3)
high_pass_filter [2,2] <- -5
Image.high <- filter2(image_1, high_pass_filter)
display(Image.high)
# combine
new <- Image.high/5+ image_1 # combine with original photo and new one
comb <- combine(image_1, new)
display(comb) # Display the combined image
To display all frames use 'all = TRUE'.
Conclusion
Having explored the popular and commonly used image preprocessing
techniques with RStudio, what now remains is modelling your machine
learning models to the desired level of high accuracy and performance.
Therefore, we are now ready to jump into building custom computer vision
projects.

Good luck!
Image Processing

You might also like