yolo:detect.py改变检测框为椭圆框

文章介绍了如何修改plots.py代码,将检测框从矩形改为椭圆。通过阈值处理和寻找轮廓,选择最大轮廓进行椭圆拟合,然后使用cv2.fitEllipse进行绘制。过程中需要注意轮廓点数量以避免不足六个点时的错误。
摘要由CSDN通过智能技术生成

提示:改变检测框为椭圆框


前言

例如:改变检测框为椭圆框


一、步入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的函数,还需大致懂得它的原理,不然遇到噪点不足六个点不可绘制就会报错。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亿巫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值