python图像学习2 简单cv2识别单一颜色物体数量

import cv2
import pandas as pd
# 存储每张图片的过滤后的色块数目
blobCounts = []

# 遍历200张图片
for i in range(1, 201):
    # 读取图像
    src = cv2.imread(r"c:\Users\ASUS\Desktop\image machine learning\Attachment\Attachment\Attachment 1\{}.jpg".format(i))
    if src is None:
        print(f"Error: Unable to load image {i}.jpg")
        continue  # 如果图像加载失败,跳过当前循环
# 检查图像是否加载成功
    # 高斯滤波
    blurredImage = cv2.GaussianBlur(src, (3, 3), 0)

    # R通道减去G通道
    diffImage = cv2.subtract(src[:, :, 2], src[:, :, 1])

    # 二值化
    thresholdValue = 64  # 阈值
    maxValue = 255  # 最大值
    _, binaryImage = cv2.threshold(diffImage, thresholdValue, maxValue, cv2.THRESH_BINARY)

    # 连通组件分析
    numLabels, labels, stats, centroids = cv2.connectedComponentsWithStats(binaryImage)

    # 过滤小色块并统计数量
    minAreaThreshold = 10  # 最小面积阈值
    filteredBlobCount = 0

    for j in range(1, numLabels):
        area = stats[j, cv2.CC_STAT_AREA]

        if area >= minAreaThreshold:
            fi
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值