Image Processing Using OpenCV in Python
In this blog, we are going to create an Image, crop two images face then swap, and at last going to collage all.
First, we will be exploring various details about image processing and the OpenCV library in python.
What is Image Processing?
Images are Multi-Dimensional arrays in the computer world. 3D Arrays are Collor Images and 2D Arrays are white and Black images.
Colored images are stored in the form of three-dimensional (3D) arrays in the computer, where only 0–255 values are stored furthermore 0 and 255 represent black and white color respectively.
Image processing focuses on developing a computer system that is able to perform processing on an image. It was aimed at human beings to improve the visual effect of people.
Video Processing is performing operations on the video frame by frame. Frames are nothing but a single image at a single point in time. Video is nothing but multiple images at a time. The repeated loop of capturing the image and displaying it gives out a video which can also be processed inside the loop to get a processed video.
Python provides some of the libraries of image process, listed a few famous libraries
- OpenCV- The library is focused on image processing, face detection, object detection, and more. Feature toolkits, facial & gesture recognition, Human-computer interaction, Mobile robotics, Object identification, and others.
- Scikit-image- uses NumPy arrays as image objects by transforming the original pictures. These ndarrys(2D or 3D) can either be integers (signed or unsigned) or floats. Among different methods, data scientists often utilize the greyscale technique where each pixel is a shade of grey.
- Mahotas- Mahotas allows developers to use its advanced features such as haralick, local binary patterns, and more. It can compute 2D and 3D images through mahotas.features.haralick module and perform advanced image processing by extracting information from pictures.
- Numpy and Scipy libraries − For image manipulation and processing.
- Sckikit − Provides lots of algorithms for image processing.
- Python Imaging Library (PIL) − To perform basic operations on images like create thumbnails, resize, rotation, convert between different file formats, etc.
- SimplelTK, Pillow, and Matplotlib few more
Here we are going to use OpenCV
- OpenCV is one of the most popular computer vision libraries. If you want to start your journey in the field of computer vision, then a thorough understanding of the concepts of OpenCV is best to understand core.
- OpenCV has become a popular and open source library due to its ease of use and readability. The library is focused on image processing, face detection, object detection, and more. It is written in C++ but also comes with Python wrapper and can work in tandem with NumPy, SciPy, and Matplotlib.
- Color code is RGB but the color code format is used in OpenCV is BGR(Blue Green Red).
Install OpenCV
Install opencv before that make sure pip is upgraded
python -m pip install –upgrade pip
Install openCV
pip install opencv-python
First of all, to use OpenCV import cv2 module
import cv2
Now our module is loaded, to read a image use imread(args) method of the cv2 module, specify the path of image in args place.
img = cv2.imread(Vinu.jpeg)
The image is now treated as a matrix with rows and columns values stored in img. This can be proved by seeing type of image:
print(type(img))
<class 'numpy.ndarray'>
It’s showing NumPy array that is why using OpenCV is so easy. All the time you are working with a NumPy array.
To display image, we can use imshow(args) method of the cv2 module, specify the path of image in args place.
The waitkey(0) functions take time as an argument in milliseconds as a delay for the window to close. Here we set the time to zero to show the window forever until we close it manually.
cv2.imshow("MyImage", img)
cv2.waitkey(0)
Capturing Image using WebCam
Convert image to grayscale (Black & White)
The easy way to convert an image in grayscale is to load it like this,
img = cv2.imread("Vinu.jpeg", 0)
There is another method using BGR2GRAY.
To convert a color image into a grayscale image, use the BGR2GRAY attribute of the cv2 module.
Use the cvtColor() method of the cv2 module which takes the original image and the COLOR_BGR2GRAY attribute as an argument.
This is demonstrated in the example below:
import cv2img = cv2.imread("pyimg.jpg")
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Original Image", img)
cv2.imshow("Gray Scale Image", gray_img)
cv2.waitKey(0)
Bellow image after changing a Colorful to Gray(black and weight)
Resize an Image
To resize an image, you can use the resize() method of openCV. In the resize method, you can either specify the values of x and y axis or the number of rows and columns which tells the size of the image.
Import and read the image
import cv2
img = cv2.imread("pyimg.jpg")
Now using the resize method with axis values
import cv2
img = cv2.imread("pyimg.jpg")newImg = cv2.resize(img, (0,0), fx=0.75, fy=0.75)
cv2.imshow('Resized Image', newImg)
cv2.waitKey(0)
Now using the row and column values to resize the image:
newImg = cv2.resize(img, (750, 450))
cv2.imshow('Resized Image', newImg)
cv2.waitKey(0)
We say we want 550 columns (the width) and 350 rows (the height).
Video Streaming using WebCam
Below video for video stream
Detect Face Using “haarcascade_frontalface_default.xml” Model
Before starting download this model using below link
Detect face on Image or Live Streaming
Creating An Image Using Python Code
The output is as follows:
Detect face and swap faces
The output is as follows:
Collage Images: Combine all images to one image
The output is as follows:
GitHub Code: