这是一个可能的解决方案。总体思路是计算阴影的位置,生成一个标识阴影位置的二进制掩码,并使用此信息从所有裁剪的子图像中复制像素。
让我们看看代码。第一个问题是定位三个图像。我使用黑盒分割和裁剪每辆车,如下所示:
# 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)
前一位使用图像的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)
过滤每个轮廓得到最大的后,我需要定位阴影的位置。阴影在HSV 色彩空间中大部分可见,特别是在V 通道中。我裁剪了图像的两个版本:原始的BGR 图像,现在被裁剪,以及HSV 图像的V 裁剪通道。这是在S 通道上应用自动阈值处理产生的二进制掩码:
要定位阴影,我只需要起始 x 坐标和它的 width,因为每个裁剪图像的阴影都是均匀的。它的height 等于每个裁剪图像的高度。我使用SUM 模式将V 图像缩小为一行。这将对所有列中的每个像素求和。最大值将对应阴影的位置:
# 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)
缩小的图像只是一行:
白色像素表示最大值。阴影的位置绘制成一条面积最大的水平线,也就是最连续的白色像素。我通过获取轮廓并再次过滤到最大区域来处理这一行:
# 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()))
好的,我根据这些信息创建了一个蒙版,其中唯一用白色绘制的就是蒙版的位置。我将这个掩码和原始的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)
结果是这样的: