首页 > 其他分享 >2023年亚太杯A题:果园采摘机器人的图像识别,一二题

2023年亚太杯A题:果园采摘机器人的图像识别,一二题

时间:2024-06-02 23:33:34浏览次数:11  
标签:plt 图像识别 area cv2 采摘 contours 2023 np mianji

问题一:基于附件1中提供的可收获苹果的图像数据集,提取图像特征,建立数学模型,计算每幅图像中的苹果的数量,并绘制附件1中所有苹果的分布直方图。

对于自动采摘机器人,首要的能力就是识别出苹果对象,因此如何从画面(图像)中准确的识别出苹果对象对于自动采摘机器人有重要影响。附件1给出了200张有苹果对象的图像,要计算出每个图像中苹果的数量,并分析附件1中苹果的数量分布。考虑从颜色空间(HSV,Hue Saturation Value),通过对不同色调、明度和饱和度的识别,结合轮廓检测对苹果与周围环境做出识别,并进行计数。

import os
import cv2
import numpy as  np
# 图片文件夹路径
folder_path = 'D:/math_model/2023yatai/Attachment/Attachment 1'
image_files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
count = []
for file in image_files:
    # 读取图片
    image_path = os.path.join(folder_path, file)
    img = cv2.imread(image_path)
    # 将图片变为灰度图片
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 进行腐蚀膨胀操作
    kernel = np.ones((2, 2), np.uint8)
    erosion = cv2.erode(gray, kernel, iterations=5)  # 腐蚀
    dilation = cv2.dilate(erosion, kernel, iterations=5)  # 膨胀
    
    # 颜色阈值化提取红色区域
    lower_red = np.array([20, 0, 100])
    upper_red = np.array([80, 100, 255])
    mask = cv2.inRange(img, lower_red, upper_red)
    '''
    # 定义红色苹果的HSV范围
    lower_red = np.array([0, 50, 50])
    upper_red = np.array([10, 255, 255])
    mask_red = cv2.inRange(img, lower_red, upper_red)

    # 定义青色苹果的HSV范围
    lower_green = np.array([35, 50, 50])
    upper_green = np.array([85, 255, 255])
    mask_green = cv2.inRange(img, lower_green, upper_green)

    # 合并红色和青色苹果的掩码
    mask = cv2.bitwise_or(mask_red, mask_green)
    '''
    # 找出红色区域的轮廓
    contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # 建立空数组,放减去最小面积的连通域
    contours_filtered = []

    # 设定面积阈值
    mianji = []
    for contour in contours:
        area = cv2.contourArea(contour)
        mianji.append(area)
    '''
    mianji = [x for i, x in enumerate(mianji) if x not in mianji[:i]]  #去重
    #mianji = list(filter(lambda x: x != 0, mianji))  #删去0
    #mianji = [x for x in mianji if x >= 30]
    mianji = sorted(mianji) 
    min_area = np.median(mianji)
    '''
    min_area = np.max(mianji)/80
    # 过滤面积太小的连通域
    for contour in contours:
        area = cv2.contourArea(contour)
        if area > min_area:
            contours_filtered.append(contour)

    # 绘制红色区域的轮廓并计数
    cv2.drawContours(img, contours_filtered, -1, (0, 0, 255), 1)
    apple_count = len(contours_filtered)
    if apple_count > 100:
        apple_count = apple_count*0.7
    count.append(apple_count)
count_all = np.sum(count)


import matplotlib.pyplot as plt
plt.hist(count, bins=30, density=True, alpha=0.5,
         histtype='stepfilled',  color='steelblue',
         edgecolor='none')
plt.title('Histogram of apple count distribution')
plt.xlabel('Number of apples')
plt.ylabel('Frequency')
# 显示数值(除了0)
n, bins, patches = plt.hist(count, bins=30, color='skyblue', edgecolor='black', alpha=0.7)
for i in range(len(patches)):
    if n[i] != 0:
        plt.text(patches[i].get_x() + patches[i].get_width() / 2, patches[i].get_height(),
                 str(int(n[i])), ha='center', va='bottom')
#plt.savefig('D:/math_model/2023yatai/图/Histogram of apple count distribution.png', dpi = 600) #保存图片
plt.show()

700cdc310067481f801372c8c932f2c3.png

