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