好的用户体验网站,黄国外网站,现在房地产的最新情况,做类似3d溜溜的网站前言
选修的是蔡mj老师的计算机视觉#xff0c;上课还是不错的#xff0c;但是OpenCV可能需要自己学才能完整把作业写出来。由于没有认真学#xff0c;这门课最后混了80多分#xff0c;所以下面作业解题过程均为自己写的#xff0c;并不是标准答案#xff0c;仅供参考
…前言
选修的是蔡mj老师的计算机视觉上课还是不错的但是OpenCV可能需要自己学才能完整把作业写出来。由于没有认真学这门课最后混了80多分所以下面作业解题过程均为自己写的并不是标准答案仅供参考
任务1
修改test.py的task_one()函数对task1.jpg进行去噪处理处理结果保存为task1_proc.jpg
提示请观察分析task1.jpg的噪声特点并选择合适的处理方法
def task_one():img cv2.imread(task1.jpg)#---------your code-----------------#median cv2.medianBlur(img, 3)#---------draw figures--------------#plt.imshow(cv2.cvtColor(median, cv2.COLOR_BGR2RGB)),plt.title(task1 output)plt.show()#---------save figures--------------#cv2.imwrite(task1_proc.jpg, median)效果如下
任务2
修改test.py的task_two()函数对task2.jpg进行去噪处理处理结果保存为task2_proc.jpg
提示请观察分析task2.jpg的噪声特点并选择合适的处理方法 def task_two():img cv2.imread(task2.jpg)#---------your code-----------------#blur cv2.bilateralFilter(img,5,50,50)#---------draw figures--------------##plt.imshow(cv2.cvtColor(blur, cv2.COLOR_BGR2RGB)),plt.title(task2 output)#plt.show()#---------save figures--------------#cv2.imwrite(task2_proc.jpg, blur)效果如下
任务3
修改test.py的task_three()函数对task3.jpg进行去噪处理处理结果保存为task3_proc.jpg
提示task3.jpg中的噪声为y轴方向的周期噪声周期为图像高度(height)的1/10 这个不会做弄了半天
def task_three():#img cv2.imread(task3.jpg,1)#---------your code-----------------## 读取图像img cv2.imread(task3.jpg)# 分离RGB通道b, g, r cv2.split(img)# 对每个通道进行傅里叶变换fb np.fft.fft2(b)fg np.fft.fft2(g)fr np.fft.fft2(r)# 将频域中的原点移动到图像中心fb_shift np.fft.fftshift(fb)fg_shift np.fft.fftshift(fg)fr_shift np.fft.fftshift(fr)# 获取频谱图像magnitude_spectrum_b 20 * np.log(np.abs(fb_shift))magnitude_spectrum_g 20 * np.log(np.abs(fg_shift))magnitude_spectrum_r 20 * np.log(np.abs(fr_shift))# 获取图像高度height, width img.shape[:2]# 计算周期噪声的频率成分dft_height np.ceil(height / 10)cy np.arange(dft_height, height, dft_height)cx np.arange(width)# 将周期噪声的频率成分设置为0for y in cy:fb_shift[int(y) - 1:int(y) 1, :] 0fg_shift[int(y) - 1:int(y) 1, :] 0fr_shift[int(y) - 1:int(y) 1, :] 0# 进行反傅里叶变换得到去噪后的图像ib np.fft.ifft2(np.fft.ifftshift(fb_shift))ig np.fft.ifft2(np.fft.ifftshift(fg_shift))ir np.fft.ifft2(np.fft.ifftshift(fr_shift))# 将每个通道的结果合并为一张去噪后的彩色图像denoised_img cv2.merge((ib.real, ig.real, ir.real))#---------draw figures--------------##plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB)),plt.title(task3 output)#plt.show()#---------save figures--------------#cv2.imwrite(task3_proc.jpg, denoised_img)效果和原图没啥区别。。。
源代码
# -*- coding: utf-8 -*-Created on Fri Mar 31 14:51:59 2023author: cai-mj
import numpy as np
import cv2
from matplotlib import pyplot as pltdef task_one():img cv2.imread(task1.jpg)#---------your code-----------------#median cv2.medianBlur(img, 3)#---------draw figures--------------#plt.imshow(cv2.cvtColor(median, cv2.COLOR_BGR2RGB)),plt.title(task1 output)plt.show()#---------save figures--------------#cv2.imwrite(task1_proc.jpg, median)def task_two():img cv2.imread(task2.jpg)#---------your code-----------------#blur cv2.bilateralFilter(img,5,50,50)#---------draw figures--------------##plt.imshow(cv2.cvtColor(blur, cv2.COLOR_BGR2RGB)),plt.title(task2 output)#plt.show()#---------save figures--------------#cv2.imwrite(task2_proc.jpg, blur)def task_three():#img cv2.imread(task3.jpg,1)#---------your code-----------------## 读取图像img cv2.imread(task3.jpg)# 分离RGB通道b, g, r cv2.split(img)# 对每个通道进行傅里叶变换fb np.fft.fft2(b)fg np.fft.fft2(g)fr np.fft.fft2(r)# 将频域中的原点移动到图像中心fb_shift np.fft.fftshift(fb)fg_shift np.fft.fftshift(fg)fr_shift np.fft.fftshift(fr)# 获取频谱图像magnitude_spectrum_b 20 * np.log(np.abs(fb_shift))magnitude_spectrum_g 20 * np.log(np.abs(fg_shift))magnitude_spectrum_r 20 * np.log(np.abs(fr_shift))# 获取图像高度height, width img.shape[:2]# 计算周期噪声的频率成分dft_height np.ceil(height / 10)cy np.arange(dft_height, height, dft_height)cx np.arange(width)# 将周期噪声的频率成分设置为0for y in cy:fb_shift[int(y) - 1:int(y) 1, :] 0fg_shift[int(y) - 1:int(y) 1, :] 0fr_shift[int(y) - 1:int(y) 1, :] 0# 进行反傅里叶变换得到去噪后的图像ib np.fft.ifft2(np.fft.ifftshift(fb_shift))ig np.fft.ifft2(np.fft.ifftshift(fg_shift))ir np.fft.ifft2(np.fft.ifftshift(fr_shift))# 将每个通道的结果合并为一张去噪后的彩色图像denoised_img cv2.merge((ib.real, ig.real, ir.real))#---------draw figures--------------##plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB)),plt.title(task3 output)#plt.show()#---------save figures--------------#cv2.imwrite(task3_proc.jpg, denoised_img)if __name__ __main__:task_one()task_two()task_three()