在较大的图像python OpenCv上覆盖较小的图像

嗨,我正在创建一个程序,用别人的脸代替图像中的脸。但是,我一直试图将新面孔插入原始的较大图像中。我研究了ROI和addWeight(需要图像大小相同),但是我没有找到在python中执行此操作的方法。任何建议都很棒。我是opencv的新手。

我正在使用以下测试图像:

较小的图片:

大图:

到目前为止,这是我的代码...其他示例的混合器:


import cv2

import cv2.cv as cv

import sys

import numpy


def detect(img, cascade):

    rects = cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=3, minSize=(10, 10), flags = cv.CV_HAAR_SCALE_IMAGE)

    if len(rects) == 0:

        return []

    rects[:,2:] += rects[:,:2]

    return rects


def draw_rects(img, rects, color):

    for x1, y1, x2, y2 in rects:

        cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)


if __name__ == '__main__':

    if len(sys.argv) != 2:                                         ## Check for error in usage syntax


    print "Usage : python faces.py <image_file>"


else:

    img = cv2.imread(sys.argv[1],cv2.CV_LOAD_IMAGE_COLOR)  ## Read image file


    if (img == None):                                     

        print "Could not open or find the image"

    else:

        cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")

        gray = cv2.cvtColor(img, cv.CV_BGR2GRAY)

        gray = cv2.equalizeHist(gray)


        rects = detect(gray, cascade)


        ## Extract face coordinates         

        x1 = rects[0][3]

        y1 = rects[0][0]

        x2 = rects[0][4]

        y2 = rects[0][5]

        y=y2-y1

        x=x2-x1

        ## Extract face ROI

        faceROI = gray[x1:x2, y1:y2]


        ## Show face ROI

        cv2.imshow('Display face ROI', faceROI)

        small = cv2.imread("average_face.png",cv2.CV_LOAD_IMAGE_COLOR)  

        print "here"

        small=cv2.resize(small, (x, y))

        cv2.namedWindow('Display image')          ## create window for display

        cv2.imshow('Display image', small)          ## Show image in the window


        print "size of image: ", img.shape        ## print size of image

        cv2.waitKey(1000)              


森栏
浏览 2058回答 3
3回答

湖上湖

根据以上fireant的出色回答,此处是alpha混合,但更易于理解。您可能需要进行交换,1.0-alpha并alpha取决于您要合并的方向(地雷从消防人员的答复中被交换)。o* == s_img.* b* == b_img.*for c in range(0,3):&nbsp; &nbsp; alpha = s_img[oy:oy+height, ox:ox+width, 3] / 255.0&nbsp; &nbsp; color = s_img[oy:oy+height, ox:ox+width, c] * (1.0-alpha)&nbsp; &nbsp; beta&nbsp; = l_img[by:by+height, bx:bx+width, c] * (alpha)&nbsp; &nbsp; l_img[by:by+height, bx:bx+width, c] = color + beta

料青山看我应如是

一种实现所需目标的简单方法:import cv2s_img = cv2.imread("smaller_image.png")l_img = cv2.imread("larger_image.jpg")x_offset=y_offset=50l_img[y_offset:y_offset+s_img.shape[0], x_offset:x_offset+s_img.shape[1]] = s_img结果图像更新资料我想您也要注意alpha通道。这是一种快速而肮脏的方法:s_img = cv2.imread("smaller_image.png", -1)y1, y2 = y_offset, y_offset + s_img.shape[0]x1, x2 = x_offset, x_offset + s_img.shape[1]alpha_s = s_img[:, :, 3] / 255.0alpha_l = 1.0 - alpha_sfor c in range(0, 3):&nbsp; &nbsp; l_img[y1:y2, x1:x2, c] = (alpha_s * s_img[:, :, c] +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alpha_l * l_img[y1:y2, x1:x2, c])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python