Skip to content

Instantly share code, notes, and snippets.

@sld
Created December 27, 2011 21:10
Show Gist options
  • Save sld/1525118 to your computer and use it in GitHub Desktop.
Save sld/1525118 to your computer and use it in GitHub Desktop.
Hornetseye motion object bounding
def get_human_with_place(thresholed_frame, start_frame)
components = thresholed_frame.components
mask = components.largest.erode(10).conditional(255, 0)
x = lazy( *start_frame.shape ) {|i,j| i}
y = lazy( *start_frame.shape ) {|i,j| j}
if x.mask(mask).empty? || y.mask(mask).empty?
return start_frame
else
x_range = x.mask(mask).range
y_range = y.mask(mask).range
box = [x_range, y_range]
start_frame[*box] = start_frame[ *box ] / 2 + 0x7F
return start_frame
end
end
@wedesoft
Copy link

Hi Idris,
Not sure what you ultimately want to do. But you can detect and filter multiple
components of a thresholded difference image as follows:

#!/usr/bin/env ruby
require 'rubygems'
require 'hornetseye_v4l2'
require 'hornetseye_xorg'
include Hornetseye
THRESHOLD = 25
AREA = 0.05 .. 0.3
ERODE = 5
input = V4L2Input.new
size = input.width * input.height
area = (AREA.begin * size).to_i .. (AREA.end * size).to_i
ref = X11Display.show { input.read_ubyte }
X11Display.show do
  img = input.read_ubyte
  diff = img.to_sint - ref
  comp = (diff.abs.erode(ERODE) >= THRESHOLD).dilate(ERODE).components
  n = comp.max
  hist = comp.histogram n + 1
  msk = hist > 0 # .between? area.begin, area.end
  msk[0] = false
  comp.lut(msk).conditional RGB(255, 0, 0), img
end

Note that if two people are next to each other, they would be seen as a single component
though.
Hope it helps.

Regards Jan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment