OpenCV offers a wide range of functions for various computer vision tasks.
Here’s an
overview of some commonly used functions in OpenCV, categorized by purpose:
1. Image Reading and Writing
• [Link](): Reads an image from a file.
Python code:
img = [Link]("[Link]")
• [Link](): Writes an image to a file.
Python code:
[Link]("[Link]", img)
• [Link](): Displays an image in a window.
Python code:
[Link]("Window Name", img)
[Link](0) # Waits until a key is pressed
[Link]()
2. Basic Image Manipulation
[Link](): Resizes an image to a specified size.
Python code:
resized_img = [Link](img, (width, height))
[Link](): Flips an image horizontally, vertically, or both.
Python code:
flipped_img = [Link](img, 1) # 1 for horizontal, 0 for vertical
[Link](): Rotates an image by 90, 180, or 270 degrees.
Python code:
rotated_img = [Link](img, cv2.ROTATE_90_CLOCKWISE)
3. Drawing Shapes
[Link](): Draws a line between two points.
Python code:
[Link](img, (x1, y1), (x2, y2), color, thickness)
[Link](): Draws a rectangle.
Python code:
[Link](img, (x, y), (x+w, y+h), color, thickness)
[Link](): Draws a circle.
Python code:
[Link](img, (x, y), radius, color, thickness)
4. Image Filtering
[Link](): Averages pixel values within a given kernel size.
Python code:
blurred = [Link](img, (5, 5))
[Link](): Applies median blur, helpful in reducing noise.
Python code:
median_blur = [Link](img, 5)
[Link](): Preserves edges while blurring.
Python code:
bilateral = [Link](img, 9, 75, 75)
5. Edge Detection
[Link](): Detects edges using the Canny edge detection algorithm.
Python code:
edges = [Link](img, threshold1, threshold2)
6. Color Space Conversion
[Link](): Converts an image to a different color space.
Python code:
gray = [Link](img, cv2.COLOR_BGR2GRAY) # Convert to grayscale
hsv = [Link](img, cv2.COLOR_BGR2HSV) # Convert to HSV
7. Thresholding
[Link](): Applies a binary threshold to an image.
Python code:
ret, thresh = [Link](gray, 127, 255, cv2.THRESH_BINARY)
[Link](): Applies adaptive thresholding, which is useful for images
with varying illumination.
Python code:
adaptive_thresh = [Link](gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 11, 2)
8. Contours
[Link](): Detects contours in a binary image.
Python code:
contours, hierarchy = [Link](thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
[Link](): Draws contours on an image.
Python code:
[Link](img, contours, -1, (0, 255, 0), 2)
9. Face Detection (using Haar Cascades)
• [Link](): Loads a pre-trained Haar cascade for face detection.
Python code:
face_cascade = [Link]("haarcascade_frontalface_default.xml")
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)