下面是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)
結果: