Spaces:
Sleeping
Sleeping
Add image enhancement module
Browse files- src/module/image_enhance.py +82 -0
src/module/image_enhance.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import os
|
3 |
+
from config import file_Directory
|
4 |
+
import numpy as np
|
5 |
+
class Image_Enhance():
|
6 |
+
|
7 |
+
def __init__(self, image_path) -> None:
|
8 |
+
self.image_path = image_path
|
9 |
+
|
10 |
+
def brightness_Adjust(self):
|
11 |
+
# Load the image
|
12 |
+
image = cv2.imread(self.image_path)
|
13 |
+
#Plot the original image
|
14 |
+
alpha = 1.5
|
15 |
+
# control brightness by 50
|
16 |
+
beta = -150
|
17 |
+
image2 = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)
|
18 |
+
#Save the image
|
19 |
+
# imagepth = os.path.join(os.path.dirname(self.image_path), 'Brightness & contrast.jpg')
|
20 |
+
imagepth = os.path.join(file_Directory, 'Brightness & contrast.jpg')
|
21 |
+
cv2.imwrite(imagepth, image2)
|
22 |
+
return imagepth
|
23 |
+
|
24 |
+
def sharpen(self, imagepth):
|
25 |
+
image = cv2.imread(imagepth)
|
26 |
+
# Create the sharpening kernel
|
27 |
+
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
|
28 |
+
# Sharpen the image
|
29 |
+
sharpened_image = cv2.filter2D(image, -1, kernel)
|
30 |
+
#Save the image
|
31 |
+
imagepath = os.path.join(file_Directory, 'sharpened_image.jpg')
|
32 |
+
cv2.imwrite(imagepath, sharpened_image)
|
33 |
+
return imagepath
|
34 |
+
|
35 |
+
|
36 |
+
def lapacian_sharpen(self, imagepth):
|
37 |
+
#Load the image
|
38 |
+
image = cv2.imread(imagepth)
|
39 |
+
|
40 |
+
# Sharpen the image using the Laplacian operator
|
41 |
+
sharpened_image2 = cv2.Laplacian(image, cv2.CV_64F)
|
42 |
+
imagepath = os.path.join(file_Directory, 'Laplacian_sharpened_image.jpg')
|
43 |
+
#Save the image
|
44 |
+
cv2.imwrite(imagepath, sharpened_image2)
|
45 |
+
|
46 |
+
def removing_noise(self, imagepth):
|
47 |
+
# Load the image
|
48 |
+
image = cv2.imread(imagepth)
|
49 |
+
# Remove noise using a median filter
|
50 |
+
filtered_image = cv2.medianBlur(image, 1)
|
51 |
+
imagepath = os.path.join(file_Directory, 'Median Blur.jpg')
|
52 |
+
#Save the image
|
53 |
+
cv2.imwrite(imagepath, filtered_image)
|
54 |
+
|
55 |
+
return imagepath
|
56 |
+
|
57 |
+
|
58 |
+
def enhance_color(self, imagepth):
|
59 |
+
# Load the image
|
60 |
+
image = cv2.imread(imagepth)
|
61 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
|
62 |
+
|
63 |
+
# Adjust the hue, saturation, and value of the image
|
64 |
+
# Adjusts the hue by multiplying it by 0.7
|
65 |
+
image[:, :, 0] = image[:, :, 0] * 0.7
|
66 |
+
# Adjusts the saturation by multiplying it by 1.5
|
67 |
+
image[:, :, 1] = image[:, :, 1] * 1.5
|
68 |
+
# Adjusts the value by multiplying it by 0.5
|
69 |
+
image[:, :, 2] = image[:, :, 2] * 0.5
|
70 |
+
|
71 |
+
image2 = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)
|
72 |
+
imagepath = os.path.join(file_Directory, 'enhanced coloured.jpg')
|
73 |
+
#Save the image
|
74 |
+
cv2.imwrite(imagepath, image2)
|
75 |
+
|
76 |
+
|
77 |
+
obj = Image_Enhance(r"/home/vrush/Catalog-Digitization-/src/module/data/Catalog Digitization/ONDC Test Data _ Images/Product Images/Bru_Instant_Coffee_Powder.png")
|
78 |
+
pth = obj.brightness_Adjust()
|
79 |
+
sharpen = obj.sharpen(pth)
|
80 |
+
lapacian_sharpen = obj.lapacian_sharpen(sharpen)
|
81 |
+
noise = obj.removing_noise(pth)
|
82 |
+
obj.enhance_color(noise)
|