draw rectangle with CV2

import os
import cv2
import numpy as np
import glob
import random

image_path = '/wgb_tensorflow/tfapi_vrep_detection531/11/1515530110_ang-55.png'
neg_path = '/wgb_tensorflow/tfapi_vrep_detection531/11/1515530110_ang-55_neg.txt'
obbs_path = '/wgb_tensorflow/tfapi_vrep_detection531/11/1515530110_ang-55_obbbs.txt'
pos_path = '/wgb_tensorflow/tfapi_vrep_detection531/11/1515530110_ang-55_pos.txt'

save_image = cv2.imread(image_path)
neg_f = open(neg_path)
neg_read = neg_f.readline()
i = 0
neg_points = []
while neg_read:
    i = i+1
    neg_read = neg_f.readline()
    if i>5:
        neg_points.append(neg_read)

neg_p = [];neg_ps =[]
i = 0
for point in neg_points:
    i +=1
    f =point.split(',')
    if len(f)>1:
        p = (int(point.split(',')[0]),int(point.split(',')[1]))
        neg_p.append(p)
        if i%4.0 ==0:
            neg_ps.append(neg_p)
            neg_p = []


pos_f = open(pos_path)
pos_read = pos_f.readline()
pos_points = []
i = 0
while pos_read:
    i = i+1
    pos_read = pos_f.readline()
    if i>5:
        pos_points.append(pos_read)

pos_p = [];pos_ps =[]
i = 0
for point in pos_points:
    i +=1
    f =point.split(',')
    if len(f)>1:
        p = (int(point.split(',')[0]),int(point.split(',')[1]))
        pos_p.append(p)
        if i%4.0 ==0:
            pos_ps.append(pos_p)
            pos_p = []


obbs_f = open(obbs_path)
obbs_read = obbs_f.readline()
obbs_points = []
while obbs_read:
    i = i+1
    obbs_read = obbs_f.readline()
    if i>5:
        obbs_points.append(obbs_read)


points_draw = pos_ps
drawing_image_path = '/wgb_tensorflow/tfapi_vrep_detection531/11/pos.png'

for ppoint in points_draw:
    pp0 = (ppoint[0][1], ppoint[0][0])
    pp1 = (ppoint[1][1], ppoint[1][0])
    pp2 = (ppoint[2][1], ppoint[2][0])
    pp3 = (ppoint[3][1], ppoint[3][0])
    color1 = (0, 0, 255)  # red
    color2 = (100, 250, 100)  # green

    lineWidth = 2
    cv2.line(save_image, pp0, pp1, color1, lineWidth)
    cv2.line(save_image, pp1, pp2, color2, lineWidth)
    cv2.line(save_image, pp2, pp3, color1, lineWidth)
    cv2.line(save_image, pp3, pp0, color2, lineWidth)

cv2.imwrite(drawing_image_path, save_image)

猜你喜欢

转载自blog.csdn.net/wuguangbin1230/article/details/80527491