分割结果可视化,把标签mask轮廓显示在原图上

import cv2
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image

# Load the original and label images
original_image_path = r'C:\Users\xia94\Desktop\1656\32.png'
label_image_path = r'C:\Users\xia94\Desktop\1656\label.png'

# Load the images
original_img = cv2.imread(original_image_path)
label_img = cv2.imread(label_image_path, cv2.IMREAD_GRAYSCALE)

# Ensure the label image is binary
_, binary_label = cv2.threshold(label_img, 1, 255, cv2.THRESH_BINARY)

# Find contours in the label image
contours, _ = cv2.findContours(binary_label, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Draw contours on the original image with yellow color and thinner borders
contoured_img = original_img.copy()

# Modify the thickness and color in the following line
cv2.drawContours(contoured_img, contours, -1, (0, 255, 255), 1)  # label contours (BGR format) with thickness 1
# cv2.drawContours(contoured_img, contours, -1, (255,144,30), 1)  #  ct (BGR format) with thickness 1
# cv2.drawContours(contoured_img, contours, -1, (0,255,0), 1)  #  seg3d (BGR format) with thickness 1

# Show the result
plt.figure(figsize=(10, 10))
plt.imshow(cv2.cvtColor(contoured_img, cv2.COLOR_BGR2RGB))
plt.title('Original Image with Yellow Contours')
plt.axis('off')
plt.show()

# Convert the OpenCV image (BGR) to RGB for Pillow
contoured_img_rgb = cv2.cvtColor(contoured_img, cv2.COLOR_BGR2RGB)

# Save the image as a TIFF file with DPI using Pillow
output_tif_path = r'C:\Users\xia94\Desktop\1656\label.tif'
pil_img = Image.fromarray(contoured_img_rgb)
dpi_value = (300, 300)  # Set the DPI (horizontal, vertical)
pil_img.save(output_tif_path, dpi=dpi_value)

本文作者:梅雨明夏

本文链接:https://www.cnblogs.com/xyf9474/p/18453336

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   梅雨明夏  阅读(154)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 一路向北 Jay
  2. 2 七里香 Jay
  3. 3 不爱我就拉倒 Jay
一路向北 - Jay
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

后视镜里的世界

越来越远的道别

你转身向背

侧脸还是很美

我用眼光去追

竟听见你的泪

在车窗外面排徊

是我错失的机会

你站的方位

跟我中间隔着泪

街景一直在后退

你的崩溃在窗外零碎

我一路向北

离开有你的季节

你说你好累

已无法再爱上谁

风在山路吹

过往的画面

全都是我不对

细数惭愧 我伤你几回

☆痴人说梦☆

无与伦比→为杰沉沦

后视镜里的世界

越来越远的道别

你转身向背

侧脸还是很美

我用眼光去追

竟听见你的泪

在车窗外面排徊

是我错失的机会

你站的方位

跟我中间隔着泪

街景一直在后退

你的崩溃在窗外零碎

我一路向北

离开有你的季节

你说你好累

已无法再爱上谁

风在山路吹

过往的画面

全都是我不对

细数惭愧 我伤你几回

我一路向北

离开有你的季节

方向盘周围

回转着我的后悔

我加速超越

却甩不掉紧紧跟随的伤悲

细数惭愧 我伤你几回

停止狼狈 就让错纯粹