Smooth image to remove noise
This commit is contained in:
+10
-7
@@ -1,23 +1,26 @@
|
|||||||
import cv2
|
import cv2
|
||||||
|
|
||||||
def enhance_image_for_ai(input_path: str) -> str:
|
def enhance_image_for_ai(input_path: str) -> str:
|
||||||
"""Enhances dark areas and contrast using CLAHE without blowing out bright areas."""
|
"""Smooths camera sensor noise and brightens dark areas for vision LLMs."""
|
||||||
img = cv2.imread(input_path)
|
img = cv2.imread(input_path)
|
||||||
if img is None:
|
if img is None:
|
||||||
return input_path
|
return input_path
|
||||||
|
|
||||||
# Convert to LAB color space to modify luminance channel only
|
# 1. Remove ISO sensor noise (grain)
|
||||||
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
|
denoised = cv2.fastNlMeansDenoisingColored(img, None, h=7, hColor=7, templateWindowSize=7, searchWindowSize=21)
|
||||||
|
|
||||||
|
# 2. Convert to LAB color space for luminance enhancement
|
||||||
|
lab = cv2.cvtColor(denoised, cv2.COLOR_BGR2LAB)
|
||||||
l, a, b = cv2.split(lab)
|
l, a, b = cv2.split(lab)
|
||||||
|
|
||||||
# Apply CLAHE to Lightness channel
|
# 3. Apply softer CLAHE (lower clipLimit prevents noise amplification)
|
||||||
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
|
clahe = cv2.createCLAHE(clipLimit=1.8, tileGridSize=(8, 8))
|
||||||
cl = clahe.apply(l)
|
cl = clahe.apply(l)
|
||||||
|
|
||||||
# Merge channels and convert back to BGR
|
# 4. Merge channels back
|
||||||
limg = cv2.merge((cl, a, b))
|
limg = cv2.merge((cl, a, b))
|
||||||
enhanced = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
|
enhanced = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
|
||||||
|
|
||||||
enhanced_path = input_path.replace(".jpg", "_enhanced.jpg")
|
enhanced_path = input_path.replace(".jpg", "_enhanced.jpg")
|
||||||
cv2.imwrite(enhanced_path, enhanced)
|
cv2.imwrite(enhanced_path, enhanced, [cv2.IMWRITE_JPEG_QUALITY, 90])
|
||||||
return enhanced_path
|
return enhanced_path
|
||||||
Reference in New Issue
Block a user