图像过滤和混合图像

2023-10-30

一、原理:

这项任务的目标是编写一个图像过滤功能,并使用它来创建混合图像,使用Oliva,Torralba和Schyns 的SIGGRAPH 2006 论文的简化版本。 混合图像是静态图像,其在解释中随观看距离而变化。基本思想是高频率在可用时倾向于支配感知,但是,在远处,只能看到信号的低频(平滑)部分。通过将一个图像的高频部分与另一个图像的低频部分混合,您可以获得混合图像,从而在不同距离处产生不同的解释。

二、目的:

对不同图像分别进行高通和低通滤波,融合图片。

三、步骤:

        图像过滤。图像过滤(或卷积)是一种基本的图像处理工具。您将编写自己的函数以从头开始实现图像过滤。更具体地说,您将实现 OpenCV库中my_imfilter()模仿该filter2D函数。如上所述student.py,您的过滤算法必须

1)支持灰度和彩色图像

2)支持任意形状的滤镜,只要两个尺寸都是奇数(例如7x9滤镜但不是4x5滤镜)

3)用零填充输入图像或反射图像内容和

4)返回与输入图像具有相同分辨率的滤波图像。

        混合图像。混合图像是一个图像的低通滤波版本和第二图像的高通滤波版本的总和。有一个自由参数,其可被调谐为每个图像对,其控制多少高频到从所述第一图像和多少低频到所述第二图像中离开除去。这被称为截止频率。在论文中,建议使用两个截止频率(每个图像调整一个),你也可以自由尝试。在起始码中,通过改变用于构造混合图像的Gausian滤波器的标准偏差来控制截止频率。您将create_hybrid_image()根据入门代码实现student.py。你的功能会打电话my_imfilter() 创建低频和高频图像,然后将它们组合成混合图像。

四、环境配置:     

        Windows:

        CPU: Intel® Core™ i7-6700HQ CPU @ 2.60GHz

        Cache: L1:256KB
        L2:1.0MB,

        L3:6.0MB

        开发环境:python 3.10

五、具体操作:

        1. 利用滤波函数操作图像

                my_imfilter()实现

1.	def my_imfilter1(image, filter):  
2.	    """ 
3.	    Your function should meet the requirements laid out on the project webpage. 
4.	    Apply a filter to an image. Return the filtered image. 
5.	    Inputs: 
6.	    - image -> numpy nd-array of dim (m, n, c) 
7.	    - filter -> numpy nd-array of odd dim (k, l) 
8.	    Returns 
9.	    - filtered_image -> numpy nd-array of dim (m, n, c) 
10.	    Errors if: 
11.	    - filter has any even dimension -> raise an Exception with a suitable error message. 
12.	    """  
13.	    filter_reshape = filter.reshape(-1,1)  
14.	    filter_size = filter.size  
15.	    filter_size_sqrt = round(math.sqrt(filter_size))  
16.	    filter_size_floor = math.floor(filter_size_sqrt/2)  
17.	    filtered_image = np.zeros(image.shape)   
18.	    image_padding = np.zeros([image.shape[0] + 2 * filter_size_floor,
19.	         image.shape[1] + 2 * filter_size_floor, image.shape[2]])   
20.	    image_padding[filter_size_floor:image_padding.shape[0]-filter_size_floor,
21.	         filter_size_floor:image_padding.shape[1]-filter_size_floor] = image  
22.	    for i in range(image.shape[0]):  
23.	        for j in range(image.shape[1]):  
24.	            convolute_image = image_padding[i:i+filter_size_sqrt, j:j+filter_size_sqrt]  
25.	            reshape_image = np.reshape(convolute_image[0:filter_size_sqrt,
26.	                 0:filter_size_sqrt], (filter_size,3))  
27.	            filtered_image[i,j] = sum(np.multiply(reshape_image, filter_reshape))  
28.	    return filtered_image  

        2. 生成高低通分量滤除图片,图像融合

                gen_hybrid_image()实现

