Open CV Basic

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

OPEN CV BASIC

Certainly! Here are some basic concepts and operations in OpenCV to help you get started:

Image Loading and Display:

python

Copy code

import cv2

# Load an image from file

image = cv2.imread('image.jpg')

# Display the image in a window

cv2.imshow('Image', image)

# Wait for a key press to close the window

cv2.waitKey(0)

# Close all OpenCV windows

cv2.destroyAllWindows()

Image Operations:

Grayscale Conversion:

python

Copy code

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Resizing an Image:

python
Copy code

resized_image = cv2.resize(image, (new_width, new_height))

Image Cropping:

python

Copy code

cropped_image = image[y:y+h, x:x+w]

Image Rotation:

python

Copy code

M = cv2.getRotationMatrix2D((center_x, center_y), angle, scale)

rotated_image = cv2.warpAffine(image, M, (width, height))

Drawing on Images:

python

Copy code

# Draw a line

cv2.line(image, (start_x, start_y), (end_x, end_y), (blue, green, red), thickness)

# Draw a rectangle

cv2.rectangle(image, (top_left_x, top_left_y), (bottom_right_x, bottom_right_y), (blue, green, red),


thickness)

# Draw a circle

cv2.circle(image, (center_x, center_y), radius, (blue, green, red), thickness)

# Put text on the image

cv2.putText(image, 'Text', (x, y), font, font_size, (blue, green, red), thickness, cv2.LINE_AA)

Image Filtering:
Applying Gaussian Blur:

python

Copy code

blurred_image = cv2.GaussianBlur(image, (kernel_width, kernel_height), sigma)

Applying Median Blur:

python

Copy code

blurred_image = cv2.medianBlur(image, kernel_size)

Applying Image Thresholding:

python

Copy code

ret, thresholded_image = cv2.threshold(image, threshold_value, max_value, threshold_type)

These are just a few basic operations in OpenCV to help you get started. OpenCV offers a wide range of
functionalities for advanced image and video processing tasks, including feature detection, object
recognition, contour detection, and more. The official OpenCV documentation and various online
tutorials provide detailed explanations, examples, and additional operations you can explore to expand
your knowledge of OpenCV.

You might also like