IPMV Codes
IPMV Codes
import cv2, numpy as np, matplotlib.pyplot as plt
# Example usage:
link = 'path_to_your_image.jpg' # Insert your image path here
display_results(link)
Gray Level Slicing Without Background and Log Transform
def gray_slice_log_transform(link):
img = cv2.imread(link, 0)
# Log Transform
log_img = np.uint8(45 * np.log1p(img))
# Example usage:
link = 'path_to_your_image.jpg' # Replace with your image path
gray_slice_log_transform(link)
Contrast Stretching and Obtain the Histogram of the
Original Contrast Stretched Image
import cv2, numpy as np, matplotlib.pyplot as plt
def contrast_stretching_hist(link):
img = cv2.imread(link, 0)
p2, p98 = np.percentile(img, (2, 98))
stretched = lambda x: np.uint8(np.clip((x - p2) / (p98 - p2) * 255, 0, 255))
# Example usage:
link = '/content/download.jpg' # Replace with your image path
contrast_stretching_hist(link)
Histogram Equalization for a Grayscale Image
def histogram_equalization(link):
img = cv2.imread(link, 0)
# Compute histogram
hist = np.bincount(img.flatten(), minlength=256)
# Normalize histogram to get PDF
pdf = hist / hist.sum()
# Compute cumulative distribution function (CDF)
cdf = np.cumsum(pdf)
# Map old values to new using the CDF
equalized = np.uint8(np.floor(255 * cdf[img]))
# Example usage:
link = 'path_to_your_image.jpg' # Replace with your image path
histogram_equalization(link)
Histogram Specification
import cv2, numpy as np, matplotlib.pyplot as plt
# Histogram mapping
mapping = np.interp(cdf_img, cdf_target, np.arange(256))
# Apply transformation
spec_img = np.uint8(np.interp(img.flatten(), np.arange(256), mapping).reshape(img.shape))
# Plotting
plt.subplot(1, 2, 1); plt.imshow(img, cmap='gray'); plt.title('Original'); plt.axis('off')
plt.subplot(1, 2, 2); plt.imshow(spec_img, cmap='gray'); plt.title('Specified'); plt.axis('off')
plt.tight_layout(); plt.show()def histogram_specification(link, target_link):
img, target = cv2.imread(link, 0), cv2.imread(target_link, 0)
hist = lambda x:np.bincount(x.flatten(), minlength=256)
# Histogram mapping
mapping = np.interp(cdf_img, cdf_target, np.arange(256))
# Apply transformation
spec_img = np.uint8(np.interp(img.flatten(), np.arange(256), mapping).reshape(img.shape))
# Plotting
for i, (result, title) in enumerate(zip(results, titles)):
plt.subplot(2, 3, i + 1)
if i > 2:
plt.hist(result.flatten(), 256, [0, 256]) # Histogram for the last subplot
else:
plt.imshow(result, cmap='gray') # For image display
plt.axis('off')
plt.title(title)
plt.tight_layout()
plt.show()
# Example usage:
link = 'path_to_your_image.jpg' # Replace with your image path
target_link = 'path_to_target_image.jpg' # Replace with your target image path
histogram_specification(link, target_link)
Averaging Filter
def averaging_filter(link):
img = cv2.imread(link, 0)
kernel = np.ones((5, 5), np.float32) / 25
filtered = cv2.filter2D(img, -1, kernel)
# Example usage:
link = 'path_to_your_image.jpg' # Replace with your image path
averaging_filter(link)
Sobel and Prewitt
for i in range(h):
for j in range(w):
roi = padded[i:i+kh, j:j+kw]
result[i, j] = np.sum(roi * kernel)
return result
def sobel_prewitt_manual(link):
img = cv2.imread(link, 0).astype(np.float32)
# Sobel Kernels
sobel_x_kernel = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
sobel_y_kernel = np.array([[-1, -2, -1],
[ 0, 0, 0],
[ 1, 2, 1]])
# Prewitt Kernels
prewitt_x_kernel = np.array([[1, 0, -1],
[1, 0, -1],
[1, 0, -1]])
prewitt_y_kernel = np.array([[ 1, 1, 1],
[ 0, 0, 0],
[-1, -1, -1]])
plt.subplot(2, 2, 1)
plt.imshow(sobel, cmap='gray')
plt.title('Sobel Edge Detection'); plt.axis('off')
plt.subplot(2, 2, 2)
plt.hist(sobel.flatten(), 256, [0,256])
plt.subplot(2, 2, 3)
plt.imshow(prewitt, cmap='gray')
plt.title('Prewitt Edge Detection'); plt.axis('off')
plt.subplot(2, 2, 4)
plt.hist(prewitt.flatten(), 256, [0,256])
plt.tight_layout(); plt.show()
# Example usage:
link = '/content/download.jpg' # Replace with your image path
sobel_prewitt_manual(link)
Detection of Horizontal and Vertical Lines
import cv2, numpy as np, matplotlib.pyplot as plt
def line_detection(link):
img = cv2.imread(link, 0)
edges = cv2.Canny(img, 50, 150)
plt.subplot(1, 3, 1)
plt.imshow(edges, cmap='gray')
plt.title('Edge Detection'); plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(horizontal, cmap='gray')
plt.title('Horizontal Lines'); plt.axis('off')
plt.subplot(1, 3, 3)
plt.imshow(vertical, cmap='gray')
plt.title('Vertical Lines'); plt.axis('off')
plt.tight_layout(); plt.show()
# Example usage:
link = 'path_to_your_image.jpg' # Replace with your image path
line_detection(link)
Low Pass Filter in Frequency Domain
import cv2, numpy as np, matplotlib.pyplot as plt
def low_pass_filter_frequency(link):
img = cv2.imread(link, 0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
rows, cols = img.shape
crow, ccol = rows // 2, cols // 2
# Create mask
mask = np.zeros((rows, cols), np.uint8)
r = 30 # Radius of low-pass filter
center = [crow, ccol]
y, x = np.ogrid[:rows, :cols]
mask_area = (x - center[1])**2 + (y - center[0])**2 <= r**2
mask[mask_area] = 1
# Apply mask
fshift = fshift * mask
f_ishift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f_ishift)
img_back = np.abs(img_back)
plt.subplot(1, 2, 1)
plt.imshow(np.log(1 + np.abs(fshift)), cmap='gray')
plt.title('Frequency Domain'); plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(img_back, cmap='gray')
plt.title('Low-pass Filtered Image'); plt.axis('off')
plt.tight_layout(); plt.show()
# Example usage:
link = 'path_to_your_image.jpg' # Replace with your image path
low_pass_filter_frequency(link)
2D DFT
import cv2, numpy as np, matplotlib.pyplot as plt
def dft_2d(link):
img = cv2.imread(link, 0)
dft = np.fft.fft2(img)
dft_shifted = np.fft.fftshift(dft)
magnitude = np.abs(dft_shifted)
plt.subplot(1, 2, 1)
plt.imshow(img, cmap='gray')
plt.title('Original Image'); plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(np.log(1 + magnitude), cmap='gray')
plt.title('2D DFT'); plt.axis('off')
plt.tight_layout(); plt.show()
# Example usage:
link = 'path_to_your_image.jpg' # Replace with your image path
dft_2d(link)
Erosion and Dilation for Binary and Grayscale Images
import cv2, numpy as np, matplotlib.pyplot as plt
def erosion_dilation(link):
img = cv2.imread(link, 0)
_, binary = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
plt.subplot(1, 3, 1)
plt.imshow(binary, cmap='gray')
plt.title('Binary Image'); plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(eroded, cmap='gray')
plt.title('Eroded Image'); plt.axis('off')
plt.subplot(1, 3, 3)
plt.imshow(dilated, cmap='gray')
plt.title('Dilated Image'); plt.axis('off')
plt.tight_layout(); plt.show()
# Example usage:
link = 'path_to_your_image.jpg' # Replace with your image path
erosion_dilation(link)
Morphological Boundary Extraction
import cv2, numpy as np, matplotlib.pyplot as plt
def boundary_extraction(link):
img = cv2.imread(link, 0)
_, binary = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
plt.subplot(1, 2, 1)
plt.imshow(binary, cmap='gray')
plt.title('Binary Image'); plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(boundary, cmap='gray')
plt.title('Boundary Extraction'); plt.axis('off')
plt.tight_layout(); plt.show()
# Example usage:
link = 'path_to_your_image.jpg' # Replace with your image path
boundary_extraction(link)
Morphological Smoothing on Color, Grayscale, and Binary
Images
import cv2, numpy as np, matplotlib.pyplot as plt
def morphological_smoothing(link):
img = cv2.imread(link)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
plt.subplot(1, 3, 1)
plt.imshow(img)
plt.title('Original Color Image'); plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(smoothed_gray, cmap='gray')
plt.title('Grayscale Smoothed'); plt.axis('off')
plt.subplot(1, 3, 3)
plt.imshow(smoothed_binary, cmap='gray')
plt.title('Binary Smoothed'); plt.axis('off')
plt.tight_layout(); plt.show()
# Example usage:
link = 'path_to_your_image.jpg' # Replace with your image path
morphological_smoothing(link)
Global Thresholding for Color and Grayscale Image
import cv2, numpy as np, matplotlib.pyplot as plt
# Plotting
plt.subplot(1, 3, 1); plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)); plt.title('Original');
plt.axis('off')
plt.subplot(1, 3, 2); plt.imshow(gray_thresh, cmap='gray'); plt.title('Grayscale Thresholding');
plt.axis('off')
plt.subplot(1, 3, 3); plt.imshow(color_thresh, cmap='gray'); plt.title('Color Thresholding');
plt.axis('off')
plt.tight_layout(); plt.show()
# Example usage:
link = 'path_to_your_image.jpg' # Replace with your image path
global_thresholding(link)
Adaptive Thresholding
import cv2, numpy as np, matplotlib.pyplot as plt
def adaptive_thresholding(link):
img = cv2.imread(link, 0)
# Adaptive thresholding
adaptive_thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 11, 2)
# Example usage:
link = 'path_to_your_image.jpg' # Replace with your image path
adaptive_thresholding(link)