Skip to main content

Opencv Image Blending: How to blend two images?

Opencv Image Blending

This process is very similar to image addition, but in this process images are given different weights. Thus, a transparent mixture is obtained. Images are added according to the following equation:

𝑔(π‘₯) = (1 − 𝛼)𝑓0(π‘₯) + 𝛼𝑓1(π‘₯)

By changing the value of a between 0 and 1, transparency can be adjusted. In this study, 2 images were used to put them together. The weight was given 0.3 to the first picture and 0.7 to the second picture. cv2.addWeighted () applies the following equation to the image.

𝑑𝑠𝑑 = 𝛼 · π‘–π‘šπ‘”1 + 𝛽 · π‘–π‘šπ‘”2 + y


Open images:

image1 = cv2.imread('flower.jpg')
image2 = cv2.imread('bird.jpg')




Blending:
rs = cv2.addWeighted(image1, 0.3, image2, 0.7, 0.0);

Result:


All Code:

import cv2

image1 = cv2.imread('flower.jpg')
image2 = cv2.imread('bird.jpg')

image1 = cv2.resize(image1, (300, 300))
image2 = cv2.resize(image2, (300, 300))

rs = cv2.addWeighted(image1, 0.3, image2, 0.7, 0.0);

cv2.imshow('blend', rs)
cv2.imshow('img1', image1)
cv2.imshow('img2', image2)

cv2.waitKey(0)
cv2.destroyAllWindows()







Comments

Popular posts from this blog

OpenCV Add Border to Image in Python

How to add border to image in python? To create a frame around the image, the function cv2.copyMakeBorder () is used. This function takes the following arguments: src - login image top, bottom, left, right - edge width in number of pixels in corresponding directions borderType - defines what type of border to add. There may be the following types cv2.BORDER_CONSTANT  cv2.BORDER_REFLECT - cv2.BORDER_REFLECT_101 cv2.BORDER_REPLICATE cv2.BORDER_WRAP  value  - Color of border if border type is cv2.BORDER_CONSTANT import cv2 import numpy as np from matplotlib import pyplot as plt BLUE = [ 255 , 0 , 0 ] img1 = cv2.imread( 'flower.jpg' ) replicate = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REPLICATE) reflect = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REFLECT) reflect101 = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REFLECT_101) wrap = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_WRAP) constant= cv2.copyMakeBorder(img1 ...

How to create trackbar in Opencv Python?

Create trackbar in Opencv and Python import cv2 import numpy as np def func ( x ): pass img = cv2.imread( "flower.jpg" ) cv2.namedWindow( 'image' ) cv2.createTrackbar( 'R' , 'image' , 0 , 255 , func) cv2.createTrackbar( 'G' , 'image' , 0 , 255 , func) cv2.createTrackbar( 'B' , 'image' , 0 , 255 , func) switch = '0 : OFF \n 1 : ON' cv2.createTrackbar(switch , 'image' , 0 , 1 , func) while ( 1 ): cv2.imshow( 'image' , img) k = cv2.waitKey( 1 ) & 0xFF if k == 27 : break r = cv2.getTrackbarPos( 'R' , 'image' ) g = cv2.getTrackbarPos( 'G' , 'image' ) b = cv2.getTrackbarPos( 'B' , 'image' ) s = cv2.getTrackbarPos(switch , 'image' ) if s == 0 : img[:] = 0 else : img[:] = [b , g , r] cv2.destroyAllWindows() Trackbar