今天学习了 图像的混合

教程上的代码很简单,但是绝对运行不出来

教程名称: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的更多相关文章

  1. 学习 opencv---(3) ROI 区域图像叠加&初级图像混合

    在这篇文章里,我们一起学习了在OpenCV中如何定义感兴趣区域ROI,如何使用addWeighted函数进行图像混合操作,以及将ROI和addWeighted函数结合起来使用,对指定区域进行图像混合操 ...

  2. Atitti 图像处理 图像混合 图像叠加 blend 原理与实现

    Atitti 图像处理 图像混合 图像叠加 blend 原理与实现 混合模式 编辑 本词条缺少信息栏,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 混合模式是图像处理技术中的一个技术名词,不 ...

  3. opencv3.2.0 分离颜色通道&多通道图像混合

    ##名称:分离颜色通道&多通道图像混合 ##平台:QT5.7.1+OpenCV3.2.0 ##时间:2017年12月11日 /***************创建QT控制台程序********* ...

  4. PorterDuffXfermode 图像混合技术在漫画APP中的应用

    此文已由作者游葳授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 写在开头 随着应用开发的深入,视觉同学在完成了页面的基本设计后,再也按耐不住心中的寂寞,开始对各种细节不满意, ...

  5. opencv 3 core组件进阶(2 ROI区域图像叠加&图像混合;分离颜色通道、多通道图像混合;图像对比度,亮度值调整)

    ROI区域图像叠加&图像混合 #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp&g ...

  6. openCV - 5~7 图像混合、调整图像亮度与对比度、绘制形状与文字

    5. 图像混合 理论-线性混合操作.相关API(addWeighted) 理论-线性混合操作 用到的公式 (其中 α 的取值范围为0~1之间) 相关API(addWeighted) 参数1:输入图像M ...

  7. 利用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 ...

  8. 图像混合学习。运用加权函数,学习opencv基础操作

               {          cout<<     }           {          cout<<     }       ,,logoImage.c ...

  9. opencv学习笔记-图像叠加、混合

    在图像处理中,目标区域定义为感兴趣区域ROI(region of Interest),这是后期图像处理的基础,在获取ROI后,进行一些列的处理.ROI区域在Opencv中就是Rect,先构建Rect, ...

随机推荐

  1. Angular 自定义管道

    管道的作用就是将原始值进行转化处理,转换为所需要的值: 1. 新建sex-reform.pipe.ts文件 ng g pipe sex-reform 2. 编辑sex-reform.pipe.ts文件 ...

  2. PHP5.5 mysqli如何连接MySQL数据库和读取数据

    在学习 1. 开启PHP的API支持 (1)首先修改您的php.ini的配置文件.查找下面的语句:;extension=php_mysqli.dll将其修改为:extension=php_mysqli ...

  3. Python入门系列【附】进阶教程

    如题,本篇将讲解Python提升之路:Python作为语法简单易学的语言,入门容易精通却很难,这是共识,那么为什么会有这样的共识?精通Python的难度在哪里? Python拥有简单.形象.直观的语法 ...

  4. CRS-2674: Start of 'ora.cssd' on 'rac2' failed 引发的rac集群服务起不来问题

    问题背景:客户反馈Oracle rac集群节点宕机 1.首先查看宕机原因,归档日志满导致服务重启,查看归档日志路径是USE_DB_RECOVERY_FILE_DEST (默认路径), 安装的时候没有做 ...

  5. Java字段初始化规律

    首先先附上一段代码:public class InitializeBlockDemo { public static void main(String[] args) { InitializeBloc ...

  6. 子网掩码!如何划分子网掩码,计算IP地址

    作者:chli1806 一.子网掩码的含义和根据子网掩码划分子网一个IP地址必然属于某一个网络,或者叫子网.子网掩码就是用来指定某个IP地址的网络地址的,换一句话说,就是用来划分子网的.例如,一个A类 ...

  7. gitlab 提交

    gitlab 提交 Git global setup git config --global user.name "lial" git config --global user.e ...

  8. MFC::使用mysql

    下载mysql-installer-community-5.7.16.0.msi,安装 mysql server即可. 创建工程包含头文件 #include "winsock.h" ...

  9. WPF使用border画框

    以前的界面中使用的框大都是由美工做好的,但是这样就遇到几个问题: 框只是换一个颜色,就需要多做出一张图,资源包中也要多一个图片资源: 文字的数量会改变,用一张固定的图进行拉伸,边角处会变得越来越不尽如 ...

  10. GO实现简单(命令行)工具:sftp,文檔压解,RDS备份,RDS备份下载

    GO实现简单(命令行)工具:sftp,文檔压解,RDS备份,RDS备份下载 轉載請註明出處:https://www.cnblogs.com/funnyzpc/p/11721978.html 内容提要: ...