GOT Snapchat Filter

How do i overlay two images in opencv? is there a function available for that

hey @devchopra999_11c6416ab7f09bbf,
yes there are functions to do that in opencv

but the thing is they will confuse you a lot , actually a lot.

So , just iterate over the pixels and update there values with ones you want

1 Like

@prashant_ml how do i iterate over pixels?

just using loops.

image is an array of values,
you just need to iterate over them in a nested way

1 Like

@prashant_ml i tried iterating over the pixels but it doesnt work, please help
import cv2
import numpy as np

eyes_cascade=cv2.CascadeClassifier(“frontalEyes35x16.xml”)
glasses=cv2.imread(“glasses.png”)
Person=cv2.imread(“Before.png”)
print(glasses.shape)
print(Person.shape)
EYES=eyes_cascade.detectMultiScale(Person,1.3,5)

for eyes in EYES:
x,y,w,h=eyes
for i in range(glasses.shape[0]):
for j in range(glasses.shape[1]):
eyes[i][j]=glasses[i][j]

the glasses image is a 4dimensional image, you need to just ignore that 4 channel if it is 1 ( check its value once. )

and then it will work , else your code looks good to me.

@prashant_ml when I printed the shape of glasses it gave me (221, 483, 3)
so i converted it to BGRA image and its shape changed to (221, 483, 4) and I checked the vale of all the rows and columns for this 4th dimension and it gave all rows and columns filled with 255. do I have to change it to 1?
if all rows and columns of the 4th dimension are 1, it shows a blank white image as i increase the number its opacity increases with 255 being the highest

You don’t need to convert it explicitly…
Your file is a png fie , it has that 4 channel itself in it.

try this, and check the shape
glasses=cv2.imread(“glasses.png”,-1)

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

@prashant_ml yes it has 4 channels, the last one is 0 , I need to make it 1?

i did this, but i am getting a red box on the eyes instead of the glasses image.

EYES=eyes_cascade.detectMultiScale(Person,1.3,5)

        for eyes in EYES:
            x,y,w,h=eyes
            for z in range(0,2):
                for i in range(y,y+h):
                    for j in range(x,x+w):
                        Person[i][j][z]=glasses[i][j][z]
                        print(Person[i][j][z])

Python doubt 21

hey @devchopra999_11c6416ab7f09bbf ,
just share me your code.
I will check it.

python doubt 22

python doubt 22-3

@prashant_ml i shared as screenshots so its easier to understand with the imagesPython doubt 22-4

two things

  1. dont convert your glasses to RGB , as you need the 4th channel.
  2. kindly share you code on github , so that i can edit it

@prashant_ml does this work here ? , i don’t know github enough yet

hey @devchopra999_11c6416ab7f09bbf ,
sorry to be so late

try this
EYES = eyes_cascade.detectMultiScale(Person,1.3,5)

x,y,w,h = EYES[0]

overlay = cv2.resize(glasses,(w,h))

for i in range(overlay.shape[0]):

for j in range(overlay.shape[1]):

    if overlay[i,j,3]>0:

        Person[y+i,x+j,:]=overlay[i,j,:-1]

@prashant_ml please explain the code, why are we taking Person[y+i,x+j,:] instead of Person[x+i,y+j,:] ?

and how the challenge says i need to convert the image into (-1,3) but i am getting this error

error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-ldg9ejcr\opencv\modules\imgproc\src\resize.cpp:4054: error: (-215:Assertion failed) inv_scale_x > 0 in function 'cv::resize

on using this line -
a = cv2.resize(Person,(-1,3))

@prashant_ml please reply

Sorry there was some issue with my account , hence couldn’t reply.

This is how opencv reads the images , not by width x height , but as height x width.