First time here? Check out the FAQ!

Ask Your Question
1

Simple Way to Scale Channels

asked Aug 9 '17

Adi gravatar image

updated Aug 9 '17

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?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
3

answered Aug 9 '17

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 *=)

Preview: (hide)

Comments

1

@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 (Aug 9 '17)edit

You should past this answer here too: https://stackoverflow.com/questions/4...

Adi gravatar imageAdi (Aug 9 '17)edit
1

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

Adi gravatar imageAdi (Aug 9 '17)edit

Question Tools

1 follower

Stats

Asked: Aug 9 '17

Seen: 5,081 times

Last updated: Aug 09 '17