yodish
yodish

Reputation: 807

Using Python and Numpy to blend 2 images into 1

I need to take 2 numpy.ndarrays as an arguments and iterate through each of them pixel by pixel, adding the 2 values and dividing by 2.

Essentially creating a blended image of the two and returning it as a numpy.ndarray

This is what i've come up with, but could really use some advice.

    def blendImages(image1, image2):            
        it1 = np.nditer(image1)
        it2 = np.nditer(image2)            
        for (x) in it1:
            for (y) in it2:
                newImage = (x + y) / 2
        return newImage

Upvotes: 5

Views: 15808

Answers (3)

freshNfunky
freshNfunky

Reputation: 69

the map blending function is adding a weighted array with an inverse weight of the second array like this:

result =  array1 * weight + array2 * (1-weight)

using a weight for array1 of 0.8 this calculates to:

result = array1 * 0.8 + array2 * 0.2 

NOTE: we assume weight 1.0 is the maximum weight representing 100%

Upvotes: 0

Miki
Miki

Reputation: 41775

You can use OpenCV function addWeighted like:

 cv2.addWeighted(img1,0.5,img2,0.5,0)`

Upvotes: 9

ebo
ebo

Reputation: 9215

As long as the arrays are the same size:

newImage = 0.5 * image1 + 0.5 * image2

Upvotes: 6

Related Questions