I am glad that you have found a working solution to your problem, but this seems to be a workaround. The real reason for this behaviour lies somewhere else. The problem here is that mean = (img1 * 0.5) + (img2 * 0.5) is returning a matrix with float32 data type which contains values in range 0.0 - 255.0. You can verify this by using print mean.dtype. Since the new matrix values have been converted to float unintentionally, we can revert this operation by using (img_1 * 0.5 + img_2 * 0.5).astype("uint8"). In case of cv2.addWeighted() it automatically returns you a matrix of data type uint8 and all things would work fine.

My concern is with the conclusion that you have drawn:

The issue is that the cv2.imshow() method used to display images, expects your image arrays to be normalized, i.e. in the range [0,1].

cv2.imshow() works just fine with range of [0-255] and [0.0-1.0], but the issue arises when you pass a matrix whose values are in range [0-255], but the dtype is float32 instead of uint8.

Answer from ZdaR on Stack Overflow
🌐
OpenCV-Python Tutorials
opencv24-python-tutorials.readthedocs.io › en › latest › py_tutorials › py_core › py_image_arithmetics › py_image_arithmetics.html
Arithmetic Operations on Images — OpenCV-Python Tutorials beta documentation
img1 = cv2.imread('ml.png') img2 = cv2.imread('opencv_logo.jpg') dst = cv2.addWeighted(img1,0.7,img2,0.3,0) cv2.imshow('dst',dst) cv2.waitKey(0) cv2.destroyAllWindows()
🌐
EDUCBA
educba.com › home › software development › software development tutorials › programming languages tutorial › opencv addweighted
OpenCV addWeighted | How does addWeighted Function Work | Example
April 7, 2023 - import cv2 # readin the two images source1 = cv2.imread('eduCBA.JPG', cv2.IMREAD_COLOR) source2 = cv2.imread('eduCBA.JPG', cv2.IMREAD_COLOR) # blending the image with alpha and beta values as 1 dest = cv2.addWeighted(source1, 1, source2, 1, 0.0) # Saving the output image cv2.imwrite('img.png', dest) cv2.imshow('img.png', dest) # Wait for a key cv2.waitKey(0) # Destroy the window which is open cv2.destroyAllWindows()
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
OpenCV
docs.opencv.org › 3.4.20 › d5 › dc4 › tutorial_adding_images.html
OpenCV: Adding (blending) two images using OpenCV
Core.addWeighted( src1, alpha, src2, beta, 0.0, dst); HighGui.imshow("Linear Blend", dst); HighGui.waitKey(0); System.exit(0); } } public class AddingImages { public static void main(String[] args) { // Load the native library. System.loadLibrary(Core.NATIVE_LIBRARY_NAME); new AddingImagesRun().run(); } } Python · Download the source code from here. from __future__ import print_function · import cv2 as cv ·
🌐
Medium
medium.com › featurepreneur › blending-images-using-opencv-bfc9ab3697b7
Blending images using OpenCV. Have you ever wondered how blended… | by Maximin Joshua | featurepreneur | Medium
December 1, 2021 - The documentation says that image blending basically images addition, but different weights are given to images so that it gives a feeling of blending or transparency. Images are added as per the equation below: By varying α from 0→1, you ...
Top answer
1 of 2
4

I am glad that you have found a working solution to your problem, but this seems to be a workaround. The real reason for this behaviour lies somewhere else. The problem here is that mean = (img1 * 0.5) + (img2 * 0.5) is returning a matrix with float32 data type which contains values in range 0.0 - 255.0. You can verify this by using print mean.dtype. Since the new matrix values have been converted to float unintentionally, we can revert this operation by using (img_1 * 0.5 + img_2 * 0.5).astype("uint8"). In case of cv2.addWeighted() it automatically returns you a matrix of data type uint8 and all things would work fine.

My concern is with the conclusion that you have drawn:

The issue is that the cv2.imshow() method used to display images, expects your image arrays to be normalized, i.e. in the range [0,1].

cv2.imshow() works just fine with range of [0-255] and [0.0-1.0], but the issue arises when you pass a matrix whose values are in range [0-255], but the dtype is float32 instead of uint8.

2 of 2
3

Answering my own question, to help others who get confused by this:

Both methods 1 and 2 yield the same result. You can verify this by writing the mean image to disk using cv2.imwrite. The issue is not with the methods.

The issue is that the cv2.imshow method used to display images, expects your image arrays to be normalized, i.e. in the range [0,1]. In my case, both the image arrays are 8-bit unsigned integers and so, its pixel values are in the range [0,255]. Since mean is an average of the two arrays, its pixel values are also in the range [0,255]. So when I passed mean to cv2.imshow, pixels having values greater than 1 were interpreted as having a value of 255, resulting in vastly different visuals.

The solution is to normalize mean before passing it to cv2.imshow:

# Method 1
mean = (img1 * 0.5) + (img2 * 0.5)

# Method 2
mean = cv2.addWeighted(img1,0.5,img2,0.5,0)

# Note that the division by 255 results in the image array values being squeezed to [0,1].

cv2.imshow("Averaged", mean/255.)
🌐
GeeksforGeeks
geeksforgeeks.org › python › addition-blending-images-using-opencv-python
Addition and Blending of images using OpenCV in Python - GeeksforGeeks
April 1, 2023 - First image is given a weight of 0.3 and second image is given 0.7, cv2.addWeighted() applies following equation on the image : img = a . img1 + b . img 2 + y Here y is taken as zero. Below is code for Blending of images using OpenCV : ... # Python program for blending of # images using OpenCV # import OpenCV file import cv2 # Read Image1 mountain = cv2.imread('F:\mountain.jpg', 1) # Read image2 dog = cv2.imread('F:\dog.jpg', 1) # Blending the images with 0.3 and 0.7 img = cv2.addWeighted(mountain, 0.3, dog, 0.7, 0) # Show the image cv2.imshow('image', img) # Wait for a key cv2.waitKey(0) # Distroy all the window open cv2.distroyAllWindows()
🌐
OpenCV
docs.opencv.org › 3.0-beta › doc › py_tutorials › py_core › py_image_arithmetics › py_image_arithmetics.html
Arithmetic Operations on Images — OpenCV 3.0.0-dev documentation
img1 = cv2.imread('ml.png') img2 = cv2.imread('opencv_logo.jpg') dst = cv2.addWeighted(img1,0.7,img2,0.3,0) cv2.imshow('dst',dst) cv2.waitKey(0) cv2.destroyAllWindows()
🌐
Idiot Developer
idiotdeveloper.com › home › image masking with opencv addweighted
Image Masking with OpenCV AddWeighted - Idiot Developer
August 15, 2024 - import cv2 import numpy as np image ... the original image. This is done by using the addWeighted function, which blends the original image with the mask....
Find elsewhere
🌐
OpenCV Q&A Forum
answers.opencv.org › question › 122370 › addweighted-function-in-cv2
addWeighted() function in cv2 - OpenCV Q&A Forum
January 11, 2017 - The addWeighted function can be defined as cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) → dst src1 – first input array. alpha – weight of the first array elements. src2 – second input array of the same size and channel number as src1. beta – weight of the second ...
🌐
ProgramCreek
programcreek.com › python › example › 89436 › cv2.addWeighted
Python Examples of cv2.addWeighted
def colorize(self, label_map, image_canvas=None): height, width = label_map.shape color_dst = np.zeros((height, width, 3), dtype=np.uint8) color_list = self.configer.get('details', 'color_list') for i in range(self.configer.get('data', 'num_classes')): color_dst[label_map == i] = color_list[i % len(color_list)] color_img_rgb = np.array(color_dst, dtype=np.uint8) color_img_bgr = cv2.cvtColor(color_img_rgb, cv2.COLOR_RGB2BGR) if image_canvas is not None: image_canvas = cv2.addWeighted(image_canvas, 0.6, color_img_bgr, 0.4, 0) return image_canvas else: return color_img_bgr
🌐
LabEx
labex.io › tutorials › opencv-arithmetic-operations-on-images-38502
Arithmetic Operations on Images with OpenCV-Python | LabEx
Use .addWeighted() to apply the equation to images that were read in the previous step. result = cv2.addWeighted(img1,0.7,img2,0.3,0) Write the result out. cv2.imwrite('Image_Blending.jpg', result) The generated image Image_Blending.jpg looks ...
🌐
Note.nkmk.me
note.nkmk.me › home › python › opencv
Alpha blending and masking of images with Python, OpenCV, NumPy | note.nkmk.me
May 14, 2019 - Composite two images according ... not change much, but OpenCV2 may be different, so be careful. Use cv2.addWeighted() to do alpha blending with OpenCV....
🌐
TutorialsPoint
tutorialspoint.com › opencv_python › opencv_python_image_addition.htm
OpenCV Python - Image Addition
Cv2.addWeighted(src1, alpha, src2, beta, gamma) The parameters of the addWeighted() function are as follows − · src1 − First input array. alpha − Weight of the first array elements. src2 − Second input array of the same size and channel number as first ·
🌐
Readthedocs
opencv2-python-tutorials.readthedocs.io › en › latest › py_tutorials › py_core › py_image_arithmetics › py_image_arithmetics.html
Arithmetic Operations on Images — Python documentation
img1 = cv2.imread('ml.png') img2 = cv2.imread('opencv_logo.jpg') dst = cv2.addWeighted(img1,0.7,img2,0.3,0) cv2.imshow('dst',dst) cv2.waitKey(0) cv2.destroyAllWindows()
🌐
OpenCV
docs.opencv.org › 4.13.0 › d0 › d86 › tutorial_py_image_arithmetics.html
OpenCV: Arithmetic Operations on Images
void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1)
🌐
OpenCV
docs.opencv.org › 2.4.13.7 › modules › core › doc › operations_on_arrays.html
Operations on Arrays — OpenCV 2.4.13.7 documentation
December 31, 2019 - C++: void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1)¶ · Python: cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) → dst¶ · C: void cvAddWeighted(const CvArr* src1, double alpha, const CvArr* src2, double beta, double gamma, CvArr* dst)¶ ·
🌐
Python Examples
pythonexamples.org › python-opencv-add-blend-two-images
Python OpenCV - Add or Blend Two Images
Python Program to Blend Two Images - Using OpenCV library, you can add or blend two images with the help of cv2.addWeighted() method. The syntax is: dst = cv.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]])
🌐
Huihoo
docs.huihoo.com › opencv › 3.0 › d0 › d86 › tutorial_py_image_arithmetics.html
OpenCV: Arithmetic Operations on Images - Huihoo
Here I took two images to blend them together. First image is given a weight of 0.7 and second image is given 0.3. cv2.addWeighted() applies following equation on the image.