I have an EMGU (openCV wrapper) program that subtracts the background from
a camera feed and extracts nice clean blobs.
Now I need something that will track these blobs and assign them with IDs.
Any suggestions/libraries ?
Thanks,
SW
well if you have multiple objects that you would like to track you could try a Particle Filter.
Particle filters basically "disposes" particles on the image which each have a certain weight. In each time step these weights are then updated by comparing them with the actual measured value of the object at that time. Particles with high weight will then dispose more particles in its direction (with adding a slight random part on the direction) for the next time step.
After a few time steps the particles will then group around the objects measured position. That's why this method is sometimes also called Survival of the fittest method...
So this whole thing builds a circle:
Initialization ----> Sampling
> \
/ >
Updating Prediction
< /
\ <
Association
So this provides a good method of tracking objects in a given scene. One way to do multi-object tracking would be to use this one particle filter on all the objects, which would work, but has disadvantages when you try to give IDs to the objects and also when the objects cross each other since the particle clouds might lose one object and follow another one.
To solve this you could try a Mixture-Particle-Filter (by Vermaak et al. [2003]). This one tracks each of the objects by an individual Particle filter (with of course less necessary particles).
A good paper on that can be found here: http://www.springerlink.com/content/qn4704415gx65315/
(I can also supply you with several other stuff on that if you like and if you speak German I can even give you a presentation I held about that in my university a while ago)
EDIT:
Forgot to mention: Since you try to do this in OpenCV: as far as I know there is an implementation of the Condensation algorithm (the first one where you use one particle filter on the whole image) is part of the OpenCV distribution, though it might be outdated a bit. There might be newer ways of the particle filter in OpenCV directly but if not you will find a lot of results on Google if you look for OpenCV and particle filters
Hope that helps... if not, please keep asking...
You could simply adapt one of the EMGU CV examples that makes use of
VideoSurveillance namespace:
public partial class VideoSurveilance : Form
{
private static MCvFont _font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0);
private static Capture _cameraCapture;
private static BlobTrackerAuto<Bgr> _tracker;
private static IBGFGDetector<Bgr> _detector;
public VideoSurveilance()
{
InitializeComponent();
Run();
}
void Run()
{
try
{
_cameraCapture = new Capture();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return;
}
_detector = new FGDetector<Bgr>(FORGROUND_DETECTOR_TYPE.FGD);
_tracker = new BlobTrackerAuto<Bgr>();
Application.Idle += ProcessFrame;
}
void ProcessFrame(object sender, EventArgs e)
{
Image<Bgr, Byte> frame = _cameraCapture.QueryFrame();
frame._SmoothGaussian(3); //filter out noises
#region use the background code book model to find the forground mask
_detector.Update(frame);
Image<Gray, Byte> forgroundMask = _detector.ForgroundMask;
#endregion
_tracker.Process(frame, forgroundMask);
foreach (MCvBlob blob in _tracker)
{
frame.Draw(Rectangle.Round(blob), new Bgr(255.0, 255.0, 255.0), 2);
frame.Draw(blob.ID.ToString(), ref _font, Point.Round(blob.Center), new Bgr(255.0, 255.0, 255.0));
}
imageBox1.Image = frame;
imageBox2.Image = forgroundMask;
}
}
Related
In the examples/quadrotor/ example, a custom QuadrotorPlant is specified and its output is passed into QuadrotorGeometry where the QuadrotorPlant state is packaged into FramePoseVector for the SceneGraph to visualize.
The relevant code segment in QuadrotorGeometry that does this:
...
builder->Connect(
quadrotor_geometry->get_output_port(0),
scene_graph->get_source_pose_port(quadrotor_geometry->source_id_));
...
void QuadrotorGeometry::OutputGeometryPose(
const systems::Context<double>& context,
geometry::FramePoseVector<double>* poses) const {
DRAKE_DEMAND(frame_id_.is_valid());
const auto& state = get_input_port(0).Eval(context);
math::RigidTransformd pose(
math::RollPitchYawd(state.segment<3>(3)),
state.head<3>());
*poses = {{frame_id_, pose.GetAsIsometry3()}};
}
In my case, I have a floating based multibody system (think a quadrotor with a pendulum attached) of which I've created a custom plant (LeafSystem). The minimal coordinates for such a system would be 4 (quaternion) + 3 (x,y,z) + 1 (joint angle) = 7. If I were to follow the QuadrotorGeometry example, I believe I would need to specify the full RigidTransformd for the quadrotor and the full RigidTransformd of the pendulum.
Question
Is it possible to set up the visualization / specify the pose such that I only need to specify the 7 (pose of quadrotor + joint angle) state minimal coordinates and have the internal MultibodyPlant handle the computation of each individual body's (quadrotor and pendulum) full RigidTransform which can then be passed to the SceneGraph for visualization?
I believe this was possible with the "attic-ed" (which I take to mean "to be deprecated") RigidBodyTree, which was accomplished in examples/compass_gait
lcm::DrakeLcm lcm;
auto publisher = builder.AddSystem<systems::DrakeVisualizer>(*tree, &lcm);
publisher->set_name("publisher");
builder.Connect(compass_gait->get_floating_base_state_output_port(),
publisher->get_input_port(0));
Where get_floating_base_state_output_port() was outputting the CompassGait state with only 7 states (3 rpy + 3 xyz + 1 hip angle).
What is the MultibodyPlant, SceneGraph equivalent of this?
Update (Using MultibodyPositionToGeometryPose from Russ's deleted answer
I created the following function which, attempts to create a MultibodyPlant from the given model_file and connects the given plant pose_output_port through MultibodyPositionToGeometryPose.
The pose_output_port I'm using is the 4(quaternion) + 3(xyz) + 1(joint angle) minimal state.
void add_plant_visuals(
systems::DiagramBuilder<double>* builder,
geometry::SceneGraph<double>* scene_graph,
const std::string model_file,
const systems::OutputPort<double>& pose_output_port)
{
multibody::MultibodyPlant<double> mbp;
multibody::Parser parser(&mbp, scene_graph);
auto model_id = parser.AddModelFromFile(model_file);
mbp.Finalize();
auto source_id = *mbp.get_source_id();
auto multibody_position_to_geometry_pose = builder->AddSystem<systems::rendering::MultibodyPositionToGeometryPose<double>>(mbp);
builder->Connect(pose_output_port,
multibody_position_to_geometry_pose->get_input_port());
builder->Connect(
multibody_position_to_geometry_pose->get_output_port(),
scene_graph->get_source_pose_port(source_id));
geometry::ConnectDrakeVisualizer(builder, *scene_graph);
}
The above fails with the following exception
abort: Failure at multibody/plant/multibody_plant.cc:2015 in get_geometry_poses_output_port(): condition 'geometry_source_is_registered()' failed.
So, there's a lot in here. I have a suspicion there's a simple answer, but we may have to converge on it.
First, my assumptions:
You've got an "internal" MultibodyPlant (MBP). Presumably, you also have a context for it, allowing you to perform meaningful state-dependent calculations.
Furthermore, I presume the MBP was responsible for registering the geometry (probably happened when you parsed it).
Your LeafSystem will directly connect to the SceneGraph to provide poses.
Given your state, you routinely set the state in the MBP's context to do that evaluation.
Option 1 (Edited):
In your custom LeafSystem, create the FramePoseVector output port, create the calc callback for it, and inside that callback, simply invoke the Eval() of the pose output port of the internal MBP that your LeafSystem own (having previously set the state in your locally owned Context for the MBP and passing in the pointer to the FramePoseVector that your LeafSystem's callback was provided with).
Essentially (in a very coarse way):
MySystem::MySystem() {
this->DeclareAbstractOutputPort("geometry_pose",
&MySystem::OutputGeometryPose);
}
void MySystem::OutputGeometryPose(
const Context& context, FramePoseVector* poses) const {
mbp_context_.get_mutable_continuous_state()
.SetFromVector(my_state_vector);
mbp_.get_geometry_poses_output_port().Eval(mpb_context_, poses);
}
Option 2:
Rather than implementing a LeafSystem that has an internal plant, you could have a Diagram that contains an MBP and exports the MBP's FramePoseVector output directly through the diagram to connect.
This answer addresses, specifically, your edit where you are attempting to use the MultibodyPositionToGeometryPose approach. It doesn't address the larger design issues.
Your problem is that the MultibodyPositiontToGeometryPose system takes a reference to an MBP and keeps a reference to that same MBP. That means the MBP must be alive and well for at least as long as the MPTGP is. However, in your code snippet, your MBP is local to the add_plant_visuals() function so it is destroyed as soon as the function is over.
You'll need to create something that is persisted and owned by someone else.
(This is tightly related to my option 2 - now edited for improved clarity.)
I am somewhat new to SVMs and object recognition, and am currently attempting to train an SVM using Emgu CV 3.0, save it to a file, and then load it (for use in HOGDescriptor.SetSVMDetector).
However, among other problems, I cannot find a way to load the SVM after I have saved it.
So far, my code basically does the following:
SVM myFirstSVM = new SVM();
// do some stuff, set some parameters...
myFirstSVM.Train(someParameters);
myFirstSVM.Save("filePath");
From here, the problem lies with reloading the SVM after being saved. I have checked several help topics and pages, and the only related things I could find pertained to OpenCV, which used the following method:
SVM mySecondSVM;
mySecondSVM.load("filePath");
However, I could find no method ".load()" in Emgu 3.0, although it appeared to be present in previous versions. Is there an equivalent of this OpenCV method in Emgu 3.0? I would assume there is, and I am sure it is fairly simple, but I cannot for the life of me find it.
For EmguCV 3.0.0, the Save/Load functionality doesn't seem to be supported (Load doesn't exist), you could use Write/Read instead.
A function to save an SVM model:
public static void SaveSVMToFile(SVM model, String path) {
if (File.Exists(path)) File.Delete(path);
FileStorage fs = new FileStorage(path, FileStorage.Mode.Write);
model.Write(fs);
fs.ReleaseAndGetString();
}
A function to load the SVM model provided the correct path:
public static SVM LoadSVMFromFile(String path) {
SVM svm = new SVM();
FileStorage fs = new FileStorage(path, FileStorage.Mode.Read);
svm.Read(fs.GetRoot());
fs.ReleaseAndGetString();
return svm;
}
I have saved and read the SVM model using the specified functions. But I am working with 3.1.0 version and hope it works for you as well:
I have saved the model in an XML file because the read function works on xml file as far as I know:
Emgu.CV.ML.SVM model = new Emgu.CV.ML.SVM();
model.SetKernel(Emgu.CV.ML.SVM.SvmKernelType.Linear);
model.Type = Emgu.CV.ML.SVM.SvmType.CSvc;
model.C = 1;
model.TermCriteria = new MCvTermCriteria(100, 0.00001);
bool trained = model.TrainAuto(my_trainData, 5);
model.Save("SVM_Model.xml");
and I read the model as follows:
Emgu.CV.ML.SVM model_loaded = new Emgu.CV.ML.SVM();
FileStorage fsr = new FileStorage("SVM_Model.xml", FileStorage.Mode.Read);
model_loaded.Read(fsr.GetFirstTopLevelNode());
and it works correctly.
I hope it works for you so.
For EmguCV 1.5.0:
Load Method (fileName):
Inherited from StatModel
Load the statistic model from file
fileName (String)
The file to load the model from
For EmguCV 3.0+:
Load() is not available, as you can see in the source code: https://sourceforge.net/p/emgucv/code/ci/master/tree/Emgu.CV.ML/StatModel.cs
I have an application which is written entirely using the FRP paradigm and I think I am having performance issues due to the way that I am creating the streams. It is written in Haxe but the problem is not language specific.
For example, I have this function which returns a stream that resolves every time a config file is updated for that specific section like the following:
function getConfigSection(section:String) : Stream<Map<String, String>> {
return configFileUpdated()
.then(filterForSectionChanged(section))
.then(readFile)
.then(parseYaml);
}
In the reactive programming library I am using called promhx each step of the chain should remember its last resolved value but I think every time I call this function I am recreating the stream and reprocessing each step. This is a problem with the way I am using it rather than the library.
Since this function is called everywhere parsing the YAML every time it is needed is killing the performance and is taking up over 50% of the CPU time according to profiling.
As a fix I have done something like the following using a Map stored as an instance variable that caches the streams:
function getConfigSection(section:String) : Stream<Map<String, String>> {
var cachedStream = this._streamCache.get(section);
if (cachedStream != null) {
return cachedStream;
}
var stream = configFileUpdated()
.filter(sectionFilter(section))
.then(readFile)
.then(parseYaml);
this._streamCache.set(section, stream);
return stream;
}
This might be a good solution to the problem but it doesn't feel right to me. I am wondering if anyone can think of a cleaner solution that maybe uses a more functional approach (closures etc.) or even an extension I can add to the stream like a cache function.
Another way I could do it is to create the streams before hand and store them in fields that can be accessed by consumers. I don't like this approach because I don't want to make a field for every config section, I like being able to call a function with a specific section and get a stream back.
I'd love any ideas that could give me a fresh perspective!
Well, I think one answer is to just abstract away the caching like so:
class Test {
static function main() {
var sideeffects = 0;
var cached = memoize(function (x) return x + sideeffects++);
cached(1);
trace(sideeffects);//1
cached(1);
trace(sideeffects);//1
cached(3);
trace(sideeffects);//2
cached(3);
trace(sideeffects);//2
}
#:generic static function memoize<In, Out>(f:In->Out):In->Out {
var m = new Map<In, Out>();
return
function (input:In)
return switch m[input] {
case null: m[input] = f(input);
case output: output;
}
}
}
You may be able to find a more "functional" implementation for memoize down the road. But the important thing is that it is a separate thing now and you can use it at will.
You may choose to memoize(parseYaml) so that toggling two states in the file actually becomes very cheap after both have been parsed once. You can also tweak memoize to manage the cache size according to whatever strategy proves the most valuable.
I'm actually tryin to place SharpDX Window in Winforms Window like in the following video:
http://www.youtube.com/watch?v=g-JupOxwB-k
In SharpDX this method doesn't work.Can anyone tell me how to EASILY do this ?
don't think of it as putting a sharpDX window into a winforms window.
instead think of it as how to output SharpDX to the windows handle (sorta)
the key is in the SharpDX.DXGI.SwapChain. when creating this you will need a SwapChainDescription
I like to create mine like
SwapChainDescription scd = new SwapChainDescription()
{
//set other fields
OutputHandle = yourform.Handle,
//set other fields
};
Handle
SwapChainDescription
so when you call SwapChain.Present() it will render to the form.
this is the basic way to do it with straight SharpDX and not the toolkit stuff
EDIT 04222019 LINKS DO NOT WORK --- 01052022 Link fixed
if you want to use the toolkit's GraphicsDevice you will have to set the Presenter property. in almost the same way you set the window handle in the presentation parameters.
https://github.com/sharpdx/Toolkit/tree/master/Documentation
also the toolkit has the RenderForm which plays nice with the Game class
04222019
EDIT (DirectX Example)
here is an example using straight SharpDX (No Toolkit). for complete examples you should refer to the github examples HERE
As stated above all you need to do to render to a WindowsForm window is pass the Handle to the SwapChain
visual studio 2012
add the references: (all other references are default winforms project references)
some using statements to make things easier:
namespace YourNameSpaceHere
{
using Device = SharpDX.Direct3D11.Device;
using Buffer = SharpDX.Direct3D11.Buffer;
...the rest of the application
}
the Form class: here we make the device, swap chain, render target , and render target view a variable of the Form class we are declaring
public partial class Form1 : Form //default vs2012 declaration
{
Device d; //Direct311
SwapChain sc; //DXGI
Texture2D target; //Direct3D11
RenderTargetView targetveiw;//DIrect3D11
...the rest of the form
}
Initializing the Device and SwapChain: this is what works for me on my system. if you have problems than you need to research your specific implementation and hardware. DirectX (and by extension SharpDX) has methods by which you can detect what the hardware will support.
the main code Example:
using System;
using System.ComponentModel;//needed to overide OnClosing
//I removed useless usings
using System.Windows.Forms;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX;
namespace WindowsFormsApplication2
{
using Device = SharpDX.Direct3D11.Device;
using Buffer = SharpDX.Direct3D11.Buffer;
public partial class Form1 : Form
{
Device d;
SwapChain sc;
Texture2D target;
RenderTargetView targetveiw;
public Form1()
{
InitializeComponent();
SwapChainDescription scd = new SwapChainDescription()
{
BufferCount = 1, //how many buffers are used for writing. it's recommended to have at least 2 buffers but this is an example
Flags = SwapChainFlags.None,
IsWindowed = true, //it's windowed
ModeDescription = new ModeDescription(
this.ClientSize.Width, //windows veiwable width
this.ClientSize.Height, //windows veiwable height
new Rational(60,1), //refresh rate
Format.R8G8B8A8_UNorm), //pixel format, you should resreach this for your specific implementation
OutputHandle = this.Handle, //the magic
SampleDescription = new SampleDescription(1, 0), //the first number is how many samples to take, anything above one is multisampling.
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
Device.CreateWithSwapChain(
SharpDX.Direct3D.DriverType.Hardware,//hardware if you have a graphics card otherwise you can use software
DeviceCreationFlags.Debug, //helps debuging don't use this for release verion
scd, //the swapchain description made above
out d, out sc //our directx objects
);
target = Texture2D.FromSwapChain<Texture2D>(sc, 0);
targetveiw = new RenderTargetView(d, target);
d.ImmediateContext.OutputMerger.SetRenderTargets(targetveiw);
}
protected override void OnClosing(CancelEventArgs e)
{
//dipose of all objects
d.Dispose();
sc.Dispose();
target.Dispose();
targetveiw.Dispose();
base.OnClosing(e);
}
protected override void OnPaint(PaintEventArgs e)
{
//I am rendering here for this example
//normally I use a seperate thread to call Draw() and Present() in a loop
d.ImmediateContext.ClearRenderTargetView(targetveiw, Color.CornflowerBlue);//Color to make it look like default XNA project output.
sc.Present(0, PresentFlags.None);
base.OnPaint(e);
}
}
}
this is meant to get you started with using DirectX using ShaprDX in a managed environment, specifically C# on Windows. there is much much more you will need to get something real off the ground. and this is meant as the gateway to rendering on a Winforms window using SharpDX. I don't explain things like vertex/index buffers or rendering Textures/Sprites. because it is out of scope for the question.
I'm coding my first game with XNA and i'm a bit confused.
The game is a 2D platform game, in pixel-perfect, NOT Tiles based.
Currently, my code looks like this
public class Game1 : Microsoft.Xna.Framework.Game
{
//some variables
Level currentLevel, level1, level2;
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
//a level contains 3 sprites (background, foreground, collisions)
//and the start position of the player
level1 = new Level(
new Sprite(Content.Load<Texture2D>("level1/intro-1er-plan"), Vector2.Zero),
new Sprite(Content.Load<Texture2D>("level1/intro-collisions"), Vector2.Zero),
new Sprite(Content.Load<Texture2D>("level1/intro-decors-fond"), Vector2.Zero),
new Vector2(280, 441));
level2 = new Level(
new Sprite(Content.Load<Texture2D>("level2/intro-1er-plan"), Vector2.Zero),
new Sprite(Content.Load<Texture2D>("level2/intro-collisions"), Vector2.Zero),
new Sprite(Content.Load<Texture2D>("level2/intro-decors-fond"), Vector2.Zero),
new Vector2(500, 250));
...//etc
}
protected override void UnloadContent() {}
protected override void Update(GameTime gameTime)
{
if(the_character_entered_zone1())
{
ChangeLevel(level2);
}
//other stuff
}
protected override void Draw(GameTime gameTime)
{
//drawing code
}
private void ChangeLevel(Level _theLevel)
{
currentLevel = _theLevel;
//other stuff
}
Every sprites are loaded since the beginning, so it's not a good idea for the computer's RAM.
Well, Here are my questions :
How can i save a level with his own number of sprites, their events and objects ?
How can i load/unload these levels ?
Give each level its own ContentManager, and use that instead of Game.Content (for per-level content).
(Create new ContentManager instances by passing Game.Services to the constructor.)
A single ContentManager will share all instances of content that it loads (so if you load "MyTexture" twice, you will get the same instance of it both times). Due to this fact, the only way to unload content is to unload the entire content manager (with .Unload()).
By using multiple content managers, you can get more granularity with unloading (so you can unload the content for just a single level).
Just note that different instances of ContentManager don't know about each other and won't share content. For example if you load "MyTexture" on two different content managers, you get two separate instances (so you use twice the memory).
The simplest way to deal with this is to load all the "shared" stuff with Game.Content, and all the per-level stuff with the separate level content managers.
If this still doesn't provide enough control, you can derive a class from ContentManager and implement your own load/unload policies (example in this blog post). Although this is rarely worth the effort.
Remember that this is an optimisation (for memory) - so don't spend too much time on it until it becomes an actual issue.
I recommend reading this answer (on the gamedev site) that provides some tips and links to further answers that explain in more depth how ContentManager works.