1.	def gen_hybrid_image(image1, image2, cutoff_frequency):  
2.	    """ 
3.	     Inputs: 
4.	     - image1 -> The image from which to take the low frequencies. 
5.	     - image2 -> The image from which to take the high frequencies. 
6.	     - cutoff_frequency -> The standard deviation, in pixels, of the Gaussian 
7.	                           blur that will remove high frequencies. 
8.	 
9.	     Task: 
10.	     - Use my_imfilter to create 'low_frequencies' and 'high_frequencies'. 
11.	     - Combine them to create 'hybrid_image'. 
12.	    """  
13.	  
14.	    assert image1.shape[0] == image2.shape[0]  
15.	    assert image1.shape[1] == image2.shape[1]  
16.	    assert image1.shape[2] == image2.shape[2]  
17.	  
18.	    # Steps: (1) Remove the high frequencies from image1 by blurring it.
19.	    #  The amount of blur that works best will vary  
20.	    #  with different image pairs generate a 1x(2k+1) gaussian kernel with mean=0 and sigma = s,  
21.	    # see https://stackoverflow.com/questions/17190649/how-to-obtain-a-gaussian-filter-in-python  
22.	    s, k = cutoff_frequency, cutoff_frequency * 2  
23.	    probs = np.asarray([exp(-z * z / (2 * s * s)) / sqrt(2 * pi * s * s) for z in range(-k, k + 1)], dtype=np.float32)  
24.	    kernel = np.outer(probs, probs)  
25.	    large_1d_blur_filter = kernel.reshape(-1,1)  
26.	      
27.	    # Your code here:  
28.	    large_blur_image1 = my_imfilter1(image1, large_1d_blur_filter)  
29.	    low_frequencies = large_blur_image1  
30.	  
31.	    # (2) Remove the low frequencies from image2. The easiest way to do this is to  
32.	    #     subtract a blurred version of image2 from the original version of image2.  
33.	    #     This will give you an image centered at zero with negative values.  
34.	    # Your code here #  
35.	    large_blur_image2 = my_imfilter1(image2, large_1d_blur_filter)  
36.	    # large_blur_image2 = my_imfilter(large_blur_image2, large_1d_blur_filter.T)  
37.	    high_frequencies = image2 - large_blur_image2  # Replace with your implementation  
38.	  
39.	    # (3) Combine the high frequencies and low frequencies  
40.	    # Your code here #  
41.	    hybrid_image = low_frequencies + high_frequencies  # Replace with your implementation  
42.	  
43.	    # (4) At this point, you need to be aware that values larger than 1.0  
44.	    # or less than 0.0 may cause issues in the functions in Python for saving  
45.	    # images to disk. These are called in proj1_part2 after the call to  
46.	    # gen_hybrid_image().  
47.	    # One option is to clip (also called clamp) all values below 0.0 to 0.0,  
48.	    # and all values larger than 1.0 to 1.0.  
49.	    for i in range(hybrid_image.shape[0]):  
50.	        for j in range(hybrid_image.shape[1]):  
51.	            for k in range(hybrid_image.shape[2]):  
52.	                if hybrid_image[i,j,k] > 1.0:  
53.	                    hybrid_image[i,j,k] = 1.0  
54.	                if hybrid_image[i,j,k] < 0.0:  
55.	                    hybrid_image[i,j,k] = 0.0  
56.	    return low_frequencies, high_frequencies, hybrid_image

        3. 主函数

                防止存储失败做了一些判断

