标注的轮廓点转segnet标签图
一张图对应txt
45.0,1400.0,320.0,1317.0,3387.0,1447.0,3575.0,1537.0,3537.0,1592.0,3367.0,1640.0,287.0,1502.0
drawContours这个函数接受的contours格式搞死我了,一定要这样arr = np.array([arr])。。后面再看看吧!
#coding=utf-8
import cv2
import os
import numpy as np
dir_img = "/data_2/2019biaozhushuju/20191018_cjh_pt/rot/m_rot/"
dir_txt = "/data_2/2019biaozhushuju/20191018_cjh_pt/rot/txt/"
dir_gray_save = "/data_2/2019biaozhushuju/20191018_cjh_pt/1128gray/"
dir_img_save = "/data_2/2019biaozhushuju/20191018_cjh_pt/1128img/"
def read_txt(txt_path):
with open(txt_path,'r') as fr:
for line in fr.readlines():
line = line.strip()
list_num = line.split(',')
pt = []
v_pt = []
for cnt,val in enumerate(list_num):
val = val.replace('.0','')
val = int(val)
if 0 == cnt%2:
pt.append(val)
else:
pt.append(val)
v_pt.append(pt)
pt = []
break #### 1 行
return v_pt
def get_gray(txt_path,path_img,path_gray_save,path_img_save):
img = cv2.imread(path_img)
arr = read_txt(txt_path)
arr = np.array([arr])
gray = np.zeros((img.shape[0],img.shape[1]),dtype=np.uint8)
cv2.drawContours(gray,[arr],-1,(1),-1)
img = cv2.resize(img,(512,512),interpolation = cv2.INTER_NEAREST)
gray = cv2.resize(gray, (512, 512),interpolation = cv2.INTER_NEAREST)
cv2.imwrite(path_gray_save,gray)
cv2.imwrite(path_img_save, img)
# cv2.namedWindow("src",0)
# cv2.namedWindow("gray",0)
# cv2.imshow("src",img)
# cv2.imshow("gray",gray)
# cv2.waitKey(0)
list_img = os.listdir(dir_img)
for cnt,img_name in enumerate(list_img):
print(cnt,img_name)
path_img = dir_img + img_name
path_txt = dir_txt + img_name.replace('.jpg','.txt')
#img = cv2.imread(path_img)
path_gray_save = dir_gray_save + img_name.replace('.jpg','.png')
path_img_save = dir_img_save + img_name.replace('.jpg','.png')
# get_gray(txt_path,path_img,path_gray_save,path_img_save):
get_gray(path_txt, path_img, path_gray_save,path_img_save)
好记性不如烂键盘---点滴、积累、进步!