Lab Session
Lab Session
4. If you’re just getting started with Python and you want to install it with default features as
described in the dialog, then click Install Now and go to Step 4 - Verify the Python
Installation. To install other optional and advanced features, click Customize installation
and continue.
5. The Optional Features include common tools and resources for Python and you can
install all of them, even if you don’t plan to use them.
Step 3 — Verify the Python Installation
You can verify whether the Python installation is successful either through the command line or
through the Integrated Development Environment (IDLE) application, if you chose to install it.
Go to Start and enter cmd in the search bar. Click Command Prompt.
Enter the following command in the command prompt:
python --version
or
py --version
You can also check the version of Python by opening the IDLE application. Go to Start and enter
python in the search bar and then click the IDLE app, for example IDLE (Python 3.10 64-bit).
You can start coding in Python using IDLE or your preferred code editor.
You can also check pip version in cmd by:
python pip --version
or
py -m pip --version
or
py -m pip install opencv-python
Reading an image in OpenCV using Python
Some basic functions of the OpenCV library
Reading an image
img = cv2.imread('LOCATION OF THE IMAGE')
The above function imread stores the image at the given location to the variable img.
Converting an image to grayscale
img = cv2.imread('watch.jpg',cv2.IMREAD_GRAYSCALE)
The above function converts the image to grayscale and then stores it in the variable img.
Showing the stored image
cv2.imshow('image',img)
The above function stores the image in the file. The image is stored in the variable of type Mat that is in
the form of a matrix.
Reading video directly from the webcam
cap = cv2.VideoCapture(0)
cap is the variable that contains the video. The above function returns true if the video is successfully
opened else returns false.
Release the stored video after processing is done
cap.release()
In this section, we’ll try to open an image by using OpenCV (Open Source Computer Vision) library.
Following types of files are supported in OpenCV library:
Below codes are implementations to read images and display images on the screen using OpenCV and
matplotlib libraries functions.
Example #1 (Using OpenCV) :
Image used is:
Output:
Image read using cv2 and displaying using matplotlib will display picture in BGR format.
Note: See the difference in colors of images read by cv2 and matplotlib library. Because cv2 uses BGR
color format and matplotlib uses RGB color format. To convert BGR to RGB, we us a function:
Example#3:
Output:
# importing cv2
import cv2
# path
path = r'strawberries.jpg'
Output :