根据点坐标画框
应用场景:
文件夹img,txt,
img存放的是原图,txt存放的是img中图片名字一样的.txt。
其中一个txt样例如下:
514,6,531,6,548,6,565,6,582,6,599,6,615,6,628,74,611,74,594,74,577,74,560,74,543,74,526,74
881,106,913,110,944,113,975,116,1007,119,1038,123,1070,126,1058,327,1029,324,1000,320,971,317,942,314,913,311,884,307
400,239,446,239,492,239,538,239,584,239,630,239,676,239,681,317,635,317,589,317,543,317,497,317,451,317,405,317
396,359,509,359,623,359,736,359,850,359,963,359,1077,359,1082,440,968,440,855,440,741,440,628,440,514,440,400,440
777,469,828,469,879,469,929,469,980,469,1031,469,1082,469,1079,554,1029,554,978,554,927,554,876,554,826,554,775,554
396,482,442,482,487,482,533,482,579,482,625,482,671,482,678,560,632,560,586,560,541,560,495,560,449,560,403,560
一个框一行
import cv2
import numpy as np
import os
dir_img = "/img/"
dir_txt = "/txt/"
save_dir = os.path.dirname(os.path.dirname(dir_img)) + '/show_pt/'
if not os.path.exists(save_dir):
os.makedirs(save_dir)
def deal_txt_pt(path_txt):
lll_pt = []
with open(path_txt, 'r') as fr:
for line in fr.readlines():
ll_pt = []
line = line.strip()
list_num = line.split(',')
l_pt = []
for cnt, num in enumerate(list_num):
num = float(num)
l_pt.append(num)
if 2 == len(l_pt):
ll_pt.append(l_pt)
l_pt = []
lll_pt.append(ll_pt)
return lll_pt
list_img = os.listdir(dir_img)
for cnt,img_name in enumerate(list_img):
print("cnt=%d,img_name=%s"%(cnt,img_name))
txt_name = img_name.replace('.jpg','.txt')
path_img = dir_img + img_name
path_txt = dir_txt + txt_name
save_path = save_dir + img_name
img = cv2.imread(path_img)
lll_pt = deal_txt_pt(path_txt)
for ll_pt in lll_pt:
pts = np.array(ll_pt,np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(img, [pts], 1, (0, 255, 255),2)
cv2.imwrite(save_path,img)
好记性不如烂键盘---点滴、积累、进步!