添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
本文介绍了在深度学习模型训练完成后,如何使用OpenCV库中的cv2.threshold,cv2.findContours和cv2.drawContours函数,以两种方式对分割效果(label和mask)进行可视化,包括label填充和勾线描边。 摘要由CSDN通过智能技术生成 from torchvision . transforms import transforms def show_mask ( mask , ax , random_color = False ) : if random_color : color = np . concatenate ( [ np . random . random ( 3 ) , np . array ( [ 0.6 ] ) ] , axis = 0 ) else : color = np . array ( [ 30 / 255 , 144 / 255 , 255 / 255 , 0.5 ] ) h , w = mask . shape [ - 2 : ] mask_image = mask . reshape ( h , w , 1 ) * color . reshape ( 1 , 1 , - 1 ) ax . imshow ( mask_image ) return mask_image image = cv2 . imread ( './data/0.png' ) label = Image . open ( "./data/0_gt.jpg" ) mask = Image . open ( "./data/0_pred.jpg" ) h , w , _ = image . shape label = transforms . Resize ( ( h , w ) ) ( label ) label = np . uint8 ( label ) label = 255 * ( label > 127 ) label = label == 255 mask = transforms . Resize ( ( h , w ) ) ( mask ) mask = np . uint8 ( mask ) ret_mask , thresh_mask = cv2 . threshold ( mask , 127 , 255 , 0 ) contours_mask , hierarchy_mask = cv2 . findContours ( thresh_mask , cv2 . RETR_TREE , cv2 . CHAIN_APPROX_SIMPLE ) for cnt_mask in contours_mask : cv2 . drawContours ( image , [ cnt_mask ] , 0 , ( 0 , 255 , 0 ) , 2 ) plt . figure ( dpi = 300 ) plt . imshow ( image ) show_mask ( label , plt . gca ( ) ) plt . axis ( 'off' ) plt . savefig ( "output_image.jpg" )

2. 标签label勾线,预测mask勾线

第二种是利用不用的颜色均采用勾线描边的方式, import numpy as np from PIL import Image , ImageDraw from torchvision . transforms import transforms image = cv2 . imread ( './data/0.png' ) label = Image . open ( "./data/0_gt.jpg" ) mask = Image . open ( "./data/0_pred.jpg" ) h , w , _ = image . shape label = transforms . Resize ( ( h , w ) ) ( label ) label = np . uint8 ( label ) mask = transforms . Resize ( ( h , w ) ) ( mask ) mask = np . uint8 ( mask ) ret_label , thresh_label = cv2 . threshold ( label , 127 , 255 , 0 ) contours_label , hierarchy_label = cv2 . findContours ( thresh_label , cv2 . RETR_TREE , cv2 . CHAIN_APPROX_SIMPLE ) ret_mask , thresh_mask = cv2 . threshold ( mask , 127 , 255 , 0 ) contours_mask , hierarchy_mask = cv2 . findContours ( thresh_mask , cv2 . RETR_TREE , cv2 . CHAIN_APPROX_SIMPLE ) for cnt_label in contours_label : cv2 . drawContours ( image , [ cnt_label ] , 0 , ( 0 , 0 , 255 ) , 2 ) for cnt_mask in contours_mask : cv2 . drawContours ( image , [ cnt_mask ] , 0 , ( 0 , 255 , 0 ) , 2 ) cv2 . imwrite ( 'output_image.jpg' , image )

import xml . etree . ElementTree as ET # 读取xml。 import os from PIL import Image, ImageDraw, ImageFont def parse_rec(filename): tree = ET . parse(filename) # 解析读取xml函数 objects = [] img_dir = [] for xml_name in tree . findall('filename'): 一、 python 给图像加上 mask ,并提取 mask 区域 python 给图像加上 mask ,并提取 mask 区域 _xnholiday的博客-CSDN博客_ mask python import os import cv2 import numpy as np
靠数据做事当然首先要知道数据是什么样的 本文的 可视化 是基于pycocotools来实现的(有方便的轮子为什么不用),网上的大多数教程就是把pycocotools的demo拿来说了一下,而demo中 标签 可视化 都需要指定一个类,只显示这个类的 标签 ,但我想 可视化 一张图片中所有目标的 标签 该怎么办。。。。 一时半会在网上没找到想要的 ???? 自己写一个算了 ???? 所以有了本文 默认已经安装了pycocotools 直接上代码: 下面的代码可以直接 可视化 train的 标签 ,其中’test‘模式是为了打比赛的时候
1 . Jupyter notebook不具备的功能是 () Jupyter notebook可以直接生成一份交互式文档 Jupyter notebook可以安装 Python 库 Jupyter notebook可以导出HTML文件 Jupyter notebook可以将文件分享给他人 right_answers: Jupyter notebook可以将文件分享给他人 2 . 在Jupyter notebook的命令模式下,要查看所有快捷 . .
current_path = os . getcwd() img_path = os . path . join(current_path, r'data/landslide_data/img/0 . tif') mask _path = os . path . join(current_path, r'data/landslide_data/ mask /0 . tif') imshow_path = os . path . join(current_pat
做数据集处理实验时突然意识到,越多的统计实验可能更适合比赛里,关于发现创新点的实验需要有针对性的实验才行。 还有就是怎么才能提升代码能力啊,有找别人的代码的时间,自己都能改了,呜呜呜呜。 三种 可视化 实验 简单有效的 目的:将原图片(img) 其xml(xml),合成为打标记的图片( label led),矩形框标记用红色即可 已有:(1)原图片文件夹(imgs_path),(2)xml文件夹(xmls_path) step1: 读取(原图片文件夹中的)一张图片