10 OpenCV Basic Functions Python- Tech2 etc

10 OpenCV Basic Functions Python- Tech2 etc

In this Python Tutorial, OpenCV Basic Methods For Beginners will be discussed.

OpenCV Is An Open Source Computer Vision Library. It’s written in C++ and has more than 2500 optimized algorithms. Big giant companies like Google, Amazon, Microsoft, and Toyota are using OpenCV Library. Anyone can easily use this library for computer vision applications such as Image Processing/Image Analysis, Video Analysis, CCTV Footage Analysis, etc.

Today We will learn Top 10 Basic OpenCV Methods which you can apply in your real-life projects.

  1. Read Image Using OpenCV.
  2. How to Write Image.
  3. Collect Image Information.
  4. Rotate Image.
  5. Image Transpose.
  6. Capture Video Using Open-CV.
  7. Crop Image.
  8. How to Blur Image.
  9. Image Pyramid.
  10. Edge Detection in Open-CV.

Reference Video πŸ‘‡

How To Install OpenCV

Run this command in your terminal/CMD to install OpenCV Library.

pip install opencv-python

To get more information, please check this.

How To Read Images

For reading images, You can use a function called imread(). And use imshow() function to show that image. Use the syntax below to read image.

# Read/Show Image using opencv
import cv2

# Read Image
img = cv2.imread("face.jpg")
# Show Image
cv2.imshow('Original Image', img)

cv2.waitKey(0)

How To Write Image

Use imwrite() function to store or write an image in file directory. Python Program To Write an Image.

# Write/Store Image using opencv
import cv2

img = cv2.imread("face.jpg")
# Show Image
cv2.imshow('Original Image', img)
# Store/Write Image in directory
cv2.imwrite('output.jpg', img)

cv2.waitKey(0)

Get Image Information

It provides the width and height pixel values of an image. Source Code For Collecting Image Info.

# Get Image Information using opencv
import cv2

img = cv2.imread("face.jpg")
print(img.shape)
print("Height pixel values : ", img.shape[0])
print("Width pixel values : ", img.shape[1])

cv2.waitKey(0)

Rotate Image

Open-CV gives us an amazing function to rotate images easily. Rotate an Image using the code below.

# Rotate Image using opencv
import cv2

img = cv2.imread("face.jpg")
height, width = img.shape[: 2]
rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), 70, .5)
rotated_image = cv2.warpAffine(img, rotation_matrix, (width, height))
cv2.imshow('Rotated Image', rotated_image)
cv2.imshow('Original image', img)

cv2.waitKey(0)

Image Transpose

Use transpose method to rotate image directly at 90 degree. Syntax to Transpose Image.

# Transpose Image using opencv
import cv2

img = cv2.imread("face.jpg")
rotated_image = cv2.transpose(img)
cv2.imshow('Rotated Image', rotated_image)
cv2.imshow('original image', img)

cv2.waitKey(0)

Capture Video Using OpenCV Library

Using cv2.VideoCapture() function we can easily read video. By passing 0 in function parameter we can capture our webcam too. Video Capture Syntax.

# Capture Video using opencv
import cv2

cam = cv2.VideoCapture(0)
while True:
    check, frame = cam.read()

    cv2.imshow('video', frame)

    key = cv2.waitKey(1)
    if key == 27:
        break

cam.release()
cv2.destroyAllWindows()
cv2.waitKey(0)

How To Crop Image

We can crop an image using Open-cV. Just need to add some mathematics. Use Below Python Code to Crop Images.

# Crop Image using opencv
import cv2

img = cv2.imread("face.jpg")
height, width = img.shape[: 2]
start_row, start_col = int(height * .15), int(width * .15)
end_row, end_col = int(height * .65), int(width * .65)
cropped = img[start_row: end_row, start_col: end_col]
cv2.imshow('original image', img)
cv2.imshow('cropped image', cropped)

cv2.waitKey(0)

How To Blur Image

We usually use blur effect, when we don’t want others to see it. Using kernel method we can easily blur an image. Use this method to blur an image.

# Blur Image using opencv
import cv2
import numpy as np

img = cv2.imread("face.jpg")
kernel = np.ones((7, 7), np.float32) / 49
blurred = cv2.filter2D(img, -1, kernel)
cv2.imshow('blur image', blurred)
cv2.imshow('original image', img)

cv2.waitKey(0)

Make Image Pyramid

Use this functionalities pyrDown() and pyrUp() to make image pyramid.

# Image Pyramid using opencv
import cv2

img = cv2.imread("face.jpg")
smaller = cv2.pyrDown(img)
larger = cv2.pyrUp(img)
cv2.imshow('original image', img)
cv2.imshow('smaller image', smaller)
cv2.imshow('larger image', larger)

cv2.waitKey(0)

Edge Detection Using OpenCV

In general word, Edges = pixels. For edge detection, we need to play around with pixels. Use canny() function for edge detection.

# Edge detection using opencv
import cv2

img = cv2.imread("face.jpg")
canny = cv2.Canny(img, 20, 170)
cv2.imshow("Canny", canny)

cv2.waitKey(0)

Congrats! You just learned the Top 10 Basic OpenCV Methods.

20+ Interesting Python Projects For Beginners

Get all the python projects..

Feel free to visit my YouTube channel: @Tech2etc

Follow me on Instagram where I'm sharing lots of useful resources! @fahimulkabir.chowdhury πŸ˜‰

More Useful Articles:

Upload soon!

Β