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, ...
随机推荐
- .Net Core快速创建Windows服务
1.新建.Net Core控制台程序,添加新建项Windows服务: NuGet引用 System.ServiceProcess.ServiceController,然后修改Progran.cs: c ...
- mybatis - 通用mapper
title: 玩转spring-boot-mybatis date: 2019-03-11 19:36:57 type: "mybatis" categories: mybatis ...
- dedecms新增联动类别后的使用方法
近期用织梦的联动类别,后台明明可以直接新增联动类别,但是你直接调用是绝对调用不出来的............. 折腾了好几天终于全部解决,回忆下过程以便日后再遇到的时候参考. 第一步:先按照常规的在后 ...
- Cocos Creator 3D 打砖块图文教程(一)
在线体验链接: http://example.creator-star.cn/block3d/ 上面图中是打砖块游戏的主要 3D 节点元素,Shawn 这两天在学习 Unity 与 Creator3D ...
- ELK 学习笔记之 Logstash基本语法
Logstash基本语法: 处理输入的input 处理过滤的filter 处理输出的output 区域 数据类型 条件判断 字段引用 区域: Logstash中,是用{}来定义区域 区域内,可以定义插 ...
- ELK 学习笔记之 elasticsearch Bulk操作
Bulk操作: Bulk操作用于批量插入数据: 请求体格式: 编辑一个文件:(插入2个新的文档) curl -XPOST 'http://192.168.1.151:9200/library/book ...
- 【Java】后台将文件上传至远程服务器
问题:由于系统在局域网(能访问外网)内,但外网无法请求局域网内服务器文件和进行处理文件. 解决:建立文件服务器,用于存储文件及外网调用. 客户端(文件上传): package cn.hkwl.lm.u ...
- inkscape 无法打开文档属性
从文件->文档属性 点击了无反应 参考https://bugs.launchpad.net/inkscape/+bug/1664031 其实不是无反应,只是因为我们自己的某些操作,让文档属性这个 ...
- SQL提高查询效率的几点建议
1.如果要用子查询,那就用EXISTS替代IN.用NOT EXISTS替代NOT IN.因为EXISTS引入的子查询只是测试是否存在符合子查询中指定条件的行,效率较高.无论在哪种情况下,NOT IN都 ...
- 【干货系列之萌新知识点】python与变量和运算符
一.注释 注释一行:# 为注释符 注释多行:'或者"为注释符 二.print输出 print()函数,作用是打印一些信息语屏幕上. 例如:print("hello world!&q ...