Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使一个图像的渐变外观与另一个相等?_Python_Opencv_Gradient_Opencv3.0 - Fatal编程技术网

Python 如何使一个图像的渐变外观与另一个相等?

Python 如何使一个图像的渐变外观与另一个相等?,python,opencv,gradient,opencv3.0,Python,Opencv,Gradient,Opencv3.0,我有一个图像A,其中有阴影,我有一个图像B,它是纯色的。我想将图像A的阴影从python中的OpenCV复制到图像B。 若我并没有错的话,这和图像的梯度有关吗 您可以使用ImageMagick或Python Wand轻松地实现这一点,或者在Python/OpenCV中多做一些工作 请注意,Python Wand使用ImageMagick ImageMagick已在大多数Linux发行版上可用,并可用于Windows和Mac OSX 该过程是将图像B转换为灰度,然后使用硬光合成方法将其与图像A合成

我有一个图像A,其中有阴影,我有一个图像B,它是纯色的。我想将图像A的阴影从python中的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
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)
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:

在“级别”操作符中更改0.25以使着色更暗或更亮

使用Python魔杖代码如下:

convert skirt_A.png \
\( skirt_B.png -colorspace gray -level 25x100% \) \
-compose hardlight -composite skirt_A_shaded.png
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)
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:

使用Python/OpenCV,代码将是:

convert skirt_A.png \
\( skirt_B.png -colorspace gray -level 25x100% \) \
-compose hardlight -composite skirt_A_shaded.png
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)
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:


欢迎来到StackOverflow。本网站不作为一般问题解决门户。社区将帮助您获得一个有效的解决方案,但是您应该分享您的代码,显示您已经做了哪些不起作用的事情。阅读“如何创建一个最小的、可复制的示例”()、“什么是好的主题”()和“我如何提出一个好的问题”()?我们如何确定x和y的值?您已经在线性变换中选择了它(1,1)和(0,25)@FMW42只需根据需要进行调整,然后查看结果。找到一个对你来说合理的价值。我试着将它从0变为0.05,直到它看起来像你的图像。你所需要做的就是将0.25改为其他值,同时求解得到A和B常数。您还可以编写方程式,利用值0.25或其他值,让Python计算A和B常量。用1=A+B(这样A=1-B)和0=A*c+B求解y=A*x+B。解为B=-c/(1-c)和A=1-B=1+c/(1-c),其中c介于0和1之间,我使用了c=0.25谢谢@fmw42。它工作得很好。但它会稍微扭曲立体图像的颜色。我猜我必须根据系数,即A和B进行调整。问题是,图像在任何地方都会被灰度形式的修改图像_B修改,而不是精确的50%灰度。需要进一步调整,使其大部分为50%的灰色,阴影只会更暗。这并不容易。您可以尝试调整A和B常量,这可能会有所帮助。尝试使用0.20而不是0.25,以使结果更轻一些。根据需要进行调整。