Skip to content

Instantly share code, notes, and snippets.

@EmilHvitfeldt
Created February 16, 2018 18:19
Show Gist options
  • Save EmilHvitfeldt/aac4de0672ecb47a2f05b1e13d063aa2 to your computer and use it in GitHub Desktop.
Save EmilHvitfeldt/aac4de0672ecb47a2f05b1e13d063aa2 to your computer and use it in GitHub Desktop.
Frame rate for color blob detection using Rvision
# Packages
library(Rvision)
library(dplyr)
library(lubridate)
# Functions
blob_fun <- function(img, fun, color = character()) {
img %>%
split() %>%
do.call(fun, .) %>%
medianBlur(15) %>%
simpleBlobDetector(max_area = Inf, min_area = 10, blob_color = 255,
filter_by_convexity = FALSE,
filter_by_inertia = FALSE, min_threshold = 0) %>%
mutate(color = color)
}
multi_draw <- function(img, blobs) {
if (nrow(blobs) > 0) {
for (i in 1:nrow(blobs)) {
drawRectangle(img,
blobs$x[i] - 1 + blobs$size[i],
blobs$y[i] - 1 + blobs$size[i],
blobs$x[i] - 1 - blobs$size[i],
blobs$y[i] - 1 - blobs$size[i],
thickness = 5, color = blobs$color[1])
}
}
}
blue <- function(b, g, r) b > 150 & r < 200 & g < 200
red <- function(b, g, r) r > 150 & b < 200 & g < 150
green <- function(b, g, r) g > 150 & b < 200 & r < 200
yellow <- function(b, g, r) g > 150 & b < 200 & b > 150 & r > 150
orange <- function(b, g, r) g > 150 & b < 150 & r > 150
# streams
# Comment out one color at a time
my_stream <- stream(0)
newDisplay("Live test", 360, 640)
tim_check <- c()
while(TRUE) {
img <- readNext(my_stream)
#blue_mms <- blob_fun(img, blue, "blue")
#red_mms <- blob_fun(img, red, "red")
#green_mms <- blob_fun(img, green, "green")
#yellow_mms <- blob_fun(img, yellow, "yellow")
#orange_mms <- blob_fun(img, orange, "orange")
#multi_draw(img, blue_mms)
#multi_draw(img, red_mms)
#multi_draw(img, green_mms)
#multi_draw(img, yellow_mms)
#multi_draw(img, orange_mms)
display(img, "Live test", 25, 360, 640)
tim_check <- c(tim_check, Sys.time())
}
destroyDisplay("Live test")
release(my_stream)
# Fps calculations
secdons <- duration(tim_check)
fps <- length(secdons)/ as.numeric(stringr::str_replace(secdons[1] - secdons[length(secdons)], "s", ""))
fps
# Results
# 5 colors: 3.151997
# 4 colors: 4.200356
# 3 colors: 4.800911
# 2 colors: 6.521467
# 1 colors: 9.651368
# 0 colors: 15.09349
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment