zoukankan      html  css  js  c++  java
  • Python-OpenCV 图像叠加加权实现

    函数说明

    cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]]) → dst
    • 1

    参数说明

    • src1 – first input array.
    • alpha – weight of the first array elements.
    • src2 – second input array of the same size and channel number as src1.
    • beta – weight of the second array elements.
    • dst – output array that has the same size and number of channels as the input arrays.
    • gamma – scalar added to each sum.
    • dtype – optional depth of the output array; when both input arrays have the same depth, dtype can be set to -1, which will be equivalent to src1.depth().

    此函数可以用一下矩阵表达式来代替:

    dst = src1 * alpha + src2 * beta + gamma;

    注意:由参数说明可以看出,被叠加的两幅图像必须是尺寸相同、类型相同的;并且,当输出图像array的深度为CV_32S时,这个函数就不适用了,这时候就会内存溢出或者算出的结果压根不对。

    CV_32S is a signed 32bit integer value for each pixel

    python代码实现:

    # coding=utf-8
    
    import cv2
    
    # 底板图案
    bottom_pic = 'girl1.jpg'
    # 上层图案
    top_pic = 'girl2.jpg'
    
    bottom = cv2.imread(bottom_pic)
    top = cv2.imread(top_pic)
    
    h, w, _ = bottom.shape
    
    img2 = cv2.resize(top, (w,h), interpolation=cv2.INTER_AREA)
    
    #alpha,beta,gamma可调
    alpha = 0.7
    beta = 1-alpha
    gamma = 0
    
    # 权重越大,透明度越低
    overlapping = cv2.addWeighted(bottom, alpha, img2, beta, gamma)
    #cv2.addWeighted(bottom, 0.5, img2, 0.5, 0)
    #overlapping = cv2.addWeighted(bottom,0.8,top,0.2,0)
    # 保存叠加后的图片
    cv2.imwrite('overlap(8:2).jpg', overlapping)
    
    cv2.namedWindow('newImage')
    cv2.imshow('newImage',overlapping)
    cv2.waitKey()
    cv2.destroyAllWindows()
  • 相关阅读:
    获取一个日期月的所有日期
    elementUI 日期 周一、周二、周三、周四、周五、周六、周日快捷键
    两个日期之间的天数集合
    数组去重并排序
    日历封装
    <转载> VUE项目中CSS管理
    【转载】Vue项目自动转换 px 为 rem,高保真还原设计图
    HTML5新标签
    pChart图表插件使用
    JavaScript ajax请求
  • 原文地址:https://www.cnblogs.com/dylancao/p/9529881.html
Copyright © 2011-2022 走看看