寻找对图像进行按位XOR的方法 [英] Searching for a way to do Bitwise XOR on images
问题描述
我正在寻找一种方法来获取两个图像的按位异或命令行(或以可以在程序或脚本中实现的另一种方式).
I am looking for a way to get the Bitwise XOR of two images on the command line(or in another way that can be implemented in a program or script).
这将产生与在支持它的图片编辑器(Paint.NET,Photoshop等)中使用异或混合"模式相同的最终图片.
This should result in the same final picture as using the XOR Blending mode in picture editors that support it (Paint.NET, Photoshop, etc)
举例来说,假设我有图片A:
和图像B:
然后结果应如下所示:
其中最有趣的部分是,当您再次将图像C与图像B异或时,您将获得图像A的精确副本.
The fun part of this is of course, that when you XOR image C with image B again, you will get an exact copy of image A.
现在,我一直在互联网上寻找以编程方式执行此操作的方法,但是我什么也没发现.甚至ImageMagick也不支持对图像进行按位XOR.
Now, I have been looking all over the internet for a way to do this programmatically, but I have found nothing. Even ImageMagick does not support doing a bitwise XOR on images.
某人知道这样做的方法吗?
Does sombebody know a way to do this?
推荐答案
ImageMagick可以做到,尽管有点令人费解.一种方法是:
ImageMagick can do it, although it's a bit convoluted. One way is:
convert img1 img2 -fx "(((255*u)&(255*(1-v)))|((255*(1-u))&(255*v)))/255" img_out
(img1
,img2
,img_out
分别是两个输入和单个输出文件名).
(img1
,img2
,img_out
are the two input and single output file names respectively).
这有点丑陋(我敢肯定有人拥有比我更多的ImageMagick-fu可以清理它,但是它的工作原理是这样的:
It's a bit ugly (I'm sure someone with more ImageMagick-fu than me could clean it up but it works like this:
-
-fx "xxx"
基本上说对图像执行操作xxx
". 在上面的表达式中,u
和v
分别代表第一和第二输入图像.
-fx "xxx"
basically says "perform the operationxxx
on the image". In the expression above,u
andv
stand for the first and second input images respectively.
现在,-fx
仅以按位运算符的方式具有按位与&
和按位OR |
.
要重构按位异或,我们需要
Now, -fx
only has bitwise AND &
and bitwise OR |
in the way of bitwise operators.
To reconstruct bitwise XOR, we need
convert img1 img2 -fx "(u & NOT v) | (NOT u & v)" img_out
要获取NOT
(存在逻辑NOT
,但没有按位的NOT
),请记住,NOT x = 255-x
if x
是8位.
因此,要获得NOT u
,我们可以做255-u
,假设图像u
是8位的.
因此,ImageMagick命令将为:
To get the NOT
(there is a logical NOT
but no bitwise NOT
), we remember that NOT x = 255-x
if x
is 8-bit.
So to get NOT u
we can just do 255-u
, assuming image u
is 8-bit.
Hence, the ImageMagick command would be:
convert img1.png img2.img -fx "((255-u)&v)|(u&(255-v))" image_xor.png
-
这里的一个问题是,当ImageMagick执行
fx
时,它会标准化[0,1]
范围内的u
和v
中的所有像素,而不是我们期望的[0,255]
,并按位进行非整数螺钉塞满. The one problem here is that when ImageMagick does
fx
it normalises all the pixels inu
andv
in the range[0,1]
instead of[0,255]
as we expect, and doing bitwise on non-integers screws stuff up.因此,我们必须将上述表达式中
u
和v
的所有出现次数乘以255(因此按位运算有效),然后将255除以最后返回ImageMagick期望的范围[0,1]
.Hence, we have to multiply all occurrences of
u
andv
in the above expression by 255 (so the bitwise operations work), and divide by 255 at the very end to get back in the range[0,1]
that ImageMagick expects.这给了我们原始的命令,
This gives us the original command,
convert img1 img2 -fx "(((255*u)&(255*(1-v)))|((255*(1-u))&(255*v)))/255" img_out
Voila!
这篇关于寻找对图像进行按位XOR的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!