数字图像处理基础与应用 第四章

3-1
(1) 感觉就是图像模糊了,并没有去噪

from cv2 import cv2
import numpy as np
import randomdef spNoise(img,prob):# 添加椒盐噪声,prob:噪声比例 output = np.zeros(img.shape,np.uint8)thres = 1 - prob for i in range(img.shape[0]):for j in range(img.shape[1]):rdn = random.random()if rdn < prob:output[i][j] = 0elif rdn > thres:output[i][j] = 255else:output[i][j] = img[i][j]return outputdef gaussNoise(img, mean=0, var=0.0001):# 添加,高斯噪声mean : 均值,var : 方差img = np.array(img/255, dtype=float)noise = np.random.normal(mean, var ** 0.5, img.shape)out = img + noiseif out.min() < 0:low_clip = -1.else:low_clip = 0.out = np.clip(out, low_clip, 1.0)out = np.uint8(out*255)return outdef meanFilter(img, c):# 均值滤波,2c+1*2c+1矩阵取平均值img_shape = np.shape(img)out = np.zeros(img_shape)for i in range(img_shape[0]):for j in range(img_shape[1]):if i >= c and i < img_shape[0] - c and j >= c and j < img_shape[1] - c:out[i][j] = min(sum(img[i - c:i + c + 1, j - c:j + c + 1].flatten()) // ((2 * c + 1) * (2 * c + 1)), img[i][j])else:out[i][j] = img[i][j]return outdef medianFilter(img, c):# 中值滤波,2c+1*2c+1矩阵再取中值img_shape = np.shape(img)out = np.zeros(img_shape)for i in range(img_shape[0]):for j in range(img_shape[1]):if i >= c and i < img_shape[0] - c and j >= c and j < img_shape[1] - c:out[i][j] = medianValueOdd(img[i - c:i + c + 1, j - c:j + c + 1].flatten())else:out[i][j] = img[i][j]return outdef medianValueOdd(arr):# 希尔排序length = len(arr)gap = length//2while gap > 0:for i in range(gap, length):tem = arr[i]j = i while  j >= gap and arr[j-gap] >tem: arr[i], arr[i - gap] = arr[i - gap], arr[i]j -= gap gap = gap // 2return arr[length//2]img = cv2.imread("C:\\test\\1.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)img_noise = spNoise(gray, 0.0005)
img_noise = gaussNoise(img_noise)img_mean_filter1 = meanFilter(img_noise, 1)
img_mean_filter2 = meanFilter(img_noise, 2)
img_mean_filter3 = meanFilter(img_noise, 3)
img_median_filter1 = medianFilter(img_noise, 1)
img_median_filter2 = medianFilter(img_noise, 2)
img_median_filter3 = medianFilter(img_noise, 3)cv2.imwrite("C:\\test\\img_mean_filter1.jpg", img_mean_filter1)
cv2.imwrite("C:\\test\\img_mean_filter2.jpg", img_mean_filter2)
cv2.imwrite("C:\\test\\img_mean_filter3.jpg", img_mean_filter3)
cv2.imwrite("C:\\test\\img_median_filter1.jpg", img_median_filter1)
cv2.imwrite("C:\\test\\img_median_filter2.jpg", img_median_filter2)
cv2.imwrite("C:\\test\\img_median_filter3.jpg", img_median_filter3)img_mean_filter1 = cv2.imread("C:\\test\\img_mean_filter1.jpg")
img_mean_filter2 = cv2.imread("C:\\test\\img_mean_filter2.jpg")
img_mean_filter3 = cv2.imread("C:\\test\\img_mean_filter3.jpg")
img_median_filter1 = cv2.imread("C:\\test\\img_median_filter1.jpg")
img_median_filter2 = cv2.imread("C:\\test\\img_median_filter2.jpg")
img_median_filter3 = cv2.imread("C:\\test\\img_median_filter3.jpg")cv2.imshow('gray', gray)
cv2.imshow('img_noise', img_noise)
cv2.imshow('img_mean_filter1', img_mean_filter1)
cv2.imshow('img_mean_filter2', img_mean_filter2)
cv2.imshow('img_mean_filter3', img_mean_filter3)
cv2.imshow('img_median_filter1', img_median_filter1)
cv2.imshow('img_median_filter2', img_median_filter2)
cv2.imshow('img_median_filter3', img_median_filter3)
cv2.waitKey()
cv2.destroyAllWindows

