当前位置: 首页 > news >正文

混合图像python旗舰版

仔细看这个图像。然后后退几米再看。你看到了什么?

混合图像是指将一张图片的低频与另一张图片的高频相结合的图片。根据观看距离的不同,所得到的图像有两种解释。在上面的图片中,你可以看到阿尔伯特·爱因斯坦,一旦你离开屏幕或缩小观众的图像大小,他就变成了玛丽莲·梦露。这个概念是在2006年的论文中提出的。为了实现这一效果,您必须实现低通和高通滤波操作来应用于您选择的两幅图像,并线性组合过滤后的图像,得到具有所需的两种解释的混合图像,即最后将只有低频信息的图片和只有高频信息的图像叠加在一起。

对于图像的低频部分:可以理解为图像的“轮廓”,比如一幅画的线条等。

对于图像的高频部分:可以理解为图像的“细节”,比如一幅画的颜色搭配,颜色深度等。

值得一提的是:对图像做模糊处理后得到了图像的低频部分,对图像做锐化处理会让图像的高频信息更多。

实现过滤功能

步骤

您的目标是在hybrid.py中实现以下函数:

  1. cross_correlation_2d:实现了你的过滤功能的核心;

  1. convolve_2d:必须使用cross_correlation_2d功能;

  1. gaussian_blur_kernel_2d:你在这里创建的高斯核,与convolve_2d配对,创建一个高斯模糊滤波器;

  1. low_pass:从图像中删除细节,你的实现必须使用高斯模糊;

  1. high_pass:保留很细的细节和删除低频,您的实现必须使用高斯模糊作为一个子例程。

注意,您必须从头开始实现所有的函数,只使用基本的矩阵操作,而任何来自NumPy、OpenCV、Scipy或类似包的任何过滤函数都是禁止的。功能,如填充,创建网格网格,等。被认为是基本的操作,如果您想快速编写代码并避免多个嵌套的Python循环,则是允许的。

生成混合图像

一旦在hybrid.py中实现了函数,使用提供的创建混合图像。然而,创建一个被我们的大脑很好地解释的混合图像的一个重要因素是对齐两个图像的显著特征。注意:如果您使用多个嵌套的Python循环来实现过滤操作,那么您的函数可能会非常慢。在更改参数后,您必须保持耐心,或者使用基本的矩阵功能来更加努力地优化代码。

最终,你应该得到一张像下面这样的图片:

