OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to remove noise from image in Python?

  • Thread starter Thread starter amin007
  • Start date Start date
A

amin007

Guest
I use the code below to preprocess an image

Code:
import numpy as np
from skimage import io, morphology, measure, filters
import matplotlib.pyplot as plt

def preprocess_binary_image(image_path, output_dir):
    # Load the binary image
    binary_image = io.imread(image_path)
    print("Loaded binary image, unique values:", np.unique(binary_image))
    
    # Check if the image is already binary
    if np.array_equal(np.unique(binary_image), [0, 255]) or np.array_equal(np.unique(binary_image), [False, True]):
        binary_image = binary_image > 0
    else:
        # Convert to binary if it's not already
        threshold = filters.threshold_otsu(binary_image)
        binary_image = binary_image > threshold
    
    print("Processed binary image, unique values:", np.unique(binary_image))
    io.imsave(f"{output_dir}/binary_image_debug.png", (binary_image * 255).astype(np.uint8))

    return binary_image

output_dir = 'output_directory'
preprocess_binary_image('sample_image.png', output_dir)

Here's the processed image

enter image description here

The issue lies in the noise removal. As you can see, the noise and the lines in the image are thinner than the main text. I was thinking of adding some code that analyze the thickness of the elements in the image and remove the ones that are thinner. But the result was a complete black image! Do you have any idea for noise removal in such an image?
<p>I use the code below to preprocess an image</p>
<pre><code>import numpy as np
from skimage import io, morphology, measure, filters
import matplotlib.pyplot as plt

def preprocess_binary_image(image_path, output_dir):
# Load the binary image
binary_image = io.imread(image_path)
print("Loaded binary image, unique values:", np.unique(binary_image))

# Check if the image is already binary
if np.array_equal(np.unique(binary_image), [0, 255]) or np.array_equal(np.unique(binary_image), [False, True]):
binary_image = binary_image > 0
else:
# Convert to binary if it's not already
threshold = filters.threshold_otsu(binary_image)
binary_image = binary_image > threshold

print("Processed binary image, unique values:", np.unique(binary_image))
io.imsave(f"{output_dir}/binary_image_debug.png", (binary_image * 255).astype(np.uint8))

return binary_image

output_dir = 'output_directory'
preprocess_binary_image('sample_image.png', output_dir)
</code></pre>
<p>Here's the processed image</p>
<p><a href="https://i.sstatic.net/m67tQNDs.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/m67tQNDs.png" alt="enter image description here" /></a></p>
<p>The issue lies in the noise removal. As you can see, the noise and the lines in the image are thinner than the main text. I was thinking of adding some code that analyze the thickness of the elements in the image and remove the ones that are thinner. But the result was a complete black image! Do you have any idea for noise removal in such an image?</p>
 

Latest posts

Top