Skip to main content

Make an object invisible using Python- OpenCV

In this blog, you will learn how to make an object disappear in a video in python using the OpenCV library.  Check out the demo below.




So let's get started. 
We will start by importing the required libraries and define a variable to capture the video from my webcam.

import cv2
import numpy as np
cap = cv2.VideoCapture(0)

Now, start capturing the image frames from the camera and save the first frame in a variable called replace_image. But why we did this? Keep Reading.

ret, frame = cap.read()
frame = cv2.flip(frame, +1)     ##Mirror image framereplace_image = frame           ##live image to replace with

Let's write a while loop to capture the image frames continuously.


while(1):
    ret, frame = cap.read()         ##Read image frame    frame = cv2.flip(frame, +1)     ##Mirror image frame
    if not ret:                     ##If frame is not read then exit        break    if cv2.waitKey(1) == ord('s'):  ##While loop exit condition        break



Now we need to create an image mask to detect the object which will be completely invisible in the final result. I used a yellow color paper for this purpose.


frame2 = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)         ##BGR to HSVlb = np.array([0, 238, 0])
ub = np.array([255, 255, 255])
mask = cv2.inRange(frame2, lb, ub)


We will first convert the image frame from BGR to HSV and then apply upper and lower bounds to the frame and create a mast. If you did not understand this step then you must check out my blog on Object Detection and Tracking. The original image and image mask are shown below.

Original Image

Masked Image


Now we will apply two morphological techniques on this mask. the first one is opening morphology and the second is dilation.


opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, Kernal)        ##Morphology
Dilate = cv2.dilate(opening, Kernal, iterations=2)


The opening morphology is used to remove any noise(small white patches) in out mask. after implementing this method the mask will be noise-free. But then, what is the use of dilation? Well, after using opening method, you will notice that the image mask has shrunk a little bit. Because of this, in the resultant image yellow borders will be visible. We do not want that. The result should be as realistic as possible. So we dilate the mask which will increase the white portion in out mask a little large.


Dilate


And now comes the tricky part. Pay attention. 
To make an object invisible we simply want to detect an object and replace the area covered by that object with the original image(Where the object was not present at all). In our case, this original image is the replace_image which we saved at the start of our code. I hope the picture below explains the whole idea.




To achieve this, we need to perform the following 3 steps. 
1. Look at the original image shown above. We need to replace the yellow part with the background(background is saved in replace_image). To do this we need to apply dilate mask to the replace_image. So now the yellow part will be replaced by the background. We will call this as midresult1.


midResult1 = cv2.bitwise_and(replace_image, replace_image, mask=Dilate)


midresult1

2. As you can see above we replaced the yellow part with the background. But the rest of the part is black. We want this black part to be the live image frames that are being captured in real-time. For this, first, we have to invert the dilate mask. If you see the result, now the yellow object is black and the rest of the part is white. Now, apply this mask on the frames that are continuously acquired from the camera. let us call this as midresult2.

Invert

Invert = cv2.bitwise_not(Dilate, Dilate, mask=None)  ##invert the mask
midResult2 = cv2.bitwise_and(frame, frame, mask = Invert)


midresult2

3. In the final part just add midresult1 and midresult2. You will get the result as shown below.

Final Result



That is it. Enjoy coding.




Comments

Popular posts from this blog

Iris Detection | Python | OpenCv

 Hello there! Welcome to another blog. In this blog you are going to learn to detect iris using OpenCv python. Here is the video in case you missed it. So, let's get started. We will start by importing the necessary libraries. import cv2 import numpy as np Now, let us import the face and eye classifier files and set the camera resolution as follows. eye = cv2.CascadeClassifier( 'haarcascade_eye.xml' ) face = cv2.CascadeClassifier( 'haarcascade_frontalface_alt.xml' ) Kernal = np.ones(( 3 , 3 ) , np.uint8) #Declare kernal for morphology cap = cv2.VideoCapture( 0 ) cap.set(cv2.CAP_PROP_FRAME_WIDTH , 320 ) ##Set camera resolution cap.set(cv2.CAP_PROP_FRAME_HEIGHT , 240 ) In a while loop let us capture an image frame, flip it(in case your camera captures inverted images) and convert it into a gray scale image. ret , frame = cap.read() ##Read image frame frame = cv2.flip(frame , + 1 ) ##Flip the image in case your camera

Object Distance Calculation Using Contour Area Method In Python - Opencv

Today we will discuss how you can find the distance of an object from the camera using python OpenCV. Check out the video below. Before we continue, you should know how to detect a colored object. Click this link to check out my previous blog on object detection and tracking. I hope after checking out my previous blog, you are able to write your own code to detect and track objects. We will take forward the Object detection and tracking code to find the distance of an object from the camera. So let's start. Let us first understand the principle using which we will find the distance of the object from the camera. Principle:- Area enclosed by the contours of an object decreases as the object moves farther from the camera. This simply means that, if your object is near to the camera, the object will appear bigger. Thus the pixel area occupied by the object will be very large. As you move the object farther from the camera, the object size in the image will start to d

Object Detection And Tracking using Python - Opencv

Let us discuss today how you can detect and track an object in real-time. We will be using Python language and Opencv library for this purpose. Check out the video below. If you have read my previous blogs, you can directly skip down to the contour part. As usual, we need to make a few assumptions for the proper working of this application. This background is always static i.e. there is no addition or subtraction of objects in the background scene. The background-color is always constant. It does not change with time. The object that will be used for writing/painting is of a different color than the background to give us sufficient contrast between foreground and background. We are ready to begin now. Let us start by installing necessary python libraries for our project using  pip install.  We will be needing  Numpy  and  Opencv  libraries. Now create a python project and create a new script. Import the required libraries into python script as shown below. import cv2