#%%  拼接几个图展示
def imge_single(i):
    ii = str(i)
    img = cv2.imread(r'D:/math_model/2023yatai/Attachment/Attachment 1/' + ii +'.jpg', 1)  # 读取图片

    # 将图片变为灰度图片
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 进行腐蚀膨胀操作
    kernel = np.ones((2, 2), np.uint8)
    erosion = cv2.erode(gray, kernel, iterations=5)  # 腐蚀
    dilation = cv2.dilate(erosion, kernel, iterations=5)  # 膨胀
    lower_red = np.array([20, 0, 100])
    upper_red = np.array([80, 100, 255])
    mask = cv2.inRange(img, lower_red, upper_red)
    contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    contours_filtered = []
    mianji = []
    for contour in contours:
        area = cv2.contourArea(contour)
        mianji.append(area)
    min_area = np.max(mianji)/80
    cv2.contourArea
    for contour in contours:
        area = cv2.contourArea(contour)
        if area > min_area:
            contours_filtered.append(contour)
    # 绘制红色区域的轮廓并计数
    cv2.drawContours(img, contours_filtered, -1, (0, 0, 255), 1)
    apple_count = len(contours_filtered)
    # 在图像上显示苹果数量
    cv2.putText(img, f"Apple Count: {apple_count}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (225, 25, 25), 2)
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))  # 将BGR图像转换为RGB格式
    plt.axis('off')  # 不显示坐标轴


plt.figure()
plt.tight_layout()
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, \
    wspace=0.0005, hspace=0.1)
plt.subplot(2,2,1)
imge_single(55)
plt.subplot(2,2,2)
imge_single(2)
plt.subplot(2,2,3)
imge_single(7)
plt.subplot(2,2,4)
imge_single(11)
#plt.savefig('D:/math_model/2023yatai/图/苹果拼图', dpi=500, bbox_inches='tight')  # 保存为JPEG格式,设置dpi和bbox_inches参数
plt.show()

52b8c599913c4f18be91c090dbebcb50.png

问题二:根据附件1中提供的可收获苹果的图像数据集,以图像的左下角为坐标原点,确定每个图像中苹果的位置,并绘制附件1中所有苹果的几何坐标的二维散点图。

对于前方的苹果,人类可以通过感觉精准分摘取,但机器人没有感官,它只能通过数字定位去获取苹果的位置。因此,识别图像中每个苹果的位置,并以图像左下角为原点,精准地给出苹果的坐标就很有必要。考虑在问题一的基础上,针对问题一已经找到的苹果,输出其中心点的位置坐标。苹果位置的分布规律在散点图中并不明显。从图中只可以看出四周的苹果分布会少一些,具体哪一个位置分布最广并不清晰。所以考虑使用热力图呈现图像中的苹果位置分布规律。

import os
import cv2
import numpy as np
import matplotlib.pyplot as plt

# 图片文件夹路径
folder_path = 'D:/math_model/2023yatai/Attachment/Attachment 1'
image_files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
all_apple_positions = []

for file in image_files:
    # 读取图片
    image_path = os.path.join(folder_path, file)
    img = cv2.imread(image_path)
    # 将图片变为灰度图片
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 进行腐蚀膨胀操作
    kernel = np.ones((2, 2), np.uint8)
    erosion = cv2.erode(gray, kernel, iterations=5)  # 腐蚀
    dilation = cv2.dilate(erosion, kernel, iterations=5)  # 膨胀
    
    # 颜色阈值化提取红色区域
    lower_red = np.array([20, 0, 100])
    upper_red = np.array([80, 100, 255])
    mask = cv2.inRange(img, lower_red, upper_red)

    # 找出红色区域的轮廓
    contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # 建立空数组,放减去最小面积的连通域
    contours_filtered = []

    # 设定面积阈值
    mianji = []
    for contour in contours:
        area = cv2.contourArea(contour)
        mianji.append(area)
    '''
    mianji = [x for i, x in enumerate(mianji) if x not in mianji[:i]]  # 去重
    mianji = list(filter(lambda x: x != 0, mianji))  # 删去0
    mianji = sorted(mianji)
    min_area = np.median(mianji)
    '''
    min_area = np.max(mianji)/80
    # 过滤面积太小的连通域,并绘制红色区域的轮廓
    for contour in contours:
        area = cv2.contourArea(contour)
        if area > min_area:
            contours_filtered.append(contour)
            # 计算中心点位置
            M = cv2.moments(contour)
            if M["m00"] != 0:
                center_x = int(M["m10"] / M["m00"])
                center_y = int(M["m01"] / M["m00"])
                all_apple_positions.append((center_x, center_y))

# 绘制所有苹果位置的二维散点图
x_coords, y_coords = zip(*all_apple_positions)
plt.scatter(x_coords, y_coords)
plt.xlabel('Horizontal position')
plt.ylabel('Vertical position')
plt.title('Apple location scatterplot')
#plt.savefig('D:/math_model/2023yatai/图/散点图(不建模-备用).png', dpi = 600)
plt.show()

