基于Python+OpenCV计算苹果淀粉分解率的技术问询
苹果淀粉分解率自动化计算完整实现方案
需求背景
我是仁果类产业科研人员,在苹果成熟度试验中需要测定淀粉分解率:将苹果对半切开后用碘液处理,淀粉遇碘变黑,白色区域是已转化为糖的淀粉。之前靠对照淀粉分解图表估算百分比,现在想用Python实现自动化计算——先识别苹果边界,再统计黑、白像素数量并转化为百分比。以下是我写的部分代码,求完整实现方案。
已完成的苹果轮廓识别代码
import cv2 # 读取原始图像 img = cv2.imread('C:\Users\marnes\Downloads\starch-staining-patterns-in-Honeycrisp-applesx.png') # 转换为灰度图 img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 高斯模糊优化边缘检测效果 img_blur = cv2.GaussianBlur(img_gray, (3, 3), 0) # 显示中间结果 cv2.imshow('Original', img) cv2.waitKey(0) cv2.imshow('img_gray', img_gray) cv2.waitKey(0) cv2.imshow('img_blur', img_blur) cv2.waitKey(0) # Canny边缘检测 edges = cv2.Canny(image=img_blur, threshold1=100, threshold2=200) # 显示边缘检测结果 cv2.imshow('Canny Edge Detection', edges) cv2.waitKey(0) cv2.destroyAllWindows()
已尝试的黑白像素分割代码
import cv2 # 读取图像 image = cv2.imread('C:\Users\marnes\Downloads\starch-staining-patterns-in-Honeycrisp-applesx.png') # 转换为灰度图 img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 二值化阈值处理 ret, thresh = cv2.threshold(img_gray, 150, 255, cv2.THRESH_BINARY) # 显示二值化图像 cv2.imshow('Binary image', thresh) cv2.waitKey(0) cv2.imwrite('image_thres1.jpg', thresh) cv2.destroyAllWindows() # 检测轮廓(保留所有轮廓点) contours, hierarchy = cv2.findContours(image=thresh, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE) # 在原图上绘制轮廓 image_copy = image.copy() cv2.drawContours(image=image_copy, contours=contours, contourIdx=-1, color=(0, 50, 0), thickness=2, lineType=cv2.LINE_AA) # 显示轮廓绘制结果 cv2.imshow('None approximation', image_copy) cv2.waitKey(0) cv2.imwrite('contours_none_image1.jpg', image_copy) cv2.destroyAllWindows()
完整实现方案
核心思路
- 精准识别苹果区域,排除背景干扰
- 在苹果区域内区分黑(未分解淀粉)、白(已分解淀粉)像素
- 计算两类像素占比,得到淀粉分解率
完整代码
import cv2 import numpy as np def calculate_starch_decomposition_rate(image_path): # 1. 读取图像并预处理 img = cv2.imread(image_path) if img is None: print("无法读取图像,请检查路径是否正确") return None img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_blur = cv2.GaussianBlur(img_gray, (5, 5), 0) # 增大模糊核减少噪声干扰 # 2. 识别苹果轮廓,提取苹果区域掩码 # Canny边缘检测 edges = cv2.Canny(img_blur, threshold1=50, threshold2=150) # 形态学闭运算,填补边缘间隙 kernel = np.ones((5,5), np.uint8) edges_closed = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel) # 查找轮廓,筛选最大轮廓(苹果区域) contours, _ = cv2.findContours(edges_closed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if not contours: print("未检测到苹果轮廓") return None # 取面积最大的轮廓作为苹果区域 apple_contour = max(contours, key=cv2.contourArea) # 创建苹果区域掩码 mask = np.zeros_like(img_gray) cv2.drawContours(mask, [apple_contour], -1, 255, thickness=cv2.FILLED) # 3. 分割苹果区域内的黑白像素 # 提取苹果区域内的灰度图 masked_gray = cv2.bitwise_and(img_gray, img_gray, mask=mask) # 自适应二值化,适配光照不均场景 thresh = cv2.adaptiveThreshold(masked_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) # 用掩码过滤,确保只统计苹果区域内的像素 thresh_masked = cv2.bitwise_and(thresh, thresh, mask=mask) # 4. 统计像素数量并计算百分比 total_apple_pixels = cv2.countNonZero(mask) black_pixels = cv2.countNonZero(thresh_masked) # 黑色区域(未分解淀粉)像素数 white_pixels = total_apple_pixels - black_pixels # 白色区域(已分解淀粉)像素数 starch_decomposition_rate = (white_pixels / total_apple_pixels) * 100 starch_remaining_rate = (black_pixels / total_apple_pixels) * 100 # 5. 可视化结果(可选) result_img = img.copy() cv2.drawContours(result_img, [apple_contour], -1, (0, 255, 0), 2) cv2.putText(result_img, f"淀粉分解率: {starch_decomposition_rate:.2f}%", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.putText(result_img, f"剩余淀粉占比: {starch_remaining_rate:.2f}%", (20, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.imshow('计算结果', result_img) cv2.waitKey(0) cv2.destroyAllWindows() return starch_decomposition_rate, starch_remaining_rate # 调用函数,替换为你的图像路径 image_path = 'C:\Users\marnes\Downloads\starch-staining-patterns-in-Honeycrisp-applesx.png' decomposition_rate, remaining_rate = calculate_starch_decomposition_rate(image_path) if decomposition_rate is not None: print(f"苹果淀粉分解率: {decomposition_rate:.2f}%") print(f"剩余淀粉占比: {remaining_rate:.2f}%")
代码说明
- 轮廓优化:用形态学闭运算填补边缘间隙,确保苹果轮廓完整;筛选最大轮廓排除背景干扰
- 自适应阈值:相比固定阈值,更能应对图像光照不均的情况,精准区分黑白区域
- 掩码过滤:全程用苹果区域掩码限制统计范围,避免背景像素干扰计算结果
- 可视化:输出标注结果的图像,方便直观验证计算准确性
内容的提问来源于stack exchange,提问作者marnes
相关产品推荐
相关产品推荐