4-1
(2)使用K邻近滤波和对称邻近滤波,发现K邻近滤波时,若仅取K=3,会产生很多早点,取K=5,K=7时,能有效去噪

from cv2 import cv2
import numpy as np
import randomdef spNoise(img,prob):# 添加椒盐噪声,prob:噪声比例 output = np.zeros(img.shape,np.uint8)thres = 1 - prob for i in range(img.shape[0]):for j in range(img.shape[1]):rdn = random.random()if rdn < prob:output[i][j] = 0elif rdn > thres:output[i][j] = 255else:output[i][j] = img[i][j]return outputdef gaussNoise(img, mean=0, var=0.0001):# 添加,高斯噪声mean : 均值,var : 方差img = np.array(img/255, dtype=float)noise = np.random.normal(mean, var ** 0.5, img.shape)out = img + noiseif out.min() < 0:low_clip = -1.else:low_clip = 0.out = np.clip(out, low_clip, 1.0)out = np.uint8(out*255)return outdef shellSort(arr):# 希尔排序length = len(arr)gap = length//2while gap > 0:for i in range(gap, length):tem = arr[i]j = i while  j >= gap and arr[j-gap] >tem: arr[i], arr[i - gap] = arr[i - gap], arr[i]j -= gap gap = gap // 2return arrdef kNeighborFilter(img, N):square = N * NK = 2 * N - 1q = N // 2img_shape = np.shape(img)out = np.zeros(img_shape)for i in range(img_shape[0]):for j in range(img_shape[1]):if i >= q and i < img_shape[0] - q and j >= q and j < img_shape[1] - q:img_flatten = img[i - q:i + q + 1, j - q:j + q + 1].flatten()arr = np.append(img_flatten[0:square // 2], (img_flatten[square // 2 + 1:]))arr = shellSort(arr)p = 0while p < len(arr)-1:if arr[p] > img[i][j]:breakif arr[p] <= img[i][j] and arr[p + 1] >= img[i][j]:breakp += 1if p < K // 2:out[i][j] = sum(arr[0:K]) // Kelif p > square - K // 2:out[i][j] = sum(arr[square - K:]) // Kelse:out[i][j] = sum(arr[p - K // 2:p + K // 2 + 1]) // Kelse:out[i][j] = img[i][j]return outdef symmetricNeighborFilter(img, N):p = N // 2img_shape = np.shape(img)out = np.zeros(img_shape)for i in range(img_shape[0]):for j in range(img_shape[1]):if i >= p and i < img_shape[0] - p and j >= p and j < img_shape[1] - p:tem = []for k in range(i - p, i + p + 1):for l in range(j - p, j):tem.append([img[k][l], img[2 * i - k][2 * j - l]])for k in range(i - p, i):tem.append([img[k, j], img[2 * i - k][j]])total = 0print(tem)for m in range(len(tem)):if abs(tem[m][0] - img[i][j]) < abs(tem[m][1] - img[i][j]):total += tem[m][0]else:total += tem[m][1]print(total)out[i][j] = total // len(tem)print(out[i][j])else:out[i][j] = img[i][j]return outimg = cv2.imread("C:\\test\\1.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)img_noise = spNoise(gray, 0.0005)
img_noise = gaussNoise(img_noise)k_neighbor_filter1 = kNeighborFilter(img_noise, 3)
k_neighbor_filter2 = kNeighborFilter(img_noise, 5)
k_neighbor_filter3 = kNeighborFilter(img_noise, 7)
symmetric_neighbor_filter1 = symmetricNeighborFilter(img_noise, 3)
symmetric_neighbor_filter2 = symmetricNeighborFilter(img_noise, 5)
symmetric_neighbor_filter3 = symmetricNeighborFilter(img_noise, 7)cv2.imwrite("C:\\test\\kNeighborFilter3.jpg", k_neighbor_filter1)
cv2.imwrite("C:\\test\\kNeighborFilter5.jpg", k_neighbor_filter2)
cv2.imwrite("C:\\test\\kNeighborFilter7.jpg", k_neighbor_filter3)
cv2.imwrite("C:\\test\\symmetricNeighborFilter3.jpg", symmetric_neighbor_filter1)
cv2.imwrite("C:\\test\\symmetricNeighborFilter5.jpg", symmetric_neighbor_filter2)
cv2.imwrite("C:\\test\\symmetricNeighborFilter7.jpg", symmetric_neighbor_filter3)k_neighbor_filter1 = cv2.imread("C:\\test\\kNeighborFilter3.jpg")
k_neighbor_filter2 = cv2.imread("C:\\test\\kNeighborFilter5.jpg")
k_neighbor_filter3 = cv2.imread("C:\\test\\kNeighborFilter7.jpg")
symmetric_neighbor_filter1 = cv2.imread("C:\\test\\symmetricNeighborFilter3.jpg")
symmetric_neighbor_filter2 = cv2.imread("C:\\test\\symmetricNeighborFilter5.jpg")
symmetric_neighbor_filter3 = cv2.imread("C:\\test\\symmetricNeighborFilter7.jpg")cv2.imshow('gray', gray)
cv2.imshow('img_noise', img_noise)
cv2.imshow('kNeighborFilter3', k_neighbor_filter1)
cv2.imshow('kNeighborFilter5', k_neighbor_filter2)
cv2.imshow('kNeighborFilter7', k_neighbor_filter3)
cv2.imshow('kNeighborFilter3', symmetric_neighbor_filter1)
cv2.imshow('kNeighborFilter5', symmetric_neighbor_filter2)
cv2.imshow('kNeighborFilter7', symmetric_neighbor_filter3)
cv2.waitKey()
cv2.destroyAllWindows