import seaborn as sns
# 绘制散点图热力图
plt.figure(figsize=(10, 6))
sns.kdeplot(x=x_coords, y=y_coords, cmap="Reds", fill=True, bw_adjust=0.5)
plt.title('Heat map of the geometric coordinates of all apples', fontsize = 16)
plt.xlabel('Horizontal position', fontsize = 14)
plt.ylabel('Vertical position', fontsize = 14)
plt.gca().invert_yaxis()
#plt.savefig('D:/math_model/2023yatai/图/散点热力图.png', dpi = 600)
plt.show()

a39cc1ff8d8d465e93e4ab8dec490807.png8eec7e4a78214fcbb94bde947124a136.png

 

 

标签:plt,图像识别,area,cv2,采摘,contours,2023,np,mianji
From: https://blog.csdn.net/wodertianna/article/details/138250670

相关文章

  • 202305青少年软件编程(Python)等级考试试卷(四级)
    第1题【单选题】有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。问第n年的时候,共有多少头母牛?由递推法可推测,当年数小于等于4的时候,第几年就是有几头牛,即a[1]=1;a[2]=2;a[3]=3;a[4]=4。当n大于4的时候,这时候第......
  • 【计算机毕业设计】谷物识别系统Python+人工智能深度学习+TensorFlow+卷积算法网络模
    谷物识别系统,本系统使用Python作为主要编程语言,通过TensorFlow搭建ResNet50卷积神经算法网络模型,通过对11种谷物图片数据集('大米','小米','燕麦','玉米渣','红豆','绿豆','花生仁','荞麦','黄豆','黑米','黑豆')进行训练......
  • 墨天轮《2023年中国数据库行业年度分析报告》正式发布!
    为明晰发展脉络,把握未来趋势,墨天轮于5月29日正式发布 《2023年中国数据库年度行业分析报告》。该报告由墨天轮联合业界专家学者共同编写,共330页,旨在梳理和洞察中国数据库行业的发展趋势、技术创新、市场动态以及面临的挑战,为数据库行业的产学研用提供有价值的参考信息,推动行业的......
  • 2023 蓝桥杯国赛
    vp了3h。AWA(想错了,也没手玩),B不会(应该是欧拉定理,忘了),H40%(背不过板子)。其他过了H\(O(n^2\logn)\)本地1s+,I本地3.4s/jk,想了下这么典的问题应该没有更优做法。相信评测机大部分题都随手测了一下,只拍了E(二分)I(点分治),FH(正解)I也值得拍。今天状态不错,几乎没挂分,也没怎么调,......
  • 2023年中国高校计算机大赛-团队程序设计天梯赛(GPLT)上海理工大学校内选拔赛 (vp + 补题
    比赛主页:https://ac.nowcoder.com/acm/contest/52244AXorBProblem思路:如果i!=j代表(i,j)&(j,i)是两对,也就是说如果i==j代表只有一对,综上得出公式cnt[i]*cnt[i]的累加就是我要的答案Code:#include<bits/stdc++.h>usingnamespacestd;typedeflo......
  • 关于 IDEA 2023.3.1总管理配置maven路径
    先调出主页面,再选择主页面中的maven路径配置1、调出主页面. 在设置中搜索System,选中SystemSettings模块,取消Confirm和Reopen模块的勾选     2、重新启动进入主页面点击Customise中的Allsettings,进入总设置,在此进行maven配置即可......
  • 【赛题样题】【大数据应用开发】2023年全国职业院校技能大赛高职组“大数据应用开发”
    2023年全国职业院校技能大赛赛题第03套赛项名称:        大数据应用开发        英文名称: BigDataApplicationDevelopment 赛项组别:        高等职业教育组               赛项编号:            ......
  • 【赛题样题】【大数据应用开发】2023年全国职业院校技能大赛高职组“大数据应用开发”
    2023年全国职业院校技能大赛赛题第04套赛项名称:        大数据应用开发        英文名称: BigDataApplicationDevelopment 赛项组别:        高等职业教育组               赛项编号:             ......
  • 20231325 贾罗祁 《Python程序设计》实验四报告
    20231325贾罗祁2023-2024-2《Python程序设计》实验四报告课程:《Python程序设计》班级:2313姓名:贾罗祁学号:20231325实验教师:王志强实验日期:2024年5月15日必修/选修:公选课1.实验内容Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。课......
  • P9327 [CCC 2023 S4] Minimum Cost Roads
    原题链接题解贪心,我管这种叫做策略贪心,即按照某种顺序或者角度去贪心可以得到最优解既然题目要求任意两点间最短路最小的同时,价格也最小,那么我们就按长度为第一关键字,花费为第二关键字排序。然后遍历所有边看看这条边能否使用遍历过程的策略:如果这条边加入后,这条边两端的节点......