<p>如果我完全理解的话,我仍然不确定你想要实现什么,但我试图回答你的问题:</p>
<p>基本上,对于每一组坐标,您需要执行以下操作:</p>
<ol>
<li>获取原始图像的ROI剪切图。(你已经有了。)</li>
<li>生成一些二进制掩码(关于某些度量或任何您拥有的信息),以指示您要操作的ROI内部的哪个部分,例如图像中显示的汽车。在我下面的代码中,我选择用一些高蓝色通道值屏蔽任何像素</李>
<li>生成具有相同ROI大小的混合图像,并使用与生成的遮罩相关的纯色填充所有像素。在下面的代码中,我选择设置一些实心绿色</李>
<li>使用OpenCV的<a href="https://docs.opencv.org/3.4/d2/de8/group__core__array.html#gafafb2513349db3bcff51f54ee5592a19" rel="nofollow noreferrer">^{<cd1>}</a>方法进行alpha混合</李>
<li>将更新的ROI剪切图复制回原始图像</李>
</ol>
<p>下面是我的示例图像和坐标的最终输出:</p>
<p><a href="https://i.stack.imgur.com/Yat4x.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yat4x.png" alt="Final output"/></a></p>
<p>这是我的密码:</p>
<pre class="lang-py prettyprint-override"><code>import cv2
from matplotlib import pyplot as plt
import numpy as np
# Load image
orig = cv2.imread('path/to/your/image.png')
orig_back = orig.copy()
# Set up list of list of coordinates (i.e. rectangular ROIs)
list_of_coords = [[90, 0, 260, 30],
[60, 180, 100, 250],
[300, 300, 380, 370]]
# Set up alpha for alpha blending
alpha = 0.5
for coords in list_of_coords:
# Get ROI cutout of image
roi = orig_back[coords[1]:coords[3], coords[0]:coords[2]]
roi_back = roi.copy()
# Generate some mask; examplary here: Mask anything with some high
# blue channel value
mask = roi[:, :, 0] > 172
# Generate blend image; exemplary here: Blend with solid green
blend = np.zeros_like(roi)
blend[mask, 1] = 255
# Alpha blending original image with blend image
roi[mask, :] = cv2.addWeighted(roi[mask, :], alpha,
blend[mask, :], 1 - alpha,
0)
# Copy updated ROI cutout back to image
orig[coords[1]:coords[3], coords[0]:coords[2]] = roi
# Add rectangle to image for location
orig = cv2.rectangle(orig,
(coords[0], coords[1]),
(coords[2], coords[3]),
(0, 255, 0),
2)
# Some visualization output for better understanding
plt.figure(0, figsize=(16, 8))
plt.subplot(221)
plt.imshow(cv2.cvtColor(roi_back, cv2.COLOR_BGR2RGB))
plt.title('ROI cutout of image')
plt.subplot(222)
plt.imshow(mask, cmap='gray')
plt.title('Masked portion of ROI with high blue channel value')
plt.subplot(223)
plt.imshow(cv2.cvtColor(blend, cv2.COLOR_BGR2RGB))
plt.title('Blend image, solid green')
plt.subplot(224)
plt.imshow(cv2.cvtColor(roi, cv2.COLOR_BGR2RGB))
plt.title('Updated ROI cutout of image')
plt.tight_layout()
plt.show()
# Final output
cv2.imshow('Original image with updates and rectangles', orig)
cv2.waitKey(0)
cv2.destroyAllWindows()
</code></pre>
<p>下面是第一个ROI的额外可视化:</p>
<p><a href="https://i.stack.imgur.com/H3NYO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H3NYO.png" alt="Additional visualization"/></a></p>
<p>希望这就是你想要的</p>
<pre class="lang-none prettyprint-override"><code>
System information
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
Matplotlib: 3.3.1
NumPy: 1.19.1
OpenCV: 4.4.0
</code></pre>