2

I'm trying to remove noise from image, i'm trying to make white pixel if certain condition met but i'm struggling to make that happen.

This is my image and i want to remove all gray color lines only want high intensity color like blue red and green . Sorry for my editing

enter image description here

This is my code where i have tried to check the condition which succeed then i'll change the pixel to white

height, width = image.shape[0:2]
for i in range(0, height):  # looping at python speed...
    for j in range(0, width):
        # print(image)
        if ((image[i][j][1] * 255 == image[i][j][2] * 255 == image[i][j][3] * 255) or (
                (image[i][j][0] * 255 == image[i][j][1] * 255) and (
                image[i][j][3] * 255 >= 245))):
            # print(image[i][j][2] * 255)
            image[i][j] = 0

plt.imshow(image)
plt.savefig("filename.png")
plt.show()
Abhijeet Gulve
  • 799
  • 1
  • 9
  • 23
  • you might be able to get your desired result by using one of the built in threshold functions on the grayscale image to create a mask. If that doesn't work then you can repeat that the thresholding to create a mask on each channel and combine them. the docs are good. – chris Jun 25 '18 at 13:04
  • Thanks i'll try it out – Abhijeet Gulve Jun 25 '18 at 13:20

2 Answers2

3

I have tried with opacity and it work for me. Then i have use kernel. One issue with this answer is that it taking bit more time. Please let me know if their any better way

import matplotlib.pyplot as plt
import cv2
import numpy as np

image = plt.imread('../heatmapwms.png')

height, width = image.shape[0:2]
for i in range(0, height):  
    for j in range(0, width):
        if (image[i][j][3] <= .34 or (
                (image[i][j][2] * 255 > 170) and (image[i][j][1] * 255 > 150) and (image[i][j][0] * 255 > 150))):
            image[i][j] = 0

kernel = np.ones((3, 3), np.float32) / 9
image = cv2.filter2D(image, -1, kernel)

for i in range(0, height):  
    for j in range(0, width):
        if (image[i][j][3] <= .30 or (
                (image[i][j][2] * 255 > 170) and (image[i][j][1] * 255 > 150) and (image[i][j][0] * 255 > 150))):
            image[i][j] = 0

kernel = np.ones((3, 3), np.float32) / 9
image = cv2.filter2D(image, -1, kernel)

plt.imshow(image)
plt.savefig("filename.png")
plt.show()
Abhijeet Gulve
  • 799
  • 1
  • 9
  • 23
1

Although it's not best practice but you can achieve this by replacing unwanted intensity values with white pixel values (255).

Using skimage you can achieve this as below.

from skimage import io
import numpy as np

img = io.imread('your_image_file.png')

img[(img > 215) & (img < 235)] = 255

The threshold of values' range (from 215 to 235) could be changed for desired results.

Here's the output of this code.

enter image description here