20190730_图像混合_opencv_python

今天学习了 图像的混合

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

教程名称:OpenCV-Python 中文教程

  1 #图像融合
  2 import cv2
  3 import numpy as np
  4 import matplotlib.pyplot as plt
  5 
  6 imgO = cv2.imread(r"C:\Users\lijin\Desktop\opencvImg\opencvSource\orange.png")
  7 imgA = cv2.imread(r"C:\Users\lijin\Desktop\opencvImg\opencvSource\apple.png")
  8 
  9 cv2.imshow('apple', imgA)
 10 cv2.imshow('orange', imgO)
 11 
 12 print(imgO.shape) #高y--rows, 宽x--cols
 13 print(imgA.shape)
 14 
 15 imgO = cv2.resize(imgO, (300, 300))
 16 imgA = cv2.resize(imgA, (300, 300))  #居然在这里错了,浪费了一下午
 17 
 18 print(imgO.shape) #高y--rows, 宽x--cols
 19 print(imgA.shape)
 20 
 21 #Orange Operation
 22 #gaussian pyramid list for Orange
 23 gOperator = imgO.copy()
 24 gpO = [gOperator]
 25 for i in range(6):
 26     gOperator = cv2.pyrDown(gOperator)
 27     #cv2.imshow(test[i], gOperator)
 28     gpO.append(gOperator)  #add to list
 29     #cv2.imshow(testx[i], gpO[i])
 30 
 31 #laplacian pyramid list for Orange
 32 lpO = [gpO[6]]
 33 for i in range(6, 0, -1):
 34     lOperator = cv2.pyrUp(gpO[i]) #this operator is for gpO[i -1]
 35     #print(lOperator.shape)
 36     #print(gpO[i - 1].shape)
 37     rows, cols = gpO[i - 1].shape[:2]
 38     
 39     #必须要加这一步,使得尺寸相等
 40     lOperator = cv2.resize(lOperator, (cols, rows))
 41     
 42     lOperator = cv2.subtract(gpO[i - 1], lOperator)
 43     lpO.append(lOperator)  #add to list
 44     
 45 
 46 #Apple Operation
 47 #gaussian pyramid list for Apple
 48 gOperator = imgA.copy()
 49 gpA = [gOperator]
 50 for i in range(6):
 51     gOperator = cv2.pyrDown(gOperator)
 52     gpA.append(gOperator)  #add to list
 53     
 54   
 55 #laplacian pyramid list for Apple
 56 lpA = [gpA[6]]
 57 for i in range(6, 0, -1):
 58     lOperator = cv2.pyrUp(gpA[i]) #this operator is for gpO[i -1]
 59     
 60     rows, cols = gpA[i - 1].shape[:2]
 61     
 62     #必须要加这一步,使得尺寸相等
 63     lOperator = cv2.resize(lOperator, (cols, rows))
 64     
 65     lOperator = cv2.subtract(gpA[i - 1], lOperator)
 66     lpA.append(lOperator)   #add to list
 67 
 68 for i in range(7):
 69     print('lpO size: ', lpO[i].shape)
 70     print('lpA size: ', lpA[i].shape)
 71     
 72     
 73 #add left part of Apple and right part of Orange in each level
 74 #numpy.hstack(tup)
 75 #take a sequence of arrays and stack them horizontally
 76 #to make a single array
 77 combinedList = []
 78 for appleLeft, orangeRight in zip(lpA, lpO):
 79     rows, cols, dpt = appleLeft.shape
 80     
 81     #cv2.imshow('appleLeft ', appleLeft)
 82     #cv2.imshow('orangeRight ', orangeRight)
 83     #print('appleLeft.shape ', appleLeft.shape)
 84     #print('orangeRight.shape ', orangeRight.shape)
 85 
 86     combineElement = np.hstack((appleLeft[:, 0:cols//2], orangeRight[:, cols//2:])) #0:cols//2 不包括右边,cols//2: 包括左边
 87     combinedList.append(combineElement)
 88     print('appleLeft.shape   ',appleLeft.shape)
 89     print('orangeRight.shape ',orangeRight.shape)
 90     print('combineElement.shape', combineElement.shape)
 91     
 92 print('combinedList[1].shape ', combinedList[1].shape)
 93 print('combinedList[2].shape ', combinedList[2].shape)
 94 #"""
 95 #reconstruct
 96 combinedElement_ = combinedList[0]  #从 a (5, 5, 3),o (5, 5, 3) 开始
 97 for i in range(1, 6):   #combinedList[1].shape  (10, 10, 3)
 98     combinedElement_ = cv2.pyrUp(combinedElement_)   #combinedList[1].shape  (10, 10, 3) #combinedList[2].shape  (19, 19, 3)
 99     #print('combinedElement_.shape...', combinedElement_.shape)
100     #print('combinedList.shape...', combinedList[i].shape)
101     rows, cols = combinedList[i].shape[:2]
102     combinedElement_ = cv2.resize(combinedElement_,(cols, rows))
103     combinedElement_ = cv2.add(combinedElement_, combinedList[i])  #combinedList[1].shape  (10, 10, 3) 
104   
105 #reconstruct
106 pyramidBlending = combinedList[0]  
107 for i in range(1, 7):
108     pyramidBlending = cv2.pyrUp(pyramidBlending)
109     
110     rows, cols = combinedList[i].shape[:2]
111     pyramidBlending = cv2.resize(pyramidBlending,(cols, rows))
112     
113     pyramidBlending = cv2.add(pyramidBlending, combinedList[i])
114     
115 #image with direct cnnecting each half
116 cv2.imshow('appleL', imgA[:, : cols // 2])
117 cv2.imshow('orangeR', imgO[ : , cols // 2 : ])
118 directBlending = np.hstack((imgA[:, : cols // 2], imgO[:, cols // 2 : ]))
119 
120 cv2.imshow("directBlending", directBlending)
121 cv2.imshow("pyramidBlending", pyramidBlending)
122 
123 #"""
124     
125     
126 cv2.waitKey(0)
127 cv2.destroyAllWindows()
View Code

 

在调试代码的时候,需要不停的使用 resize() 函数在对两个生成图像做加减法时进行调整,不然会出现两图大小不一而无法运算的情况。

 

比如,如果没有这段代码

 

会出现的错误提示

20190730_图像混合_opencv_python_第1张图片

 

所以要不停的使用 cv2.imshow(), img.shape 进行调试

这是最终的运行结果

20190730_图像混合_opencv_python_第2张图片

转载于:https://www.cnblogs.com/codebeef0911/p/11271577.html

你可能感兴趣的:(人工智能,python)