Image Processing using OpenCV and Python
Vivek Sharma
RedHat Certified in Containers and Kubernetes(EX180) ||Arth || Aspiring DevOps Engineer || Python || Docker || Ansible || Jenkins || Kubernetes || OpenShift || Terraform || GitHub || AWS || AZURE
Task Description ??
?? Task 35.1
?? Create image by yourself Using Python Code
?? Task 35.2
?? Take 2 images, crop some part of both the images and swap them. \
?? Task 35.3
?? Take 2 images and combine it to form a single image. For example, collage
What is OpenCV
OpenCV is the huge open-source library for the computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today's systems. By using it, one can process images and videos to identify objects, faces, or even handwriting of a human.
What is Image
An image is an artifact that depicts visual perception, such as a photograph or other two-dimensional picture, that resembles a subject—usually a physical object—and thus provides a depiction of it.
According to a programmer Image can be said a collection of pixels. Pixel is the smallest unit of an image. Technically it is an entry inside an array at a particular position of row and column. Each pixel consists of three colors namely, Red, Green, and Blue. Upon combining these different colors are formed. Multiple pixels together form a complete image. If you go in depth of an image how it is stored in system you will find out that image is stored in the form of a 3-Dimentional array.
The above image shows that image "p1" is stored as numpy array. Each of the list of 3 numbers (in range 0 to 255) is a pixel, we can change these numbers or array dimensions to create or manipulate images.
Create image by yourself Using Python Code
This is the python code to create an image. We need numpy module to deal with multidimensional arrays, PIL module to convert that array to image. Here I have created 3 arrays a, b and c with all entries equal to 0.
import numpy as np import cv2 import random a = np.zeros((500,500,3),dtype=np.uint8) b = np.zeros((1000,1000,3),dtype=np.uint8) c = np.zeros((800,800,3),dtype=np.uint8)
Here I have assigned different values to the different range of pixels. While using cv2 module the color pattern followed is 'BGR' Combination of different numbers in the list gives different colors to pixel. for eg. [0,255,255] is yellow and [255,0,0] is blue.
a[0:500,0:500]=[255,255,0] a[100:200,100:200]=[0,0,255] a[100:200,300:400]=[0,0,255] a[300:350,100:150]=a[350:400,150:350]=a[300:350,350:400]=[0,0,255] cv2.imwrite("viv1.jpg",a)
cv2.imwrite() function saves the image to permanent memory of our system.
To view a image use the following code
viv1 = cv2.imread("viv1.jpg") cv2.imshow("viv1",viv1) cv2.waitKey() cv2.destroyAllWindows()
Here imread() function is used to load an image to RAM and imshow() is used to view the loaded image. waitKey() is used to stop the window until user press any key and destroyAllwindows() is used to close the image window.
New window will pop to show the image.
Similarly I have created two more images.
lis = [[0,0,255],[0,255,0],[255,0,0],[255,255,255],[255,255,0],[255,0,255],[0,255,255]] for i in range(100): b[(0+10*i):(10+10*i) ,(0+10*i):(10+10*i)]=random.choice(lis) b[(990-10*i):(1000-10*i) , (0+10*i):(10+10*i)]=random.choice(lis) cv2.imwrite("viv2.jpg",b) viv2 = cv2.imread("viv2.jpg") cv2.imshow("viv2",viv2) cv2.waitKey() cv2.destroyAllWindows()
for i in range(80): c[0:800-i*10,0:800-i*10]=random.choice(lis) cv2.imwrite("viv3.jpg",c) viv3 = cv2.imread("viv3.jpg") cv2.imshow("viv3",viv3) cv2.waitKey() cv2.destroyAllWindows()
Take 2 images, crop some part of both the images and swap them.
In this section we will learn to detect the faces in two pictures , crop them and swap them using the opencv.
import cv2 import numpy p1 = cv2.imread("p1.jpg") p2 = cv2.imread("p2.jpg") model = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') f1 = model.detectMultiScale(p1) f2 = model.detectMultiScale(p2)
In the above code I have used two images p1.jpg and p2.jpg. Here I am using haarcascade_frontal_face_default.xml model to detect faces. Haar Cascades can be used to detect any types of objects.
model.detectMultiscale() function returns the coordinates of the detected face.
The first two elements are x and y coordinates of top left corner and the next two are width and height of face.
x1=f1[0][0] y1=f1[0][1] x2=x1 + f1[0][2] y2=y1 + f1[0][3] rp1 = cv2.rectangle(p1, (x1,y1),(x2,y2), [0,255,0], 5 ) xx1=f2[0][0] yy1=f2[0][1] xx2=xx1 + f2[0][2] yy2=yy1 + f2[0][3] rp2 = cv2.rectangle(p2, (xx1,yy1),(xx2,yy2), [0,255,0], 5 ) cv2.imshow("rp1",rp1) cv2.imshow("rp2",rp2) cv2.waitKey() cv2.destroyAllWindows()
In this piece of code I have collected the x and y coordinates of the two images and then placed a rectangle over that coordinates using cv2.rectangle() function.
We have detected the faces successfully and now we have to crop and swap them. For this I will use Numpy array slicing concept.
temp = numpy.zeros((y2-y1,x2-x1,3),dtype=numpy.uint8) temp[0:y2-y1,0:x2-x1] = p1[y1:y2,x1:x2] p1[y1:y2,x1:x2] = cv2.resize(p2[yy1:yy2 , xx1:xx2] , (y2-y1 , x2-x1)) p2[yy1:yy2 , xx1:xx2] = cv2.resize(temp , (yy2-yy1 , xx2-xx1))
Here I have created a numpy array temp of same dimension as that of cropped face of image p1.jpg. I have also used resize function to resize the cropped face to fit in the face of other person.
To view the final output.
cv2.imshow("p1",p1) cv2.imshow("p2",p2) cv2.waitKey() cv2.destroyAllWindows()
We have successfully cropped the faces from two images and swapped them.
Take 2 images and combine it to form a single image. For example, collage
To combine to images we will used the numpy array slicing concept again.
In the code below I have used two images p1.jpg and p2.jpg . Then created an array of height equal to sum of heights of two images and width equal to width of p1.
import numpy,cv2 p1 = cv2.imread("p1.jpg") p2 = cv2.imread("p2.jpg") photo = numpy.zeros(( p1.shape[0]+p2.shape[0] ,p1.shape[1], 3),dtype=numpy.uint8) photo[0:p1.shape[0],:]=p1 photo[p1.shape[0]: , :p2.shape[1]]=p2 cv2.imshow("photo",photo) cv2.waitKey() cv2.destroyAllWindows()
This will create a vertical photo collage of the two images.
photo1 = numpy.zeros(( p1.shape[0] ,p1.shape[1]+p2.shape[1], 3),dtype=numpy.uint8) photo1[:,0:p1.shape[1]]=p1 photo1[:,p1.shape[1]:] = cv2.resize(p2,(p2.shape[1],photo1.shape[0])) cv2.imshow("photo1",photo1) cv2.waitKey() cv2.destroyAllWindows()
Similarly the above code will create a horizontal photo collage. In this collage I have resized the p2 image to height before adding it to image photo1.
THANK YOU FOR READING.............
Data Science | GenAI | LLMs
3 年gjb ????????????