import cv2
import numpy as npdef cross_correlation_2d(img, kernel):'''Given a kernel of arbitrary m x n dimensions, with both m and n beingodd, compute the cross correlation of the given image with the givenkernel, such that the output is of the same dimensions as the image and thatyou assume the pixels out of the bounds of the image to be zero. Note thatyou need to apply the kernel to each channel separately, if the given imageis an RGB image.Inputs:img:    Either an RGB image (height x width x 3) or a grayscale image(height x width) as a numpy array.kernel: A 2D numpy array (m x n), with m and n both odd (but may not beequal).Output:Return an image of the same dimensions as the input image (same width,height and the number of color channels)'''# TODO-BLOCK-BEGIN# rotating kernel with 180 degreeskernel = np.rot90(kernel, 2)kernel_heigh = int(np.array(kernel).shape[0])kernel_width = int(np.array(kernel).shape[1])# set kernel matrix to random int matrixif ((kernel_heigh % 2 != 0) & (kernel_width % 2 != 0)):  # make sure that the scale of kernel is odd# the scale of resultconv_heigh = img.shape[0] - kernel.shape[0] + 1conv_width = img.shape[1] - kernel.shape[1] + 1conv = np.zeros((conv_heigh, conv_width))# convolvefor i in range(int(conv_heigh)):for j in range(int(conv_width )):result = (img[i:i + kernel_heigh, j:j + kernel_width] * kernel).sum()if(result<0):result = 0elif(result>255):result = 255conv[i][j] = resultreturn convelse: raise Exception('make sure that the scale of kernel is odd')# raise Exception("TODO in hybrid.py not implemented")# TODO-BLOCK-ENDdef convolve_2d(img, kernel):'''Use cross_correlation_2d() to carry out a 2D convolution.Inputs:img:    Either an RGB image (height x width x 3) or a grayscale image(height x width) as a numpy array.kernel: A 2D numpy array (m x n), with m and n both odd (but may not beequal).Output:Return an image of the same dimensions as the input image (same width,height and the number of color channels)'''# TODO-BLOCK-BEGIN# zero paddingkernel_half_row = int((kernel.shape[0]-1)/2)kernel_half_col = int((kernel.shape[1]-1)/2)# judge how many channelsif len(img.shape) == 3:img = np.pad(img, ((kernel_half_row, kernel_half_row), (kernel_half_col, kernel_half_col),(0, 0)), 'constant', constant_values=0)# if image.shape[2] == 3 or image.shape[2] == 4:# if style is png, there will be four channels, but we just need to use the first three# if the style is bmp or jpg, there will be three channelsimage_r = img[:, :, 0]image_g = img[:, :, 1]image_b = img[:, :, 2]result_r = cross_correlation_2d(image_r, kernel)result_g = cross_correlation_2d(image_g, kernel)result_b = cross_correlation_2d(image_b, kernel)result_picture = np.dstack([result_r, result_g, result_b])# if the picture is black and whiteelif len(img.shape) == 2:img = np.pad(img, ((kernel_half_row, kernel_half_row), (kernel_half_col, kernel_half_col)), 'constant', constant_values=0)result_picture = cross_correlation_2d(img, kernel)# returns the convolved image (of the same shape as the input image)return result_picture# raise Exception("TODO in hybrid.py not implemented")# TODO-BLOCK-ENDdef gaussian_blur_kernel_2d(sigma, height, width):'''Return a Gaussian blur kernel of the given dimensions and with the givensigma. Note that width and height are different.Input:sigma:  The parameter that controls the radius of the Gaussian blur.Note that, in our case, it is a circular Gaussian (symmetricacross height and width).width:  The width of the kernel.height: The height of the kernel.Output:Return a kernel of dimensions height x width such that convolving itwith an image results in a Gaussian-blurred image.'''# TODO-BLOCK-BEGINm,n = [(ss-1.)/2. for ss in (height, width)]y, x = np.ogrid[-m:m+1, -n:n+1]h = np.exp( - (x*x + y*y) / (2.*sigma*sigma))h[ h < np.finfo(h.dtype).eps*h.max()] = 0sumh = h.sum()if sumh != 0:h /= sumhreturn h# raise Exception("TODO in hybrid.py not implemented")# TODO-BLOCK-ENDdef low_pass(img, sigma, size):'''Filter the image as if its filtered with a low pass filter of the givensigma and a square kernel of the given size. A low pass filter supressesthe higher frequency components (finer details) of the image.Output:Return an image of the same dimensions as the input image (same width,height and the number of color channels)'''# TODO-BLOCK-BEGIN# make kernellow_kernel = gaussian_blur_kernel_2d(sigma, size, size)# convolve low-pass pictureslow_image = convolve_2d(img, low_kernel)return low_image# raise Exception("TODO in hybrid.py not implemented")# TODO-BLOCK-ENDdef high_pass(img, sigma, size):'''Filter the image as if its filtered with a high pass filter of the givensigma and a square kernel of the given size. A high pass filter suppressesthe lower frequency components (coarse details) of the image.Output:Return an image of the same dimensions as the input image (same width,height and the number of color channels)'''# TODO-BLOCK-BEGIN# make kernelhigh_kernel = gaussian_blur_kernel_2d(sigma, size, size)# make high-pass picturehigh_image = (img - convolve_2d(img, high_kernel))return high_image# raise Exception("TODO in hybrid.py not implemented")# TODO-BLOCK-ENDdef create_hybrid_image(img1, img2, sigma1, size1, high_low1, sigma2, size2,high_low2, mixin_ratio, scale_factor):'''This function adds two images to create a hybrid image, based onparameters specified by the user.'''high_low1 = high_low1.lower()high_low2 = high_low2.lower()if img1.dtype == np.uint8:img1 = img1.astype(np.float32) / 255.0img2 = img2.astype(np.float32) / 255.0if high_low1 == 'low':img1 = low_pass(img1, sigma1, size1)else:img1 = high_pass(img1, sigma1, size1)if high_low2 == 'low':img2 = low_pass(img2, sigma2, size2)else:img2 = high_pass(img2, sigma2, size2)img1 *=  (1 - mixin_ratio)img2 *= mixin_ratiocv2.imshow('img1', img1)cv2.imshow('img2', img2)cv2.imwrite('high_left.png', img1)cv2.imwrite('low_right.png', img2)hybrid_img = (img1 + img2) * scale_factorreturn (hybrid_img * 255).clip(0, 255).astype(np.uint8)if __name__ == "__main__":hybrid_image = create_hybrid_image(img1=cv2.imread(r'resources\cat.jpg'),img2=cv2.imread(r'resources\dog.jpg'),sigma1=7,size1=29,high_low1='high',sigma2=7.0,size2=29,high_low2='low',mixin_ratio=0.5,scale_factor=1)cv2.imshow('hybrid_image', hybrid_image)cv2.waitKey(0)cv2.imwrite('hybrid_image.png', hybrid_image)

