Gaussian Blur with openCV -> cannot blend

Hello everyone !
I try to apply a gaussian blur to a mesh with openCv but I don’t understand why my ofImage with the blur effect doesn’t blend with the mesh ?

in setup()

ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD);
this->fbo.allocate(ofGetWidth(), ofGetHeight());
this->fbo.readToPixels(this->pixels);
this->pixels_mat = cv::Mat(this->pixels.getHeight(), this->pixels.getWidth(), CV_8UC4, this->pixels.getData());

in update()

this->fbo.begin();
    ofClear(0);
    mesh.drawWireframe();
this->fbo.end();
this->fbo.readToPixels(this->pixels);
cv::GaussianBlur(this->pixels_mat, this->pixels_mat, cv::Size(49, 49), 50, 50);

then in draw()

ofImage draw_image;
draw_image.setFromPixels(this->pixels);
draw_image.draw(0,0);
mesh.drawWireframe();

Hi @GllmSlvn , are you using RGBA colors, and not RGB colors? Alpha blending needs the alpha color channel. Also, there are some posts in the forum about the difficulties of using ofEnableBlendMode() and ofEnableDepthTest() together, just in case that is relevant with drawing the ofMesh. Also, do you want to blur pixels from the fbo after the mesh has been drawn into it? Maybe these lines of code need some editing:

// in ofApp::update()
// pixels will hold the ofPixels from fbo
this->fbo.readToPixels(this->pixels);
// blur pixels_mat?, or get pixels into pixels_mat first?
cv::GaussianBlur(this->pixels_mat, this->pixels_mat, cv::Size(49, 49), 50, 50);

Also, if you have a gpu, you could use a shader to blur the mesh after its drawn into the fbo; this might require a 2nd ofFbo (read from one, write to the other).

1 Like

Hi @GllmSlvn , beside what @TimChi mentions, a shader would be a lot more efficient. But I would recommend you to use the excellent ofxCv addon, which already implements the bluring and several other useful stuff

1 Like