4-1(3)看起来开运算去掉了白点,闭运算去掉了黑点

from cv2 import cv2
import numpy as np
import randomdef spNoise(img,prob):# 添加椒盐噪声,prob:噪声比例 output = np.zeros(img.shape,np.uint8)thres = 1 - prob for i in range(img.shape[0]):for j in range(img.shape[1]):rdn = random.random()if rdn < prob:output[i][j] = 0elif rdn > thres:output[i][j] = 255else:output[i][j] = img[i][j]return outputdef gaussNoise(img, mean=0, var=0.0001):# 添加,高斯噪声mean : 均值,var : 方差img = np.array(img/255, dtype=float)noise = np.random.normal(mean, var ** 0.5, img.shape)out = img + noiseif out.min() < 0:low_clip = -1.else:low_clip = 0.out = np.clip(out, low_clip, 1.0)out = np.uint8(out*255)return outdef grayErosion(img, N):arr = np.ones((N, N))p = N // 2img_shape = np.shape(img)out = np.zeros(img_shape)for i in range(img_shape[0]):for j in range(img_shape[1]):if i >= p and i < img_shape[0] - p and j >= p and j < img_shape[1] - p:out[i][j] = max(0, min(img[i - p:i + p + 1, j - p:j + p + 1].flatten()-arr.flatten()))else:out[i][j] = img[i][j]return outdef grayScale(img, N):arr = np.ones((N, N))p = N // 2img_shape = np.shape(img)out = np.zeros(img_shape)for i in range(img_shape[0]):for j in range(img_shape[1]):if i >= p and i < img_shape[0] - p and j >= p and j < img_shape[1] - p:out[i][j] = min(255, max(img[i - p:i + p + 1, j - p:j + p + 1].flatten()-arr.flatten()))else:out[i][j] = img[i][j]   return outdef openFilter(img, arr):return grayScale(grayErosion(img, arr), arr)def closeFilter(img, arr):return grayErosion(grayScale(img, arr), arr)img = cv2.imread("C:\\test\\1.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)img_noise = spNoise(gray, 0.0005)
img_noise = gaussNoise(img_noise)image_open_filter = openFilter(img_noise, 3)
image_close_filter = closeFilter(img_noise, 3)cv2.imwrite("C:\\test\\image_open_filter.jpg", image_open_filter)
cv2.imwrite("C:\\test\\image_close_filter.jpg", image_close_filter)image_open_filter = cv2.imread("C:\\test\\image_open_filter.jpg")
image_close_filter = cv2.imread("C:\\test\\image_close_filter.jpg")cv2.imshow('gray', gray)
cv2.imshow('img_noise', img_noise)
cv2.imshow('image_open_filter', image_open_filter)
cv2.imshow('image_close_filter', image_close_filter)
cv2.waitKey()
cv2.destroyAllWindows

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/550506.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数字图像处理基础与应用 第五章

