Python 将二维数组插入到另一个二维数组中,同时考虑值

Python 将二维数组插入到另一个二维数组中,同时考虑值,python,arrays,numpy,opencv,Python,Arrays,Numpy,Opencv,我试图在特定坐标上将一张图片(transparent.png)插入另一张图片。 而解决方案来自 frame[y:y+insert\u size[1],x:x+insert\u size[0]]=image(其中insert\u size-插入图片的宽度和高度)起作用,我也不希望最终图像上出现黑色像素(opencv就是这样表示透明像素的) 我编写了一个逐像素迭代的函数,虽然它可以工作,但速度非常慢(每秒大约完成2次图像插入),代码: 在中找到了一个解决方案,它比我的速度快了大约20倍。代码: im

我试图在特定坐标上将一张图片(transparent.png)插入另一张图片。 而解决方案来自
frame[y:y+insert\u size[1],x:x+insert\u size[0]]=image
(其中insert\u size-插入图片的宽度和高度)起作用,我也不希望最终图像上出现黑色像素(opencv就是这样表示透明像素的)

我编写了一个逐像素迭代的函数,虽然它可以工作,但速度非常慢(每秒大约完成2次图像插入),代码:

在中找到了一个解决方案,它比我的速度快了大约20倍。代码:

import cv2
import numpy as np
import time


def insert_image_v2(frame, image, insert_coordinates):
    x = insert_coordinates[0]
    y = insert_coordinates[1]
    insert_size = (image.shape[1], image.shape[0])
    background = frame[y: y+insert_size[1], x: x+insert_size[0]]
    foreground = image
    kernel = np.ones((5,5), np.uint8)
    image_gray = cv2.cvtColor(foreground, cv2.COLOR_BGR2GRAY)
    ret, mask = cv2.threshold(image_gray, 1, 255, cv2.THRESH_BINARY_INV)
    opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
    output = np.zeros(foreground.shape, dtype=foreground.dtype)
    for i in range(3):
        output[:,:,i] = background[:,:,i] * (opening/255)+foreground[:,:,i]*(1-opening/255)
    frame[y: y+insert_size[1], x: x+insert_size[0]] = output
    return frame


if __name__ == "__main__":
    frame = cv2.imread('frame.png')
    image = cv2.imread('image_1.png')
    insert_size = (image.shape[0], image.shape[1])
    insert_coordinates = (100, 100)
    t1 = time.time()
    frame = insert_image_v2(frame, image, insert_coordinates)
    t2 = time.time()
    print(f"{t2-t1}")
    cv2.imshow('img', frame)
    cv2.waitKey(0)
在中找到了一个解决方案,它比我的速度快了大约20倍。代码:

import cv2
import numpy as np
import time


def insert_image_v2(frame, image, insert_coordinates):
    x = insert_coordinates[0]
    y = insert_coordinates[1]
    insert_size = (image.shape[1], image.shape[0])
    background = frame[y: y+insert_size[1], x: x+insert_size[0]]
    foreground = image
    kernel = np.ones((5,5), np.uint8)
    image_gray = cv2.cvtColor(foreground, cv2.COLOR_BGR2GRAY)
    ret, mask = cv2.threshold(image_gray, 1, 255, cv2.THRESH_BINARY_INV)
    opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
    output = np.zeros(foreground.shape, dtype=foreground.dtype)
    for i in range(3):
        output[:,:,i] = background[:,:,i] * (opening/255)+foreground[:,:,i]*(1-opening/255)
    frame[y: y+insert_size[1], x: x+insert_size[0]] = output
    return frame


if __name__ == "__main__":
    frame = cv2.imread('frame.png')
    image = cv2.imread('image_1.png')
    insert_size = (image.shape[0], image.shape[1])
    insert_coordinates = (100, 100)
    t1 = time.time()
    frame = insert_image_v2(frame, image, insert_coordinates)
    t2 = time.time()
    print(f"{t2-t1}")
    cv2.imshow('img', frame)
    cv2.waitKey(0)

需要说明的是,
insert
指的是将
image
中的一个像素值分配给
帧的某个像素?因此,条件动作是
frame[y,x]=image[y\u diff,x\u diff]
。我这样问是因为有时候
insert
意味着扩展数组的大小(例如
np.insert
)。请发布您的数据。我想你在暗示一些事情,但我不想猜测。还要更详细地描述目标。或者更好的是,用图片来表达你的意思。试着把它变成一个例子。我添加了数据和代码示例,也回答了@hpaulj问题-通过“插入”我的意思是将图像中的像素值分配给帧的某个像素只是为了清楚,通过
insert
你的意思是将
图像中的像素值分配给
帧的某个像素?因此,条件动作是
frame[y,x]=image[y\u diff,x\u diff]
。我这样问是因为有时候
insert
意味着扩展数组的大小(例如
np.insert
)。请发布您的数据。我想你在暗示一些事情,但我不想猜测。还要更详细地描述目标。或者更好的方法是,用图片来表达你的意思。试着让它成为一个例子。我添加了数据和代码示例,还回答了@hpaulj问题-通过“插入”,我的意思是将图像中的像素值指定给帧中的某个像素
import cv2
import numpy as np
import time


def insert_image_v2(frame, image, insert_coordinates):
    x = insert_coordinates[0]
    y = insert_coordinates[1]
    insert_size = (image.shape[1], image.shape[0])
    background = frame[y: y+insert_size[1], x: x+insert_size[0]]
    foreground = image
    kernel = np.ones((5,5), np.uint8)
    image_gray = cv2.cvtColor(foreground, cv2.COLOR_BGR2GRAY)
    ret, mask = cv2.threshold(image_gray, 1, 255, cv2.THRESH_BINARY_INV)
    opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
    output = np.zeros(foreground.shape, dtype=foreground.dtype)
    for i in range(3):
        output[:,:,i] = background[:,:,i] * (opening/255)+foreground[:,:,i]*(1-opening/255)
    frame[y: y+insert_size[1], x: x+insert_size[0]] = output
    return frame


if __name__ == "__main__":
    frame = cv2.imread('frame.png')
    image = cv2.imread('image_1.png')
    insert_size = (image.shape[0], image.shape[1])
    insert_coordinates = (100, 100)
    t1 = time.time()
    frame = insert_image_v2(frame, image, insert_coordinates)
    t2 = time.time()
    print(f"{t2-t1}")
    cv2.imshow('img', frame)
    cv2.waitKey(0)