Multiply ofxCvGrayscaleImage with float

I am trying to multiply an ofxCvGrayscaleImage with a float in such a way:

ofxCvGrayscaleImage grayImg;
grayImg = anotherGrayImg;
grayImg = grayImg *0.99;

The last line throws the error:
“Invalid operands to binary expression (‘ofxCvGrayscaleImage’ and ‘double’)”

When trying this:
grayImg *= 0.99;

I get the Error:
"No viable overloaded ‘*=’ "

On the other Hand, adding and substracting floats in the following way compiles:
myCvImageB -= 0.99;

But it doesn’t compile like this:
myCvImageB = myCvImageB -0.99;

How can I multiply an ofxCvGrayscaleImage with a float?

System is: OSX Catalina, OF 0.11.0, XCode 12.4

Hey @mativa , I wonder if the underlying data are unsigned chars in an ofxCvGrayscaleImage? Anyway, I looked at he documentation page for ofxOpenCv. An ofxCvImage is a parent class for many of the different image classes; it has an overload for operator*= that multiplies by another image, but not a scalar like a float. However, an ofxCvFloatImage does have an overload for *= that will use a float, so you could convert to one of those with operator= before multiplying it by a float.

ofxCvImage::getPixels() returns a raw pointer to the pixel data. So, you could probably loop thru the pixels of an ofxCvGrayscaleImage and multiply the pixels by a float. And you may want to do this if the data are unsigned chars, so you could convert them to floats first before multiplying with another float. Also there may be a more direct way to do this with openCV types, or maybe with the ofxCv addon (by Kyle McDonald).

Finally, a shader would make quick work of multiplying an image, if it makes sense to use one. ofxCv also has some nice methods for converting oF types (ofImage) to openCV types and back again.

1 Like