Ask Your Question

Simple Way to Scale Channels

asked 2017-08-09 08:01:36 -0500

Adi gravatar image

updated 2017-08-09 08:04:51 -0500

It seems that given a multichannel image, e.g. BGR, img I cannot do this:

img *= cv::Scalar(1.5,0.5,2.1)

I'd like to scale each channel by a different float factor. Is there a simple way to do this? I could use cv::transform() but that seems like overkill (I also obviously don't want to iterate on all the pixels).

Any suggestions?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-08-09 08:12:57 -0500

berak gravatar image

you can use multiply:

Mat a(1,1,CV_8UC3,Scalar(20,40,60));
cerr << a << endl;
multiply(a, Scalar(1.2,0.5,.2), a);
cerr << a << endl;

[ 20,  40,  60]
[ 24,  20,  12]

(i can only guess, but since the * operator applied on Mat is a matrix multiplication, which does not make sense for a Scalar, there is no overload for *=)

edit flag offensive delete link more



@berak: Yeah, this seems to work. IIUC, this is essentially the same as using cv::transform(). It is matrix multiplication after a reshape.

Adi gravatar imageAdi ( 2017-08-09 08:20:38 -0500 )edit

You should past this answer here too:

Adi gravatar imageAdi ( 2017-08-09 08:20:51 -0500 )edit

For completeness, this also works: img = img.mul(Scalar(1.2,0.5,.2));

Adi gravatar imageAdi ( 2017-08-09 08:38:50 -0500 )edit

Question Tools

1 follower


Asked: 2017-08-09 08:01:36 -0500

Seen: 2,762 times

Last updated: Aug 09 '17