feat generate 新增色阶调整 亮度调整
This commit is contained in:
@@ -207,6 +207,87 @@ def generate_category_recognition(image, gender):
|
|||||||
return category, scores, image
|
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<Hin
|
||||||
|
Hin = min(high, 255) # Hin, 白场阈值, Sin<Hin<=255
|
||||||
|
difIn = Hin - Sin
|
||||||
|
V1 = np.array([(min(max(255 * (i - Sin) / difIn, 0), 255)) for i in range(256)])
|
||||||
|
# 灰场伽马调节
|
||||||
|
gradMed = np.median(img[:, :, ch]) # 拉伸前的中值
|
||||||
|
Mt = V1[int(gradMed)] / 128. # 拉伸后的映射值
|
||||||
|
V2 = 255 * np.power(V1 / 255, 1 / Mt) # 伽马调节
|
||||||
|
# 输出线性拉伸
|
||||||
|
Sout, Hout = 5, 250 # Sout 输出黑场阈值, Hout 输出白场阈值
|
||||||
|
difOut = Hout - Sout
|
||||||
|
table[0, :, ch] = np.array([(min(max(Sout + difOut * V2[i] / 255, 0), 255)) for i in range(256)])
|
||||||
|
return cv2.LUT(img, table)
|
||||||
|
|
||||||
|
|
||||||
|
def luminance_adjust(alpha, img):
|
||||||
|
if alpha > 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__':
|
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")
|
image = cv2.imread("1.png")
|
||||||
remove_background(image)
|
remove_background(image)
|
||||||
|
|||||||
Reference in New Issue
Block a user