Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
36 views

OpenCV Functions

Uploaded by

aayannkjain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

OpenCV Functions

Uploaded by

aayannkjain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

• cv2.imread(): Reads an image from a file.


Python code:
img = cv2.imread("image.jpg")

• cv2.imwrite(): Writes an image to a file.


Python code:
cv2.imwrite("output.jpg", img)

• cv2.imshow(): Displays an image in a window.


Python code:
cv2.imshow("Window Name", img)
cv2.waitKey(0) # Waits until a key is pressed
cv2.destroyAllWindows()

2. Basic Image Manipulation

cv2.resize(): Resizes an image to a specified size.


Python code:
resized_img = cv2.resize(img, (width, height))

cv2.flip(): Flips an image horizontally, vertically, or both.


Python code:
flipped_img = cv2.flip(img, 1) # 1 for horizontal, 0 for vertical

cv2.rotate(): Rotates an image by 90, 180, or 270 degrees.


Python code:
rotated_img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
3. Drawing Shapes

cv2.line(): Draws a line between two points.


Python code:
cv2.line(img, (x1, y1), (x2, y2), color, thickness)

cv2.rectangle(): Draws a rectangle.


Python code:
cv2.rectangle(img, (x, y), (x+w, y+h), color, thickness)

cv2.circle(): Draws a circle.


Python code:
cv2.circle(img, (x, y), radius, color, thickness)

4. Image Filtering

cv2.blur(): Averages pixel values within a given kernel size.


Python code:
blurred = cv2.blur(img, (5, 5))

cv2.medianBlur(): Applies median blur, helpful in reducing noise.


Python code:
median_blur = cv2.medianBlur(img, 5)

cv2.bilateralFilter(): Preserves edges while blurring.


Python code:
bilateral = cv2.bilateralFilter(img, 9, 75, 75)

5. Edge Detection

cv2.Canny(): Detects edges using the Canny edge detection algorithm.


Python code:
edges = cv2.Canny(img, threshold1, threshold2)
6. Color Space Conversion

cv2.cvtColor(): Converts an image to a different color space.


Python code:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Convert to grayscale
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # Convert to HSV

7. Thresholding

cv2.threshold(): Applies a binary threshold to an image.


Python code:
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

cv2.adaptiveThreshold(): Applies adaptive thresholding, which is useful for images


with varying illumination.
Python code:
adaptive_thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, 11, 2)

8. Contours

cv2.findContours(): Detects contours in a binary image.


Python code:
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(): Draws contours on an image.


Python code:
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)

9. Face Detection (using Haar Cascades)

• cv2.CascadeClassifier(): Loads a pre-trained Haar cascade for face detection.


Python code:
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

You might also like