5-1感觉这些方法主体都差不多&#xff0c;就是微分算子不同&#xff0c;懒得一个个写了 from cv2 import cv2 import numpy as np import randomdef singleDirectionsharpen(img, N3):p N // 2img_shape np.shape(img)out np.zeros(img_shape)for i in range(img_shape[0])…

新版scipy中的imread,imsave,imresize被弃用解决方法

阅读文献代码时发现新版scipy中的imread,imsave,imresize被弃用报错 搜索了一下发现可以用imageio中的imread和imsave代替原有的&#xff0c;用numpy的reshape来代替imresize 试了一下&#xff0c;不太行&#xff0c;文献中imread有mode‘L’&#xff0c;即读取灰度图&#xff…

anaconda中tensorflow-estimator版本应与tensorflow-gpu版本相同

把tensorflow升级到2.1.0版本是发现import tensorflow as tf出错 发现是anaconda安装的tensorflow-estimator版本是2.2.0&#xff0c;将版本回退到2.1.0后解决了问题

tf.contrib在tf2中无法使用

在尝试文献中代码时发现tf.comtrib无法使用 官方文档中说 It is still possible to run 1.X code, unmodified (except for contrib), in TensorFlow 2.0: import tensorflow.compat.v1 as tf tf.disable_v2_behavior()除了contrib应该都用能两行代码解决问题,contrib则用kera…

发现了imageio文档中有代替scipy.misc的说明

原文&#xff1a;https://imageio.readthedocs.io/en/latest/scipy.html?highlightimread imageio.imread可以代替scipy.misc.imread 用pilmode代替mode 用as_gray代替flatten pilmode类型&#xff1a; ‘L’ (8-bit pixels, grayscale) ‘P’ (8-bit pixels, mapped to an…

fastai学习笔记——安装

虽然说是推荐linux&#xff0c;windows可能有bug&#xff0c;但是我还是没办法只用linux win10anaconda python3.7 安装很简单 conda install -c fastchan fastai anaconda 好了也没发现有啥问题 测试torch是否可用 import torch cuda.test.is_available()True

fastai学习——第一个bug

跟着视频学习&#xff0c;在运行第一段测试代码的时候出现问题 from fastai.vision.all import * path untar_data(URLs.PETS)/imagesdef is_cat(x): return x[0].isupper() dls ImageDataLoaders.from_name_func(path, get_image_files(path), valid_pct0.2, seed42,label_…

fastai学习:01_intro Questionnaire

fastAI Questionnaire 感觉还挺多的&#xff0c;怪不得说每一课要额外8小时进行学习。 1.Do you need these for deep learning? Lots of math T / F Lots of data T / F Lots of expensive computers T / F A PhD T / F F F F F 2.Name five areas where deep learning is …

fastai学习——第二个问题

第二节课需要使用bing image search api获取bing图片搜索中的熊图片&#xff0c;此时发现获取api需要注册azure&#xff0c;卡在绑定卡上很久&#xff0c;想了想还要去弄一张带visa的卡&#xff0c;还是算了&#xff0c;就用猫狗大战数据集实验吧&#xff0c;按照与学习视频中类…

