簡體   English   中英

疊加不同尺寸且沒有通道的圖像

[英]Overlay images of different size and no of channels

我正在嘗試使用OpenCV和Python覆蓋隨機圖像(自然場景圖像應與符號圖像覆蓋)。 它們的大小,文件擴展名可以不同。 渠道(我想還有更多)。 因此,我要根據自然場景圖像的大小調整標志圖像的大小,然后將它們放在后者上。

我已經實現了在這里找到的fireant的代碼: 在較大的圖像python OpenCv上覆蓋較小的圖像

但是它僅適用於具有4個通道的圖像。

使用cv2.addWeighted()總是將較大的圖像(場景圖像)裁剪為較小的圖像(符號圖像)的大小。 有誰知道怎么做嗎? 非常感謝您的幫助。

編輯:請參閱下面的預期輸出。 首先,逃生路線標志和背景是分開的圖像。 預期產量

這是我的代碼,可以正常工作,但是由於我的很多圖像似乎只有3個通道,因此我也希望它也可以正常工作。

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提示以及如何從圖像中裁剪或去除白色背景 ,我制定了解決方案。 以下代碼將首先從較小的圖像中刪除白色背景,然后將所有圖像設置為4個通道,然后將較大的圖像與較小的圖像重疊。 為我工作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM