DIP First 5 Prog
DIP First 5 Prog
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Negative Transformation
def negative_transformation(img):
return 255 - img
# Logarithmic Transformation
def log_transformation(img):
c = 255 / np.log(1 + np.max(img))
return np.uint8(c * np.log(1 + img))
# Apply transformations
negative_img = negative_transformation(image)
log_img = log_transformation(image)
gamma_img = gamma_transformation(image, gamma=0.4) # try
gamma=0.4 or 2.2 for different effects
# Display results
titles = ['Original Image', 'Negative', 'Logarithmic', 'Gamma (0.4)']
images = [image, negative_img, log_img, gamma_img]
plt.figure(figsize=(12, 8))
for i in range(4):
plt.subplot(2, 2, i+1)
plt.imshow(images[i], cmap='gray')
plt.title(titles[i])
plt.axis('off')
plt.tight_layout()
plt.show()
2.import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
for i in range(height):
for j in range(width):
for c in range(channels):
negative_img[i, j, c] = 255 - img_array[i, j, c]
plt.subplot(1, 2, 2)
plt.imshow(negative_img)
plt.title("Negative Image")
plt.axis('off')
plt.tight_layout()
plt.show()
3.import cv2
import numpy as np
import matplotlib.pyplot as plt
if image is None:
raise ValueError("Image not found. Please check the path.")
# Operation (a) s = r + 50
add_50 = np.clip(image + 50, 0, 255).astype(np.uint8)
# Operation (b) s = r - 50
sub_50 = np.clip(image - 50, 0, 255).astype(np.uint8)
# Operation (d) s = r * 2
mul_2 = np.clip(image * 2, 0, 255).astype(np.uint8)
# Display results
titles = [
"Original Image",
"s = r + 50",
"s = r - 50",
"s = r * 0.5",
"s = r * 2"
]
images = [image, add_50, sub_50, mul_05, mul_2]
plt.figure(figsize=(12, 8))
for i in range(5):
plt.subplot(2, 3, i+1)
plt.imshow(images[i], cmap='gray')
plt.title(titles[i])
plt.axis('off')
plt.tight_layout()
plt.show()
4.import cv2
import numpy as np
import matplotlib.pyplot as plt
# 1. Contrast Stretching
def contrast_stretch(img, a=70, b=180):
stretched = np.zeros_like(img)
for i in range(img.shape[0]):
for j in range(img.shape[1]):
r = img[i, j]
if r < a:
s=0
elif r > b:
s = 255
else:
s = (r - a) * 255 / (b - a)
stretched[i, j] = np.clip(s, 0, 255)
return stretched.astype(np.uint8)
# Apply transformations
contrast_img = contrast_stretch(image)
sliced_img = intensity_level_slicing(image)
bit_planes = bit_plane_slicing(image)
# Display results
plt.figure(figsize=(12, 10))
plt.subplot(2, 3, 2)
plt.imshow(contrast_img, cmap='gray')
plt.title("Contrast Stretching")
plt.axis('off')
plt.subplot(2, 3, 3)
plt.imshow(sliced_img, cmap='gray')
plt.title("Intensity Level Slicing")
plt.axis('off')
plt.tight_layout()
plt.show()
5.import cv2
import numpy as np
import matplotlib.pyplot as plt
# Plotting helper
def plot_histogram(img, title):
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
plt.plot(hist, color='black')
plt.title(title)
plt.xlabel('Pixel Intensity')
plt.ylabel('Frequency')
plt.grid()
# Original Image
plt.subplot(2, 3, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
plt.axis('off')
# Histogram of Original
plt.subplot(2, 3, 4)
plot_histogram(image, 'Original Histogram')
plt.tight_layout()
plt.show()