0

Input Image::

I have tried to remove the noise from the image using the following code:

Remove wavy noise from image background using OpenCV

But it's not removing the noise from the image. Is there any other way to remove this kind of noise?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

1

You can do this with opencv morphology operations. The steps I used to remove the noise are as follows

Here is the resulting image: enter image description here

Here is the code. I will leave it up to you play the parameters and find good combinations to get the exact results that you desire. I recommend displaying the image at each step to get a better understanding of what each operation does.

import numpy as np
import cv2

# read in image as grayscale
image = cv2.imread(r"path\to\image", -1)
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# blurr image
blurred = cv2.GaussianBlur(image, (15,15), 0)

# threshold image
thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 15, 3)

# apply Morphological operations
kernel_3 = np.ones((3,3), dtype=np.uint8)
kernel_5 = np.ones((5,5), dtype=np.uint8)

# perform Morphological Operations
dilation = cv2.dilate(thresh, kernel_5, iterations=1) 
blackhat = cv2.morphologyEx(dilation, cv2.MORPH_BLACKHAT, kernel_3) 
weighted = cv2.addWeighted(dilation, 1.0, blackhat, -1.0, 0) 
erosion = cv2.erode(weighted, kernel_5, iterations=1) 

# Use the simple blob detector to remove small unwanted artifacts
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

# Change thresholds
params.minThreshold = 0
params.maxThreshold = 255

# Filter by Area.
params.filterByArea = True
params.minArea = 10
params.maxArea = 250

# Filter by Circularity
params.filterByCircularity = False
params.minCircularity = 0.1

# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.1

# Filter by Inertia
params.filterByInertia = False
params.minInertiaRatio = 0.01

# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs.
keypoints = detector.detect(erosion)

# (OPTIONAL) Draw detected blobs as red circles.
# im_with_keypoints = cv2.drawKeypoints(erosion, keypoints, np.array([]), (0),  cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# instead use the key points to erase unwanted blobs
filtered = erosion.copy()
for kp in keypoints:
    cv2.circle(filtered, 
               center=np.round(kp.pt).astype(int), 
               radius=np.ceil(kp.size).astype(int), 
               color=(255), 
               thickness=-1)

# display
cv2.imshow("image", filtered)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • Thank you so much for you help. It's really helpful and i have tested with other images as well it's working perfectly fine. – Rishav Banerjee Feb 03 '23 at 06:23
  • i have tested your code and it's working fine for this kind of image but if the image is clear it's also affecting those images. I have tried with params fine tune but its not working. Can you let me know any other approach? – Rishav Banerjee Feb 05 '23 at 10:06
  • Another approach would be to determine if the noise is present and then apply the removal. – Isaac Berrios Feb 06 '23 at 14:03
  • If you need more help with this, please make a new question (i.e. how to determine if noise is present) and provide image examples. – Isaac Berrios Feb 06 '23 at 14:04
  • i have raised a new question with few images. Here is the link for that https://stackoverflow.com/questions/75364617/how-to-determine-if-noise-is-present-then-remove-the-noise – Rishav Banerjee Feb 07 '23 at 19:33