如何通过图像处理或机器学习统计半切玉米的籽粒数量?
玉米籽粒识别问题及尝试方案
我已尝试使用OpenCV实现玉米籽粒识别功能,执行了以下处理步骤:
- 将图像转换为灰度图(Grayscale)
- 应用阈值处理(Apply threshold)
- 提取并绘制轮廓(Get Contours and draw them)
- 输出轮廓数量(Print the number of contours)
尝试1:调整了阈值和轮廓面积参数,但系统将整根玉米识别为单个籽粒,无法逐个识别。
尝试2:调整色调(Hue)值以突出黄色籽粒,未得到满意结果。
输出结果:
源图像:
# 加载图像 img = cv2.imread('img2.png') img = cv2.resize(img,(250,250)) # 将图像转换为灰度图 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # gray = cv2.bitwise_not(gray) cv2.imshow("gray", gray) # 应用阈值处理得到二值图像 ret, thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU) # 在二值图像中查找轮廓 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 遍历轮廓并统计籽粒数量 num_seeds = 0 for contour in contours: area = cv2.contourArea(contour) if area > 100 : # 过滤小轮廓 num_seeds += 1 # 输出识别到的籽粒数量 print("Number of seeds:", num_seeds) # 显示绘制了轮廓的图像 cv2.drawContours(img, contours, -1, (0, 0, 255), 2) cv2.imshow('thresh', thresh) cv2.imshow('img', img) cv2.waitKey(0) cv2.destroyAllWindows()
内容的提问来源于stack exchange,提问作者snehit vaddi
相关产品推荐
相关产品推荐

