Hello,
I am trying to draw an image on top of a tracked face with ofxCvHaarFinder.
With my current code, the image would be drawn a face (face1) is being detected, but when another face (face2) is being detected on the same screen, it would draw the image on face2 and image on face1 would be gone.
Hence, I would like to ask what is the best possible way to allow an image to draw and stay on every tracked faces please?
You may find my code below:
//--------------------------------------------------------------
void ofApp::draw(){
cam.draw(0,0);
ofBackground(255);
if (finder.blobs.size() > 0) {
ofRectangle cur = finder.blobs[0].boundingRect;
cropped.allocate(cur.width, cur.height, OF_IMAGE_COLOR);
cropped = crop(&im, cur.x, cur.y, cur.width, cur.height);
drawCharacter = true;
}
float sclX = 1280 / 320.0;
float sclY = 720 / 180.0;
ofNoFill();
for(int i = 0; i < finder.blobs.size(); i++) {
ofRectangle cur = finder.blobs[i].boundingRect;
ofRect(cur.x, cur.y, cur.width, cur.height);
ofRect(cur.x*sclX, cur.y*sclY, cur.width*sclX, cur.height*sclY);
}
if (drawCharacter){
ofRectangle cur = finder.blobs[0].boundingRect;
// draw the image according to the size of the head
float scl = 1280 / 320.0;
cur.x *= scl;
cur.y *= scl;
cur.width *= scl;
cur.height *= scl;
float characterW = ofMap(character.getWidth(), 0, 1280, 0 , cur.width);
float characterH = ofMap(character.getHeight(), 0, 720, 0, cur.height);
character.draw(cur.x-characterW*.8, cur.y-characterH*.25, characterW*5, characterH*5);
}
}
//--------------------------------------------------------------
ofImage ofApp::crop(ofImage* sImg, int x, int y, int w, int h){
int sW = sImg->getWidth();
int sH = sImg->getHeight();
ofImage tmpImg;
tmpImg.allocate(w, h, OF_IMAGE_COLOR);
unsigned char subRegion[ w * h * 3 ];
unsigned char * srcPixels = sImg->getPixels();
for (int i = 0; i < w; i++){
for (int j = 0; j < h; j++){
int mainPixelPos = ((j + y) * sW + (i + x)) * 3;
int subPixlPos = (j * w + i) * 3;
subRegion[subPixlPos] = srcPixels[mainPixelPos]; // R
subRegion[subPixlPos + 1] = srcPixels[mainPixelPos + 1]; // G
subRegion[subPixlPos + 2] = srcPixels[mainPixelPos + 2]; // B
}
}
tmpImg.setFromPixels(subRegion, w, h, OF_IMAGE_COLOR);
return tmpImg;
}
Thanks a lot and have a lovely day :)))
Karen x