Image Processing Lab – Cheat Sheet (Python Functions Only)
Required Imports:
import cv2
import numpy as np
import [Link] as plt
Image Loading & Display:
[Link](path, cv2.IMREAD_GRAYSCALE)
[Link](img, cmap='gray')
[Link]('off')
Noise Reduction / Smoothing Filters:
[Link](img, (k, k))
[Link]([Link]([Link](region)))
(k*k) / [Link](1.0 / (region + 1e-8))
[Link](region**(Q+1)) / [Link](region**Q)
[Link](img, k)
cv2.filter2D(img, -1, kernel)
[Link](img, kernel)
[Link](img, kernel)
([Link](region) + [Link](region)) / 2
[Link]([Link]([Link]())[d:-d])
[Link](img, (k, k), sigma)
[Link](img, d, sigmaColor, sigmaSpace)
Image Resizing:
[Link](img, (width, height))
[Link](img, None, fx=scale_x, fy=scale_y)
[Link](img, (width, height), interpolation=cv2.INTER_LINEAR)
[Link](img, (width, height), interpolation=cv2.INTER_NEAREST)
[Link](img, (width, height), interpolation=cv2.INTER_CUBIC)
ROI:
#Crop the central 128x128 region
# Calculate center
center_y, center_x = 256 // 2, 256 // 2
half_crop = 128 // 2
y1, y2 = center_y - half_crop, center_y + half_crop
x1, x2 = center_x - half_crop, center_x + half_crop
roi = resized[y1:y2, x1:x2]
Image Enhancement – Contrast Adjustment:
[Link](img)
[Link](img, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
[Link](img, alpha=contrast, beta=brightness)
[Link](img, thresh, maxval, cv2.THRESH_BINARY)
[Link](img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
[Link](alpha * img + beta, 0, 255)
Contrast Stretching:
min_val = [Link](img)
max_val = [Link](img)
contrast_stretched = ((img - min_val) / (max_val - min_val) * 255).astype(np.uint8)Q5) mean_filtered =
[Link](img, (5,5))
Sharpening & Edge Detection:
[Link](img, cv2.CV_64F)
[Link](img, cv2.CV_64F, dx, dy, ksize=3)
cv2.filter2D(img, -1, prewitt_kernel)
cv2.filter2D(img, -1, roberts_kernel)
[Link](img, 1.5, [Link](img,(k,k),0), -0.5, 0)
[Link](img, A, blurred, -(A-1), 0)
[Link](img, t1, t2)
cv2.filter2D(img, -1, emboss_kernel)
cv2.filter2D(img, -1, motion_kernel)
Padding:
[Link](img, t, b, l, r, cv2.BORDER_CONSTANT)
[Link](img, t, b, l, r, cv2.BORDER_REPLICATE)
[Link](img, t, b, l, r, cv2.BORDER_REFLECT)
Noise Generation:
img + [Link](0, sigma, [Link])
[Link]([0,255], [Link], p=[p,1-p])
Utilities:
cv2.filter2D(img, ddepth, kernel)
[Link](img1, a, img2, b, c)
[Link]() [Link]() [Link]()
Plotting:
[Link](image, cmap='gray') [Link]()
[Link]([Link](), 256, [0,256])
[Link](): Flattens an array into 1D. Used for histogram computation.
[Link](data, bins, range): Plots the histogram of given data.
Other:
[Link](img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 11, 2)
cv2.getRotationMatrix2D(center, angle_45, scale)
[Link](lena_original, rotation_matrix_45, (lena_original.shape[1], lena_original.shape[0]))
[Link](image / 255.0, gamma) * 255
[Link](gray, template, cv2.TM_CCOEFF_NORMED)
[Link](img, max_loc, (max_loc[0] + w, max_loc[1] + h), (0, 255, 0), 2)
kernel = [Link]((5,5), np.uint8)
[Link](gray, kernel, iterations=1)
[Link](gray, kernel, iterations=1)
[Link](gray, cv2.MORPH_CLOSE/cv2.MORPH_OPEN, kernel)
from [Link] import generic_filter