if blobs: # max_blob不一定是赛道,但是如果如果硬件动作组不偏离中线,应可以直接这么写。 # 应设计多个不同条件下的转弯和移动动作组,使用手掰编程或者改变原有动作的转角即可。 max_blob = find_max(blobs) img.draw_cross(max_blob.cx(), max_blob.cy(),(0,0,0)) img.draw_rectangle(max_blob.rect()) #print(max_blob.cx(), max_blob.cy(), max_blob.rect()[2], max_blob.rect()[3], max_blob.pixels()) #print(max_blob.density(), max_blob.w(), max_blob.h()) blob_white_height_prob = 0.4 # TODO 可调 height_prob = max_blob.h()/ROI_height print('height_prob: '+ str(height_prob) + ' ' + 'density:' + str(max_blob.density())) if flag == 1 and sent_count <= 3: if max_blob.cx()<ROI_height: # TODO 重点改的对象。使用height_prob, max_blob.density(), max_blob.cx()进行赛道的识别 uart.write('8''\r\n') # print('turn left!') led.on() sent_count = sent_count + 1 elif action_cx >=130 : uart.write('3''\r\n') # print('turn right!' led.on() sent_count = sent_count + 1 continue你帮我在代码里修改判断机器狗左转的条件,给我一个更好的代码

时间: 2023-06-24 16:09:09 浏览: 65
好的,我可以帮你修改一下。根据你的代码,我理解你想要在机器狗检测到赛道时,判断其偏向左侧,然后控制机器狗向左转。你可以尝试修改以下代码: ``` if flag == 1 and sent_count <= 3: if max_blob.cx() < ROI_height and max_blob.cx() < 100: # 如果最大的blob的中心点坐标小于ROI_height且小于100 uart.write('8''\r\n') # 向左转 led.on() sent_count = sent_count + 1 elif action_cx >=130 : uart.write('3''\r\n') # 向右转 led.on() sent_count = sent_count + 1 ``` 上面的代码中,我添加了一个新的判断条件 `max_blob.cx() < 100`,表示当最大的blob的中心点坐标小于100时,机器狗会向左转。你可以根据实际情况调整这个值。 同时,我也发现你在 `uart.write()` 函数中的参数没有用引号包裹起来,我在代码中已经修改了,你可以参考。
相关问题

def find_max(blobs): max_size=0 for blob in blobs: if blob[2]*blob[3] > max_size: max_blob=blob max_size = blob[2]*blob[3] return max_blob

这个函数的作用是在给定的一组 blobs 中,找到面积最大的 blob,并返回它的信息。 它使用了一个变量 `max_size` 来记录当前最大的面积,并通过遍历 blobs 列表来更新它。对于每个 blob,它会计算出面积(blob[2]*blob[3])并与当前最大面积比较。如果当前 blob 的面积更大,则更新 `max_blob` 和 `max_size`。 最后,函数返回 `max_blob`,即面积最大的 blob 的信息。 请注意,你提供的代码中没有定义 `max_blob` 变量,你可能需要在函数开始时添加 `max_blob = None` 来避免错误。

if blobs: most_pixels = 0 largest_blob = 0 for i in range(len(blobs)):什么意思

这段代码是一个Python的控制流语句,主要用于遍历一个名为`blobs`的列表。如果`blobs`列表不为空,就会执行`for`循环中的代码块。`len(blobs)`会返回`blobs`列表中元素的个数,`range(len(blobs))`会创建一个从0到`len(blobs)`-1的整数序列。在每次循环中,变量`i`会被赋值为序列中的一个整数,从而可以通过`blobs[i]`来访问`blobs`列表中的元素。 循环中的代码块会逐个检查`blobs`列表中的元素,并比较它们的像素数量。这段代码的目的是找到`blobs`列表中像素数量最多的元素,并将其赋值给`largest_blob`变量。在每次循环中,如果当前元素的像素数量比`most_pixels`变量中的值还要多,就将`most_pixels`更新为当前元素的像素数量,并将`largest_blob`更新为当前元素的索引值。最终,`largest_blob`变量将保存像素数量最多的元素在`blobs`列表中的索引值。

相关推荐