1.	# Project Image Filtering and Hybrid Images - Generate Hybrid Image  
2.	# Based on previous and current work  
3.	# by James Hays for CSCI 1430 @ Brown and  
4.	# CS 4495/6476 @ Georgia Tech  
5.	import numpy as np  
6.	import matplotlib  
7.	import matplotlib.pyplot as plt  
8.	from helpers import vis_hybrid_image, load_image, save_image, my_imfilter, gen_hybrid_image, gen_hybrid_image_cv  
9.	  
10.	# Before trying to construct hybrid images, it is suggested that you  
11.	# implement my_imfilter in helpers.py and then debug it using proj1_part1.py  
12.	  
13.	# Debugging tip: You can split your python code and print in between  
14.	# to check if the current states of variables are expected.  
15.	  
16.	## Setup  
17.	# Read images and convert to floating point format  
18.	image1 = load_image('../data/bird.bmp')  
19.	image2 = load_image('../data/plane.bmp')  
20.	  
21.	# display the dog and cat images  
22.	# plt.figure(figsize=(3,3)); plt.imshow((image1*255).astype(np.uint8));#  
23.	# plt.figure(figsize=(3,3)); plt.imshow((image2*255).astype(np.uint8));  
24.	  
25.	# For your write up, there are several additional test cases in 'data'.  
26.	# Feel free to make your own, too (you'll need to align the images in a  
27.	# photo editor such as Photoshop).  
28.	# The hybrid images will differ depending on which image you  
29.	# assign as image1 (which will provide the low frequencies) and which image  
30.	# you asign as image2 (which will provide the high frequencies)  
31.	  
32.	## Hybrid Image Construction ##  
33.	# cutoff_frequency is the standard deviation, in pixels, of the Gaussian#  
34.	# blur that will remove high frequencies. You may tune this per image pair  
35.	# to achieve better results.  
36.	cutoff_frequency = 7  
37.	low_frequencies, high_frequencies, hybrid_image = gen_hybrid_image_cv(image1, image2, cutoff_frequency)  
38.	  
39.	## Visualize and save outputs ##  
40.	plt.figure(); plt.imshow((low_frequencies*255).astype(np.uint8));  
41.	plt.figure(); plt.imshow(((high_frequencies+0.5)*255).astype(np.uint8));  
42.	vis = vis_hybrid_image(hybrid_image)  
43.	plt.figure(figsize=(20, 20)); plt.imshow(vis);  
44.	high_frequencies_plus = high_frequencies+0.5  
45.	  
46.	for i in range(high_frequencies_plus.shape[0]):  
47.	        for j in range(high_frequencies_plus.shape[1]):  
48.	            for k in range(high_frequencies_plus.shape[2]):  
49.	                if high_frequencies_plus[i,j,k] > 1.0:  
50.	                    high_frequencies_plus[i,j,k] = 1.0  
51.	                if high_frequencies_plus[i,j,k] < 0.0:  
52.	                    high_frequencies_plus[i,j,k] = 0.0  
53.	  
54.	save_image('../results/low_frequencies.jpg', low_frequencies)  
55.	save_image('../results/high_frequencies.jpg', high_frequencies_plus)  
56.	save_image('../results/hybrid_image.jpg', hybrid_image)  
57.	save_image('../results/hybrid_image_scales.jpg', vis)

六、实验数据及结果分析

        两个原始图像如下所示:

1. 自己实现的滤波函数my_imfilter()

这些图像的低通(模糊)和高通版本如下所示:

高频图像实际上是零均值,负值,因此通过添加0.5可视化。在得到的可视化中,亮值为正,暗值为负。可视化效果的有用方法是逐步对混合图像进行下采样,如下所示:

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

图像过滤和混合图像 的相关文章

