问题
I have an image A with shadow appearance in it and I have an image B which is of solid color. I want to copy shadows of image A to Image B from openCV in python. If I am not wrong it has something to do with gradient of the image? Image A Image B
回答1:
You can do that easily with ImageMagick or Python Wand or with a bit more effort in Python/OpenCV.
Note that Python Wand uses ImageMagick.
ImageMagick is already available on most Linux distributions and is available for Windows and Mac OSX.
The process is to convert image B to grayscale and then composite it with image A using the hard light compose method.
Image A:
Image B:
With ImageMagick (Unix syntax), the code would be:
convert skirt_A.png \
\( skirt_B.png -colorspace gray -level 25x100% \) \
-compose hardlight -composite skirt_A_shaded.png
Image A Shaded from Image B:
Change the 0.25 in the level operator to make the shading darker or lighter.
Using Python Wand the code would be:
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)
Image A Shaded from Image B:
With Python/OpenCV, the code would be:
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)
Image A Shaded from Image B:
来源:https://stackoverflow.com/questions/59504637/how-can-i-make-the-gradient-appearance-of-one-image-equal-to-the-other