如何使用多张图片制作一张图片?

yak*_*hyo 5 python opencv

我有这些图像,所有图像中都有阴影。我的目标是使用这三个图像制作没有阴影的汽车的单个图像:

在此输入图像描述

最后,如何才能得到如下所示的这种图像:

在此输入图像描述

任何形式的帮助或建议表示赞赏。

编辑

根据评论,我使用np.maximum并轻松实现了我的目标:

import cv2
import numpy as np

img_1 = cv2.imread('1.png', cv2.COLOR_BGR2RGB)
img_2 = cv2.imread('2.png', cv2.COLOR_BGR2RGB)

img = np.maximum(img_1, img_2)

cv2.imshow('img1', img_1)
cv2.imshow('img2', img_2)

cv2.imshow('img', img)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

sta*_*ine 1

这是一个可能的解决方案。总体思路是计算阴影的位置,生成识别阴影位置的二进制掩模,并使用此信息从所有裁剪的子图像中复制像素。

让我们看看代码。第一个问题是定位这三个图像。我使用黑匣子来分割和裁剪每辆车,如下所示:

# Imports:
import cv2
import numpy as np

# image path
path = "D://opencvImages//"
fileName = "qRLI7.png"

# Reading an image in default mode:
inputImage = cv2.imread(path + fileName)

# Get the HSV image:
hsvImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2HSV)

# Get the grayscale image:
grayImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
showImage("grayImage", grayImage)

# Threshold via Otsu:
_, binaryImage = cv2.threshold(grayImage, 5, 255, cv2.THRESH_BINARY_INV)

cv2.imshow("binaryImage", binaryImage)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

前一位使用grayscale图像的版本并使用阈值应用固定二值化5。我还预先计算了HSV原始图像的版本。阈值化的结果是这样的:

我正在尝试获取黑色矩形并使用它们来裁剪每辆车。让我们获取轮廓并按面积过滤它们,因为二值图像上的黑色矩形面积最大:

for i, c in enumerate(currentContour):

    # Get the contour's bounding rectangle:
    boundRect = cv2.boundingRect(c)

    # Get the dimensions of the bounding rect:
    rectX = boundRect[0]
    rectY = boundRect[1]
    rectWidth = boundRect[2]
    rectHeight = boundRect[3]

    # Get the area:
    blobArea = rectWidth * rectHeight
    minArea = 20000

    if blobArea > minArea:

        # Deep local copies:
        hsvImage = hsvImage.copy()
        localImage = inputImage.copy()

        # Get the S channel from the HSV image:
        (H, S, V) = cv2.split(hsvImage)

        # Crop image:
        croppedImage = V[rectY:rectY + rectHeight, rectX:rectX + rectWidth]
        localImage = localImage[rectY:rectY + rectHeight, rectX:rectX + rectWidth]

        _, binaryMask = cv2.threshold(croppedImage, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)
Run Code Online (Sandbox Code Playgroud)

过滤每个轮廓以获得最大的轮廓后,我需要找到阴影的位置。阴影在HSV色彩空间中大部分可见,特别是在V通道中。我裁剪了图像的两个版本:BGR现在裁剪的原始图像和图像V的裁剪通道HSV。这是在通道上应用自动阈值处理产生的二进制掩码S

要定位阴影,我只需要起始x坐标及其width,因为每个裁剪图像上的阴影都是均匀的。它height等于每个裁剪图像的高度。我V使用该模式将图像缩小为一行SUM。这将对所有列中的每个像素求和。最大值将对应于阴影的位置:

        # Image reduction:
        reducedImg = cv2.reduce(binaryMask, 0, cv2.REDUCE_SUM, dtype=cv2.CV_32S)
    
        # Normalize image:
        max = np.max(reducedImg)
        reducedImg = reducedImg / max

        # Clip the values to [0,255]
        reducedImg = np.clip((255 * reducedImg), 0, 255)

        # Convert the mat type from float to uint8:
        reducedImg = reducedImg.astype("uint8")

        _, shadowMask = cv2.threshold(reducedImg, 250, 255, cv2.THRESH_BINARY)
Run Code Online (Sandbox Code Playgroud)

缩小后的图像只是一行:

白色像素表示最大值。阴影的位置画成一条面积最大的水平线,即最连续的白色像素。我通过获取轮廓并再次过滤到最大区域来处理这一行:

        # Get the biggest rectangle:
        subContour, _ = cv2.findContours(shadowMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        for j, s in enumerate(subContour):

            # Get the contour's bounding rectangle:
            boundRect = cv2.boundingRect(s)

            # Get the dimensions of the bounding rect:
            rectX = boundRect[0]
            rectY = boundRect[1]
            rectWidth = boundRect[2]
            rectHeight = boundRect[3]

            # Get the area:
            blobArea = rectWidth * rectHeight
            minArea = 30

            if blobArea > minArea:

                # Get image dimensions:
                (imageHeight, imageWidth) = localImage.shape[:2]

                # Set an empty array, this will be the binary mask
                shadowMask = np.zeros((imageHeight, imageWidth, 3), np.uint8)
                color = (255, 255, 255)
                cv2.rectangle(shadowMask, (int(rectX), int(0)),
                          (int(rectX + rectWidth), int(0 + imageHeight)), color, -1)
                # Invert mask:
                shadowMask = 255 - shadowMask

                # Store mask and cropped image:
                shadowRois.append((shadowMask.copy(), localImage.copy()))
Run Code Online (Sandbox Code Playgroud)

好吧,根据这些信息,我创建了一个蒙版,其中唯一用白色绘制的就是蒙版的位置。我将这个蒙版和原始BGR裁剪存储在shadowRois列表中。

以下是使用此信息并创建完整图像的可能方法。我的想法是,我使用每个掩模的信息来复制所有非掩模像素。我将这些信息累积在缓冲区上,最初是一个空图像,如下所示:

# Prepare image buffer:
buffer = np.zeros((100, 100, 3), np.uint8)

# Loop through cropped images and produce the final image:
for r in range(len(shadowRois)):

    # Get data from the list:
    (mask, img) = shadowRois[r]
    # Get image dimensions:
    (imageHeight, imageWidth) = img.shape[:2]

    # Resize the buffer:
    newSize = (imageWidth, imageHeight)
    buffer = cv2.resize(buffer, newSize, interpolation=cv2.INTER_AREA)

    # Get the image mask:
    temp = cv2.bitwise_and(img, mask)

    # Set info in buffer, substitute the black pixels
    # for the new data:
    buffer = np.where(temp == (0, 0, 0), buffer, temp)

    cv2.imshow("Composite Image", buffer)
    cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

结果是这样的: