Hi all, i’m using ofxCv addon and FaceTracker to grab the mouth of a recognized face and put it over the eyes,so far so googd.
The problem is that i would like to have a transparent background for the ofImage that I’m putting over the eyes, but looks like I’m messing up something with the the alpha blending, and the background is white. I even don’t know if there is a better approach to achieve this using OpenCV.
Here the code I’m using:
ofApp.h
#pragma once
#include "ofMain.h"
#include "ofxFaceTracker.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void drawMouth(ofVec2f eye, ofImage mouth);
ofImage grabMouth();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofVideoGrabber cam;
ofxFaceTracker tracker;
ofVec2f leftEye;
ofVec2f rightEye;
ofVec2f mouthPosition;
ofImage mouthImage;
};
ofApp.cpp
#include "ofApp.h"
//--------------------------------------------------------------
using namespace ofxCv;
using namespace cv;
void ofApp::setup() {
ofSetVerticalSync(true);
cam.initGrabber(640, 480);
tracker.setup();
tracker.setRescale(.5);
}
void ofApp::update() {
cam.update();
if(cam.isFrameNew()) {
tracker.update(toCv(cam));
}
//get eyes position
leftEye = tracker.getImageFeature(ofxFaceTracker::LEFT_EYE).getCentroid2D();
rightEye = tracker.getImageFeature(ofxFaceTracker::RIGHT_EYE).getCentroid2D();
}
void ofApp::draw() {
mouthImage = grabMouth();
cam.draw(0, 0);
ofSetLineWidth(2);
//tracker.draw();
drawMouth(leftEye, mouthImage);
drawMouth(rightEye, mouthImage);
ofDrawBitmapString(ofToString((int) ofGetFrameRate()), 10, 20);
}
void ofApp::drawMouth(ofVec2f eye, ofImage mouth){
ofEnableAlphaBlending();
mouth.draw(eye.x -mouth.width/2, eye.y - mouth.height/2);
ofEnableAlphaBlending();
}
ofImage ofApp::grabMouth(){
ofPolyline mouthProfile = tracker.getImageFeature(ofxFaceTracker::OUTER_MOUTH);
ofPixels pixels;
cam.getTextureReference().readToPixels(pixels);
ofRectangle mouthBox = mouthProfile.getBoundingBox();
pixels.crop(mouthBox.x,mouthBox.y,mouthBox.width,mouthBox.height);
int totalPixels = pixels.getWidth()*pixels.getHeight();
for (int x = 0; x < pixels.getWidth(); x++){
for (int y = 0; y < pixels.getHeight(); y++){
ofPoint checkpoint = ofPoint(x+mouthBox.x,y+mouthBox.y);
if(mouthProfile.inside(checkpoint)){
} else {
ofColor b = ofColor(255,255,0,0);
pixels.setColor(x,y,b);
}
}
}
ofImage videoImage;
videoImage.setImageType(OF_IMAGE_COLOR_ALPHA);
videoImage.setFromPixels(pixels);
return videoImage;
}
Here the result.
Any suggestion is more than appreciated