Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

How to install Python on Windows Week 5, Lab 1 OpenCV Package for Image Processing In this Lab, you will be introduced to a huge open-source library for computer vision, machine learning and image...

1 answer below »

How to install Python on Windows
Week 5, Lab 1
OpenCV Package for Image Processing
In this Lab, you will be introduced to a huge open-source li
ary for computer vision, machine learning and image processing using OpenCV package in python.
OpenCV supports a wide variety of programming languages like Python, C++, Java, etc.
It can process images and videos to identify objects, faces or even the handwriting of a human.
When it is integrated with various li
aries , such as Numpy, then the number of weapons increases in your arsenal i.e., whatever operations one can do in Numpy can be combined with OpenCV.
Computer Vison
Computer vision is a process by which we can understand the images and videos how they are stored and how we can manipulate and retrieve data from them.
Computer Vision is the base or mostly used for Artificial Intelligence. Computer-Vision is playing a major role in self-driving cars, robotics as well as in photo co
ection apps.
OpenCV is the huge open-source li
ary 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 
Computer Vison
By using it, one can process images and videos to identify objects, faces, or even handwriting of a human.
When it integrated with various li
aries, such as NumPy, python can process the OpenCV a
ay structure for analysis.
To Identify image pattern and its various features we use vector space and perform mathematical operations on these features. 
Look at the following images
Computer Vison
From the above original image, lots of pieces of information that are present in the original image can be obtained.
Like in the above image there are two faces available and the person(I) in the images wearing a
acelet, watch, etc so by the help of OpenCV we can get all these types of information from the original image. 
It’s the basic introduction to OpenCV we can continue the Applications and all the things in our upcoming articles. 
Applications of OpenCV: There are lots of applications which are solved using OpenCV, some of them are listed below :
Application of OpenCV
face recognition
Automated inspection and surveillance
number of people – count (foot traffic in a mall, etc)
Vehicle counting on highways along with their speeds
Interactive art installations
Anamoly (defect) detection in the manufacturing process (the odd defective products)
Street view image stitching
Video/image search and retrieval
Robot and driver-less car navigation and control
object recognition
Medical image analysis
Movies – 3D structure from motion
TV Channels advertisement recognition
OpenCV Installation
Open the Pycharm software.
Go to the Terminal
Type : pip install opencv-python
This will install the OpenCV package and Numpy package into your computer.
Reading an Image
In this section, we will work with the road.png image which is provided in the LAB material.
The road.png is shown here.
Use the following python code to load the road.png file.
Make sure to save the road.png file in the same directory where the python file is executing.
Using the PyCharm software, and in the same directory where the road.png file is saved, create a new python file with the code as shown here.
Save the file under “LAB9-road-01.py”
Execute the python file.
import cv2
# Reading the image using imread() function
image = cv2.imread('road.png')
# Extracting the height and width of an image
h, w = image.shape[:2]
# Displaying the height and width
print("Height = {}, Width = {}".format(h, w))
Result
Extracting the RGB of a Pixel
Now we will focus on extracting the RGB values of an individual pixel.
Note – OpenCV a
anges the channels in BGR order.
So the 0th value will co
espond to Blue pixel and not Red.
Create a python file with the following line of codes.
Save the file under “LAB9-road-02.py”
Result
# Reading the image using imread() function
image = cv2.imread('road.png')
# Extracting the height and width of an image
h, w = image.shape[:2]
# Displaying the height and width
print("Height = {}, Width = {}".format(h, w))
# Extracting RGB values.
# Here we have randomly chosen a pixel
# by passing in 100, 100 for height and width.
(B, G, R) = image[100, 100]
# Displaying the pixel values
print("R = {}, G = {}, B = {}".format(R, G, B))
# We can also pass the channel to extract
# the value for a specific channel
B = image[100, 100, 0]
print("B = {}".format(B))
You can display an image using plt.imshow()
In order to successfully display the road image, you need the matplotlib package.
Here is the sample code. Create a new python file under the directory where the road.png exists. Then copy and paste the code into a new python file.
Save the file under “LAB9-road-03.py”
# Importing the OpenCV li
ary
import cv2
import numpy as np
# Reading the image using imread() function
import matplotlib.pyplot as plt
image = cv2.imread('road.png')
plt.imshow(image)
plt.show()
OpenCV| Displaying an Image
To write/save an image into the file directory, you can use, imwrite()
Exercise:
Read the road.png file.
Read the gray scale using the cv2.IMREAD_GRAYSCALE function and save the image under a new name.
Read the image with all original colors using the cv2.IMREAD_COLOR function and save the image under a new name.
Write the gray and color image under “road_gray.png” and “road_color.png” using cv2.imwrite()
Plot the gray and color images using the matplotlib.
Save the python file under “LAB9-road-04.py”
cv2.imwrite(filename, image).
# Importing the OpenCV li
ary
import cv2
import matplotlib.pyplot as plt
im_color = cv2.imread('road.png', cv2.IMREAD_COLOR)
im_gray = cv2.imread('road.PNG', cv2.IMREAD_GRAYSCALE)
cv2.imwrite('road_color.png', im_color)
cv2.imwrite('road_gray.png', im_gray)
plt.figure()
plt.imshow(im_color)
plt.title('im_color')
plt.show()
plt.figure()
plt.imshow(im_gray)
plt.title('im_gray')
plt.show()
OpenCV |
Saving an Image
Addition of Image
Arithmetic Operations like Addition, Subtraction, and Bitwise Operations(AND, OR, NOT, XOR) can be applied to the input images.
Addition of Image:
cv2.add(img1, img2)
Both images should b equal size and depth.
Read image1 and image 2 and display the addition.
Save the .py file under “LAB9-addition”.
import cv2
import matplotlib.pyplot as plt
image1 = cv2.imread('image1.png')
image2 = cv2.imread('image2.png')
Sum = cv2.add(image1, image2[1:,:,:])
plt.figure()
plt.imshow(image1)
plt.show()
plt.figure()
plt.imshow(image2)
plt.show()
plt.figure()
plt.imshow(Sum)
plt.show()
Addition of Image
Image 1
Image 2
Imag1 + Image 2
Object Detection
One of the application of OpenCV is object detection.
Exercise:
Load the road.png file.
Display the original road.png file.
Use the following code to detect all objects in the road.png file.
You need to install the cvlib package and this can be done using the pip install through the terminal.
Display the output image with the detected objects.
import cv2
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_
ox
import tensorflow as tf
im = cv2.imread('road.PNG')
ox, label, conf = cv.detect_common_objects(im)
output_image = draw_
ox(im,
ox, label, conf)
plt.imshow(output_image)
plt.show()
Object Detection| Results
Exercises:
Using the subplot and matplotlib package plot the original road.png and the detected object road.png (previous example) in one graph. ( The final product should be look like bellow). Save the python file as “Lab9-Ex1”.
Exercises:
2. Take a picture of the classroom and save the image file under .png file. Load the save .png file and using the sample code for the object detection display the detected objects in the image. Save the python file as “Lab9-Ex2”. Make sure to include the image inside your submission.
3. Repeat step 2 with another picture, this time a picture of yourself. Use the object detection sample code and check if you can your face can be detected. Save the python file as “Lab9-Ex3”. Make sure to include the image inside your submission.
Submission :
Submit the python files developed in this LAB:
“LAB9-road-01.py”
“LAB9-road-02.py”
“LAB9-road-03.py”
“LAB9-road-04.py”
“LAB9-addition”.
“LAB9-Ex1”.
“LAB9-Ex2”.
“LAB9-Ex3”.
References:
https:
www.geeksforgeeks.org/face-detection-using-python-and-opencv-with-webcam
https:
learnopencv.com
ead-display-and-write-an-image-using-opencv/#display-image
Answered 1 days After Sep 26, 2022

Solution

Sathishkumar answered on Sep 28 2022
56 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here