我目前正在图像中绘制 ROI,以便稍后在 Python 中进行图像处理。我已经找到了一种绘制 ROI 的方法,并且一切正常。然后我编写了将 roi 保存为图像的部分。问题如下: 我创建了一个与原始图像尺寸相同的 roi_region,其中存储 ROI。这是一个试验,但是,我只需要投资回报率本身:没有背景。
我搜索了又搜索如何做到这一点,但是我对Python相当陌生,我还没有找到方法。
if index == 2:
fig_all_roi, ax_all_roi = plt.subplots()
ax_all_roi.set_title('Extracted ROIs')
for i, (verts_list, color) in enumerate(zip(roi_verts_list, roi_colors)):
for verts in verts_list:
# Create a Path object from the vertices
path = Path(verts)
# Create a mask for the region inside the ROI
x, y = np.meshgrid(np.arange(image.shape[1]), np.arange(image.shape[0]))
points = np.column_stack((x.flatten(), y.flatten()))
mask = path.contains_points(points).reshape(image.shape[:2])
# Extract the region inside the ROI from the original image
roi_region = np.zeros_like(image, dtype=np.uint8)
for channel in range(image.shape[2]):
roi_region[:, :, channel] = (image[:, :, channel] * mask).astype(np.uint8)
roi_region[:,:, 3] = (mask*255).astype(np.uint8)
# Save the extracted ROI as an image
save_roi_image(roi_region, i)
# Display the extracted region in the new figure
ax_all_roi.imshow(roi_region, extent=[0, image.shape[1], 0, image.shape[0]])
我尝试添加一个 alpha 通道(我在 stackoverflow 上读到了一些相关内容),我尝试切换到 plt.figsave 但该代码失败了,我最终陷入了痛苦。
搜索
cv2.imwrite
并记住使用扩展名为 .png
的文件名。例如,
cv2.imwrite("filename.png", ROI)