Repository navigation
Expand file tree
/
Copy pathfilter_image.py
More file actions
67 lines (49 loc) · 1.8 KB
/
Copy pathfilter_image.py
File metadata and controls
67 lines (49 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import cv2
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
def blur_image(image):
# Gaussian blur
return cv2.GaussianBlur(image, (9, 9), 0)
def sharpen_image(image):
# Sharpening kernel
kernel = np.array([[0, -1, 0],
[-1, 5,-1],
[0, -1, 0]])
return cv2.filter2D(image, -1, kernel)
def edge_image(image):
# Canny edge detection
return cv2.Canny(image, 100, 200)
if __name__ == "__main__":
img_path = r'C:\Users\HP\Desktop\Learn\Python Image\All-basic-python-image\Input_image\1.png'
image = cv2.imread(img_path)
if image is None:
print("Image not found.")
exit()
blurred = blur_image(image)
sharpened = sharpen_image(image)
edges = edge_image(image)
#cv2.imwrite("blurred.jpg", blurred)
#cv2.imwrite("sharpened.jpg", sharpened)
#cv2.imwrite("edges.jpg", edges)
plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
plt.title('Original Image')
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) # Convert BGR to RGB for display
# cv2 reads images in BGR format, convert to RGB for matplotlib
plt.axis('off')
plt.subplot(2, 2, 2)
plt.title('Blurred Image')
plt.imshow(cv2.cvtColor(blurred, cv2.COLOR_BGR2RGB)) # Convert BGR to RGB for display
plt.imsave("blurred_image.jpg", blurred) # Save the blurred image
plt.axis('off')
plt.subplot(2, 2, 3)
plt.title('Sharpened Image')
plt.imshow(cv2.cvtColor(sharpened, cv2.COLOR_BGR2RGB)) # Convert BGR to RGB for display
plt.axis('off')
plt.subplot(2, 2, 4)
plt.title('Edges Detected Image')
plt.imshow(cv2.cvtColor(edges, cv2.COLOR_BGR2RGB), cmap='gray') # Convert BGR to RGB for display
plt.axis('off')
plt.tight_layout()
plt.show()