Drawing with OpenCv. Blobs with Id

I am working on a project called Devuison, which basicllay consists on making sound whith drawings.
I am using the openCv addon form Openframeworks for tracking the drawings, so later on I would be able to manipulate sound with the information comming from the drawings.

The problem was that each time openCv looks for blobs, it empties the array where it saves the blobs and creates new ones. What I really need is to update the information (position and size) of the previous blobs, instead of delete them and create new ones. Also I need to give them an id, so I can identify them and manipulate related information (like sound attached to each blob) over time.

Here is a video that shows the final solution:

So I solved the problem creating a new class called Line. When a new blob is created, also a new line is created and it is saved in a new array (vector) called lines. When there is a new frame, and new blobs are created, the program checks if the blob is inside a previous line. If it is inside of the line, the information of the line is updated (center position and bounding rectangle), instead of delete it and create a new one.

Here is the code that deals with it:

 
void imageProjection::update(ofPoint _centro, ofRectangle _arredorRect){
//check if the blob is inside a line
//first we check is a line was already created
if(nLineas > 0){
bool checkDrawnAtBlob = false;
for(int i = 0; i < nLineas; i++){
//Check if the blob is inside a line
if(lineas[i].isInsideMe(_centro)){
// if there is a blob inside this line, its boundingRect and center position is updated
lineas[i].setBlobOrigen(_centro, _arredorRect);
lineas[i].set(reubicarPunto(_centro), reubicarRectangulo(_arredorRect));
lineas[i].setUpdated(true);

checkDrawnAtBlob = true;
}
}
// creamos un debuxo si non hai ningún que se corresponda con este blob
if(!checkDrawnAtBlob){
creaDebuxo(_centro, _arredorRect);
}
// creamos un debuxo si todavía non hai ningún creado
}else{
creaDebuxo(_centro, _arredorRect);
}
}

This update function is called from a blobs for loop:

 
for (int i=0; i IP.update(contours.blobs[i].centroid, contours.blobs[i].boundingRect);
}

I am also using a second screen where I project the lines, so that is why I use the function reubicarPunto() and reubicarRectangulo(), to relocated the positions of the lines on the new screen.

Here si the full code of the program, if some of you find it usseful:
drawingOpenCv