将文件中的代码修改,更高效,更快
时间: 2024-11-11 19:22:06 浏览: 4
为了提高代码的效率和速度,可以进行以下优化:
1. **减少重复计算**:避免多次计算相同的值。
2. **使用向量化操作**:利用NumPy的向量化操作来加速计算。
3. **减少不必要的循环**:尽可能减少嵌套循环。
4. **提前退出循环**:一旦找到符合条件的值,立即退出循环。
5. **使用生成器表达式**:对于不需要一次性加载到内存的数据,使用生成器表达式。
以下是优化后的代码:
```python
import numpy as np
import cv2
class Up:
def __init__(self, x=0, y=0):
self.dian = np.array([x, y], dtype=int)
self.que = 0
def che_dao_xian(imgEr, imgSour, feichezhanbi, daodianju, yuanhuanyuzhi, kuanzeng, kuanzeng2, yuanhuanjingdu):
up_data = Up()
global shiziji
imgCan = imgDil = None
shizipan = 0
yuanhuanzuosign = 0
yuanhuanyousign = 0
yuanhuan = []
yuanhuanweidian = [(0, 0), (0, 0), (0, 0), (0, 0)]
high = int(imgEr.shape[0] * feichezhanbi)
# 获取每一列白色点数据
lieBais = np.sum(imgEr[:high, :], axis=0)
# 左右寻最长白条
maxzuo = np.argmax(lieBais)
maxyou = np.argmax(lieBais[::-1])
maxyou = imgEr.shape[1] - maxyou - 1
# 找车道线
zuoDao = []
youDao = []
zhongXian = []
for y in range(high - 1, high - maxzuo, -1):
zuox = maxzuo
for x in range(maxzuo, -1, -1):
if imgEr[y, x] != 0 and (len(zuoDao) == 0 or np.linalg.norm(np.array([x, y]) - zuoDao[-1].dian) <= daodianju):
zuoDao.append(Up(x, y))
zuox = x
break
tempx = maxyou
for x in range(maxyou + 5, imgEr.shape[1]):
if imgEr[y, x] != 0 and (len(youDao) == 0 or np.linalg.norm(np.array([x, y]) - youDao[-1].dian) <= daodianju):
youDao.append(Up(x, y))
tempx = x
break
if chuju == 0:
chuju = tempx - zuox
if abs(shiziZuox - maxzuo) <= 5 and abs(imgEr.shape[1] - maxyou + 4 - shiziYoux) <= 5:
shizipan = 1
# 判断圆环存在否,及其位置
if abs(len(zuoDao) - len(youDao)) >= yuanhuanyuzhi:
ji1 = 0
ji2 = 0
signs = 0
# 左圆环
if len(zuoDao) < len(youDao):
zuominx = min(point.dian[0] for point in zuoDao)
for n in range(len(youDao)):
for x in range(youDao[n].dian[0], max(zuominx - 5, 1), -1):
if imgEr[youDao[n].dian[1], x] != 0 and imgEr[youDao[n].dian[0], x - 1] == 0:
if yuanhuanyousign == 1 and ji1 >= 5:
yuanhuanyousign = 2
yuanhuanweidian[1] = tuple(youDao[n - 5].dian - np.array([kuanzeng, 0]))
ji1 = 0
elif yuanhuanyousign == 3 and ji1 >= 5:
yuanhuanyousign = 4
ji1 = 0
break
signs = 1
ji1 += 1
ji2 = 0
break
if signs == 0:
if yuanhuanyousign == 0 and ji2 >= 5:
yuanhuanyousign = 1
yuanhuanweidian[0] = tuple(youDao[n - 5].dian - np.array([kuanzeng, 0]))
ji2 = 0
elif yuanhuanyousign == 2 and ji2 >= 5:
yuanhuanyousign = 3
ji2 = 0
ji2 += 1
ji1 = 0
# 右圆环
else:
youmaxx = max(point.dian[0] for point in youDao)
for n in range(len(zuoDao)):
for x in range(zuoDao[n].dian[0], youmaxx):
if imgEr[zuoDao[n].dian[1], x] != 0 and imgEr[zuoDao[n].dian[1], x + 1] == 0:
if yuanhuanzuosign == 1 and ji1 >= yuanhuanjingdu:
yuanhuanzuosign = 2
yuanhuanweidian[1] = tuple(zuoDao[n - 5].dian + np.array([kuanzeng2, 0]))
ji1 = 0
elif yuanhuanzuosign == 3 and ji1 >= yuanhuanjingdu:
yuanhuanzuosign = 4
ji1 = 0
break
signs = 1
ji1 += 1
ji2 = 0
break
if signs == 0:
if yuanhuanzuosign == 0 and ji2 >= yuanhuanjingdu:
yuanhuanzuosign = 1
yuanhuanweidian[0] = tuple(zuoDao[n - 5].dian + np.array([kuanzeng2 + 10, 0]))
ji2 = 0
elif yuanhuanzuosign == 2 and ji2 >= yuanhuanjingdu:
yuanhuanzuosign = 3
ji2 = 0
ji2 += 1
ji1 = 0
# 圆环判断
if yuanhuanzuosign == 4:
cv2.putText(imgSour, "right circular", (10, 40), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 0, 255), 2)
for n in range(2):
cv2.line(imgSour, (imgEr.shape[1] - 1, yuanhuanweidian[n][1]), (yuanhuanweidian[n][0], yuanhuanweidian[n][1]), (255, 0, 255), 3.5)
elif yuanhuanyousign == 4:
cv2.putText(imgSour, "left circular", (10, 40), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 0, 255), 2)
for n in range(2):
cv2.line(imgSour, (0, yuanhuanweidian[n][1]), (yuanhuanweidian[n][0], yuanhuanweidian[n][1]), (255, 0, 255), 3.5)
# 找中线
sizes = min(len(zuoDao), len(youDao))
kai = 0
for n in range(sizes):
if zuoDao[n].que == 0 and youDao[n].que == 0:
zhongx = (zuoDao[n].dian[0] + youDao[n].dian[0]) // 2
zhongy = (zuoDao[n].dian[1] + youDao[n].dian[1]) // 2
if kai == 0:
zhongXian.append(Up(zhongx, zhongy))
kai = 1
elif abs(zhongx - zhongXian[-1].dian[0]) <= daodianju and abs(zhongy - zhongXian[-1].dian[1]) >= 2:
zhongXian.append(Up(zhongx, zhongy))
# 原始中线
for n in range(len(zhongXian) - 1):
cv2.circle(imgSour, zhongXian[n].dian, 2, (0, 255, 0), -1)
cv2.line(imgSour, zhongXian[n].dian, zhongXian[n + 1].dian, (0, 255, 0), 4)
# 画出车道线
for n in range(len(zuoDao)):
cv2.circle(imgSour, zuoDao[n].dian, 2, (255, 0, 0), -1)
for n in range(len(youDao)):
cv2.circle(imgSour, youDao[n].dian, 2, (0, 0, 255), -1)
print(type(yuanhuanweidian))
return imgSour
```
### 主要改进点:
1. **减少重复计算**:通过 `np.sum` 计算每列的白色点数,避免了双重循环。
2. **向量化操作**:使用 NumPy 数组和向量化操作来加速计算。
3. **提前退出循环**:一旦找到符合条件的值,立即退出循环。
4. **简化条件判断**:减少了不必要的条件判断,提高了代码的可读性和执行效率。
这些改进应该能够显著提升代码的运行速度和效率。
阅读全文