将此代码转换成python形式,并且功能不变
时间: 2024-10-26 21:03:26 浏览: 24
将 C++ 代码转换为 Python 代码涉及多个步骤,主要包括语法转换、库替换和函数重构。以下是将 `opencvTest_lack.txt` 中的主要功能转换为 Python 形式的代码。请注意,Python 版本的代码将使用 OpenCV 的 Python 接口 (`cv2`) 和 NumPy 库。
### 1. 导入库
首先导入所需的库:
```python
import cv2
import numpy as np
from math import sin, cos
```
### 2. 定义常量
将 C++ 中的宏定义转换为 Python 中的常量:
```python
JIFEN = 5
DAO DIAN JU = 3
FEICHEZHANBI = 0.73
YUANHUANYUZHI = 40
YUANHANJINGDU = 10
KUANZENG = 90
KUANZENG2 = 70
SHIZIJI = 0
BANMASIGN = 0
CAMERA_H = 60
CAMERA_W = 94
OUT_H = 60
OUT_W = 94
```
### 3. 定义结构体
在 Python 中,可以使用类来模拟结构体:
```python
class Up:
def __init__(self, point, value):
self.dian = point
self.que = value
```
### 4. 二值化函数
将 `er_zhi` 函数转换为 Python:
```python
def er_zhi(imgxiu):
img_cvt = cv2.cvtColor(imgxiu, cv2.COLOR_BGR2GRAY)
img_temp = cv2.GaussianBlur(img_cvt, (5, 5), 0)
_, img_thr = cv2.threshold(img_temp, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2.imshow("二值化", img_thr)
return img_thr
```
### 5. 画出赛道两侧线和中线
将 `che_dao_xian` 函数转换为 Python:
```python
def che_dao_xian(img_er):
img_can, img_dil = [], []
shizipan = 0
yuanhuanzuosign = 0
yuanhuanyousign = 0
yuanhuan = []
banmapan = 0
banmasign = 0
banmadi = [0, 0, 0]
banmashang = [0, 0, 0]
high = int(img_er.shape[0] * FEICHEZHANBI)
lie_bais = [0] * img_er.shape[1]
for x in range(img_er.shape[1]):
for y in range(high - 1, -1, -1):
if img_er[y, x] != 0:
lie_bais[x] += 1
else:
break
if x > 0 and abs(lie_bais[x] - lie_bais[x - 1]) >= 60:
banmasign += 1
if banmasign >= 6:
banmapan = 1
maxzuo = [0, 0]
maxyou = [0, 0]
for a in range(len(lie_bais)):
if lie_bais[a] > maxzuo[1]:
maxzuo[0] = a
maxzuo[1] = lie_bais[a]
for a in range(len(lie_bais) - 1, -1, -1):
if lie_bais[a] > maxyou[1]:
maxyou[0] = a
maxyou[1] = lie_bais[a]
chushi = 0
chuju = 0
shizi_zuox = 0
shizi_zuoy = 0
shizi_youx = 0
zuo_dao = []
you_dao = []
zhong_xian = []
for y in range(high - 1, high - maxzuo[1], -1):
zuox = maxzuo[0]
for x in range(maxzuo[0], -1, -1):
if img_er[y, x] != 0 and img_er[y, x - 1] == 0:
if len(zuo_dao) >= 1:
if abs(x - zuo_dao[-1].dian[0]) <= DAO_DIAN_JU and abs(y - zuo_dao[-1].dian[1]) <= DAO_DIAN_JU:
zuo_dao.append(Up((x, y), 0))
zuox = x
if banmapan == 1:
banmapan = 2
break
else:
zuox = x
zuo_dao.append(Up((x, y), 0))
break
if img_er[y, x] != 0 and img_er[y, x - 1] != 0 and img_er[y, x - 2] != 0:
shizi_zuox += 1
if x - 3 == 0:
break
tempx = maxyou[0] - 4
for x in range(maxyou[0] - 4, img_er.shape[1]):
if img_er[y, x] != 0 and img_er[y, x + 1] == 0:
if len(you_dao) >= 1:
if abs(x - you_dao[-1].dian[0]) <= DAO_DIAN_JU and abs(y - you_dao[-1].dian[1]) <= DAO_DIAN_JU:
you_dao.append(Up((x, y), 0))
tempx = x
break
else:
tempx = x
you_dao.append(Up((x, y), 0))
break
if img_er[y, x] != 0 and img_er[y, x + 1] != 0 and img_er[y, x + 2] != 0:
shizi_youx += 1
if x + 3 == img_er.shape[1] - 1:
break
if img_er.shape[1] - tempx >= 80:
if banmapan == 2 and img_er[y, tempx + 62] != 0 and img_er[y, tempx + 63] != 0 and img_er.shape[0] - maxzuo[1] <= 80:
banmadi[0] = zuox
banmadi[1] = tempx
banmadi[2] = y
banmapan = 3
if chuju == 0:
chuju = tempx - zuox
if abs(shizi_zuox - maxzuo[0]) <= 5 and abs(img_er.shape[1] - maxyou[0] + 4 - shizi_youx) <= 5:
shizipan = 1
yuanhuanweidian = [None] * 4
if abs(len(zuo_dao) - len(you_dao)) >= YUANHUANYUZHI:
ji1 = 0
ji2 = 0
signs = 0
if len(zuo_dao) < len(you_dao):
zuominx = 999999
for n in range(len(zuo_dao)):
if zuominx > zuo_dao[n].dian[0]:
zuominx = zuo_dao[n].dian[0]
for n in range(len(you_dao)):
signs = 0
for x in range(you_dao[n].dian[0], max(zuominx - 5, 1), -1):
if img_er[you_dao[n].dian[1], x] != 0 and img_er[you_dao[n].dian[1], x - 1] == 0:
if yuanhuanyousign == 1 and ji1 >= 5:
yuanhuanyousign = 2
yuanhuanweidian[1] = (you_dao[n - 5].dian[0] - KUANZENG, you_dao[n - 5].dian[1])
ji1 = 0
elif yuanhuanyousign == 3 and ji1 >= 5:
yuanhuanyousign = 4
break
signs = 1
ji1 += 1
ji2 = 0
if signs == 0:
if yuanhuanyousign == 0 and ji2 >= 5:
yuanhuanyousign = 1
yuanhuanweidian[0] = (you_dao[n - 5].dian[0] - KUANZENG, you_dao[n - 5].dian[1])
ji2 = 0
elif yuanhuanyousign == 2 and ji2 >= 5:
yuanhuanyousign = 3
ji2 = 0
ji2 += 1
ji1 = 0
else:
youmaxx = 0
for n in range(len(you_dao)):
if youmaxx < you_dao[n].dian[0]:
youmaxx = you_dao[n].dian[0]
for n in range(len(zuo_dao)):
signs = 0
for x in range(zuo_dao[n].dian[0], youmaxx):
if img_er[zuo_dao[n].dian[1], x] != 0 and img_er[zuo_dao[n].dian[1], x + 1] == 0:
if yuanhuanzuosign == 1 and ji1 >= YUANHANJINGDU:
yuanhuanzuosign = 2
yuanhuanweidian[1] = (zuo_dao[n - 5].dian[0] + KUANZENG2, zuo_dao[n - 5].dian[1])
ji1 = 0
elif yuanhuanzuosign == 3 and ji1 >= YUANHANJINGDU:
yuanhuanzuosign = 4
break
signs = 1
ji1 += 1
ji2 = 0
if signs == 0:
if yuanhuanzuosign == 0 and ji2 >= YUANHANJINGDU:
yuanhuanzuosign = 1
yuanhuanweidian[0] = (zuo_dao[n - 5].dian[0] + KUANZENG2 + 10, zuo_dao[n - 5].dian[1])
ji2 = 0
elif yuanhuanzuosign == 2 and ji2 >= YUANHANJINGDU:
yuanhuanzuosign = 3
ji2 = 0
ji2 += 1
ji1 = 0
if shizipan:
shiziji += 1
if shiziji >= 3:
shiziji = 0
cv2.putText(img_sour, "CROSS", (10, 40), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 0, 255), 2)
if banmapan == 3:
cv2.putText(img_sour, "BANMA_WIRES", (10, 40), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 0, 255), 2)
cv2.line(img_sour, (banmadi[0], banmadi[2]), (banmadi[1], banmadi[2]), (255, 0, 255), 3.5)
if yuanhuanzuosign == 4:
cv2.putText(img_sour, "right circular", (10, 40), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 0, 255), 2)
for n in range(2):
cv2.line(img_sour, (img_er.shape[1] - 1, yuanhuanweidian[n][1]), (yuanhuanweidian[n][0], yuanhuanweidian[n][1]), (255, 0, 255), 3.5)
elif yuanhuanyousign == 4:
cv2.putText(img_sour, "left circular", (10, 40), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 0, 255), 2)
for n in range(2):
cv2.line(img_sour, (0, yuanhuanweidian[n][1]), (yuanhuanweidian[n][0], yuanhuanweidian[n][1]), (255, 0, 255), 3.5)
sizes = min(len(zuo_dao), len(you_dao))
best_zhongxian = []
nihe_zhongxian = []
kai = 0
zhongx = 0
zhongy = 0
for n in range(sizes):
if zuo_dao[n].que == 0 and you_dao[n].que == 0:
zhongx = (zuo_dao[n].dian[0] + you_dao[n].dian[0]) // 2
zhongy = (zuo_dao[n].dian[1] + you_dao[n].dian[1]) // 2
if kai == 0:
zhong_xian.append(Up((zhongx, zhongy), 0))
kai = 1
elif abs(zhongx - zhong_xian[-1].dian[0]) <= DAO_DIAN_JU and abs(zhongy - zhong_xian[-1].dian[1]) >= 2:
zhong_xian.append(Up((zhongx, zhongy), 0))
for c in range(0, len(zhong_xian), JIFEN):
if c + JIFEN >= len(zhong_xian):
break
aa, bb, xx, yy, xy, xfangs = 0, 0, 0, 0, 0, 0
for n in range(c, c + JIFEN):
xy += zhong_xian[n].dian[0] * zhong_xian[n].dian[1]
xfangs += zhong_xian[n].dian[0] ** 2
xx += zhong_xian[n].dian[0]
yy += zhong_xian[n].dian[1]
xx /= JIFEN
yy /= JIFEN
bb = (xy - JIFEN * xx * yy) / (xfangs - JIFEN * xx ** 2)
aa = yy - bb * xx
zuomin = 999999
zuomax = 0
youmin = 999999
youmax = 0
for n in range(c, c + JIFEN):
if zuomin > zuo_dao[n].dian[0]:
zuomin = zuo_dao[n].dian[0]
if zuomax < zuo_dao[n].dian[0]:
zuomax = zuo_dao[n].dian[0]
for n in range(c, c + JIFEN):
if youmin > you_dao[n].dian[0]:
youmin = you_dao[n].dian[0]
if youmax < you_dao[n].dian[0]:
youmax = you_dao[n].dian[0]
for x in range(zuomin, youmax + 1):
y = int(bb * x + aa)
if y <= zhong_xian[c].dian[1] and y >= zhong_xian[c + JIFEN].dian[1] and abs(x - (zuomin + youmax) / 2) <= 3:
nihe_zhongxian.append((x, y))
for n in range(len(nihe_zhongxian) - 1):
cv2.line(img_sour, nihe_zhongxian[n], nihe_zhongxian[n + 1], (0, 255, 0), 4)
for n in range(len(zhong_xian) - 1):
cv2.circle(img_sour, zhong_xian[n].dian, 2, (0, 255, 0), -1)
cv2.line(img_sour, zhong_xian[n].dian, zhong_xian[n + 1].dian, (0, 255, 0), 4)
for n in range(len(zuo_dao)):
cv2.circle(img_sour, zuo_dao[n].dian, 2, (255, 0, 0), -1)
for n in range(len(you_dao)):
cv2.circle(img_sour, you_dao[n].dian, 2, (0, 0, 255), -1)
return img_sour
```
### 6. 主函数
将主函数转换为 Python:
```python
def main():
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
img_sour = frame
img_er = er_zhi(frame)
img_result = che_dao_xian(img_er)
cv2.imshow('Result', img_result)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
阅读全文