From d448bed3af883f855ad35576f796701a36ddfe9a Mon Sep 17 00:00:00 2001 From: zhouchengrong Date: Thu, 25 Apr 2024 17:58:47 +0800 Subject: [PATCH] =?UTF-8?q?feat=20generate=20=E6=96=B0=E5=A2=9E=E8=89=B2?= =?UTF-8?q?=E9=98=B6=E8=B0=83=E6=95=B4=20=E4=BA=AE=E5=BA=A6=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../generate_image/utils/image_processing.py | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/app/service/generate_image/utils/image_processing.py b/app/service/generate_image/utils/image_processing.py index 303fc52..4862f6c 100644 --- a/app/service/generate_image/utils/image_processing.py +++ b/app/service/generate_image/utils/image_processing.py @@ -207,6 +207,87 @@ def generate_category_recognition(image, gender): return category, scores, image +import cv2 +import numpy as np + + +def autoLevels(img, cutoff=0.1): + channels = img.shape[2] # h,w,ch + table = np.zeros((1, 256, 3), np.uint8) + for ch in range(channels): + # cutoff=0.1, 计算 0.1%, 99.9% 分位的灰度值 + low = np.percentile(img[:, :, ch], q=cutoff) # ch 通道, cutoff=0.1, 0.1 分位的灰度值 + high = np.percentile(img[:, :, ch], q=100 - cutoff) # 99.9 分位的灰度值, [0, high] 占比99.9% + # 输入动态线性拉伸 + Sin = min(max(low, 0), high - 2) # Sin, 黑场阈值, 0<=Sin 0: + img_out = img * (1 - alpha) + alpha * 255.0 + else: + img_out = img * (1 + alpha) + + return np.array(img_out, dtype='uint8') + + # 14.14 Photoshop 自动色阶调整算法 + + if __name__ == '__main__': + # Photoshop 自动色阶调整算法 + img = cv2.imread("2.png", flags=1) # 读取彩色图像 + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 转换为灰度图像 + print("cutoff={}, minG={}, maxG={}".format(0.0, gray.min(), gray.min())) + + # 色阶手动调整 + # equManual = levelsAdjust(img, 63, 205, 0.8, 10, 245) # 手动调节 + # 色阶自动调整 + cutoff = 1.0 # 截断比例, 建议范围 [0.0,1.0] + # cv2.imwrite("source.png", img) + equAuto = autoLevels(img, cutoff) + # cv2.imwrite("levels.png", equAuto) + luminance = luminance_adjust(0.3, equAuto) + # cv2.imwrite("luminance.png", luminance) + # + # # 将图像转换为灰度 + # gray = cv2.cvtColor(luminance, cv2.COLOR_BGR2GRAY) + # + # # 使用Canny边缘检测算法检测图像的边缘 + # edges = cv2.Canny(gray, 150, 200) + # + # # 对边缘进行膨胀操作,增强轮廓 + # kernel = np.ones((1, 1), np.uint8) + # dilated_edges = cv2.dilate(edges, kernel, iterations=1) + # + # # 创建一个与原始图像相同大小的空白图像 + # # result = np.zeros_like(luminance) + # + # # 将增强后的轮廓叠加到原始图像上 + # luminance[dilated_edges != 0] = (255, 255, 255) + + remove_bg_img = remove_background(luminance) + # cv2.imwrite("remove_bg_img.png", remove_bg_img) + + print(1) + cv2.imshow("source", img) + cv2.imshow("levels", equAuto) + cv2.imshow("luminance", luminance) + # cv2.imshow("dilated_edges", luminance) + cv2.imshow("remove_bg_img", remove_bg_img) + + cv2.waitKey(0) + image = cv2.imread("1.png") remove_background(image)