gpt4 book ai didi

python - 叠加不同大小和 channel 数的图像

转载 作者:行者123 更新时间:2023-12-02 16:21:10 25 4
gpt4 key购买 nike

我正在尝试使用 OpenCV 和 Python 叠加随机图像(自然场景图像应该与标志图像叠加)。它们的大小、文件扩展名和编号可能不同。 channel (我猜还有更多)。所以我根据自然场景图像的大小调整标志图像的大小并将它们放到后者上。

我已经实现了在这里找到的 fireant 代码:overlay a smaller image on a larger image python OpenCv

但它仅适用于具有 4 个 channel 的图像。

使用 cv2.addWeighted() 总是将较大的图像(场景图像)裁剪为较小图像(标志图像)的大小。有人知道怎么做吗?高度赞赏帮助。

编辑:请参阅下面的预期输出。起初,逃生路线标志和背景是分开的图像。
Expected output

这是我的代码,它正在工作,但由于我的很多图像似乎只有 3 个 channel ,我想让它也适用于这些 channel 。

import cv2
import time
import math
import os

pathSigns = "/home/moritz/Schreibtisch/Signs"
pathScenes = "/home/moritz/Schreibtisch/Scenes"
i = 0

for fSigns in os.listdir(pathSigns):
fSigns = os.path.join(pathSigns, fSigns)
s_img = cv2.imread(fSigns, -1)

for fScenes in os.listdir(pathScenes):
try:
l_img = cv2.imread(os.path.join(pathScenes, fScenes))
l_height, l_width, l_channels = l_img.shape

TARGET_PIXEL_AREA = (l_height * l_width) * 0.05

ratio = float(s_img.shape[1]) / float(s_img.shape[0])
s_new_h = int(math.sqrt(TARGET_PIXEL_AREA / ratio) + 0.5)
s_new_w = int((s_new_h * ratio) + 0.5)

s_img = cv2.resize(s_img,(s_new_w, s_new_h))

x_offset=y_offset=50
# l_img[y_offset:y_offset+s_img.shape[0],
x_offset:x_offset+s_img.shape[1]] = s_img

y1, y2 = y_offset, y_offset + s_img.shape[0]
x1, x2 = x_offset, x_offset + s_img.shape[1]

height, width, channels = s_img.shape

if channels <= 3:
alpha_s = s_img[:, :, 2] / 255.0
alpha_l = 1.0 - alpha_s
else:
alpha_s = s_img[:, :, 3] / 255.0
alpha_l = 1.0 - alpha_s

for c in range(0, 3):
l_img[y1:y2, x1:x2, c] = (alpha_s * s_img[:, :, c] +
alpha_l * l_img[y1:y2, x1:x2, c])

fResult = "/home/moritz/Schreibtisch/results/data_" + str(i) +
".png"
i += 1
cv2.imwrite(fResult, l_img)
except IndexError:
pass

最佳答案

感谢@DanMašek 提示和How to crop or remove white background from an image ,我已经制定了解决方案。以下代码将首先从较小的图像中删除白色背景,然后将所有图像设置为 4 个 channel ,然后用较小的图像覆盖较大的图像。为我工作。

import cv2
import time
import math
import os
import numpy as np

pathSigns = "/home/moritz/Schreibtisch/Signs"
pathScenes = "/home/moritz/Schreibtisch/Scenes"
i = 0

for fSigns in os.listdir(pathSigns):
fSigns = os.path.join(pathSigns, fSigns)
s_img = cv2.imread(fSigns, -1)
s_height, s_width, s_channels = s_img.shape

# crop image
gray = cv2.cvtColor(s_img, cv2.COLOR_BGR2GRAY)
th, threshed = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11))
morphed = cv2.morphologyEx(threshed, cv2.MORPH_CLOSE, kernel)

_, cnts, _ = cv2.findContours(morphed, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnt = sorted(cnts, key=cv2.contourArea)[-1]
x,y,w,h = cv2.boundingRect(cnt)
s_img = s_img[y:y+h, x:x+w]

# set channels to 4
if s_channels < 4:
s_img = cv2.cvtColor(s_img, cv2.COLOR_BGR2BGRA)

for fScenes in os.listdir(pathScenes):
try:
l_img = cv2.imread(os.path.join(pathScenes, fScenes))
l_height, l_width, l_channels = l_img.shape

if l_channels < 4:
l_img = cv2.cvtColor(l_img, cv2.COLOR_BGR2BGRA)

TARGET_PIXEL_AREA = (l_height * l_width) * 0.05

ratio = float(s_img.shape[1]) / float(s_img.shape[0])
s_new_h = int(math.sqrt(TARGET_PIXEL_AREA / ratio) + 0.5)
s_new_w = int((s_new_h * ratio) + 0.5)

s_img = cv2.resize(s_img,(s_new_w, s_new_h))

x_offset=y_offset=50

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.0
alpha_l = 1.0 - alpha_s

for c in range(0, 3):
l_img[y1:y2, x1:x2, c] = (alpha_s * s_img[:, :, c] + alpha_l *
l_img[y1:y2, x1:x2, c])

fResult = "/home/moritz/Schreibtisch/results/data_" + str(i) + ".png"
i += 1
cv2.imwrite(fResult, l_img)
except IndexError:
pass

关于python - 叠加不同大小和 channel 数的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53451749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com