pip install opencv-python
- Importing OpenCV to your .py project:
- Importing an Image and Viewing it:
You can close the window showing the image by pressing any button.
img = cv2.imread("<../Path/to/Image.Extension>")
Note: OpenCV reads are in BGR color scheme instead of the usual RGB scheme.
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
Note: From now, the image is referred to as
- Create a function to display images quickly
def viewImage(img, name_of_window):
Cropping an Image:
To crop an image, we can directly provide width and height similar to slicing in Python.
# Syntax for cropping <image_name>[y:y+h, x:x+w]
crop = rgb_img[10:500, 500:2000]
viewImage(crop, "Cropped Image")
Resizing an Image:
For image scaling, we can use the
# Provide the % times of the original image you want to scale
scaling = 20#Change Width and Height
width = int(rgb_img.shape * scaling / 100)
height = int(rgb_img.shape * scaling / 100)
dim = (width,height)resized = cv2.resize(img,dim,interpolation = cv2.INTER_AREA)viewImage(resized, "Resized Image by 20%")
image.shape outputs the height, width, and channels of an image.
Rotating an Image:
Images can be rotated clockwise and counterclockwise by applying a
-ve angle rotation to it respectively.
(h, w, d) = rgb_img.shape
center_img = (w//2, h//2)
rotationMatrix = cv2.getRotationMatrix2D(center, 180, 1.0)
rotated = cv2.warpAffine(rgb_img, rotationMatrix, (w, h))viewImage(rotated, "Rotated Image 180 degrees")
Grayscaling and Thresholding an Image:
Grayscale is a black and white, single-channeled version of an Image. Whereas, Thresholding will turn all dark shades (less than 127 value) to 0 and all the bright shades to 255.
grey_img = cv2.cvtColor(rgb_img, cv2.COLOR_RGB2GRAY)
ret, threshold_img = cv2.threshold(gray_img,127,255,0)
viewImage(gray_img, "Grayscale Image")
viewImage(threshold_img, "Threshold Image")
retstand for retVal which is used in Otsu’s Binarization. You can read about it here.
Blurring/Smoothing an Image:
There are multiple functions in OpenCV that can be used to control the Blurring and Smoothing of an Image. For the purpose of this blog, we will be using the
Gaussian Blur function.
Gaussian Blur function takes 3 parameters:
- Image you want to Blur.
- A tuple of 2 positive odd numbers. When increased, the blur effect increases.
- The sigmaX and sigmaY for the Gaussian Kernel. When set as 0, they’re automatically calculated from kernel size.
Gaussian Blur is highly effective in removing noise from an image.
blurred = cv2.GaussianBlur(rgb_img, (61,61), 0)
viewImage(blurred, "Blurred Image")
Drawing a Bounding Box on an Image:
A bounding box is nothing but a rectangle or a square around an object. This creation is extensively used by many face detecting or object detecting applications.
output = rgb_img.copy()
cv2.rectangle(output, (2600,800), (4100, 2400), (0, 255, 255), 10)
viewImage(output, "Rectangle on an Image")
- Image import.
- x1,y1 — Top Left Corner.
- x2,y2 — Bottom Right Corner.
- Rectangle Color — Depends on the Image imported. (In our case RGB).
- Thickness of the production.
Similarly, we can draw lines using
cv2.line and add text using
cv2.putText on an Image.
Face detection can be done by using inbuilt
detectMultiScale function in OpenCV. We also make use of the
image_path = "<../Path/To/Image.Extension>"
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
Haar feature-based cascade classifier can be read in the paper “Rapid Object Detection using a Boosted Cascade of Simple Features” by Paul Viola and Michael Jones.
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor = 1.1,
minNeighbors = 5, minSize = (10,10)
)faces_detected = format(len(faces)) + "Face detected!"
print(face_detected)for(x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255,255,0), 2)viewImage(image, face_detected)
detectMultiScale function uses four parameters:
- Grayscale Image Input.
scaleFactorcompensates for the fact that some faces may be close or far away from the camera.
- The detection algorithm uses a moving window to detect objects.
minNeighborsdefines how many objects are detected near the current one before it declares the face found.
minSizegives the size of each window.
Saving an Image
Finally, to save an Image, we can use the
image = cv2.imread("<../Path/To/Image.Extension>")
Other functions can be read here.