相关文章:

混合图像python旗舰版

仔细看这个图像。然后后退几米再看。你看到了什么&#xff1f;混合图像是指将一张图片的低频与另一张图片的高频相结合的图片。根据观看距离的不同&#xff0c;所得到的图像有两种解释。在上面的图片中&#xff0c;你可以看到阿尔伯特爱因斯坦&#xff0c;一旦你离开屏幕或缩小…...

开发手册——一、编程规约_5.集合处理

这篇文章主要梳理了在java的实际开发过程中的编程规范问题。本篇文章主要借鉴于《阿里巴巴java开发手册终极版》 下面我们一起来看一下吧。 1. 【强制】关于 hashCode 和 equals 的处理&#xff0c;遵循如下规则&#xff1a; 只要重写 equals&#xff0c;就必须重写 hashCod…...

【elastic】elastic高可用集群部署

文章目录前言一、资源分享1、包含源码包、配置文件二、部署过程三、报错锦集四、es的部分相关命令前言 本博客内容仅为记录博主思路&#xff0c;仅供参考&#xff0c;一切以自己实践结果为准。 一、资源分享 1、包含源码包、配置文件 链接&#xff1a;https://pan.baidu.com…...

初识Liunx下的进程状态和环境变量以及进程优先级

文章目录前言1.进程状态1.阻塞与挂起2.Linux下的进程状态1.概念知识2.R状态2.休眠状态(S/D&#xff09;3.T状态4.Z状态(僵尸进程)和X状态5.孤儿进程3.环境变量1.概念2.获取环境变量1.环境变量表2.函数获取环境变量3.关于环境变量的理解和main函数中的两个参数1.环境变量的理解2…...

JavaEE——何为线程及创建线程

文章目录一、认识线程1. 线程的概念2. 出现多线程的原因3. 进程与线程4. 对多线程的详细解释二、初次实现多线程代码1. 初步了解2. 使用 Java 中的工具查看当前的所有线程3. Java 中创建线程的多种方式一、认识线程 1. 线程的概念 所谓线程&#xff0c;就是指在一个 ‘执行流…...

linux配置核查MySQL 配置规范 (Linux)_S3A3G3

linux的配置核查问题&#xff1a; 解决&#xff1a; 1.检查是否禁止mysql对本地文件存取 方法一&#xff1a;在my.cnf的mysql字段下加local-infile0 方法二&#xff1a;启动mysql时加参数local-infile0 /etc/init.d/mysql start --local-infile0 假如需要获取本地文件&#xf…...

Protobuf简介

Protobuf简介 1. Protocol Buffers1.1. 什么是Protocol Buffers?1.2. 选择你最喜欢的语言1.3. 如何开始2. Protocol Buffer Basics: C++2.1. 问题领域2.2. 在哪里找到示例代码2.3. 定义协议格式(Defining Your Protocol Format)1. Protocol Buffers Protocol Buffers(协议缓冲…...

【Kubernetes】第十七篇 - ECS 服务停机和环境修复

一&#xff0c;前言 上一篇&#xff0c;介绍了 Secret 镜像的使用&#xff1b; 三台服务每天大概 15 块钱的支出&#xff0c;用一个月也是不少钱&#xff1b; 闲时可以停掉&#xff0c;这样每天只有 4 块钱支出&#xff0c;剩下一大笔&#xff1b; ECS 服务停机后公网 IP 会…...

Vue2的生命周期(详解)

Vue的生命周期一、生命周期的概念二、钩子函数三、Vue2的生命周期3.1 初始化阶段3.2 挂载阶段3.3 更新阶段3.4 销毁阶段一、生命周期的概念 Vue实例的生命周期: 从创建到销毁的整个过程 二、钩子函数 Vue框架内置函数,随着组件的生命周期阶段,自动执行 作用:特定的时间点,执行特…...

Potions (Hard Version) and (Easy Version)(背包DP + 反悔贪心)

[TOC](Potions (Hard Version) and (Easy Version)) 一、Potions(Easy Version) 1、问题 2、分析&#xff08;背包DP 贪心&#xff09; 简而言之就是我们需要从左到右开始选数字&#xff0c;选的过程中我们需要保证我们选的数字的和始终是大于等于0的&#xff0c;在满足这个…...

剑指 Offer II 017. 含有所有字符的最短字符串

题目链接 剑指 Offer II 017. 含有所有字符的最短字符串 hard 题目描述 给定两个字符串 s和 t。返回 s中包含 t的所有字符的最短子字符串。如果 s中不存在符合条件的子字符串&#xff0c;则返回空字符串 ""。 如果 s中存在多个符合条件的子字符串&#xff0c;返回任…...

Modbus协议初探(C#实现)

由于作者水平有限&#xff0c;如有写得不对得地方请指正 趁着今天休息&#xff0c;就折腾一下Modbus协议&#xff0c;之前零零散散的看过几篇博客&#xff0c;听说搞上位机开发的要会这个协议&#xff0c;虽然我不是搞上位机开发的&#xff0c;但个人对这个比较感兴趣。按照我个…...

【华为OD机试2023】静态扫描 C++ Java Python

【华为OD机试2023】静态扫描 C++ Java Python 前言 如果您在准备华为的面试,期间有想了解的可以私信我,我会尽可能帮您解答,也可以给您一些建议! 本文解法非最优解(即非性能最优),不能保证通过率。 Tips1:机试为ACM 模式 你的代码需要处理输入输出,input/cin接收输入、…...

函数栈帧的创建和销毁(详解)

函数栈帧的创建和销毁&#x1f996;函数栈帧是什么&#xff1f;&#x1f996;函数栈帧的创建和销毁解析&#x1f40b;栈是什么&#xff1f;&#x1f40b;认识相关寄存器和汇编指令&#x1f40b;解析函数栈帧的创建和销毁&#x1f433;预备知识&#x1f433;函数的调用堆栈&…...

【100个 Unity实用技能】 | 脚本无需挂载到游戏对象上也可执行的方法

Unity 小科普 老规矩&#xff0c;先介绍一下 Unity 的科普小知识&#xff1a; Unity是 实时3D互动内容创作和运营平台 。包括游戏开发、美术、建筑、汽车设计、影视在内的所有创作者&#xff0c;借助 Unity 将创意变成现实。Unity 平台提供一整套完善的软件解决方案&#xff…...

条件期望5

条件期望例题 随机图 从节点1开始, N为一个随机变量, 表示整个过程第一次出现"贪吃蛇"情形时, 所进行的步数.即Nk⇒Xk(1)∈{1,X(1),X2(1),...Xk−1(1)}其中1,X(1),X2(1),...Xk−1(1)各不相同N k \Rightarrow X^k(1) \in \{1,X(1), X^2(1),...X^{k-1}(1)\} \\ 其中1…...

RecyclerView ViewType二级

实现效果描述&#xff1a; 1、点击recyclerview中item&#xff0c;列表下方出现其他样式的item&#xff0c;作为子item&#xff0c;如下所示 所需要的java文件和xml文件有&#xff1a; 1、创建FoldAdapteradapter, 在FoldAdapter中&#xff0c;定义两种不同的类型&#xff…...

将对象或数组存在 dom元素的属性上,最后取不到完整数据,只取到 [{

目录 一、问题 二、问题及解决方法 三、总结 一、问题 1.我需要在dom元素里面添加了一个属性test存一个对象数组temp&#xff0c;以便我下一次找到这个dom元素时可以直接拿到属性里面的数据来渲染页面。 2.dom 属性上存 对象和数组&#xff0c;必须先JSON.stringify(arr),转…...

Flask源码篇:Flask路由规则与请求匹配过程(超详细,易懂)

目录1 启动时路由相关操作&#xff08;1&#xff09;分析app.route()&#xff08;2&#xff09;分析add_url_rule()&#xff08;3&#xff09;分析Rule类&#xff08;4&#xff09;分析Map类&#xff08;5&#xff09;分析MapAdapter类&#xff08;6&#xff09;分析 url_rule_…...

Jmeter接口测试教程之【参数化技巧总结】,总有一个是你不知道的

目录&#xff1a;导读 一、随机值 二、随机字符串 三、时间戳 四、唯一字符串UUID 说起接口测试&#xff0c;相信大家在工作中用的最多的还是Jmeter。 大家看这个目录就知道jmeter的应用有多广泛了&#xff1a;https://www.bilibili.com/video/BV1e44y1X78S/? JMeter是一个…...

缓存与数据库的双写一致性

背景 在高并发的业务场景下&#xff0c;系统的性能瓶颈往往是出现在数据库上&#xff0c;用户并发访问过大&#xff0c;压力都打到数据库上。所以一般都会用redis做缓存层&#xff0c;起到一个缓冲作用&#xff0c;让请求先访问到缓存层&#xff0c;而不是直接去访问数据库&am…...

力扣-213打家劫舍II(dp)

力扣-213打家劫舍II 1、题目 213. 打家劫舍 II 你是一个专业的小偷&#xff0c;计划偷窃沿街的房屋&#xff0c;每间房内都藏有一定的现金。这个地方所有的房屋都 围成一圈 &#xff0c;这意味着第一个房屋和最后一个房屋是紧挨着的。同时&#xff0c;相邻的房屋装有相互连通…...

关于【网格结构】岛屿类问题的通用解法DFS(深度遍历)遍历框架+回溯+剪枝总结

最近在刷力扣时遇见的问题&#xff0c;自己总结加上看了力扣大佬的知识总结写下本篇文章&#xff0c;我们所熟悉的 DFS&#xff08;深度优先搜索&#xff09;问题通常是在树或者图结构上进行的。而我们今天要讨论的 DFS 问题&#xff0c;是在一种「网格」结构中进行的。岛屿问题…...

【LeetCode】982. 按位与为零的三元组

982. 按位与为零的三元组 题目描述 给你一个整数数组 nums &#xff0c;返回其中 按位与三元组 的数目。 按位与三元组 是由下标 (i, j, k) 组成的三元组&#xff0c;并满足下述全部条件&#xff1a; 0 < i < nums.length0 < j < nums.length0 < k < num…...

Linux内核源码进程原理分析

Linux内核源码进程原理分析一、Linux 内核架构图二、进程基础知识三、Linux 进程四要素四、task_struct 数据结构主要成员五、创建新进程分析六、剖析进程状态迁移七、写时复制技术一、Linux 内核架构图 二、进程基础知识 Linux 内核把进程称为任务(task)&#xff0c;进程的虚…...

电子技术——CMOS反相器

电子技术——CMOS反相器 在本节&#xff0c;我们深入学习CMOS反相器。 电路原理 下图是我们要研究的CMOS反相器的原理图&#xff1a; 下图展示了当输入 vIVDDv_I V_{DD}vI​VDD​ 时的 iD−vDSi_D-v_{DS}iD​−vDS​ 曲线&#xff1a; 我们把 QNQ_NQN​ 当做是驱动源&#x…...

gazebo仿真轨迹规划+跟踪(不在move_base框架下)

以Tianbot为例子&#xff0c;开源代码如下&#xff1a; https://github.com/tianbot/tianbot_mini GitHub - tianbot/abc_swarm: Ant Bee Cooperative Swarm, indicating air-ground cooperation. This repository is for Tianbot Mini and RoboMaster TT swarm kit. 1.在…...

C. Good Subarrays(前缀和)

C. Good Subarrays一、问题二、分析三、代码一、问题 二、分析 这道题目的意思就是给我们一个数组&#xff0c;然后我们从数组中选取一个连续的区间&#xff0c;这个区间满足条件&#xff1a;区间内的元素和等于区间的长度。 对于区间和问题我们先想到的是前缀和的算法。 那…...

关于Facebook Messenger CRM,这里有你想要知道的一切

关于Facebook Messenger CRM&#xff0c;这里有你想要知道的一切&#xff01;想把Facebook Messenger与你的CRM整合起来吗&#xff1f;这篇博文是为你准备的! 我们将介绍有关获得Facebook Messenger CRM整合的一切信息。然后&#xff0c;我们将解释为什么你需要像SaleSmartly&a…...

ChIP-seq 分析:数据与Peak 基因注释(10)

动动发财的小手&#xff0c;点个赞吧&#xff01; 1. 数据 今天&#xff0c;我们将继续回顾我们在上一次中研究的 Myc ChIPseq。这包括用于 MEL 和 Ch12 细胞系的 Myc ChIPseq。 可在此处[1]找到 MEL 细胞系中 Myc ChIPseq 的信息和文件可在此处[2]找到 Ch12 细胞系中 Myc ChIP…...