9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
Blending and Pasting Images
Prepared by: Dr.S.Rajalakshmi
Working with Image
In [3]: import cv2
read the image
In [4]: img1 = cv2.imread("WindowsLogo.jpg")
img2 = cv2.imread("LinuxLogo.jpg")
verify the shape of the image
In [5]: img1.shape
Out[5]: (240, 320, 3)
In [6]: img2.shape
Out[6]: (240, 320, 3)
Convert BGR to RGB
In [7]: img1 = cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(img2,cv2.COLOR_BGR2RGB)
Display the image
In [8]: import matplotlib.pyplot as plt
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 1/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
In [9]: plt.imshow(img1)
Out[9]: <matplotlib.image.AxesImage at 0x2c895671c70>
In [10]: plt.imshow(img2)
Out[10]: <matplotlib.image.AxesImage at 0x2c8956df760>
Requirements for addWeighted Function:
two images must be of same size
two images must be of same type
two images must have same channel
Blending Images of same dimension
In [11]: blended = cv2.addWeighted(src1=img1,alpha=0.8,src2=img2,beta=0.2,gamma
#change the value for alpha and beta and infer the results
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 2/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
In [12]: plt.imshow(blended)
Out[12]: <matplotlib.image.AxesImage at 0x2c8957387f0>
Blending Images of different dimension
overlaying small image on top of large image
In [13]: img3 = cv2.imread("cat1.jpg")
plt.imshow(img3)
Out[13]: <matplotlib.image.AxesImage at 0x2c895795100>
In [14]: img4 = cv2.imread("scene1.jpg")
In [15]: img3.shape
Out[15]: (259, 194, 3)
In [16]: img4.shape
Out[16]: (177, 284, 3)
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 3/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
In [17]: img4_rs = cv2.resize(img4,(800,800))
In [18]: plt.imshow(img4_rs)
Out[18]: <matplotlib.image.AxesImage at 0x2c896aaf220>
In [19]: img3_rs = cv2.resize(img3,(200,200))
#img3.resize(200,200)
In [20]: plt.imshow(img3_rs)
Out[20]: <matplotlib.image.AxesImage at 0x2c896cf4850>
In [21]: large_img = img4_rs
small_img = img3_rs
In [22]: x_offset = 0 #column index
y_offset = 0 #row index
In [23]: large_img[y_offset:y_offset+small_img.shape[0], x_offset:x_offset+small
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 4/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
In [24]: plt.imshow(large_img)
Out[24]: <matplotlib.image.AxesImage at 0x2c896d55610>
Creating Region of interest
In [25]: img5 = cv2.imread("messi5.jpeg")
img6 = cv2.imread("opencv.png")
plt.imshow(img6)
Out[25]: <matplotlib.image.AxesImage at 0x2c896f86d00>
In [26]: img5.shape
Out[26]: (148, 254, 3)
In [27]: img6.shape
Out[27]: (148, 173, 3)
In [28]: img6_rs =cv2.resize(img6,(600,610)) #width,height
img5_rs = cv2.resize(img5,(1200,1200))
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 5/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
In [29]: img6_rs.shape
Out[29]: (610, 600, 3)
In [30]: plt.imshow(img6_rs)
Out[30]: <matplotlib.image.AxesImage at 0x2c895756400>
In [31]: plt.imshow(img5_rs)
Out[31]: <matplotlib.image.AxesImage at 0x2c89562b190>
Creating Region of Interest
get the shape of large image
get the shape of small image
find the position where to place the image
top left corner: roi = img1[0:rows, 0:cols ] # TOP LEFT CORNER where rows represent
height of small image, cols represent width of small image. img1 is the large image
bottom right corner: img1[y_offset:H,x_offset:W] where H, W represents height and
width of large image "img 1" start at: x_offset = width of large image- width of small
image; y_offset = height of large image - height of small image
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 6/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
In [32]: #get the shape of the resized img 5 - bigger image
img5_rs.shape
Out[32]: (1200, 1200, 3)
In [33]: #get the shape of the resized img 6 - smaller image
img6_rs.shape
Out[33]: (610, 600, 3)
In [34]: #get the postion where to place smaller image on bigger image
x_offset = 1200 - 600 # width 600 -- column
y_offset = 1200 - 610 #height 590 --row
In [35]: #To create ROI of the same shape
rows,cols,chans = img6_rs.shape
roi = img5_rs[y_offset:1200,x_offset:1200]
#roi = img5_rs[0:rows,0:cols]
plt.imshow(roi)
Out[35]: <matplotlib.image.AxesImage at 0x2c8951ba700>
In [36]: #shape of ROI
roi.shape
Out[36]: (610, 600, 3)
Steps for creating mask
convert the image to grey
create thresholding or white background using full()
create inverse mask
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 7/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
Convert Image to Grayscale (cv2.cvtColor)
In [37]: #step 1 Converting image to gray
img6gray = cv2.cvtColor(img6_rs,cv2.COLOR_BGR2GRAY)
In [38]: img6gray
Out[38]: array([[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
[255, 255, 255, ..., 255, 255, 255],
...,
[252, 252, 215, ..., 255, 255, 255],
[252, 252, 214, ..., 255, 255, 255],
[252, 252, 214, ..., 255, 255, 255]], dtype=uint8)
In [39]: img6gray.shape
Out[39]: (610, 600)
In [40]: plt.imshow(img6gray,cmap='gray')
Out[40]: <matplotlib.image.AxesImage at 0x2c89579ce80>
Invert the grey scale image
In [41]: mask_inv = cv2.bitwise_not(img6gray)
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 8/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
In [42]: mask_inv
Out[42]: array([[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
...,
[ 3, 3, 40, ..., 0, 0, 0],
[ 3, 3, 41, ..., 0, 0, 0],
[ 3, 3, 41, ..., 0, 0, 0]], dtype=uint8)
In [43]: plt.imshow(mask_inv,cmap='gray')
Out[43]: <matplotlib.image.AxesImage at 0x2c89513c8b0>
In [44]: mask_inv.shape
Out[44]: (610, 600)
Create a White Background (cv2.cvtColor)
In [45]: white_background = np.full(img6_rs.shape, 255, dtype=np.uint8)
white_background.shape
Out[45]: (610, 600, 3)
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 9/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
In [46]: white_background
Out[46]: array([[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
...,
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[255, 255, 255],
[255, 255, 255],
[255, 255, 255]]], dtype=uint8)
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 10/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
In [47]: mask_inv
Out[47]: array([[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
[ 0, 0, 0, ..., 0, 0, 0],
...,
[ 3, 3, 40, ..., 0, 0, 0],
[ 3, 3, 41, ..., 0, 0, 0],
[ 3, 3, 41, ..., 0, 0, 0]], dtype=uint8)
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 11/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
Apply Mask to White Background
In [48]: bk = cv2.bitwise_or(white_background, white_background, mask=mask_inv)
bk
Out[48]: array([[[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
...,
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0]],
[[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
...,
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0]],
[[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
...,
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0]],
...,
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0]],
[[255, 255, 255],
[255, 255, 255],
[255, 255, 255],
...,
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0]]], dtype=uint8)
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 12/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
In [49]: plt.imshow(bk)
Out[49]: <matplotlib.image.AxesImage at 0x2c897dfbaf0>
In [50]: plt.imshow(mask_inv,cmap='gray')
Out[50]: <matplotlib.image.AxesImage at 0x2c897f73790>
Extract the foreground
In [51]: foreg = cv2.bitwise_or(img6_rs,img6_rs,mask = mask_inv)
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 13/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
In [52]: plt.imshow(foreg,cmap='gray')
Out[52]: <matplotlib.image.AxesImage at 0x2c897fcb520>
In [53]: foreg.shape
Out[53]: (610, 600, 3)
Apply roi(extracted from large image) and foreground (extracted
in small image)
In [54]: Final_roi = cv2.bitwise_or(roi,foreg)
In [55]: plt.imshow(Final_roi)
Out[55]: <matplotlib.image.AxesImage at 0x2c8983743d0>
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 14/15
9/7/24, 4:42 AM 2 Blending_Images-Copy1 - Jupyter Notebook
Place the final roi on the portion of larger image
In [56]: #add the image in the rest of the image
large_img = img5_rs
small_img = Final_roi
large_img[y_offset:y_offset+small_img.shape[0], x_offset:x_offset+small
#large_img[y_offset:y_offset+small_img.shape[0],x_offset:x_offset+small
In [57]: plt.imshow(large_img)
Out[57]: <matplotlib.image.AxesImage at 0x2c8984ec100>
localhost:8889/notebooks/Computer Vision/Module 2/2 Blending_Images-Copy1.ipynb 15/15