import sensor, image, time,math,pyb from pyb import UART,LED import json import ustruct sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) # must be turned off for color tracking sensor.set_auto_whitebal(False) # must be turned off for color tracking red_threshold_01=(10, 100, 127, 32, -43, 67) clock = time.clock() uart = UART(3,115200) #定义串口3变量 uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters def find_max(blobs): #定义寻找色块面积最大的函数 max_size=0 for blob in blobs: if blob.pixels() > max_size: max_blob=blob max_size = blob.pixels() return max_blob def sending_data(cx,cy,cw,ch): global uart; #frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B]; #data = bytearray(frame) data = ustruct.pack("<bbhhhhb", #格式为俩个字符俩个短整型(2字节) 0x2C, #帧头1 0x12, #帧头2 int(cx), # up sample by 4 #数据1 int(cy), # up sample by 4 #数据2 int(cw), # up sample by 4 #数据1 int(ch), # up sample by 4 #数据2 0x5B) uart.write(data); #必须要传入一个字节数组 while(True): clock.tick() img = sensor.snapshot() blobs = img.find_blobs([red_threshold_01]) cx=0;cy=0; if blobs: max_b = find_max(blobs) #如果找到了目标颜色 cx=max_b[5] cy=max_b[6] cw=max_b[2] ch=max_b[3] img.draw_rectangle(max_b[0:4]) # rect img.draw_cross(max_b[5], max_b[6]) # cx, cy FH = bytearray([0x2C,0x12,cx,cy,cw,ch,0x5B]) #sending_data(cx,cy,cw,ch) uart.write(FH)

def state_deflection_angle(roi_blobs_result): ''' 说明:偏转状态值返回 ''' # ROI区域权重值 #ROIS_WEIGHT = [1, 1, 1, 1] ROIS_WEIGHT = [1, 0, 0, 1] state_crossing = False deflection_angle = 0 down_center = 0 center_num = 0 # 偏转值计算,ROI中心区域X值 centroid_sum = roi_blobs_result['up']['cx']*ROIS_WEIGHT[0] + roi_blobs_result['middle_up']['cx']*ROIS_WEIGHT[1] \ + roi_blobs_result['middle_down']['cx']*ROIS_WEIGHT[2] + roi_blobs_result['down']['cx']*ROIS_WEIGHT[3] if roi_blobs_result['up']['blob_flag']: center_num += ROIS_WEIGHT[0] if roi_blobs_result['middle_up']['blob_flag']: center_num += ROIS_WEIGHT[1] if roi_blobs_result['middle_down']['blob_flag']: center_num += ROIS_WEIGHT[2] if roi_blobs_result['down']['blob_flag']: center_num += ROIS_WEIGHT[3] center_pos = centroid_sum / (ROIS_WEIGHT[0]+ROIS_WEIGHT[1]+ROIS_WEIGHT[2]+ROIS_WEIGHT[3]) deflection_angle = (IMG_WIDTH/2)- center_pos # 判断两侧ROI区域检测到黑色线 if roi_blobs_result['left']['blob_flag'] and roi_blobs_result['right']['blob_flag']: # 判断两侧ROI区域检测到黑色线处于图像下方1/3处 if roi_blobs_result['left']['cy'] <= ((IMG_HEIGHT/3)) and roi_blobs_result['right']['cy'] <= ((IMG_HEIGHT/3)): # 当最下方ROI区域的黑线宽度大于140像素(检测到路口) if roi_blobs_result['down']['w'] > 235: state_crossing = True #elif roi_blobs_result['up']['blob_flag']: #state_crossing = True return down_center, state_crossing, deflection_angle 详细剖析里面的值都是怎么计算的

# Single Color Code Tracking Example # # This example shows off single color code tracking using the CanMV Cam. # # A color code is a blob composed of two or more colors. The example below will # only track colored objects which have both the colors below in them. import sensor, image, time, math # Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max) # The below thresholds track in general red/green things. You may wish to tune them... thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds -> index is 0 so code == (1 << 0) (30, 100, -64, -8, -32, 32)] # generic_green_thresholds -> index is 1 so code == (1 << 1) # Codes are or'ed together when "merge=True" for "find_blobs". sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) # must be turned off for color tracking sensor.set_auto_whitebal(False) # must be turned off for color tracking clock = time.clock() # Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are # returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the # camera resolution. "merge=True" must be set to merge overlapping color blobs for color codes. while(True): clock.tick() img = sensor.snapshot() for blob in img.find_blobs(thresholds, pixels_threshold=100, area_threshold=100, merge=True): if blob.code() == 3: # r/g code == (1 << 1) | (1 << 0) # These values depend on the blob not being circular - otherwise they will be shaky. # if blob.elongation() > 0.5: # img.draw_edges(blob.min_corners(), color=(255,0,0)) # img.draw_line(blob.major_axis_line(), color=(0,255,0)) # img.draw_line(blob.minor_axis_line(), color=(0,0,255)) # These values are stable all the time. img.draw_rectangle(blob.rect()) img.draw_cross(blob.cx(), blob.cy()) # Note - the blob rotation is unique to 0-180 only. img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20) print(clock.fps())

最新推荐

recommend-type

华为OD机试D卷 - 用连续自然数之和来表达整数 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

输出这段Python代码输出所有3位整数中,个位是5且是3的倍数的整数

``` for i in range(100,1000): if i%10 == 5 and i%3 == 0: print(i) ``` 输出结果: ``` 105 135 165 195 225 255 285 315 345 375 405 435 465 495 525 555 585 615 645 675 705 735 765 795 825 855 885 915 945 975 ```
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。