提示:改变检测框为椭圆框
前言
例如:改变检测框为椭圆框
一、步入plots.py
# Write results
for *xyxy, conf, cls in reversed(det):
if save_txt: # Write to file
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format
with open(f'{txt_path}.txt', 'a') as f:
f.write(('%g ' * len(line)).rstrip() % line + '\n')
if save_img or save_crop or view_img: # Add bbox to image
c = int(cls) # integer class
label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')
annotator.box_label(xyxy, label, color=colors(c, True))
ctrl+点击box_label进入检测框绘制函数
二、改变plots.py代码,box_label函数else部分添加代码
1.获取图片各轮廓轮廓
ret, thresh = cv2.threshold(cv2.cvtColor(self.im.copy(), cv2.COLOR_BGR2GRAY), 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
2.找到轮廓中最大的一个,椭圆拟合需要有大于5个的点来进行绘制,选择最大的一个轮廓,可以防止小噪点被识别到
for c in range(len(contours)):
areas.append(cv2.contourArea(contours[c]))
max_id = areas.index(max(areas))
3.椭圆拟合和椭圆框绘制
retval = cv2.fitEllipse(contours[max_id]) # 取其中最大轮廓拟合椭圆
cv2.ellipse(self.im, retval, color=color, thickness=self.lw, lineType=cv2.LINE_AA) # 在原图画椭圆
总结
绘制椭圆框的时候也曾遇到错误,学习如何使用fitEllipse的函数,还需大致懂得它的原理,不然遇到噪点不足六个点不可绘制就会报错。