You are on page 1of 1

import cv2

import numpy as np
from matplotlib import pyplot as plt

# Read the image from the local directory


img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)

# Image negative
img_negative = 255 - img

# Log and gamma transformation


c = 255 / np.log(1 + np.max(img))
img_log = c * np.log(1 + img)
gamma = 0.5
img_gamma = np.array(255 * (img / 255) ** gamma, dtype='uint8')

# Contrast stretching
p1, p2 = np.percentile(img, (5, 95))
img_contrast_stretching = cv2.normalize(img, None, alpha=0, beta=255,
norm_type=cv2.NORM_MINMAX)
img_contrast_stretching = cv2.convertScaleAbs(img_contrast_stretching, alpha=(255 /
(p2 - p1)), beta=(-255 * p1 / (p2 - p1)))

# Bit plane slicing


bit_planes = []
for i in range(8):
bit_plane = np.zeros((img.shape[0], img.shape[1]), dtype='uint8')
bit_plane[img & (1 << i) != 0] = 255
bit_planes.append(bit_plane)

# Display the results


plt.figure(figsize=(20, 20))

plt.subplot(2, 3, 1)
plt.imshow(img, cmap='gray')
plt.title('Original Image')

plt.subplot(2, 3, 2)
plt.imshow(img_negative, cmap='gray')
plt.title('Image Negative')

plt.subplot(2, 3, 3)
plt.imshow(img_log, cmap='gray')
plt.title('Log Transformation')

plt.subplot(2, 3, 4)
plt.imshow(img_gamma, cmap='gray')
plt.title('Gamma Transformation')

plt.subplot(2, 3, 5)
plt.imshow(img_contrast_stretching, cmap='gray')
plt.title('Contrast Stretching')

plt.subplot(2, 3, 6)
plt.imshow(np.hstack(bit_planes), cmap='gray')
plt.title('Bit Plane Slicing')

plt.show()

You might also like