Spaces:
Sleeping
Sleeping
Update image enhancement module
Browse files- src/module/image_enhance.py +34 -5
src/module/image_enhance.py
CHANGED
@@ -2,6 +2,8 @@ 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:
|
@@ -11,9 +13,9 @@ class Image_Enhance():
|
|
11 |
# Load the image
|
12 |
image = cv2.imread(self.image_path)
|
13 |
#Plot the original image
|
14 |
-
alpha = 1.
|
15 |
# control brightness by 50
|
16 |
-
beta =
|
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')
|
@@ -21,6 +23,32 @@ class Image_Enhance():
|
|
21 |
cv2.imwrite(imagepth, image2)
|
22 |
return imagepth
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
def sharpen(self, imagepth):
|
25 |
image = cv2.imread(imagepth)
|
26 |
# Create the sharpening kernel
|
@@ -74,9 +102,10 @@ class Image_Enhance():
|
|
74 |
cv2.imwrite(imagepath, image2)
|
75 |
|
76 |
|
77 |
-
obj = Image_Enhance(r"
|
78 |
pth = obj.brightness_Adjust()
|
79 |
sharpen = obj.sharpen(pth)
|
80 |
lapacian_sharpen = obj.lapacian_sharpen(sharpen)
|
81 |
-
noise = obj.removing_noise(
|
82 |
-
obj.enhance_color(noise)
|
|
|
|
2 |
import os
|
3 |
from config import file_Directory
|
4 |
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
class Image_Enhance():
|
8 |
|
9 |
def __init__(self, image_path) -> None:
|
|
|
13 |
# Load the image
|
14 |
image = cv2.imread(self.image_path)
|
15 |
#Plot the original image
|
16 |
+
alpha = -1.1
|
17 |
# control brightness by 50
|
18 |
+
beta = 70
|
19 |
image2 = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)
|
20 |
#Save the image
|
21 |
# imagepth = os.path.join(os.path.dirname(self.image_path), 'Brightness & contrast.jpg')
|
|
|
23 |
cv2.imwrite(imagepth, image2)
|
24 |
return imagepth
|
25 |
|
26 |
+
def remove_flash(self, imagepth):
|
27 |
+
image = cv2.imread(imagepth)
|
28 |
+
# cv2.cvtColor is applied over the
|
29 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
30 |
+
|
31 |
+
# Apply adaptive thresholding to segment the text
|
32 |
+
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 4)
|
33 |
+
|
34 |
+
# Apply Gaussian blur to the grayscale image to reduce noise
|
35 |
+
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
36 |
+
|
37 |
+
# Threshold the blurred image to create a binary mask for the flashlight glare
|
38 |
+
_, mask = cv2.threshold(blurred, 240, 255, cv2.THRESH_BINARY_INV)
|
39 |
+
|
40 |
+
# Combine the text and glare masks
|
41 |
+
mask = cv2.bitwise_or(mask, thresh)
|
42 |
+
|
43 |
+
# Apply morphological closing to further remove small areas of glare
|
44 |
+
kernel = np.ones((5,5),np.uint8)
|
45 |
+
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
|
46 |
+
|
47 |
+
# Apply the mask to the original image to remove flashlight glare
|
48 |
+
result = cv2.bitwise_and(image, image, mask=mask)
|
49 |
+
|
50 |
+
cv2.imwrite(os.path.join(file_Directory, 'remove_flash.jpg'), result)
|
51 |
+
|
52 |
def sharpen(self, imagepth):
|
53 |
image = cv2.imread(imagepth)
|
54 |
# Create the sharpening kernel
|
|
|
102 |
cv2.imwrite(imagepath, image2)
|
103 |
|
104 |
|
105 |
+
obj = Image_Enhance(r"data/Catalog Digitization/ONDC Test Data _ Images/Product Images/Bru_Instant_Coffee_Powder.png")
|
106 |
pth = obj.brightness_Adjust()
|
107 |
sharpen = obj.sharpen(pth)
|
108 |
lapacian_sharpen = obj.lapacian_sharpen(sharpen)
|
109 |
+
noise = obj.removing_noise(sharpen)
|
110 |
+
obj.enhance_color(noise)
|
111 |
+
obj.remove_flash(sharpen)
|