CvGrayscaleimgae problem

Hello guys,
When I try to translate the fbo to grayscale image and using it to find contour then the unpredictable noisy pop out.


Did anyone know why this kind of things happen?

Hello @Shaoming_Li you can share the relevant part of your code, or the entire code so others can investigate what can be happening.

Hey this kind of looks like what happens when there is a mis-match in the number of channels between the source image and the target. It would definitely help to see some code.

@dimitre @TimChi Hello brother!Here are the code!Thanks for response!

When I put the code at draw() as below

Then I get the right picture.

defaultgrayMouthROIImg = mouthImgProcessing(dep16MouthMat);
test_hoo = processMask();
contourFinder.findContours(defaultgrayMouthROIImg, minBlobSize, maxBlobSize, maxNumBlobs, findHoles, useApproximation);

But when I write the code as function(processmask) and put the grayscaleimage in update() and put into contourfinder then the problem pop up

Hum, there are a of allocations, renders, and other stuff happening in ofApp::draw(). Also its really hard to read code as a screen shot, so maybe next time try the </> button and copy/paste.

I would suggest that the following become class members in ofApp, which are allocated once in ofApp::setup(). Then do all of the rendering and image processing in ofApp::update(), and draw the final ofFbo (or whatever ends up on the screen) in ofApp::draw():

// in ofApp.h
// all ofFbo, ofImage, ofxCvImage types and related could be members of ofApp
ofFbo fbo;
ofPixels pix;
ofxCvGrayscaleImage g2;
int width; // for good practice,
int height; // convenience, and maintenance

// in ofApp::setup()
width = 50;
height = 50;
fbo.allocate(width, height, GL_RGBA); // a 4-channel ofFbo
pix.allocate(width, height, 4); // allocate pix with same number of channels as fbo
g2.allocate(width, height); // its an ofxCvGrayscaleImage, so presumably it has 1 channel

Explicit allocation of pix and test_ho might help a lot, so that the allocation is not left up to the compiler to figure this out with calls to .readToPixels(pix) and .setFromPixels(pix).

I’m not sure what test-ho is, or how the image processing works with it, but maybe just make sure things consistent with the functions and documentation in ofxCvGrayscaleImage and ofxOpenCv in general. You might be able to make good use of some of the ofxCvGrayscaleImage functions when setting the colors of g2. Also, there is maybe (intuitively) a better way to write the following line of code:

// in the nested loop:
g2.getPixels().setColor(i, j, pix.getColor(i, j)); // 3- or 4- channel color to 1-channel (?)