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, ...
随机推荐
- centos7 scrapy安装
1.anaconda3安装 wget https://repo.anaconda.com/archive/Anaconda3-2019.03-Linux-x86_64.sh 安装报错,可能是源的问题 ...
- 智慧金融时代,大数据和AI如何为业务赋能
前言:宜信技术人物专访是宜信技术学院推出的系列性专题,我们邀请软件研发行业的优秀技术人,分享自己在软件研发领域的实践经验和前瞻性观点. 第一期专访我们邀请到宜信科技中心AI中台负责人王东老师,从大数据 ...
- div模拟select/option解决兼容性问题及增加可拓展性
个人博客: http://mcchen.club 想到做这个模拟的原因是之前使用select>option标签的时候发现没有办法操控option的很多样式,比如line-height等,还会由此 ...
- Spring Cloud Feign 总结问题,注意点,性能调优,切换okhttp3
### Feign常见问题总结 **FeignClient接口如使用`@PathVariable` ,必须指定value属性** ```java //在一些早期版本中, @PathVariable(& ...
- Hyper-V虚拟机win7网络红叉,无法上网解决方法
之前一直都是玩Vmware虚拟机,后来win8之后的系统有Hyper-V虚拟机就开始接触了. Windows 中内置的Hyper-V管理器可以说是给很多人带来了惊喜!至少运行的流畅程度要比Vmware ...
- 如何编译和调试Python内核源码?
目录 写在前面 获取源代码 源代码的组织 windows下编译CPython 调试CPython 小结 参考 博客:blog.shinelee.me | 博客园 | CSDN 写在前面 如果对Pyth ...
- JZOJ 3875 星球联盟
[问题描述] 在遥远的 S 星系中一共有 N 个星球,编号为 1…N.其中的一些星球决定组成联盟, 以方便相互间的交流. 但是,组成联盟的首要条件就是交通条件.初始时,在这 N 个星球间有 M 条太空 ...
- [JOJZ]3855.选择困难症
[问题描述]又到吃饭时间,Polo 面对饭堂里琳(fei)琅(chang)满(keng)目(die)的各种食品,又陷入了痛苦的抉择中:该是吃手(jiao)打肉饼好呢,还是吃豆(cai)角(chong) ...
- macOS10.14.2 gem 更新问题
macOS10.14.2,最近cocoapods不能正常使用了. 终端输入 sudo gem update –system 显示如下错误 ERROR: While executing gem … (G ...
- tesseract 测试样例
该图片的链接为https://raw.githubusercontent.com/Python3WebSpider/TestTess/master/image.png,可以直接保存或下载. 首先用命令 ...