20190730_图像混合_opencv_python
今天学习了 图像的混合
教程上的代码很简单,但是绝对运行不出来
教程名称:OpenCV-Python 中文教程
#图像融合
import cv2
import numpy as np
import matplotlib.pyplot as plt imgO = cv2.imread(r"C:\Users\lijin\Desktop\opencvImg\opencvSource\orange.png")
imgA = cv2.imread(r"C:\Users\lijin\Desktop\opencvImg\opencvSource\apple.png") cv2.imshow('apple', imgA)
cv2.imshow('orange', imgO) print(imgO.shape) #高y--rows, 宽x--cols
print(imgA.shape) imgO = cv2.resize(imgO, (300, 300))
imgA = cv2.resize(imgA, (300, 300)) #居然在这里错了,浪费了一下午 print(imgO.shape) #高y--rows, 宽x--cols
print(imgA.shape) #Orange Operation
#gaussian pyramid list for Orange
gOperator = imgO.copy()
gpO = [gOperator]
for i in range(6):
gOperator = cv2.pyrDown(gOperator)
#cv2.imshow(test[i], gOperator)
gpO.append(gOperator) #add to list
#cv2.imshow(testx[i], gpO[i]) #laplacian pyramid list for Orange
lpO = [gpO[6]]
for i in range(6, 0, -1):
lOperator = cv2.pyrUp(gpO[i]) #this operator is for gpO[i -1]
#print(lOperator.shape)
#print(gpO[i - 1].shape)
rows, cols = gpO[i - 1].shape[:2] #必须要加这一步,使得尺寸相等
lOperator = cv2.resize(lOperator, (cols, rows)) lOperator = cv2.subtract(gpO[i - 1], lOperator)
lpO.append(lOperator) #add to list #Apple Operation
#gaussian pyramid list for Apple
gOperator = imgA.copy()
gpA = [gOperator]
for i in range(6):
gOperator = cv2.pyrDown(gOperator)
gpA.append(gOperator) #add to list #laplacian pyramid list for Apple
lpA = [gpA[6]]
for i in range(6, 0, -1):
lOperator = cv2.pyrUp(gpA[i]) #this operator is for gpO[i -1] rows, cols = gpA[i - 1].shape[:2] #必须要加这一步,使得尺寸相等
lOperator = cv2.resize(lOperator, (cols, rows)) lOperator = cv2.subtract(gpA[i - 1], lOperator)
lpA.append(lOperator) #add to list for i in range(7):
print('lpO size: ', lpO[i].shape)
print('lpA size: ', lpA[i].shape) #add left part of Apple and right part of Orange in each level
#numpy.hstack(tup)
#take a sequence of arrays and stack them horizontally
#to make a single array
combinedList = []
for appleLeft, orangeRight in zip(lpA, lpO):
rows, cols, dpt = appleLeft.shape #cv2.imshow('appleLeft ', appleLeft)
#cv2.imshow('orangeRight ', orangeRight)
#print('appleLeft.shape ', appleLeft.shape)
#print('orangeRight.shape ', orangeRight.shape) combineElement = np.hstack((appleLeft[:, 0:cols//2], orangeRight[:, cols//2:])) #0:cols//2 不包括右边,cols//2: 包括左边
combinedList.append(combineElement)
print('appleLeft.shape ',appleLeft.shape)
print('orangeRight.shape ',orangeRight.shape)
print('combineElement.shape', combineElement.shape) print('combinedList[1].shape ', combinedList[1].shape)
print('combinedList[2].shape ', combinedList[2].shape)
#"""
#reconstruct
combinedElement_ = combinedList[0] #从 a (5, 5, 3),o (5, 5, 3) 开始
for i in range(1, 6): #combinedList[1].shape (10, 10, 3)
combinedElement_ = cv2.pyrUp(combinedElement_) #combinedList[1].shape (10, 10, 3) #combinedList[2].shape (19, 19, 3)
#print('combinedElement_.shape...', combinedElement_.shape)
#print('combinedList.shape...', combinedList[i].shape)
rows, cols = combinedList[i].shape[:2]
combinedElement_ = cv2.resize(combinedElement_,(cols, rows))
combinedElement_ = cv2.add(combinedElement_, combinedList[i]) #combinedList[1].shape (10, 10, 3) #reconstruct
pyramidBlending = combinedList[0]
for i in range(1, 7):
pyramidBlending = cv2.pyrUp(pyramidBlending) rows, cols = combinedList[i].shape[:2]
pyramidBlending = cv2.resize(pyramidBlending,(cols, rows)) pyramidBlending = cv2.add(pyramidBlending, combinedList[i]) #image with direct cnnecting each half
cv2.imshow('appleL', imgA[:, : cols // 2])
cv2.imshow('orangeR', imgO[ : , cols // 2 : ])
directBlending = np.hstack((imgA[:, : cols // 2], imgO[:, cols // 2 : ])) cv2.imshow("directBlending", directBlending)
cv2.imshow("pyramidBlending", pyramidBlending) #""" cv2.waitKey(0)
cv2.destroyAllWindows()
在调试代码的时候,需要不停的使用 resize() 函数在对两个生成图像做加减法时进行调整,不然会出现两图大小不一而无法运算的情况。
比如,如果没有这段代码

会出现的错误提示

所以要不停的使用 cv2.imshow(), img.shape 进行调试
这是最终的运行结果

20190730_图像混合_opencv_python的更多相关文章
- 学习 opencv---(3) ROI 区域图像叠加&初级图像混合
在这篇文章里,我们一起学习了在OpenCV中如何定义感兴趣区域ROI,如何使用addWeighted函数进行图像混合操作,以及将ROI和addWeighted函数结合起来使用,对指定区域进行图像混合操 ...
- Atitti 图像处理 图像混合 图像叠加 blend 原理与实现
Atitti 图像处理 图像混合 图像叠加 blend 原理与实现 混合模式 编辑 本词条缺少信息栏,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 混合模式是图像处理技术中的一个技术名词,不 ...
- opencv3.2.0 分离颜色通道&多通道图像混合
##名称:分离颜色通道&多通道图像混合 ##平台:QT5.7.1+OpenCV3.2.0 ##时间:2017年12月11日 /***************创建QT控制台程序********* ...
- PorterDuffXfermode 图像混合技术在漫画APP中的应用
此文已由作者游葳授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 写在开头 随着应用开发的深入,视觉同学在完成了页面的基本设计后,再也按耐不住心中的寂寞,开始对各种细节不满意, ...
- opencv 3 core组件进阶(2 ROI区域图像叠加&图像混合;分离颜色通道、多通道图像混合;图像对比度,亮度值调整)
ROI区域图像叠加&图像混合 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp&g ...
- openCV - 5~7 图像混合、调整图像亮度与对比度、绘制形状与文字
5. 图像混合 理论-线性混合操作.相关API(addWeighted) 理论-线性混合操作 用到的公式 (其中 α 的取值范围为0~1之间) 相关API(addWeighted) 参数1:输入图像M ...
- 利用matlab编写实现显示fmri切片slice图像 混合显示 不同侧面显示 可叠加t检验图显示 by DR. Rajeev Raizada
1.参考 reference 1. tutorial主页:http://www.bcs.rochester.edu/people/raizada/fmri-matlab.htm. 2.speech_b ...
- 图像混合学习。运用加权函数,学习opencv基础操作
{ cout<< } { cout<< } ,,logoImage.c ...
- opencv学习笔记-图像叠加、混合
在图像处理中,目标区域定义为感兴趣区域ROI(region of Interest),这是后期图像处理的基础,在获取ROI后,进行一些列的处理.ROI区域在Opencv中就是Rect,先构建Rect, ...
随机推荐
- Kubernetes 系列(一):本地k8s集群搭建
我们需要做以下工作: (1)安装VMware,运行CentOs系统,一个做master,一个做node. (2)安装K8s. (3)安装docker和部分镜像会需要访问外网,所以你需要做些网络方面的准 ...
- ArcGIS Server10.1 动态图层服务
动态图层的应用场景: 1 改变现有图层:符号,渲染方式和版本,这些都可以通过客户端请求的时候给定相应的参数来进行设置,从而来达到轻易改变地图的效果. 2 添加地图服务中没有的图层 添加的数据可以是矢量 ...
- Chrome 浏览器默认样式覆盖自己 CSS 样式的解决
检查 HTML 源代码,DOCTYPE 的声明是否写正确. HTML5 的 DOCTYPE 声明规范: <!DOCTYPE html> 参考链接: css - User agent sty ...
- javascript基础修炼(13)——记一道有趣的JS脑洞练习题
目录 一. 题目 二. 解法风暴 示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:<大史住在大前端>原创博文目录 华为云社区地址 ...
- 《Java语言程序设计》编程练习6.31(财务应用程序:信用卡号的合法性)
6.31(财务应用程序:信用卡号的合法性)信用卡号遵循下面的模式.一个信用卡号必须是13到16位的整数.它的开头必须是: 4,指Visa卡 5,指Master卡 37,指American Expres ...
- egret引擎中使用tiled运行在微信小游戏中
egret的官方文档,对tiled的介绍不是很细致,很多东西都需要摸索.现在把踩的坑记录下来.作为一个备忘 引用tiledmap的库 在GitHub上下载egret的tiledmap支持库:https ...
- 教你如何判断URL的好坏
1.最核心-网站整体内容质量2.关键词整理拓展及关键词布局3.网站外部链接建设4.网站内链建设合理5.面包屑导航6.友情链接7.404页面网站的SEO站外优化+SEO外链建设 层级:三层为好,301重 ...
- 算法学习之剑指offer(七)
题目1 题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P% ...
- 计算机网络知识点总结2:IP协议(IPV4)
一.Internet网络是一种数据报网络(另一种是虚电路网络,用于ATM等),主要功能是路由和转发. 二.IP数据报(分组)格式(IPV4版本) 首部 描述 版本号(4bit) 描述IP协议的版本号, ...
- Fiddle弱网测试
1.打开Fiddler,Rules->Performance->勾选 Simulate Modem Speeds,勾选之后访问网站会发现网络慢了很多: 接下来给大家解释一下这些个都是什么意 ...