feat generate 修改中心点判断

This commit is contained in:
zhouchengrong
2024-04-29 10:49:38 +08:00
parent 0c2cf1ec02
commit 0d3b8dc747

View File

@@ -153,11 +153,38 @@ def stain_detection(image, spot_size=100):
logger.info(f"{index + 1}发现了污点")
return False, None
# 中心区域检测
center_x, center_y = width // 2, height // 2
center_image = image[center_y - spot_size // 2:center_y + spot_size // 2, center_x - spot_size // 2:center_x + spot_size // 2]
gray_center_image = cv2.cvtColor(center_image, cv2.COLOR_BGR2GRAY)
white_threshold = 200 # 设定的接近最大值的阈值
is_pure_white = (gray_center_image >= white_threshold).all()
# 将图像转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 获取图像的中心点坐标
center_x, center_y = image.shape[1] // 2, image.shape[0] // 2
# 定义中心区域的大小
patch_size = 100
half_patch = patch_size // 2
# 提取中心区域
center_patch = gray[center_y - half_patch:center_y + half_patch, center_x - half_patch:center_x + half_patch]
# 设置阈值来检测纯白区域
_, thresh = cv2.threshold(center_patch, 254, 255, cv2.THRESH_BINARY)
# 寻找轮廓
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 过滤非连续的纯白区域
filtered_contours = [cnt for cnt in contours if cv2.contourArea(cnt) >= 50] # 根据面积进行过滤这里假设面积大于50的为连续区域
# 如果有连续的纯白区域存在
if filtered_contours:
# 将纯白区域替换为灰色
if DEBUG:
for cnt in filtered_contours:
x, y, w, h = cv2.boundingRect(cnt)
# 在原始图像上进行替换
image[y + center_y - half_patch:y + center_y - half_patch + h, x + center_x - half_patch:x + center_x - half_patch + w][thresh[y:y + h, x:x + w] == 255] = (128, 128, 128)
# 显示图像
cv2.imshow('Marked Image', image)
cv2.waitKey(0)
logger.info("中心区域存在连续的纯白区域")
is_pure_white = True
else:
logger.info("中心区域不存在连续的纯白区域")
is_pure_white = False
if is_pure_white:
return False, None
if DEBUG: