二值mask图像 + RGB原图 生成可视化分割结果; 从二值mask获取分割轮廓点
生活随笔
收集整理的這篇文章主要介紹了
二值mask图像 + RGB原图 生成可视化分割结果; 从二值mask获取分割轮廓点
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?可視化分割結果:
import cv2 import numpy as np from tqdm import tqdm from PIL import Image from pathlib import Pathimage_root = Path('data/leftImg8bit/test/qdu') mask_root = Path('evaluation_logs/origin') save_root = Path('evaluation_logs/visual')for mask in tqdm(mask_root.iterdir()):name = mask.nameimagepath = image_root / name# mask = Image.open(mask)# image = Image.open(imagepath)# print(mask.mode) # L# print(image.mode) # RGBmask = cv2.imread(str(mask), cv2.IMREAD_GRAYSCALE)image = cv2.imread(str(imagepath), cv2.IMREAD_COLOR)# print(mask.shape) # 1080 1920# print(image.shape) # 1080 1920 3image = image.astype(np.float64)image[mask > 100] = (image[mask > 100] * 0.6).astype(np.int64)image[mask > 100] += np.array([100,0,0], dtype=np.int64)sp = save_root / namecv2.imwrite(str(sp), image)從二值mask獲取分割輪廓點:cv2.findcontours()
import cv2 from tqdm import tqdm from pathlib import Pathmask_root = Path('evaluation_logs/origin') image_root = Path('data/leftImg8bit/test/qdu')for maskpath in tqdm(mask_root.iterdir()):name = maskpath.namemask = cv2.imread(str(maskpath), cv2.IMREAD_GRAYSCALE)print(mask.shape) # 1080 1920contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)print(maskpath)# print(type(contours), len(contours))# print(type(contours[0]), contours[0].shape)# print(type(contours[1]), contours[1].shape)# print(type(hierarchy), hierarchy)outlines = []for cnt in contours:# print(type(cnt), cnt.shape)cnt = cnt[::,0,:]# print(type(cnt), cnt.shape)# print(cnt)cnt = cnt.tolist()# print(cnt)outlines.append(cnt)imagepath = image_root / nameimage = cv2.imread(str(imagepath), cv2.IMREAD_COLOR)for otl in outlines:for point in otl:cv2.circle(image, point, 1, (255,0,0))cv2.imshow('', image)cv2.waitKey(0)break參考:python-opencv2利用cv2.findContours()函數來查找檢測物體的輪廓_hjxu2016的博客-CSDN博客_cv2.findcontours
總結
以上是生活随笔為你收集整理的二值mask图像 + RGB原图 生成可视化分割结果; 从二值mask获取分割轮廓点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据集增广 之 多个图片贴到一张图上,以
- 下一篇: 神经网络上采样之:反卷积