装环境好累,python3.6,opencv3.4
好不容易装好了,结果
addweight的时候总是报错
The operation is neither ‘array op array‘ (where arrays have the same size and the same number of channels), nor ‘array op scalar‘, nor ‘scalar op array‘ in function cv::arithm_op
开始看到有人说时图片大小不一致的原因我还不信,因为我第二张图是基于第一张180度翻转得到的,我寻思着肯定一样大吧
然后print一下shape
print(img1.shape, img2.shape)
吐血
期间我换成py2.7,重装了一次opencv2.4版本
于是总算懂得resize一下
1 import cv2 2 3 #加载图片 4 img1 = cv2.imread(‘1.jpg‘) 5 img2 = cv2.imread(‘2.jpg‘) 6 7 #求img1的height 和 wide 8 h, w, _ = img1.shape 9 #这里打印出来检测一下是否size不同 10 print(img1.shape, img2.shape) 11 12 #将img2进行缩放,注意这里的resize中(顺序是宽高)和shape出来的不一样 13 img2 = cv2.resize(img2, (w,h), interpolation=cv2.INTER_AREA) 14 15 #设定权重 16 alpha = 0.5 17 beta = 1-alpha 18 gamma = 0 19 20 #开始叠加 21 img_add = cv2.addWeighted(img1, alpha, img2, beta, gamma) 22 23 #创建一个名为‘add’的窗口用来展示 24 cv2.imshow(‘add‘,img_add) 25 26 #等待键入,按任意键退出全部窗口 27 cv2.waitKey(0) 28 cv2.destroyAllWindows()
原图是:
官方教程是这样的:
img1 = cv2.imread(‘ml.png‘) img2 = cv2.imread(‘opencv_logo.jpg‘) dst = cv2.addWeighted(img1,0.7,img2,0.3,0) cv2.imshow(‘dst‘,dst) cv2.waitKey(0) cv2.destroyAllWindows()
然而人家的俩图尺寸一毛一样
修改后应该是这样
import cv2 img1 = cv2.imread("1.jpg") img2 = cv2.imread("2.jpg") h, w, _ = img1.shape img2=cv2.resize(img2,(w,h),interpolation=cv2.INTER_AREA) dst = cv2.addWeighted(img1,0.7,img2,0.3,0) cv2.imshow("dst",dst) cv2.waitKey(0) cv2.destroyAllWindows()