-3

I am trying to denoise multiple gray-scaled text images from a folder. I have converted all the images into gray-scale already. All I want is to remove noise or blurriness from all the images without changing text. For this, I am using opencv in order to remove blurriness or noisiness. I have written the code as shown below, when I run the code it shows no error and displays nothing.Please help me to solve this problem. I am new in image processing that's why I am confused. Here's my code...

import numpy as np
from PIL import Image
import cv2
import glob
src_path = r"C:\Users\usama\Documents\FYP-Data\FYP Project Data\grayscale images\*.png" #images folder path
def get_string(src_path):
    for filename in glob.glob(src_path):
        img = cv2.imread(filename)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        kernel = np.ones((1, 1), np.uint8)
        img = cv2.dilate(img, kernel, iterations=1)
        img = cv2.erode(img, kernel, iterations=1)
        cv2.imwrite(src_path + "filename", img)
Usama Ali
  • 3
  • 1
  • 7
  • It may be the spaces in your directory names. Can you close up their names and try again? – fmw42 Jun 09 '20 at 18:33
  • `cv2.waitKey` is missing. This is extremely important if you want to show images in the OpenCV windowing system. Not doing this will hang your system. – rayryeng Jun 09 '20 at 18:37
  • Now my code works but not properly it shows the images But it doesn't remove the noise or blurriness of images.Please help me to solve the problem...@rayryeng – Usama Ali Jun 10 '20 at 11:41
  • This is a new question. Please open a new one and actually show example images. – rayryeng Jun 11 '20 at 06:28

1 Answers1

0

You should narrow down the files you load in. This I prefer to do with glob which allows for easy regular expression patterns when searching for files. I would expect that either you get to a file that is not an image but still loaded or that you are missing a cv2.waitKey(0) to exit the view.

import cv2
from glob import glob

files = glob('*.jpg')
for filename in glob('*.jpg'):
    img = cv2.imread(filename)
    bilateral_blur = cv2.bilateralFilter(img, 9, 75, 75)
    cv2.imshow('denoised_images', bilateral_blur)
    cv2.waitKey(0)
  • I have used the above code but It just showing one image the first one @Jakob Guldberg Aaes – Usama Ali Jun 09 '20 at 18:57
  • Have you ensured your file format? – Jakob Guldberg Aaes Jun 09 '20 at 19:31
  • Of cause you have to make sure you use the correct path. This worked for me in a folder containing 3 files. – Jakob Guldberg Aaes Jun 09 '20 at 19:43
  • Yes. I have used the correct path. But still the same problem. Here is my code `import cv2 from glob import glob files = glob(r'C:\Users\usama\Documents\FYP-Data\FYP Project Data\grayscale images\*.png') for filename in glob(r'C:\Users\usama\Documents\FYP-Data\FYP Project Data\grayscale images\*.png'): #print(filename) img=cv2.imread( filename) bilateral_blur=cv2.bilateralFilter(img, 9, 75, 75) cv2.imshow('denoised_images', bilateral_blur) cv2.waitKey(0)` ...@Jakob Guldberg Aaes – Usama Ali Jun 09 '20 at 20:24
  • 1
    You have to press any key to show the next one. The code blocks until a key is pushed. – rayryeng Jun 09 '20 at 23:44
  • I haven't added the `cv2.destroyAllWindows()`, that's why I was getting the error. But in my code it does not remove the noise of the image. what thing should I need to do i order to remove the noise or blurriness of the images... – Usama Ali Jun 10 '20 at 10:28
  • For this you should open another question. But to be a bit blunt, then you can't make a blurred image sharp in any deterministic way as the information is not present in the image. – Jakob Guldberg Aaes Jun 10 '20 at 17:44