如何从Python中的图像中删除噪音

时间:2018-06-25 12:11:54

标签: python matplotlib image-processing noise-reduction

我正在尝试消除图像中的噪点,如果满足某些条件,我会尝试制作白色像素,但是我正在努力做到这一点。

这是我的图像,我想删除所有灰色线条,只希望使用高强度颜色,例如蓝红色和绿色 。 抱歉,我的编辑

enter image description here

这是我尝试检查成功条件的代码,然后将像素更改为白色

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()

2 个答案:

答案 0 :(得分:2)

我尝试过不透明,它对我有用。然后我有使用内核。这个答案的一个问题是,它花费了更多时间。请让我知道他们是否有更好的方法

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()

答案 1 :(得分:0)

虽然这不是最佳做法,但是您可以通过用白色像素值(255)替换不需要的强度值来实现。

使用skimage可以实现以下目的。

from skimage import io
import numpy as np

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

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

可以更改阈值范围(从215到235)以获得所需结果。

这是此代码的输出。

enter image description here