画边框(python)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 10:08:41 2017

@author: wgb
"""
import cv2
import random

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.cm as mpcm
import os


dir_cu = os.getcwd() 
    
scores=0.5    
classes=1
linewidth=1.5   
figsize=(10,10)
file_name = '/home/wgb/Desktop/CMU/rawDataSet/pcd0119r.png'
img =cv2.imread(file_name)
bboxes = []

fig = plt.figure(figsize=figsize)
#plt.imshow(img)
height = img.shape[0]
width = img.shape[1]
colors = dict()
#for i in range(classes.shape[0]):
cls_id = int(1)
#    if cls_id >= 0:
score = scores
if cls_id not in colors:
    colors[cls_id] =255* (random.random(), random.random(), random.random())

f = open('/home/wgb/Desktop/CMU/rawDataSet/pcd0119cpos.txt')   
line = f.readline()             # 调用文件的 readline()方法 
while line: 
    bbox = line
    #    bboxes = bboxes.append(bbox)                # 后面跟 ',' 将忽略换行符 
#    print(line)   
     
    print(line)
    line = f.readline()
    bboxes.append(bbox)

f.close()  

print(bboxes)


#xmin = float(bboxes[0].split()[0])
#ymin = float(bboxes[0].split()[1])
#xmax = float(bboxes[1].split()[0])
#ymax = float(bboxes[1].split()[1])


#
xmin = float(bboxes[3].split()[0])
ymin = float(bboxes[3].split()[1])
xmax = float(bboxes[2].split()[0])
ymax = float(bboxes[2].split()[1])



p1 = int(float(bboxes[0].split()[0]))
p2 = int(float(bboxes[0].split()[1]))
pt1 = (p1,p2)
p1 = int(float(bboxes[1].split()[0]))
p2 = int(float(bboxes[1].split()[1]))
pt2 = (p1,p2)

p1 = int(float(bboxes[2].split()[0]))
p2 = int(float(bboxes[2].split()[1]))
pt3 = (p1,p2)
p1 = int(float(bboxes[3].split()[0]))
p2 = int(float(bboxes[3].split()[1]))
pt4 = (p1,p2)
#
##ymin = int(bboxes[i, 0] )
##xmin = int(bboxes[i, 1])
##ymax = int(bboxes[i, 2])
##xmax = int(bboxes[i, 3])
##rect = plt.Rectangle((xmin, ymin), xmax - xmin,
##                     ymax - ymin, fill=False,
##                     edgecolor=colors[cls_id],
##                     linewidth=linewidth)
##plt.gca().add_patch(rect)
##class_name = 'w'

color=(222,120,233)
lineWidth = 2
cv2.line(img, pt1, pt2, color,lineWidth)
cv2.line(img, pt2, pt3, color, lineWidth)
cv2.line(img, pt3, pt4, color, lineWidth)
cv2.line(img, pt1, pt4, color, lineWidth)
plt.imshow(img)
#plt.gca().text(xmin, ymin - 2,
#               '{:s} | {:.3f}'.format(class_name, score),
#               bbox=dict(facecolor=colors[cls_id], alpha=0.5),
#               fontsize=12, color='white')
#   
plt.show()

你可能感兴趣的:(wgb)