Hey OF people,
I’m trying to use a grayscale image I’m reading from OpenCV and use it as a mask on my FBO. I can’t quite wrap my head around it, and I keep getting errors. Any hint would be extremely helpful. Here’s my code:
header:
#ifndef _MINITHESIS_APP
#define _MINITHESIS_APP
#define WEBCAM
#include “miniThesisApp.h”
#include “ofxOpenCv.h”
#include “ofxControlPanel.h”
class miniThesisApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
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);
// create a variable of the type ofImage
ofxControlPanel panel;
int width, height;
ofxCvColorImage videoColorCvImage;
ofxCvGrayscaleImage videoGrayscaleCvImage;
ofxCvGrayscaleImage videoPrevFrameImage;
ofVideoGrabber vidGrabber;
ofxCvGrayscaleImage videoDiffImage;
ofxCvGrayscaleImage videoDiffMHI;
// shader stuff
ofImage multimaskImg;
ofVideoPlayer tehranMovie, nycMovie;
ofVideoGrabber cvMask;
ofFbo fbo;
ofFbo maskFbo;
ofShader shader;
};
#endif
CPP:
#include “miniThesisApp.h”
#define STRINGIFY(A) #A
//--------------------------------------------------------------
void miniThesisApp::setup(){
ofEnableAlphaBlending();
width = vidGrabber.width;
height = vidGrabber.height;
ofTexture & tex1 = videoDiffMHI.getTextureReference();
tex1.bind();
//int camWidth = 320; // try to grab at this size.
//int camHeight = 240;
tehranMovie.loadMovie(“tehran.mov”);
tehranMovie.play();
nycMovie.loadMovie(“nyc.mov”);
nycMovie.play();
vidGrabber.initGrabber(ofGetWidth(),ofGetHeight());
vidGrabber.setVerbose(true);
videoColorCvImage.allocate(width, height);
videoGrayscaleCvImage.allocate(width, height);
videoPrevFrameImage.allocate(width, height);
videoDiffImage.allocate(width, height);
videoDiffMHI.allocate(width, height);
//logoImg.loadImage(“colors.jpg”);
//multimaskImg.loadImage(“mask.jpg”);
multimaskImg.setFromPixels(&tex1, ofGetWidth(), ofGetHeight());
fbo.allocate(width,height);
maskFbo.allocate(width,height);
string shaderProgram = STRINGIFY(
uniform sampler2DRect tex0;
uniform sampler2DRect tex1;
uniform sampler2DRect tex2;
uniform sampler2DRect maskTex;
void main (void){
vec2 pos = gl_TexCoord[0].st;
vec4 rTxt = texture2DRect(tex0, pos);
vec4 gTxt = texture2DRect(tex1, pos);
vec4 bTxt = texture2DRect(tex2, pos);
vec4 mask = texture2DRect(maskTex, pos);
vec4 color = vec4(0,0,0,0);
color = mix(color, rTxt, mask.r );
color = mix(color, gTxt, mask.g );
color = mix(color, bTxt, mask.b );
gl_FragColor = color;
}
);
shader.setupShaderFromSource(GL_FRAGMENT_SHADER, shaderProgram);
shader.linkProgram();
// Let´s clear the FBO´s
// otherwise it will bring some junk with it from the memory
fbo.begin();
ofClear(0,0,0,255);
fbo.end();
maskFbo.begin();
ofClear(0,0,0,255);
maskFbo.end();
}
//--------------------------------------------------------------
void miniThesisApp::update(){
int threshold = 9;
vidGrabber.update();
tehranMovie.idleMovie();
nycMovie.idleMovie();
// This just
/*
maskFbo.begin();
ofClear(255, 0, 0,255);
//multimaskImg.draw( mouseX-multimaskImg.getWidth()*0.5, 0 );
vidGrabber.draw(0,0);
maskFbo.end();
*/
/*
// MULTITEXTURE MIXING FBO
//
fbo.begin();
ofClear(0, 0, 0,255);
shader.begin();
// Pass NYC texture
shader.setUniformTexture(“tex0”, nycMovie.getTextureReference() , 1 );
// Pass the image texture
//shader.setUniformTexture(“tex1”, vidGrabber.getTextureReference(), 2 );
// Pass Tehran texture
shader.setUniformTexture(“tex2”, tehranMovie.getTextureReference() , 3 );
// Pass the mask texture
shader.setUniformTexture(“maskTex”, vidGrabber.getTextureReference() , 4 );
// We are using this image just as a frame where the pixels can be arrange
// this could be a mesh also.
// Comment “shader.setUniformTexture(“maskTex”, maskFbo.getTextureReference() , 4 );” to se how there is two ways
// of passing a texture to the shader
//
maskFbo.draw(0, 0);
shader.end();
fbo.end();
*/
ofSetWindowTitle( ofToString( ofGetFrameRate()));
/*
if (vidGrabber.isFrameNew()){
videoColorCvImage.setFromPixels(vidGrabber.getPixels(), width, height);
videoGrayscaleCvImage = videoColorCvImage;
videoDiffImage.absDiff(videoGrayscaleCvImage, videoPrevFrameImage);
videoDiffImage.threshold(threshold);
// this is how fast we fade the blur
videoDiffMHI -= 10;
// this is how we tell difference btw current frame and BG
videoDiffMHI += videoDiffImage;
// and obviously the blur
videoDiffMHI.blur(10);
videoPrevFrameImage = videoGrayscaleCvImage;
}
*/
}
//--------------------------------------------------------------
void miniThesisApp::draw(){
//vidGrabber.draw(0,0);
fbo.draw(0,0,ofGetWidth(),ofGetHeight());
//videoDiffMHI.draw(0, 0, ofGetWidth(), ofGetHeight());
//videoDiffMHI.draw(0, 0);
//ofSetColor(255, 255, 255);
//videoGrayscaleCvImage.draw(20,20, 320,240);
//videoPrevFrameImage.draw(320+40, 20, 320, 240);
//videoDiffMHI.draw(0, 0, ofGetWidth(), ofGetHeight());
//videoDiffImage.draw(20,240+40);
//videoDiffMHI.draw(0, 0, ofGetWidth(), ofGetHeight());
//fbo.draw(0,0,ofGetWidth(),ofGetHeight());
//panel.draw();
//videoDiffMHI.draw(0, 0, ofGetWidth(), ofGetHeight());
}