Ask Your Question

blending two color images

asked 2014-12-01 07:53:09 -0500

pmh4514 gravatar image


I have two color images (each in a cv::Mat of the same dimensions and type) and I wish to blend them into a third.

I am more-or-less successful using addWeighted but it seems like each of my source images gets "darker" during the process.

Below is an example of three images (these are not my images or even context, but the blended version of "C" from "A" & "B" represents exactly what I am going after)

image description

I assume I have to specify an "alpha" value of <1 to determine blending level, but if I do something like this (where a,b,c refer to the sample image tiles) addWeighted(a, 0.5, b, 0.5, 0.0, c)

I end up with "C" that blends "A" and "B" but it's as if the overall intensity of both A and B were reduced in the process. This example I've posted here seems to retain full intensity.

So what would be the proper way, using OpenCV, to take A and B and make C?

edit retag flag offensive close merge delete



Try Mat direct addition, like c=a+b

Haris gravatar imageHaris ( 2014-12-01 22:15:04 -0500 )edit

I think that you need to mix the channels after creating the blue one (zero mat)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-12-02 05:14:57 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2014-12-02 01:24:04 -0500

rwong gravatar image

Because Red and Green (and also Blue if you have a third input) are separate color channels, it is not necessary to apply the convex combination constraint.

The convex combination constraint is the rule that says the coefficients for input A (i.e. alpha) and for input B (beta) should add up to 1.0. If we consider the trajectory of the point (alpha, beta) in R2 space, the trajectory will be a straight line connecting (1.0, 0.0) and (0.0, 1.0), which blends the image from "all of A, none of B", to "all of B, none of A".

If this constraint is removed, then you can draw up any smooth curve connecting the two corner points (1.0, 0.0) and (0.0, 1.0), and the result will still be a smooth animated transition between the two images. The only requirement is that the curve cannot go beyond the "unit square", which is the cartesian product of [0.0, 1.0] x [0.0, 1.0].

In fact, the image result "c" is formed from a blending parameter of (1.0, 1.0), meaning that it is the pixelwise sum of A and B. This is perfectly legal, because A only contains non-zero red values and B only contains non-zero green values. Their pixelwise sum does not cause an overflow in pixel value.

edit flag offensive delete link more


Interesting. Perhaps my first post was misleading, in that these aren't "pure red" blending with "pure green" necessarily. Take two RGB color photos and merge, them, blending colors while not losing intensity.

When I do a simple addition (1.0,1.0) then the result becomes very washed out - mostly turning to white. The result is identical if I use addWeighted with 1.0,1.0 or if I simply do c=a+b

pmh4514 gravatar imagepmh4514 ( 2014-12-03 13:03:38 -0500 )edit

maybe related code is here

sturkmen gravatar imagesturkmen ( 2015-10-26 01:47:37 -0500 )edit

Question Tools


Asked: 2014-12-01 07:53:09 -0500

Seen: 2,031 times

Last updated: Dec 02 '14