Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Working With Computer Vision Using Python

Uploaded by

pallabghoshmca
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Working With Computer Vision Using Python

Uploaded by

pallabghoshmca
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Working with Computer Vision using Python:

Installing Useful Packages


For Computer vision with Python, you can use a popular library
called OpenCV (Open Source Computer Vision). It is a library of programming
functions mainly aimed at the real-time computer vision.

Reading, Writing and Displaying an Image


Most of the CV applications need to get the images as input and produce the
images as output. In this section, you will learn how to read and write image
file with the help of functions provided by OpenCV.
OpenCV provides the following functions for this purpose −
• imread() function − This is the function for reading an image. OpenCV
imread() supports various image formats like PNG, JPEG, JPG, TIFF, etc.

• imshow() function − This is the function for showing an image in a


window. The window automatically fits to the image size. OpenCV
imshow() supports various image formats like PNG, JPEG, JPG, TIFF, etc.

• imwrite() function − This is the function for writing an image. OpenCV


imwrite() supports various image formats like PNG, JPEG, JPG, TIFF, etc.

Import the OpenCV package as shown −


import cv2
Now, for reading a particular image, use the imread() function −
image = cv2.imread('image_flower.jpg')
For showing the image, use the imshow() function. The name of the window
in which you can see the image would be image_flower.
cv2.imshow('image_flower',image)
cv2.destroyAllwindows()

Now, we can write the same image into the other format, say .png by using
the imwrite() function −
cv2.imwrite('image_flower.png',image)
The output True means that the image has been successfully written as .png
file also in the same folder.
True
Note − The function destroyallWindows() simply destroys all the windows we
created.

Color Space Conversion


In OpenCV, the images are not stored by using the conventional RGB color,
rather they are stored in the reverse order i.e. in the BGR order. Hence the
default color code while reading an image is BGR. The cvtColor() color
conversion function in for converting the image from one color code to other.
Example
Consider this example to convert image from BGR to grayscale.
Import the OpenCV package as shown −
import cv2
Now, for reading a particular image, use the imread() function −
image = cv2.imread('image_flower.jpg')
Now, if we see this image using imshow() function, then we can see that this
image is in BGR.
cv2.imshow('BGR_Penguins',image)
ow, use cvtColor() function to convert this image to grayscale.
image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray_penguins',image)

Face Detection
Face detection is one of the fascinating applications of computer vision which
makes it more realistic as well as futuristic. OpenCV has a built-in facility to
perform face detection. We are going to use the Haar cascade classifier for
face detection.
Haar Cascade Data
We need data to use the Haar cascade classifier. You can find this data in our
OpenCV package. After installing OpenCv, you can see the folder
name haarcascades. There would be .xml files for different application. Now,
copy all of them for different use and paste then in a new folder under the
current project.
Example
The following is the Python code using Haar Cascade to detect the face
mport the OpenCV package as shown −
import cv2
import numpy as np
Now, use the HaarCascadeClassifier for detecting face −
face_detection=
cv2.CascadeClassifier('D:/ProgramData/cascadeclassifier/
haarcascade_frontalface_default.xml')
Now, for reading a particular image, use the imread() function −
img = cv2.imread('AB.jpg')
Now, convert it into grayscale because it would accept gray images −
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Now, using face_detection.detectMultiScale, perform actual face detection
faces = face_detection.detectMultiScale(gray, 1.3, 5)
Now, draw a rectangle around the whole face −
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w, y+h),(255,0,0),3)
cv2.imwrite('Face_AB.jpg',img)

You might also like