Reputation: 1
I tracked points (head, abdomen of an ant) from video manually and graphed the x and y coordinates of the head and abdomen, as well as its center through time (the length of a jump). I also tracked the points automatically using a function, imageToXY. I need to overlay these two graphs to compare but they have different min and max values on the x and y axes so I need to shift one graph over (preferably the manual one) to start at 0 for x and y.
Code for separate manual graph and automatic graph:
str(abdomen_final$X)
str(abdomen_final$Y)
str(head_final$X)
str(head_final$Y)
headx=head_final$X
heady=head_final$Y
abx=abdomen_final$X
aby=abdomen_final$Y
plotAnt <- function(headx, heady, abx, aby) {
# Flip y values from images
heady = -heady
aby = -aby
# Setup center calcs
centerx <- rep(0, length(headx))
centery <- centerx
# Calculate center
for (i in 1:length(headx)) {
centerx[i] = (headx[i] + abx[i]) / 2
centery[i] = (heady[i] + aby[i]) / 2
}
# Adjust y-values
yadjustment = centery[1]
heady = heady - yadjustment
aby = aby - yadjustment
centery = centery - yadjustment
# Adjust x-values
xadjustment = centerx[1]
headx = headx - xadjustment
abx = abx - xadjustment
centerx = centerx - xadjustment
# Set up the plot window
plot(x = centerx[1], y = centery[1], type = "n",
xlim = c(min(c(headx, abx)), max(c(headx, abx))),
ylim = c(min(c(heady, aby)), max(c(heady, aby))),
main = "Trap-jaw Ant Trajectory",
xlab = "Horizontal Distance (cm)", ylab = "Vertical Distance (cm)")
# Animation
for (i in 1:length(heady)) {
# Ensure the plot updates
plot.new()
# Plot the trajectory
plot(x = centerx[1:i], y = centery[1:i], type = "l", lwd = 3,
xlim = c(min(c(headx, abx)), max(c(headx, abx))),
ylim = c(min(c(heady, aby)), max(c(heady, aby))),
main = "Trap-jaw Ant Trajectory",
xlab = "Horizontal Distance (cm)", ylab = "Vertical Distance (cm)")
# Add lines for head and abdomen
lines(x = c(headx[i], abx[i]), y = c(heady[i], aby[i]), col = "blue", lwd = 2)
points(x = headx[1:i], y = heady[1:i], col = "blue", pch = 20)
points(x = abx[1:i], y = aby[1:i], col = "red", pch = 20)
# Delay to simulate animation
Sys.sleep(0.1)
}
# Return center coordinates
data.frame(x = centerx, y = centery)
}
plotAnt(headx=headx, heady=heady, abx=abx, aby=aby)
antPath <- plotAnt(headx, heady, abx, aby);antPath
install.packages("jpeg") # For readJPEG function
library(jpeg)
ant1 <-readJPEG("/Users/henryadamson/Desktop/lab04/antJumpAutomatic/ant_0001.jpeg"); ant1
automateAnt <- function(){
filenames <- grep(pattern="jpeg", x=list.files(),value=T)
x.centroid <- rep(0, length(filenames))
y.centroid <- rep(0, length(filenames))
require(jpeg)
for(i in 1:length(filenames)){
cat("processing frame ",i, " of ", length(filenames),"\n")
readJPEG(filenames[i]) -> tempAnt
imageToXY(tempAnt) -> tempAnt.xy
x.centroid[i] <- mean(tempAnt.xy$x)
y.centroid[i] <- mean(tempAnt.xy$y)
}
data.frame(x.centroid=x.centroid, y.centroid=y.centroid)
}
imageToXY <- function(image, plot=F){
x=NULL
y=NULL
for(row in 1:nrow(image)){
tempCols <- which(image[row,]<=1 & image[row,]>0)
x <- c(x, tempCols)
y <- c(y, rep(row, length(tempCols)))
}
y <- 1024-y
if(plot)
plot(x=x, y=y, pch=20, xlim=c(0,1024), ylim=c(0,1024))
data.frame(x=x,y=y)
}
ant1.xy <- imageToXY(image=ant1,plot=T)
ant1.xy
# Calculate the mean of the x and y coordinates separately
mean_x <- mean(ant1.xy$x)
mean_y <- mean(ant1.xy$y)
# Print the results
mean_x
mean_y
ant100 <-readJPEG("/Users/henryadamson/Desktop/lab04/antJumpAutomatic/ant_0100.jpeg"); ant100
automateAnt <- function(){
filenames <- grep(pattern="jpeg", x=list.files(),value=T)
x.centroid <- rep(0, length(filenames))
y.centroid <- rep(0, length(filenames))
require(jpeg)
for(i in 1:length(filenames)){
cat("processing frame ",i, " of ", length(filenames),"\n")
readJPEG(filenames[i]) -> tempAnt
imageToXY(tempAnt) -> tempAnt.xy
x.centroid[i] <- mean(tempAnt.xy$x)
y.centroid[i] <- mean(tempAnt.xy$y)
}
data.frame(x.centroid=x.centroid, y.centroid=y.centroid)
}
imageToXY <- function(image, plot=F){
x=NULL
y=NULL
for(row in 1:nrow(image)){
tempCols <- which(image[row,]<=1 & image[row,]>0)
x <- c(x, tempCols)
y <- c(y, rep(row, length(tempCols)))
}
y <- 1024-y
if(plot)
plot(x=x, y=y, pch=20, xlim=c(0,1024), ylim=c(0,1024))
data.frame(x=x,y=y)
}
ant100.xy <- imageToXY(image=ant100,plot=T)
ant100.xy
# Calculate the mean of the x and y coordinates separately
mean_x <- mean(ant100.xy$x)
mean_y <- mean(ant100.xy$y)
# Print the results
mean_x
mean_y
ant400 <-readJPEG("/Users/henryadamson/Desktop/lab04/antJumpAutomatic/ant_0400.jpeg"); ant400
automateAnt <- function(){
filenames <- grep(pattern="jpeg", x=list.files(),value=T)
x.centroid <- rep(0, length(filenames))
y.centroid <- rep(0, length(filenames))
require(jpeg)
for(i in 1:length(filenames)){
cat("processing frame ",i, " of ", length(filenames),"\n")
readJPEG(filenames[i]) -> tempAnt
imageToXY(tempAnt) -> tempAnt.xy
x.centroid[i] <- mean(tempAnt.xy$x)
y.centroid[i] <- mean(tempAnt.xy$y)
}
data.frame(x.centroid=x.centroid, y.centroid=y.centroid)
}
imageToXY <- function(image, plot=F){
x=NULL
y=NULL
for(row in 1:nrow(image)){
tempCols <- which(image[row,]<=1 & image[row,]>0)
x <- c(x, tempCols)
y <- c(y, rep(row, length(tempCols)))
}
y <- 1024-y
if(plot)
plot(x=x, y=y, pch=20, xlim=c(0,1024), ylim=c(0,1024))
data.frame(x=x,y=y)
}
ant400.xy <- imageToXY(image=ant400,plot=T)
ant400.xy
# Calculate the mean of the x and y coordinates separately
mean_x <- mean(ant400.xy$x)
mean_y <- mean(ant400.xy$y)
# Print the results
mean_x
mean_y
ant700 <-readJPEG("/Users/henryadamson/Desktop/lab04/antJumpAutomatic/ant_0700.jpeg"); ant700
automateAnt <- function(){
filenames <- grep(pattern="jpeg", x=list.files(),value=T)
x.centroid <- rep(0, length(filenames))
y.centroid <- rep(0, length(filenames))
require(jpeg)
for(i in 1:length(filenames)){
cat("processing frame ",i, " of ", length(filenames),"\n")
readJPEG(filenames[i]) -> tempAnt
imageToXY(tempAnt) -> tempAnt.xy
x.centroid[i] <- mean(tempAnt.xy$x)
y.centroid[i] <- mean(tempAnt.xy$y)
}
data.frame(x.centroid=x.centroid, y.centroid=y.centroid)
}
imageToXY <- function(image, plot=F){
x=NULL
y=NULL
for(row in 1:nrow(image)){
tempCols <- which(image[row,]<=1 & image[row,]>0)
x <- c(x, tempCols)
y <- c(y, rep(row, length(tempCols)))
}
y <- 1024-y
if(plot)
plot(x=x, y=y, pch=20, xlim=c(0,1024), ylim=c(0,1024))
data.frame(x=x,y=y)
}
ant700.xy <- imageToXY(image=ant700,plot=T)
ant700.xy
# Calculate the mean of the x and y coordinates separately
mean_x <- mean(ant700.xy$x)
mean_y <- mean(ant700.xy$y)
# Print the results
mean_x
mean_y
antPath_auto<- automateAnt()
centroidx=antPath_auto$x.centroid/87.5
centroidy=antPath_auto$y.centroid/87.5
plotAnt <- function(centroidx, centroidy) {
plot(x = centroidx, y = centroidy, type = "n",
xlim = c(min(centroidx), max(centroidx)),
ylim = c(min(centroidy), max(centroidy)),
main = "Automated Trap-jaw Ant Trajectory",
xlab = "Horizontal Distance (cm)", ylab = "Vertical Distance (cm)")
# Animation
for (i in 1:length(centroidy)) {
# Ensure the plot updates
plot.new()
# Plot the trajectory
plot(x = centroidx[1:i], y = centroidy[1:i], type = "l", lwd = 3,
xlim = c(min(centroidx), max(centroidx)),
ylim = c(min(centroidy), max(centroidy)),
main = "Automated Trap-jaw Ant Trajectory",
xlab = "Horizontal Distance (cm)", ylab = "Vertical Distance (cm)")
# Add lines for head and abdomen
# Corrected lines function call
lines(x = centroidx, y = centroidy, col = "blue", lwd = 2)
points(x = centroidx[1:i], y = centroidy[1:i], col = "red", pch = 20)
# Delay to simulate animation
Sys.sleep(0.1)
}
# Return center coordinates
data.frame(x = centroidx, y = centroidy)
}
plotAnt(centroidx=centroidx, centroidy=centroidy)
antPath <- plotAnt(centroidx, centroidy);antPath
automateAnt <- function(){
filenames <- grep(pattern="jpeg", x=list.files(),value=T)
x.centroid <- rep(0, length(filenames))
y.centroid <- rep(0, length(filenames))
require(jpeg)
for(i in 1:length(filenames)){
cat("processing frame ",i, " of ", length(filenames),"\n")
readJPEG(filenames[i]) -> tempAnt
imageToXY(tempAnt) -> tempAnt.xy
x.centroid[i] <- mean(tempAnt.xy$x)
y.centroid[i] <- mean(tempAnt.xy$y)
}
data.frame(x.centroid=x.centroid, y.centroid=y.centroid)
}
imageToXY <- function(image, plot=F){
x=NULL
y=NULL
for(row in 1:nrow(image)){
tempCols <- which(image[row,]<=1 & image[row,]>0)
x <- c(x, tempCols)
y <- c(y, rep(row, length(tempCols)))
}
y <- 1024-y
if(plot)
plot(x=x, y=y, pch=20, xlim=c(0,1024), ylim=c(0,1024))
data.frame(x=x,y=y)
}
antPath_auto<- automateAnt()
centroidx=antPath_auto$x.centroid/87.5
centroidy=antPath_auto$y.centroid/87.5
plotAnt <- function(centroidx, centroidy) {
plot(x = centroidx, y = centroidy, type = "n",
xlim = c(min(centroidx), max(centroidx)),
ylim = c(min(centroidy), max(centroidy)),
main = "Automated Trap-jaw Ant Trajectory",
xlab = "Horizontal Distance (cm)", ylab = "Vertical Distance (cm)")
# Animation
for (i in 1:length(centroidy)) {
# Ensure the plot updates
plot.new()
# Plot the trajectory
plot(x = centroidx[1:i], y = centroidy[1:i], type = "l", lwd = 3,
xlim = c(min(centroidx), max(centroidx)),
ylim = c(min(centroidy), max(centroidy)),
main = "Automated Trap-jaw Ant Trajectory",
xlab = "Horizontal Distance (cm)", ylab = "Vertical Distance (cm)")
# Add lines for head and abdomen
# Corrected lines function call
lines(x = centroidx, y = centroidy, col = "blue", lwd = 2)
points(x = centroidx[1:i], y = centroidy[1:i], col = "red", pch = 20)
# Delay to simulate animation
Sys.sleep(0.1)
}
# Return center coordinates
data.frame(x = centroidx, y = centroidy)
}
plotAnt(centroidx=centroidx, centroidy=centroidy)
antPath <- plotAnt(centroidx, centroidy);antPath
Upvotes: 0
Views: 12