0

I enchant low light image with bm3d by following code

import numpy as np
import cv2
import bm3d
import os
folder = "D:\pythonProject1\image\mask"
path_enchant = "D:\pythonProject1\enchant\mask"
path_denoised = "D:\pythonProject1\denoised\mask"
path_sharp = "D:\pythonProject1\sharpen\mask"
images = []
name = os.listdir(folder)
i = 0
for filename in os.listdir(folder):
    img = cv2.imread(os.path.join(folder,filename))
    if img is not None:
       images.append(img)
for img in images:
    # HTC enchant
    img = img / 225
    k = 5.6
    w = np.mean(np.mean(np.mean(img)))
    Iw = w * img + (1 - w) * np.tanh(k * img)
    Is = (Iw - np.min(Iw)) / (np.max(Iw) - np.min(Iw))
    cv2.imwrite(os.path.join(path_enchant, name[i]), Is * 255)
    # BM3D
    denoised_image = bm3d.bm3d(Is, sigma_psd=30 / 255, stage_arg=bm3d.BM3DStages.ALL_STAGES)
    cv2.imwrite(os.path.join(path_denoised, name[i]), denoised_image * 255)
    # sharpen image
    alpha = 0.8
    lda = 0.3
    h1 = alpha / (1 + alpha)
    h2 = (1 - alpha) / (alpha + 1)
    matrix = np.array([[-h1, -h2, -h1], [-h2, 1 + (4 / (alpha + 1)), -h2], [-h1, -h2, -h1]])
    S = cv2.filter2D(denoised_image, -1, matrix, dst=None, anchor=None, delta=None, 
                     borderType=None)
    Ysharp = denoised_image + lda * S
    cv2.imwrite(os.path.join(path_sharp, name[i]), Ysharp * 255)
    i = i+1

But with 3000 images, it used up 15gb RAM and lead to this error.Is there a way to process each batch of images without causing this error?

Traceback (most recent call last):
File "D:/pythonProject1/echan.py", line 25, in <module>
denoised_image = bm3d.bm3d(Is, sigma_psd=30 / 255,  stage_arg=bm3d.BM3DStages.ALL_STAGES)
File "D:\pythonProject1\venv\lib\site-packages\bm3d\__init__.py", line 299, in bm3d
y_hat, wie_blocks = bm3d_step (BM3DStages.WIENER_FILTERING, Z, psd_blur_mult, single_dim_psd,
File "D:\python Project1\venv\lib site-packages\bm3d\bm3d_ctypes.py", line 166, in bm3d_step
c_y_hat = conv_to_array (np.ascontiguousarray(y_hat.transpose (2, 1, 0).flatten(), dtype=np.float32))
File "D:\pythonProjectl\venv\lib site-packages\bm3d\bm3d_ctypes.py", line 121, in conv_to_array
return (ctype * len(pyarr))(*pyarr)
MemoryError
Huyng
  • 13
  • 3

0 Answers0