Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用python对opencv中的噪声图像进行中值滤波_Python_Opencv_Image Processing_Computer Vision - Fatal编程技术网

如何用python对opencv中的噪声图像进行中值滤波

如何用python对opencv中的噪声图像进行中值滤波,python,opencv,image-processing,computer-vision,Python,Opencv,Image Processing,Computer Vision,我不熟悉OpenCV和Python。我想通过首先向图像添加噪声来执行高斯滤波和中值滤波。我已成功输出高斯滤波器,但无法获得中值滤波器。请任何人解释如何使用Python在OpenCV中对噪声图像执行中值滤波。以下是我的代码: import numpy as np import cv2 img = cv2.imread('lizard.jpg').astype(np.float32) gaussian_blur = np.array([[1,2,1],[2,4,2],[1,2,1]

我不熟悉OpenCV和Python。我想通过首先向图像添加噪声来执行高斯滤波和中值滤波。我已成功输出高斯滤波器,但无法获得中值滤波器。请任何人解释如何使用Python在OpenCV中对噪声图像执行中值滤波。以下是我的代码:

 import numpy as np

 import cv2

 img = cv2.imread('lizard.jpg').astype(np.float32)



 gaussian_blur = np.array([[1,2,1],[2,4,2],[1,2,1]],dtype=np.float32)
 gaussian_blur = gaussian_blur/np.sum(gaussian_blur)


 img_noise = img + np.random.uniform(-20,20,size=np.shape(img))

 cv2.imwrite('gt3_plus_noise.jpg',img_noise)
 median = cv2.medianBlur(img_noise.astype(np.float32),(3),0)
 cv2.imshow('Median Blur',median)
 cv2.waitKey()
 cv2.destroyAllWindows()
 img_blur_g = cv2.filter2D(img_noise.astype(np.float32), -1,gaussian_blur)

 cv2.imwrite('gt3_noise_filtered_gaussian.jpg',img_blur_g)
输出:

噪声滤波高斯

噪声图像

中值滤波图像


P>> Pop> Python和C++中的函数中介函数进行中值滤波。您可以从这里获得详细信息:

要使用此函数,请执行以下代码段:

n=3; #where n*n is the size of filter
output_image = cv2.medianBlur(input_image, n)

在Python和C++中,op>具有函数中值,进行中值滤波。您可以从这里获得详细信息:

要使用此函数,请执行以下代码段:

n=3; #where n*n is the size of filter
output_image = cv2.medianBlur(input_image, n)

当OpenCV有一个
float
图像时,它假定范围在0到1之间。但是,您的图像仍然具有介于0和255之间的值(可能略高于或低于该值)。这对于操作来说很好,但为了查看图像,您需要将其规格化为0和1的范围,或者,您必须转换回
uint8
图像并饱和值。当前图像刚刚溢出超过1,这是浮动图像的假定最大值。颜色仅显示在图像的较暗区域,因为值非常小;具体来说,不到1

使
uint8
图像的值饱和意味着低于0的值固定为0,高于255的值固定为255。正常的numpy操作不会使值饱和,它们会溢出并翻转(因此
np.array(-1).astype(np.uint8)==>255
,这意味着任何减去一些位的暗值都会变亮)。有关饱和度的更多信息,请参阅

这个问题不难解决,有很多解决方案。一种明确的方法是,只需在255处固定大于255的值,并将小于0的值固定为0,然后转换为
uint8
图像:

>>> img = np.array([[150, 0], [255, 150]], dtype=np.float32)
>>> noise = np.array([[20, -20], [20, -20]], dtype=np.float32)
>>> noisy_img = img+noise
>>> noisy_img
array([[ 170.,  -20.],
       [ 275.,  130.]], dtype=float32)
>>> noisy_img[noisy_img>255] = 255
>>> noisy_img[noisy_img<0] = 0
>>> noisy_img = np.uint8(noisy_img)
>>> noisy_img
array([[170,   0],
       [255, 130]], dtype=uint8)

当OpenCV有一个
float
图像时,它假定范围在0到1之间。但是,您的图像仍然具有介于0和255之间的值(可能略高于或低于该值)。这对于操作来说很好,但为了查看图像,您需要将其规格化为0和1的范围,或者,您必须转换回
uint8
图像并饱和值。当前图像刚刚溢出超过1,这是浮动图像的假定最大值。颜色仅显示在图像的较暗区域,因为值非常小;具体来说,不到1

使
uint8
图像的值饱和意味着低于0的值固定为0,高于255的值固定为255。正常的numpy操作不会使值饱和,它们会溢出并翻转(因此
np.array(-1).astype(np.uint8)==>255
,这意味着任何减去一些位的暗值都会变亮)。有关饱和度的更多信息,请参阅

这个问题不难解决,有很多解决方案。一种明确的方法是,只需在255处固定大于255的值,并将小于0的值固定为0,然后转换为
uint8
图像:

>>> img = np.array([[150, 0], [255, 150]], dtype=np.float32)
>>> noise = np.array([[20, -20], [20, -20]], dtype=np.float32)
>>> noisy_img = img+noise
>>> noisy_img
array([[ 170.,  -20.],
       [ 275.,  130.]], dtype=float32)
>>> noisy_img[noisy_img>255] = 255
>>> noisy_img[noisy_img<0] = 0
>>> noisy_img = np.uint8(noisy_img)
>>> noisy_img
array([[170,   0],
       [255, 130]], dtype=uint8)

你的图像被破坏了,顺便说一句。你的图像被破坏了,顺便说一句。