如何使一幅图像的渐变外观与另一幅图像相同?
How can I make the gradient appearance of one image equal to the other?
我有一个 图像 A 有阴影外观,我有一个 图像 B 是纯色的。我想在 python 中将图像 A 的阴影从 OpenCV 复制到图像 B。
如果我没记错的话是不是和图片的渐变有关?
您可以使用 ImageMagick 或 Python Wand 轻松做到这一点,或者在 Python/OpenCV 中付出更多努力。
请注意 Python Wand 使用 ImageMagick。
ImageMagick 已在大多数 Linux 发行版中可用,并且可用于 Windows 和 Mac OSX.
该过程是将图像B转换为灰度,然后使用强光合成方法将其与图像A合成。
图片 A:
图片 B:
使用 ImageMagick(Unix 语法),代码为:
convert skirt_A.png \
\( skirt_B.png -colorspace gray -level 25x100% \) \
-compose hardlight -composite skirt_A_shaded.png
图像 B 的阴影图像 A:
更改级别运算符中的 0.25 以使阴影更暗或更亮。
使用Python Wand 代码将是:
from wand.image import Image
from wand.display import display
with Image(filename='skirt_A.png') as bimg:
with Image(filename='skirt_B.png') as fimg:
fimg.transform_colorspace('gray')
fimg.level(black=0.25,white=1,channel='all_channels')
bimg.composite_channel('all_channels', fimg, 'hard_light', 0, 0)
bimg.save(filename='skirt_A_shaded.png')
display(bimg)
图像 B 的阴影图像 A:
使用Python/OpenCV,代码为:
import cv2
import numpy as np
# read image_A and convert to float in range 0 to 1
image_A = cv2.imread('skirt_A.png').astype("float32") / 255.0
# read image_B as grayscale and convert to float in range 0 to 1
image_B = cv2.imread('skirt_B.png',0).astype("float32") / 255.0
# convert image_B from grayscale to 3 equal channels as rgb so that the image multiplication in the hard light compositing will work properly
image_B = cv2.cvtColor(image_B,cv2.COLOR_GRAY2RGB)
# apply linear transform to stretch image_B to make shading darker
# y = A*x+B
# x=1 -> y=1; x=0.25 -> y=0
# 1 = A + B
# 0 = 0.25*A + B
# Solve simultaneous equations to get:
# A = 1.33
# B = -0.33
image_B = 1.33 * image_B -0.33
# threshold image_B and invert
thresh = cv2.threshold(image_B,0.5,1,cv2.THRESH_BINARY)[1]
thresh_inv = 1-thresh
# do hard light composite and convert to uint8 in range 0 to 255
# see CSS specs at https://www.w3.org/TR/compositing-1/#blendinghardlight
low = 2.0 * image_A * image_B
high = 1 - 2.0 * (1-image_A) * (1-image_B)
result = ( 255 * (low * thresh_inv + high * thresh) ).clip(0, 255).astype(np.uint8)
# show results
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# save results
cv2.imwrite('skirt_A_shaded.png', result)
图像 B 的阴影图像 A:
我有一个 图像 A 有阴影外观,我有一个 图像 B 是纯色的。我想在 python 中将图像 A 的阴影从 OpenCV 复制到图像 B。 如果我没记错的话是不是和图片的渐变有关?
您可以使用 ImageMagick 或 Python Wand 轻松做到这一点,或者在 Python/OpenCV 中付出更多努力。
请注意 Python Wand 使用 ImageMagick。
ImageMagick 已在大多数 Linux 发行版中可用,并且可用于 Windows 和 Mac OSX.
该过程是将图像B转换为灰度,然后使用强光合成方法将其与图像A合成。
图片 A:
图片 B:
使用 ImageMagick(Unix 语法),代码为:
convert skirt_A.png \
\( skirt_B.png -colorspace gray -level 25x100% \) \
-compose hardlight -composite skirt_A_shaded.png
图像 B 的阴影图像 A:
更改级别运算符中的 0.25 以使阴影更暗或更亮。
使用Python Wand 代码将是:
from wand.image import Image
from wand.display import display
with Image(filename='skirt_A.png') as bimg:
with Image(filename='skirt_B.png') as fimg:
fimg.transform_colorspace('gray')
fimg.level(black=0.25,white=1,channel='all_channels')
bimg.composite_channel('all_channels', fimg, 'hard_light', 0, 0)
bimg.save(filename='skirt_A_shaded.png')
display(bimg)
图像 B 的阴影图像 A:
使用Python/OpenCV,代码为:
import cv2
import numpy as np
# read image_A and convert to float in range 0 to 1
image_A = cv2.imread('skirt_A.png').astype("float32") / 255.0
# read image_B as grayscale and convert to float in range 0 to 1
image_B = cv2.imread('skirt_B.png',0).astype("float32") / 255.0
# convert image_B from grayscale to 3 equal channels as rgb so that the image multiplication in the hard light compositing will work properly
image_B = cv2.cvtColor(image_B,cv2.COLOR_GRAY2RGB)
# apply linear transform to stretch image_B to make shading darker
# y = A*x+B
# x=1 -> y=1; x=0.25 -> y=0
# 1 = A + B
# 0 = 0.25*A + B
# Solve simultaneous equations to get:
# A = 1.33
# B = -0.33
image_B = 1.33 * image_B -0.33
# threshold image_B and invert
thresh = cv2.threshold(image_B,0.5,1,cv2.THRESH_BINARY)[1]
thresh_inv = 1-thresh
# do hard light composite and convert to uint8 in range 0 to 255
# see CSS specs at https://www.w3.org/TR/compositing-1/#blendinghardlight
low = 2.0 * image_A * image_B
high = 1 - 2.0 * (1-image_A) * (1-image_B)
result = ( 255 * (low * thresh_inv + high * thresh) ).clip(0, 255).astype(np.uint8)
# show results
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
# save results
cv2.imwrite('skirt_A_shaded.png', result)
图像 B 的阴影图像 A: