[Go to site: main page, start]

0% found this document useful (0 votes)
90 views7 pages

Python Image Processing Tutorial

The document discusses image processing and face detection using Python. It introduces how images are represented digitally as matrices of pixel values. It describes the OpenCV library for computer vision tasks and how it can be used to load, display, and resize images. The document then explains how face detection works in OpenCV by using a cascade classifier to find the coordinates of faces in an image and draw rectangles around them.

Uploaded by

Mayank Goyal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views7 pages

Python Image Processing Tutorial

The document discusses image processing and face detection using Python. It introduces how images are represented digitally as matrices of pixel values. It describes the OpenCV library for computer vision tasks and how it can be used to load, display, and resize images. The document then explains how face detection works in OpenCV by using a cascade classifier to find the coordinates of faces in an image and draw rectangles around them.

Uploaded by

Mayank Goyal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

IMAGE PROCCESSING AND FACE DETECTION

USING PYTHON

1. INTRODUCTION
Image processing is the use of computer algorithms to process the image. Using computer
graphics algorithms we can transform an image, rotate the image the scale the image.
In this chapter we will discuss the basics of image processing using pythons, here are listed
contents and topics which we are going to cover in this chapter.

 How does our system read and process images


 Reading image
 Face detection

2. HOW DOES COMPUTER READ IMAGE?


The computer reads any image as a range of values between 0 and 255.

For any color image, there are 3 primary channels – Red, green and blue. How it works is pretty
simple.

A matrix is formed for every primary color and later these matrices combine to provide a Pixel
value for the individual R, G, and B colors.

Each element of the matrices provide data pertaining to the intensity of brightness of the pixel.
3. OpenCV Library
 OpenCV library mainly focuses on real time computer vision with applications on wide
range of image processing including 2D and 3D images, gesture recognition, Mobile
robotics, Human computer interactions etc.
 OpenCV is a Python library which is designed to solve computer vision problems. OpenCV
was originally developed in 1999 by Intel but later it was supported by Willow Garage.

 OpenCV supports a wide variety of programming languages such as C++, Python, and Java
etc. Support for multiple platforms including Windows, Linux, and MacOS.

 OpenCV Python is nothing but a wrapper class for the original C++ library to be used with
Python. Using this, all of the OpenCV array structures gets converted to/from NumPy
arrays.

 This makes it easier to integrate it with other libraries which use NumPy. For example,
libraries such as SciPy and Matplotlib.

4. LOADING AN IMAGE USING OpenCV


Loading an image is the very first task that we can need to perform before processing the image .
imread i.e. image read command is used to read the image from the system
Syntax for reading the image is as follow:
Img = [Link](“ image [Link]”,0);
Img = [Link](“image [Link]”,1);
The parameters 1, 0 are used to donate weather the image is colored or not. 1 means that the
image will be read as colored image, whereas 1 donates that the image will be black and white
image .
Example
5. DISPLAY AN IMAGE USING OpenCV
In order to display the image we got into the variables img and img1 we makes the
use of imshow function
We use the imshow function to display the image by opening a window. There are 2 parameters
to the imshow function which is the name of the window and the image object to be displayed.

Later, we wait for a user event. waitKey makes the window static until the user presses a key. The
parameter passed to it is the time in milliseconds.

And lastly, we use destroyAllWindows to close the window based on the waitForKey parameter.

6. RESIZING AN IMAGE USING OpenCV


resize function is used to resize an image to the desired shape. The parameter here is the shape of
the new resized image.
Later, do note that the image object changes from img to resized image, because of the image
object changes now.

Rest of the code is pretty simple to the previous one, correct?

This is the image we were looking to output all this while!

7. FACE DETECTION USING OpenCV


This seems complex at first but it is very easy. Let me walk you through the entire process and you
will feel the same.

Step 1: Considering our prerequisites, we will require an image, to begin with. Later we need to
create a cascade classifier which will eventually give us the features of the face.
Step 2: This step involves making use of OpenCV which will read the image and the features file.
So at this point, there are NumPy arrays at the primary data points.

All we need to do is to search for the row and column values of the face NumPy n-d array. This is
the array with the face rectangle coordinates.

Step 3: This final step involves displaying the image with the rectangular face box.

Check out the following image, here we have tried to summarize the 3 steps in the form of an
image for easier readability:

First, we create a CascadeClassifier object to extract the features of the face as explained earlier.
The path to the XML file which contains the face features is the parameter here.

The next step would be to read an image with a face on it and convert it into a black and white
image using COLOR_BGR2GREY. Followed by this, we search for the coordinates for the
image. This is done using detectMultiScale.

What coordinates, you ask? It’s the coordinates for the face rectangle. The scaleFactor is used to
decrease the shape value by 5% until the face is found. So, on the whole – Smaller the value,
greater is the accuracy.

Finally, the face is printed on the window.

8. ADDING A RECTANGULAR FACE BOX


This logic is very simple – As simple as making use of a for loop statement. Check out the following
image
We define the method to create a rectangle using [Link] by passing parameters such as the
image object, RGB values of the box outline and the width of the rectangle.

Let us check out the entire code for face detection:

Common questions

Powered by AI

The waitKey function in OpenCV plays a significant role in displaying images by pausing the execution of scripts and creating an interactive window until a user event, such as a keypress, occurs. Its parameter specifies the delay in milliseconds before the window closes automatically. By default, a parameter of 0 results in an indefinite wait, which is crucial for ensuring that the user has sufficient time to view the displayed image before the window is closed, thereby enhancing user interaction in visual applications .

OpenCV plays a crucial role in real-time image processing by providing extensive tools and libraries for computer vision tasks, including 2D and 3D image processing, gesture recognition, and human-computer interaction. The benefits of using OpenCV with Python include the ease of integration with other Python libraries like NumPy, SciPy, and Matplotlib, enabling efficient handling of image array structures. Python's support for OpenCV allows for more rapid development and testing cycles due to its high-level syntax and readability .

When reading an image using OpenCV, specifying how it is read—either in color or in black and white—is done by passing the parameter 1 or 0, respectively, to the imread function. A parameter of 1 causes the image to be read in color, retaining the RGB channel information. A parameter of 0 reads the image in black and white, which simplifies the image data to a single channel representing pixel intensity, useful for certain image processing tasks where color information is unnecessary .

In image processing, a computer interprets color images by reading them as a range of values between 0 and 255, which represent the intensity of brightness for each pixel. For color images, three primary color channels—red, green, and blue—are used. A matrix is formed for each primary color, and these matrices combine to provide a pixel value for individual R, G, and B colors. Each element of the matrices provides data on the intensity of brightness of the corresponding pixel in that channel .

NumPy contributes significantly to the functionalities of OpenCV by providing a robust framework for handling image data as multidimensional arrays. It enhances OpenCV's capabilities in efficiently performing mathematical operations on pixel data. Through the seamless conversion of OpenCV's array structures to/from NumPy arrays, various matrix operations, including transformations, rotations, and scalings, can be executed rapidly. This integration allows for leveraging NumPy's optimized computational practices, thereby improving the overall performance and flexibility of OpenCV's image processing tasks .

Face detection using OpenCV involves several steps. First, an image is selected as the input, and a cascade classifier is created to extract face features. The CascadeClassifier object requires a path to an XML file containing the necessary face features. After reading the image, it is converted into a grayscale image using COLOR_BGR2GREY to simplify the detection process. The detectMultiScale function is used to search for face coordinates by analyzing numpy arrays of the image's primary data points. This function considers the scaleFactor parameter, which is adjusted to enhance detection accuracy by resizing the image. Finally, the detected face is highlighted with a rectangle, often drawn using the cv2.rectangle method .

The development of the OpenCV library originated with Intel in 1999, focusing on furthering computer vision technologies. Willow Garage later supported the library, leading to significant advancements in its capabilities. This history of organizational backing highlights a collaborative effort that has led to OpenCV's current status as a widely used and continuously evolving tool for real-time image processing, computer vision applications, and machine learning .

Python's wrapper classes for OpenCV offer the advantage of translating OpenCV's native C++ functions into Python functions, which improves code readability and accessibility. These wrapper classes automatically convert OpenCV array structures to NumPy arrays, facilitating seamless integration with libraries that use or extend NumPy, such as SciPy and Matplotlib. This interoperability simplifies complex image processing tasks, accelerates development processes, and allows for leveraging Python's rich ecosystem of scientific libraries, enhancing both productivity and performance .

The use of a for loop in adding a rectangular face box in face detection algorithms simplifies iterating through detected face coordinates. In each iteration, the cv2.rectangle function is called with parameters, including the image object, RGB values for the box outline, and the rectangle's width. The for loop ensures that all detected faces in the image are highlighted, not just a singular occurrence, allowing for efficient processing of multiple faces within an image .

Resizing an image using OpenCV involves the use of the resize function. The primary parameter for resizing is the shape of the new resized image, which dictates its dimensions. Image resizing is crucial in image processing for standardizing image sizes, which is often necessary for further processing tasks and analyses to maintain consistency across datasets, enhance computational efficiency, and normalize textures or details for machine learning applications .

You might also like