From 0d3b8dc7475bf28582903510e9242d9b7f492031 Mon Sep 17 00:00:00 2001 From: zhouchengrong Date: Mon, 29 Apr 2024 10:49:38 +0800 Subject: [PATCH] =?UTF-8?q?feat=20generate=20=E4=BF=AE=E6=94=B9=E4=B8=AD?= =?UTF-8?q?=E5=BF=83=E7=82=B9=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generate_image/utils/image_processing.py | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/app/service/generate_image/utils/image_processing.py b/app/service/generate_image/utils/image_processing.py index c2910ee..5f818a5 100644 --- a/app/service/generate_image/utils/image_processing.py +++ b/app/service/generate_image/utils/image_processing.py @@ -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: