Debug School

rakesh kumar
rakesh kumar

Posted on

Core operations in OpenCV

  1. Basic Image Read and Write Reading an Image: cv2.imread() Displaying an Image: cv2.imshow() Writing an Image: cv2.imwrite()
import cv2
import numpy as np
import matplotlib.pyplot as plt

# Read the image (make sure the filename and extension match)
image = cv2.imread('original.png')

# Display the image
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

output
rightclick==>run display_image.py

Image description

  1. Accessing and Modifying Pixel Values Accessing Pixel Value: image[y, x] Modifying Pixel Value: image[y, x] = [B, G, R]
import cv2

# Read an image


# Accessing a pixel value
pixel_value = image[100, 100]
print("Pixel value at (100, 100):", pixel_value)

# Modifying a pixel value
image[100, 100] = [0, 255, 0]
cv2.imshow('Modified Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

output

Image description

  1. Image ROI (Region of Interest) Defining a Region of Interest: roi = image[y1:y2, x1:x2] Modifying a Region of Interest: You can manipulate a specific region by working with the roi.
# Define the ROI
roi = image[100:200, 100:200]

# Display the ROI
cv2.imshow('ROI', roi)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Modify the ROI
image[100:200, 100:200] = 255  # Set ROI to white
cv2.imshow('Modified Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

output

Image description

Image description

  1. Image Resizing
Resizing an Image: cv2.resize()

resized_image = cv2.resize(image, (300, 300))

# Display the resized image
cv2.imshow('Resized Image', resized_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

Image description

  1. Image Rotation
Rotating an Image: cv2.getRotationMatrix2D() and cv2.warpAffine()
Enter fullscreen mode Exit fullscreen mode
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, 45, 1.0)

# Rotate the image
rotated_image = cv2.warpAffine(image, M, (w, h))

# Display the rotated image
cv2.imshow('Rotated Image', rotated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

output

Image description

  1. Image Translation (Shifting)
Translating an Image: cv2.warpAffine()
Enter fullscreen mode Exit fullscreen mode
# Define the translation matrix
M = np.float32([[1, 0, 50], [0, 1, 100]])

# Apply the translation
shifted_image = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))

# Display the shifted image
cv2.imshow('Shifted Image', shifted_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

output

Image description

  1. Image Blurring
Averaging: cv2.blur()
Gaussian Blurring: cv2.GaussianBlur()
Median Blurring: cv2.medianBlur()
Enter fullscreen mode Exit fullscreen mode
blurred_image = cv2.GaussianBlur(image, (15, 15), 0)

# Display the blurred image
cv2.imshow('Blurred Image', blurred_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

output

Image description

  1. Edge Detection Canny Edge Detection: cv2.Canny()
edges = cv2.Canny(image, 100, 200)

# Display the edges
cv2.imshow('Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

output

Image description

  1. Image Thresholding
Simple Thresholding: cv2.threshold()
Adaptive Thresholding: cv2.adaptiveThreshold()
Enter fullscreen mode Exit fullscreen mode
# Apply simple thresholding
_, thresholded_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)

# Display the thresholded image
cv2.imshow('Thresholded Image', thresholded_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

output

Image description

  1. Image Contours
Finding Contours: cv2.findContours()
Drawing Contours: cv2.drawContours()
Enter fullscreen mode Exit fullscreen mode
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, threshold = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# Find contours
contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# Draw contours
cv2.drawContours(image, contours, -1, (0, 255, 0), 3)

# Display the image with contours
cv2.imshow('Contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

output

Image description

  1. Morphological Operations
Erosion: cv2.erode()
Dilation: cv2.dilate()
Opening: cv2.morphologyEx() with cv2.MORPH_OPEN
Closing: cv2.morphologyEx() with cv2.MORPH_CLOSE
Enter fullscreen mode Exit fullscreen mode
kernel = np.ones((5, 5), np.uint8)

# Apply erosion
eroded_image = cv2.erode(image3, kernel, iterations=1)

# Display the eroded image
cv2.imshow('Eroded Image', eroded_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

output

Image description

  1. Color Spaces Converting Between Color Spaces: cv2.cvtColor()
# Convert to grayscale
image5 = cv2.imread('virat.jpg')
gray_image = cv2.cvtColor(image5, cv2.COLOR_BGR2GRAY)

# Display the grayscale image
cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

output

Image description

  1. Image Histograms
Calculating Histograms: cv2.calcHist()
Equalizing Histograms: cv2.equalizeHist()
# Calculate the histogram
Enter fullscreen mode Exit fullscreen mode
image6 = cv2.imread('virat.jpg', 0)
hist = cv2.calcHist([image6], [0], None, [256], [0, 256])

# Plot the histogram
plt.plot(hist)
plt.show()
Enter fullscreen mode Exit fullscreen mode

output

Image description

Image description

Top comments (0)