fastai学习:02_production Questionnaire

1.Where do text models currently have a major deficiency? Deep learning is currently not good at generating correct responses! We don’t currently have a reliable way to, for instance, combine a knowledge base of medical information with a deep learning m…

fastai学习:04_mnist_basics Questionnaire

1.How is a grayscale image represented on a computer? How about a color image? 灰度图&#xff1a;单通道&#xff0c;0-256 彩色图&#xff1a;三通道RGB或HSV&#xff0c;0-256 2.How are the files and folders in the MNIST_SAMPLE dataset structured? Why? 分为…

fastai学习:05_pet_breeds Questionnaire

1.Why do we first resize to a large size on the CPU, and then to a smaller size on the GPU? 首先&#xff0c;在训练模型时&#xff0c;我们希望能够将图片的尺寸统一&#xff0c;整理为张量&#xff0c;传入GPU&#xff0c;我们还希望最大限度地减少执行不同增强计算的…

fastai学习:06_multicat Questionnarie

1.How could multi-label classification improve the usability of the bear classifier? 可以对不存在的熊进行分类 2.How do we encode the dependent variable in a multi-label classification problem? One-hot encoding: Using a vector of zeros, with a one in each…

【论文阅读笔记】Detecting Camouflaged Object in Frequency Domain

1.论文介绍 Detecting Camouflaged Object in Frequency Domain 基于频域的视频目标检测 2022年发表于CVPR [Paper] [Code] 2.摘要 隐藏目标检测&#xff08;COD&#xff09;旨在识别完美嵌入其环境中的目标&#xff0c;在医学&#xff0c;艺术和农业等领域有各种下游应用。…

ubuntu中使用firefox浏览器播放bilibili的h5网页视频

安装好系统后&#xff0c;直接firefox打开bilibili显示没有flash插件 找了一圈没有发现自动播放h5的选项 搜索了一下发现可能是需要解码器 sudo apt-get install ubuntu-restricted-extras就能看了

ubuntu挂起唤醒后十几秒钟就自动熄屏一次

昨天晚上笔记本没关机&#xff0c;ubuntu挂起一晚上&#xff0c;今天早上打开电脑&#xff0c;发现每过十几秒钟就自动熄屏一次&#xff0c;重启之后好了&#xff0c;不知道什么原因 搜索了一下说可能是DPMS的问题&#xff0c;用xset -dpms可以关闭电源管理选项 但是本来的设置…

python3 上传文件到目标机器_Python3 +服务器搭建私人云盘,再也不怕限速了

先来看看效果电脑访问手机访问Windows版本搭建(1).首先你需要在你的电脑上或者服务器上安装Python3.X。(2).然后通过如下指令来安装updog库&#xff0c;网上有很多关于updog的介绍&#xff0c;我这里就不详细说pip3 install updog(3).静静的等他安装完成&#xff0c;然后执行以…

Ubuntu下绘图软件krita64位无中文问题

ubuntu20 sudo apt install krita-l10n 就有了 参考&#xff1a;https://bbs.deepin.org/post/181669

tableau度量值计算_Tableau图表界面组成介绍

声明&#xff1a;内容来源拉勾教育数据分析训练营课程视频1 Tableau工作表基本界面基础概念&#xff1a;维度、度量、聚合、粒度。维度: 维度包含定量值(例如名称、日期或地理数据)&#xff0c;可以使用维度进行分类、分段以及揭示数据中的详细信息。维度影响视图中的详细级别。…

小强升职记思维导图_你学会用 “思维导图” 学英语了吗?

今天我们来讲讲目前比较火爆的“思维导图学习法”。思维导图又叫“MIND MAP”&#xff0c;是英国人托尼博赞发明的一种思维工具。托尼博赞本人在心理学、语言学、数学以及科学方向均获得过学位&#xff0c;而且他还创造了世界脑力奥林匹克运动。虽然大师已逝&#xff0c;但是这…