data_605_hw1
HW1
"One of the most useful applications for linear algebra in data science is image manipulation. We often need to compress, expand, warp, skew, etc. images. To do so, we left multiply a transformation matrix by each of the point vectors.
For this assignment, build the first letters for both your first and last name using point plots in R."
My name is Jimmy Ng and my initials would be JN. In this assignment, the first part will demonstrate the steps (in loops) that transform an initial unit vector into different parts of the letters, i.e. J and N. Part two will put the images together into animation (gif) and a static image.
(1) Matrix Multiplication
Set up
Here is the initial (unit) vector [1, 1] that will drive our image processing. We begin with the identity matrix. Multiplying any vector with an identity matrix will return no transformation at all. Thus, we slowly change our matrix in order to transform our vector into a desirable shape.
y = c(1, 1)
tm1 = matrix(c(
c(1, 0),
c(0, 1)),
byrow = TRUE,
nrow = 2)
tm2 = matrix(c(
c(-1, 0),
c(0, 1)),
byrow = TRUE,
nrow = 2)
tm3 = matrix(c(
c(-1, 0),
c(0, -1)),
byrow = TRUE,
nrow = 2)
tm4 = matrix(c(
c(-1, 0),
c(0, 1)),
byrow = TRUE,
nrow = 2)
tm5 = matrix(c(
c(1, 0),
c(0, -1)),
byrow = TRUE,
nrow = 2)
tm6 = matrix(c(
c(-1, 0),
c(0, 1)),
byrow = TRUE,
nrow = 2)
matrix_list = list(tm1, tm2, tm3, tm4, tm5, tm6)
x_axis_list = list(c(0.5, 1.5),
c(1, 1),
c(0.5, 1),
c(2.5, 2.5),
c(2.5, 3.5),
c(3.5, 3.5))
print("the unit vector:"); print(y)## [1] "the unit vector:"
## [1] 1 1
print("first transformation matrix:"); print(tm1)## [1] "first transformation matrix:"
## [,1] [,2]
## [1,] 1 0
## [2,] 0 1
print("second transformation matrix:"); print(tm2)## [1] "second transformation matrix:"
## [,1] [,2]
## [1,] -1 0
## [2,] 0 1
print("third transformation matrix:"); print(tm3)## [1] "third transformation matrix:"
## [,1] [,2]
## [1,] -1 0
## [2,] 0 -1
print("fourth transformation matrix:"); print(tm4)## [1] "fourth transformation matrix:"
## [,1] [,2]
## [1,] -1 0
## [2,] 0 1
print("fifth transformation matrix:"); print(tm5)## [1] "fifth transformation matrix:"
## [,1] [,2]
## [1,] 1 0
## [2,] 0 -1
print("sixth transformation matrix"); print(tm6)## [1] "sixth transformation matrix"
## [,1] [,2]
## [1,] -1 0
## [2,] 0 1
Loop through matrix multiplication & save output
for(i in 1:length(matrix_list)){
#jpeg(file = paste0("letter_", i, ".jpeg"))
plot((matrix_list[[i]] %*% y) ~ x_axis_list[[i]],
xlim = c(0, 4),
ylim = c(-2, 2),
xlab = "", ylab = "",
type = "l",
col = "red",
lty = "solid",
lwd = 5)
#dev.off()
}(2) Animated output
gif
# get list of images saved in current wd
imgs = list("letter_1.jpeg", "letter_2.jpeg", "letter_3.jpeg",
"letter_4.jpeg", "letter_5.jpeg", "letter_6.jpeg",
"letter_7.jpeg")
img_list <- lapply(imgs, magick::image_read)
# join the images together
img_joined <- magick::image_join(img_list)
# animate at 1 frames per second
img_animated <- magick::image_animate(img_joined, fps = 1)
# view animated image
img_animatedimage
plot((tm1 %*% y) ~ c(0.5, 1.5),
xlim = c(0, 4),
ylim = c(-2, 2),
xlab = "", ylab = "",
type = "l",
col = "red",
lty = "solid",
lwd = 5)
points((tm2 %*% y) ~ c(1, 1),
type = "l",
col = "red",
lty = "solid",
lwd = 5)
points((tm3 %*% y) ~ c(0.5, 1),
type = "l",
col = "red",
lty = "solid",
lwd = 5)
points((tm4 %*% y) ~ c(2.5, 2.5),
type = "l",
col = "red",
lty = "solid",
lwd = 5)
points((tm5 %*% y) ~ c(2.5, 3.5),
type = "l",
col = "red",
lty = "solid",
lwd = 5)
points((tm6 %*% y) ~ c(3.5, 3.5),
type = "l",
col = "red",
lty = "solid",
lwd = 5)