From eddf4b0971961b60d83816979c9c88c6c3a38b57 Mon Sep 17 00:00:00 2001 From: GG <905865530@qq.com> Date: Sun, 21 Jul 2024 23:25:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=A1=A5=E5=85=8520240529RGBtest3?= =?UTF-8?q?=E5=86=85=E8=83=9C=E5=93=A5=E7=9A=84lab=E8=89=B2=E5=BD=A9?= =?UTF-8?q?=E7=A9=BA=E9=97=B4test=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20240529RGBtest3/xs/20240711lab.py | 71 ++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 20240529RGBtest3/xs/20240711lab.py diff --git a/20240529RGBtest3/xs/20240711lab.py b/20240529RGBtest3/xs/20240711lab.py new file mode 100644 index 0000000..2523f54 --- /dev/null +++ b/20240529RGBtest3/xs/20240711lab.py @@ -0,0 +1,71 @@ +import cv2 +import numpy as np +import os + +# 读取文件夹中的所有图片文件 +def read_images_from_folder(folder): + images = [] + for filename in os.listdir(folder): + img = cv2.imread(os.path.join(folder, filename)) + if img is not None: + images.append((filename, img)) + return images + +# Lab颜色空间的a阈值分割,同时处理灰度值大于190的像素 +def threshold_lab_a_and_high_gray(image, lower_threshold=0, upper_threshold=20): + lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2Lab) + _, a, _ = cv2.split(lab_image) + + # 创建一个与a通道相同大小的黑色图像 + binary_image = np.zeros_like(a) + + # 将a通道中值在指定范围内的像素设置为白色(255) + binary_image[(a >= lower_threshold) & (a <= upper_threshold)] = 255 + + # 为灰度值大于190的像素创建二值图 + gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + high_gray_image = np.zeros_like(gray_image) + high_gray_image[gray_image > 170] = 255 + + # 从a通道阈值图中移除灰度值大于190的像素 + final_image = cv2.bitwise_and(binary_image, binary_image, mask=np.bitwise_not(high_gray_image)) + + return binary_image, high_gray_image, final_image + +# 拼接并显示所有图片 +def concatenate_images(original, images, filename, scale=0.5): + # 将所有单通道图像转换为三通道图像 + resized_imgs = [] + for img in images: + if len(img.shape) == 2: # 单通道图像 + img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) + # 缩放图像 + img = cv2.resize(img, (int(img.shape[1] * scale), int(img.shape[0] * scale))) + resized_imgs.append(img) + + # 将原图也转换为相同大小和缩放 + resized_original = cv2.resize(original, (int(original.shape[1] * scale), int(original.shape[0] * scale))) + + # 水平拼接第一行和第二行 + top_row = cv2.hconcat([resized_original, resized_imgs[0]]) + bottom_row = cv2.hconcat([resized_imgs[1], resized_imgs[2]]) + + # 垂直拼接所有行 + final_image = cv2.vconcat([top_row, bottom_row]) + + # 显示图片 + cv2.imshow(f"Combined Images - {filename}", final_image) + cv2.waitKey(0) + cv2.destroyAllWindows() + +def main(): + folder = r'F:\images' # 替换为你的文件夹路径 + images = read_images_from_folder(folder) + + for filename, image in images: + lab_thresh, high_gray, final_image = threshold_lab_a_and_high_gray(image, lower_threshold=115, upper_threshold=135) + concatenate_images(image, [lab_thresh, high_gray, final_image], filename, scale=0.5) # 添加缩放因子 + +if __name__ == "__main__": + main() +