python中值滤波去除反光_数学之路-python计算实战(17)-机器视觉-滤波去噪(中值滤波)...

Blurs an image using the median filter.C++:void medianBlur(InputArray src, OutputArray dst, int ksize)Python:cv2.medianBlur(src, ksize[, dst]) → dstParameters:src – input 1-, 3-, or 4-channel image; when ksize is 3 or 5, the image depth should be CV_8U, CV_16U, or CV_32F, for larger aperture sizes, it can only be CV_8U.

dst – destination array of the same size and type as src.

ksize – aperture linear size; it must be odd and greater than 1, for example: 3, 5, 7 ...

The function smoothes an image using the median filter with the

e5e433136f4cc9f409bfbb973054e37b7ddb7192.pngaperture. Each channel of a multi-channel image is processed independently. In-place operation is supported.

中值滤波将图像的每一个像素用邻域 (以当前像素为中心的正方形区域)像素的中值取代 。

与邻域平均法相似,但计算的是中值

本博客全部内容是原创,假设转载请注明来源

#用中值法

for y in xrange(1,myh-1):

for x in xrange(1,myw-1):

lbimg[y,x]=np.median(tmpimg[y-1:y+2,x-1:x+2]

以下是调用opencv 的函数

# -*- coding: utf-8 -*-

#code:myhaspl@myhaspl.com

#中值滤波

import cv2

import numpy as np

fn="test3.jpg"

myimg=cv2.imread(fn)

img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY)

#加上椒盐噪声

#灰阶范围

w=img.shape[1]

h=img.shape[0]

newimg=np.array(img)

#噪声点数量

noisecount=50000

for k in xrange(0,noisecount):

xi=int(np.random.uniform(0,newimg.shape[1]))

xj=int(np.random.uniform(0,newimg.shape[0]))

newimg[xj,xi]=255

#滤波去噪

lbimg=cv2.medianBlur(newimg,3)

cv2.imshow('src',newimg)

cv2.imshow('dst',lbimg)

cv2.waitKey()

cv2.destroyAllWindows()

中值滤波忽略了较高阶灰度和较低阶灰度,直接取中值,由于有效得过滤椒盐噪声

Center

对高斯噪声的滤波

Center

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图像去除反光可以通过以下步骤实现: 1. 将彩色图像转换为灰度图像。 2. 对图像进行高斯模糊以减少图像噪声。 3. 提取图像的边缘,可以使用Canny算法进行边缘检测。 4. 检测边缘上的直线,可以使用霍夫变换或RANSAC算法进行直线检测。 5. 根据检测到的直线,确定反光区域。 6. 利用图像修复算法,如InPaint或图像修复算法,将反光区域填充为背景颜色,以实现反光去除。 下面是一个简单的Python示例代码: ```python import cv2 # 读取图像并转换为灰度图像 img = cv2.imread('image.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 对图像进行高斯模糊以减少噪声 blur = cv2.GaussianBlur(gray, (5, 5), 0) # 边缘检测 edges = cv2.Canny(blur, 50, 150, apertureSize=3) # 直线检测 lines = cv2.HoughLines(edges, 1, 3.14/180, 200) # 确定反光区域 for line in lines: rho, theta = line[0] if abs(theta - 3.14/2) < 0.1: a = np.cos(theta) b = np.sin(theta) x0 = a * rho y0 = b * rho x1 = int(x0 + 1000 * (-b)) y1 = int(y0 + 1000 * (a)) x2 = int(x0 - 1000 * (-b)) y2 = int(y0 - 1000 * (a)) cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2) # 图像修复 mask = np.zeros_like(edges) for line in lines: rho, theta = line[0] if abs(theta - 3.14/2) < 0.1: a = np.cos(theta) b = np.sin(theta) x0 = a * rho y0 = b * rho cv2.line(mask, (int(x0-10), 0), (int(x0+10), mask.shape[0]), 255, thickness=15) res = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA) # 显示图像 cv2.imshow('original', img) cv2.imshow('result', res) cv2.waitKey(0) ``` 注意,这只是一个基本的实现示例。实际情况下,您可能需要进行调整和优化以获得更好的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值