CGIP Program Assignment (26)
CGIP Program Assignment (26)
Programming Assignment
Implementation
import cv2
import numpy as np
import random
import matplotlib.pyplot as plt
from skimage import data
return noisy_img
def main():
image = data.camera()
noisy_image = add_salt_and_pepper_noise(image, 0.02, 0.02)
median_3x3 = apply_median_filter(noisy_image, 3)
median_5x5 = apply_median_filter(noisy_image, 5)
mean_3x3 = apply_mean_filter(noisy_image, 3)
mean_5x5 = apply_mean_filter(noisy_image, 5)
for ax in axes.ravel():
ax.axis('off')
plt.tight_layout()
plt.show()
if __name__ == "__main__":
main()
Output
2. Write a program that perform histogram equalization of the input gray scale
image. Plot the histogram of original and histogram equalized.
Implementation
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import data
def histogram_equalization():
gray_image = data.camera()
equalized_image = cv2.equalizeHist(gray_image)
plt.tight_layout()
plt.show()
histogram_equalization()
Output
3. Apply high pass filtering of input image using the mask h(m,n)=[1 1 1;1 -8
1;1 1 1].
Implementation
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import data
def high_pass_filtering():
gray_image = data.camera()
axes[0].imshow(gray_image, cmap='gray')
axes[0].set_title("Original Image")
axes[0].axis("off")
axes[1].imshow(high_pass_image, cmap='gray')
axes[1].set_title("High-Pass Filtered Image")
axes[1].axis("off")
plt.tight_layout()
plt.show()
high_pass_filtering()
Output
4. Write a function called powerlaw that accept uint8 bits intensity image and
variable gamma and applies power law transformation to the input image.
Output image should be a uint8 intensity image observe the output image by
varying the value of gamma between 0.2 and 1.2 in the steps of 0.2.
Implementation
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import data
def powerlaw(image, gamma):
return output_image
def observe_powerlaw():
gray_image = data.camera().astype(np.uint8)
plt.tight_layout()
plt.show()
observe_powerlaw()
Output
5. Read an input image and compute the edges using edge detectors like Robert,
prewitt, sobel and canny and comment the result.
Implementation
import cv2
import numpy as np
import matplotlib.pyplot as plt
from skimage import data, filters
def edge_detection(image):
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if len(image.shape) == 3
else image
roberts_edges = filters.roberts(gray_image)
prewitt_edges = filters.prewitt(gray_image)
sobel_edges = filters.sobel(gray_image)
canny_edges = cv2.Canny(gray_image, 100, 200)
axes[0].imshow(gray_image, cmap='gray')
axes[0].set_title("Original Image")
axes[0].axis("off")
axes[1].imshow(roberts_edges, cmap='gray')
axes[1].set_title("Roberts Edge Detection")
axes[1].axis("off")
axes[2].imshow(prewitt_edges, cmap='gray')
axes[2].set_title("Prewitt Edge Detection")
axes[2].axis("off")
axes[3].imshow(sobel_edges, cmap='gray')
axes[3].set_title("Sobel Edge Detection")
axes[3].axis("off")
axes[4].imshow(canny_edges, cmap='gray')
axes[4].set_title("Canny Edge Detection")
axes[4].axis("off")
fig.delaxes(axes[5])
plt.tight_layout()
plt.show()
test_image = data.camera()
edge_detection(test_image)
Output