下面是labelme做好的json标签文件,有不同的label,比如其中一个就是poly_line。还有一些其他的标签,我们只标记polyline,其他的为黑色,polyline为白色。
import json
import cv2
import numpy as np
# load img and json
data = json.load(open('1.json'))
img = cv2.imread('1.jpg')
print('shape: ',img.shape)
#创建上色颜色
color_rgb = []
color_rgb.append((0,0,0))
color_rgb.append((255,255,255))
img1 = np.zeros(img.shape, np.uint8)
#print('data: ',data['shapes'])
for content in data['shapes']:
#print("content: ",content)
#标记lane_line
'''if content['label']=='lane_line':
img_points=content['points']
#给出四个点的坐标
point1=[int(img_points[0][0]),int(img_points[0][1])]
#print('point1: ', point1)
point2=[int(img_points[0][0]),int(img_points[1][1])]
#print('point2: ', point2)
point3=[int(img_points[1][0]),int(img_points[1][1])]
#print('point3: ', point3)
point4=[int(img_points[1][0]),int(img_points[0][1])]
#print('point4: ',point4)
polyRect = np.array([point1, point2, point3,point4])
print('polyRect: ',polyRect)
#然后上色 绿色
cv2.fillPoly(img1, [polyRect], (color_rgb[0]))
#img1 = cv2.fillPoly(img1, polyRect, color_rgb[0])'''
#将多边形车道线标记出来
if content['label']=='poly_line':
img_points=content['points']
print('img_points: ',img_points)
point1=[int(img_points[0][0]),int(img_points[0][1])]
point2 = [int(img_points[1][0]), int(img_points[1][1])]
point3 = [int(img_points[2][0]), int(img_points[2][1])]
point4= [int(img_points[3][0]), int(img_points[3][1])]
polyRect = np.array([point1, point2, point3, point4])
print('polyRect: ',polyRect)
#上色 黄色
cv2.fillPoly(img1, [polyRect], (color_rgb[1]))
cv2.imwrite('1_label.png', img1)
结果: