图像过滤和混合图像
实验原理:
这项任务的目标是编写一个图像过滤功能,并使用它来创建混合图像,使用Oliva,Torralba和Schyns 的SIGGRAPH 2006 论文的简化版本。 混合图像是静态图像,其在解释中随观看距离而变化。基本思想是高频率在可用时倾向于支配感知,但是,在远处,只能看到信号的低频(平滑)部分。通过将一个图像的高频部分与另一个图像的低频部分混合,您可以获得混合图像,从而在不同距离处产生不同的解释。
实验目的:
对不同图像分别进行高通和低通滤波,融合图片
实验内容:
图像过滤:图像过滤(或卷积)是一种基本的图像处理工具。您将编写自己的函数以从头开始实现图像过滤。更具体地说,您将实现 在OpenCV库中my_imfilter()模仿该filter2D函数。如上所述student.py,过滤算法必须
- 支持灰度和彩色图像
- 支持任意形状的滤镜,只要两个尺寸都是奇数(例如7x9滤镜但不是4x5滤镜)
- 用零填充输入图像或反射图像内容和
- 返回与输入图像具有相同分辨率的滤波图像。
混合图像:混合图像是一个图像的低通滤波版本和第二图像的高通滤波版本的总和。有一个自由参数,其可被调谐为每个图像对,其控制多少高频到从所述第一图像和多少低频到所述第二图像中离开除去。这被称为“截止频率”。在论文中,建议使用两个截止频率(每个图像调整一个),你也可以自由尝试。在起始码中,通过改变用于构造混合图像的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.6
实验步骤及操作:
利用滤波函数操作图像
my_imfilter()实现
def my_imfilter1(image, filter):
"""
Your function should meet the requirements laid out on the project webpage.
Apply a filter to an image. Return the filtered image.
Inputs:
- image -> numpy nd-array of dim (m, n, c)
- filter -> numpy nd-array of odd dim (k, l)
Returns
- filtered_image -> numpy nd-array of dim (m, n, c)
Errors if:
- filter has any even dimension -> raise an Exception with a suitable error message.
"""
filter_reshape = filter.reshape(-1,1)
filter_size = filter.size
filter_size_sqrt = round(math.sqrt(filter_size))
filter_size_floor = math.floor(filter_size_sqrt/2)
filtered_image = np.zeros(image.shape)
image_padding = np.zeros([image.shape[0] + 2 * filter_size_floor,
image.shape[1] + 2 * filter_size_floor, image.shape[2]])
image_padding[filter_size_floor:image_padding.shape[0]-filter_size_floor,
filter_size_floor:image_padding.shape[1]-filter_size_floor] = image
for i in range(image.shape[0]):
for j in range(image.shape[1]):
convolute_image = image_padding[i:i+filter_size_sqrt, j:j+filter_size_sqrt]
reshape_image = np.reshape(convolute_image[0:filter_size_sqrt,
0:filter_size_sqrt], (filter_size,3))
filtered_image[i,j] = sum(np.multiply(reshape_image, filter_reshape))
return filtered_image
生成高低通分量滤除图片,图像融合
gen_hybrid_image()实现
def gen_hybrid_image(image1, image2, cutoff_frequency):
"""
Inputs:
- image1 -> The image from which to take the low frequencies.
- image2 -> The image from which to take the high frequencies.
- cutoff_frequency -> The standard deviation, in pixels, of the Gaussian
blur that will remove high frequencies.
Task:
- Use my_imfilter to create 'low_frequencies' and 'high_frequencies'.
- Combine them to create 'hybrid_image'.
"""
assert image1.shape[0] == image2.shape[0]
assert image1.shape[1] == image2.shape[1]
assert image1.shape[2] == image2.shape[2]
# Steps: (1) Remove the high frequencies from image1 by blurring it.
# The amount of blur that works best will vary
# with different image pairs generate a 1x(2k+1) gaussian kernel with mean=0 and sigma = s,
# see https://stackoverflow.com/questions/17190649/how-to-obtain-a-gaussian-filter-in-python
s, k = cutoff_frequency, cutoff_frequency * 2
probs = np.asarray([exp(-z * z / (2 * s * s)) / sqrt(2 * pi * s * s) for z in range(-k, k + 1)], dtype=np.float32)
kernel = np.outer(probs, probs)
large_1d_blur_filter = kernel.reshape(-1,1)
# Your code here:
large_blur_image1 = my_imfilter1(image1, large_1d_blur_filter)
low_frequencies = large_blur_image1
# (2) Remove the low frequencies from image2. The easiest way to do this is to
# subtract a blurred version of image2 from the original version of image2.
# This will give you an image centered at zero with negative values.
# Your code here #
large_blur_image2 = my_imfilter1(image2, large_1d_blur_filter)
# large_blur_image2 = my_imfilter(large_blur_image2, large_1d_blur_filter.T)
high_frequencies = image2 - large_blur_image2 # Replace with your implementation
# (3) Combine the high frequencies and low frequencies
# Your code here #
hybrid_image = low_frequencies + high_frequencies # Replace with your implementation
# (4) At this point, you need to be aware that values larger than 1.0
# or less than 0.0 may cause issues in the functions in Python for saving
# images to disk. These are called in proj1_part2 after the call to
# gen_hybrid_image().
# One option is to clip (also called clamp) all values below 0.0 to 0.0,
# and all values larger than 1.0 to 1.0.
for i in range(hybrid_image.shape[0]):
for j in range(hybrid_image.shape[1]):
for k in range(hybrid_image.shape[2]):
if hybrid_image[i,j,k] > 1.0:
hybrid_image[i,j,k] = 1.0
if hybrid_image[i,j,k] < 0.0:
hybrid_image[i,j,k] = 0.0
return low_frequencies, high_frequencies, hybrid_image
主函数
防止存储失败做了一些判断
# Project Image Filtering and Hybrid Images - Generate Hybrid Image
# Based on previous and current work
# by James Hays for CSCI 1430 @ Brown and
# CS 4495/6476 @ Georgia Tech
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from helpers import vis_hybrid_image, load_image, save_image, my_imfilter, gen_hybrid_image, gen_hybrid_image_cv
# Before trying to construct hybrid images, it is suggested that you
# implement my_imfilter in helpers.py and then debug it using proj1_part1.py
# Debugging tip: You can split your python code and print in between
# to check if the current states of variables are expected.
## Setup
# Read images and convert to floating point format
image1 = load_image('../data/bird.bmp')
image2 = load_image('../data/plane.bmp')
# display the dog and cat images
# plt.figure(figsize=(3,3)); plt.imshow((image1*255).astype(np.uint8));#
# plt.figure(figsize=(3,3)); plt.imshow((image2*255).astype(np.uint8));
# For your write up, there are several additional test cases in 'data'.
# Feel free to make your own, too (you'll need to align the images in a
# photo editor such as Photoshop).
# The hybrid images will differ depending on which image you
# assign as image1 (which will provide the low frequencies) and which image
# you asign as image2 (which will provide the high frequencies)
## Hybrid Image Construction ##
# cutoff_frequency is the standard deviation, in pixels, of the Gaussian#
# blur that will remove high frequencies. You may tune this per image pair
# to achieve better results.
cutoff_frequency = 7
low_frequencies, high_frequencies, hybrid_image = gen_hybrid_image_cv(image1, image2, cutoff_frequency)
## Visualize and save outputs ##
plt.figure(); plt.imshow((low_frequencies*255).astype(np.uint8));
plt.figure(); plt.imshow(((high_frequencies+0.5)*255).astype(np.uint8));
vis = vis_hybrid_image(hybrid_image)
plt.figure(figsize=(20, 20)); plt.imshow(vis);
high_frequencies_plus = high_frequencies+0.5
for i in range(high_frequencies_plus.shape[0]):
for j in range(high_frequencies_plus.shape[1]):
for k in range(high_frequencies_plus.shape[2]):
if high_frequencies_plus[i,j,k] > 1.0:
high_frequencies_plus[i,j,k] = 1.0
if high_frequencies_plus[i,j,k] < 0.0:
high_frequencies_plus[i,j,k] = 0.0
save_image('../results/low_frequencies.jpg', low_frequencies)
save_image('../results/high_frequencies.jpg', high_frequencies_plus)
save_image('../results/hybrid_image.jpg', hybrid_image)
save_image('../results/hybrid_image_scales.jpg', vis)
实验数据及结果分析:
两个原始图像如下所示:
1.自己实现的滤波函数my_imfilter()
这些图像的低通(模糊)和高通版本如下所示:
高频图像实际上是零均值,负值,因此通过添加0.5可视化。在得到的可视化中,亮值为正,暗值为负。可视化效果的有用方法是逐步对混合图像进行下采样,如下所示: