今天学习了 图像的混合

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

教程名称: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. 不吹不黑,今天我们来聊一聊 Kubernetes 落地的三种方式

    作者 | 王国梁  Kubernetes 社区成员与项目维护者原文标题<Kubernetes 应用之道:让 Kubernetes落地的"三板斧">,首发于知乎专栏:进击 ...

  2. ServiceStack.Redis高效封装和简易破解

    1.ServiceStack.Redis封装 封装的Redis操作类名为RedisHandle,如下代码块(只展示部分代码),它的特点: 1)使用连接池管理连接,见代码中的PooledClientMa ...

  3. VirtualBox 启动时提示“获取 VirtualBox COM 对象失败”的解决

    昨天给电脑打了一堆补丁和更新,今天启动 VirtualBox 的时候提示 “获取 VirtualBox COM 对象失败”,好在百度到了 CSDN 上的一篇文章解决了这个问题. 错误详情 “获取 Vi ...

  4. Spring Cloud Feign 性能优化

    #### 1.替换 tomcat 首先,把 tomcat 换成 undertow,这个性能在 Jmeter 的压测下,undertow 比 tomcat 高一倍 **第一步,pom 修改去除tomca ...

  5. 02-13 Softmax回归

    目录 Softmax回归 一.Softmax回归详解 1.1 让步比 1.2 不同类之间的概率分布 1.3 目标函数 1.4 目标函数最大化 二.Softmax回归优缺点 2.1 优点 2.2 缺点 ...

  6. C#通过对象属性名修改值

    摘自:csdn 给一个对象属性赋值可以通过PropertyInfo.SetValue()方式进行赋值,但要注意值的类型要与属性保持一致.    创建对象实例的两种方法: 1. var obj = As ...

  7. Maven插件构建Docker镜像

    背景 微服务架构下,微服务在带来良好的设计和架构理念的同时,也带来了运维上的额外复杂性,尤其是在服务部署和服务监控上.单体应用是集中式的,就一个单体跑在一起,部署和管理的时候非常简单,而微服务是一个网 ...

  8. ubuntu使用uwsgi+nginx部署django

    ls -lha export WORKON_HOME=~/venv source /usr/local/bin/vitualenvwrapper.sh VIRTUALENVWRAPPER_PYTHON ...

  9. Python基础库之jieba库的使用(第三方中文词汇函数库)

    各位学python的朋友,是否也曾遇到过这样的问题,举个例子如下: “I am proud of my motherland” 如果我们需要提取中间的单词要走如何做? 自然是调用string中的spl ...

  10. 《深入理解Java虚拟机》-----第13章 线程安全与锁优化

    概述 在软件业发展的初期,程序编写都是以算法为核心的,程序员会把数据和过程分别作为独立的部分来考虑,数据代表问题空间中的客体,程序代码则用于处理这些数据,这种思维方式直接站在计算机的角度去抽象问题和解 ...