随机推荐

  • UE4触摸屏旋转、双指缩放

    UE4触摸屏旋转 双指缩放 前言 最近接触触摸屏项目案列 自己研究了一下旋转和双指缩放的实现方法 记录一下
  • sql使用DATE_FORMAT格式化日期

    一 样例 sql使用format格式化查询字段 例如 select DATE FORMAT operatetime y m d H i s operatetime from tbrecord where id 1 结果 2020 06 08
  • RNA-seq——快速下载SRA数据、解决fq文件中测序质量全为 ‘?‘ 的问题

    写在前面 在学习RNA seq时 需要从网上下载公开数据集来上手分析 大部分教程都很古老 其中在ncbi中ftp的下载链接已经不存在了 甚至可以直接下载fastq文件 但是 直接下载的fastq文件做fastqc之后结果为一条直线 因为文件
  • [Docker] Docker 容器配置环境变量重启后失效问题解决

    Docker 容器配置的环境变量重启时失效问题解决 配置环境 在 etc profile 配置环境变量 JAVA HOME opt java jdk 15 0 2 export PATH JAVA HOME bin PATH 然后在 etc
  • 智能家居网络系统的设计

    今天开始学习制作智能家居网络系统 这个项目相对简单的多 但对于新手还是有难度的 所以今天我写出来 尽我最大努力写详细一点 单本人能力实在是有限 文章中肯定会出现很多错误 望大家指出来 一定改正 讲解过程中所涉及全部代码下载地址 智能家居网络
  • vue3中 :visible.sync 和slot报错

    1 visible sync visible指的是属性绑定 表示弹框的显示隐藏 当 visible的值为ture的时候 弹框显示 当为false的时候 弹框隐藏 visible sync指的就是同步动态双向的来表示visible的值 当我们
  • java的线程池

    一 线程池概念 若不使用线程池 在新创建的线程start 后执行完run 就自动销毁了 造成了资源的浪费 使用线程池可以暂时存储线程 线程池的主要核心原理 线程池的代码实现 注意 在实际开发中线程池并不会关闭 例如服务器要24小时接收用户的
  • 四川泡菜中的亚硝酸盐如何变化

    文章转自 http blog 163 com zhoux1025 126 blog static 28025045201241142411713 版权归原作者 自己喜欢吃泡菜 却总是被导致癌变的亚硝酸盐困扰 今天查了一些资料 仅供喜欢吃泡菜
  • 已知长度为n的线性表A采用顺序存储结构,请写一个时间复杂度为O(n)、空间复杂度为O(1)的算法,该算法可删除线性表中所有值为item的数据元素。

    include
  • TCP的连接和释放连接(三次握手和四次挥手的过程)

    参考文章 javascript 看图理解TCP的三次握手和四次挥手 个人文章 SegmentFault 思否 TCP 三次握手 和 四次挥手 通俗易懂 大黄的Java笔记的博客 CSDN博客 tcp三次握手最大时间 两张动图 彻底明白TCP
  • Unity3d 打飞碟 物理运动版

    Unity3d 打飞碟 物理运动版 github 传送 视频演示 题目要求 改进飞碟 Hit UFO 游戏 按 adapter模式 设计图修改飞碟游戏 使它同时支持物理运动与运动学 变换 运动 Adapter 模式 运动学和动力学的物体运动
  • sql基础1

    cmd mysql uroot p password mysql navicat Ctrl s 保存建的新表 表的基本操作 show databases 查看所有数据库 use kkb 进入kkb数据库 show tables 查看所有表
  • vc++ 如何使radio button ,checkbox初始为已选状态?

    CheckDlgButton IDC CHECK1 BST UNCHECKED 0 CheckDlgButton IDC CHECK2 BST CHECKED 1 CButton GetDlgItem IDC RADIO 1 gt SetC
  • 支付宝支付 第十集:支付回调

    支付宝支付 第十集 支付回调 一 注意 这里的支付回调最好是自己有一个服务器 阿里云服务器一年70多也不是很贵 博主自己尝试了一下 本机和使用虚拟机模拟服务器的话 支付宝的授权回调信息是传不过来的 无法进行支付回调 怎样使用阿里云服务器运行
  • 机器学习(二)--- 分类算法详解

    感觉狼厂有些把机器学习和数据挖掘神话了 机器学习 数据挖掘的能力其实是有边界的 机器学习 数据挖掘永远是给大公司的业务锦上添花的东西 它可以帮助公司赚更多的钱 可是相比JAVA和C 语言开发来说 机器学习 数据挖掘确实是新一些老人占的坑少一
  • MySQL面试之一条SQL是如何执行的

    一 MySQL 基础架构分析 1 1 MySQL 基本架构概览 下图是 MySQL 的一个简要架构图 从下图你可以很清晰的看到用户的 SQL 语句在 MySQL 内部是如何执行的 先简单介绍一下下图涉及的一些组件的基本作用帮助大家理解这幅图
  • 打乱 数字_打乱数组

    JavaScript实现LeetCode第384题 打乱数组 题目描述 打乱一个没有重复元素的数组 示例 以数字集合 1 2 和 3 初始化数组 int nums 1 2 3 Solution solution new Solution n
  • 前端 CSS 变量简介及基本使用方法

    背景 复杂的网站都会有大量的CSS代码 通常也会有许多重复的值 举个例子 同样一个颜色值可能在成千上百个地方被使用到 如果这个值发生了变化 需要全局搜索并且一个一个替换 效率不高且容易出错 自定义属性在某个地方存储一个值 然后在其他许多地方
  • [非线性控制理论]5_自适应控制器(Adaptive controller)

    非线性控制理论 1 Lyapunov直接方法 非线性控制理论 2 不变性原理 非线性控制理论 3 基础反馈稳定控制器设计 非线性控制理论 4 反馈线性化 反步法 非线性控制理论 5 自适应控制器 Adaptive controller 非线
  • 图像过滤和混合图像

    一 原理 这项任务的目标是编写一个图像过滤功能 并使用它来创建混合图像 使用Oliva Torralba和Schyns 的SIGGRAPH 2006 论文的简化版本 混合图像是静态图像 其在解释中随观看距离而变化 基本思想是高频率在可用时倾