python-ImageChops.difference的定义
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-ImageChops.difference的定义,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2702字,纯文字阅读大概需要4分钟。
内容图文
![python-ImageChops.difference的定义](/upload/InfoBanner/zyjiaocheng/663/bd5f7c646780401aaca31a748cb09af5.jpg)
我正在使用ImageChops库,以便使用差值功能比较两个图像:
im1 = Image.open("image1.jpg")
im2 = Image.open("image2.jpg")
diff= ImageChops.difference(im1, im2)
差异函数如何工作?里面到底在做什么?
解决方法:
ImageChops.difference计算“两个图像之间逐像素差异的绝对值”,这将导致返回差异图像.它的代码为https://github.com/python-pillow/Pillow/blob/master/src/PIL/ImageChops.py#L88.更准确地说,对于两个图像image1和image2,它将以下内容作为新图像返回:
image1.im.chop_difference(image2.im)
哪里:
> chop_difference在https://github.com/python-pillow/Pillow/blob/43c5f2cd649bdc8422dd2892b1e3349ea003fb46/_imaging.c#L3085处定义为(PyCFunction)_chop_difference
> _chop_difference映射到https://github.com/python-pillow/Pillow/blob/43c5f2cd649bdc8422dd2892b1e3349ea003fb46/_imaging.c#L2016处的ImagingChopDifference
> ImagingChopDifference是在https://github.com/python-pillow/Pillow/blob/4aba749c3c0ff0bc24b525886c0c89c3dca1d43f/libImaging/Imaging.h#L322引用的外部函数,在Chops.c中的http://svn.effbot.org/public/tags/pil-1.1.4/libImaging/Chops.c定义为:
ImagingChopDifference(Imaging imIn1, Imaging imIn2)
{
CHOP(abs((int) in1[x] - (int) in2[x]), NULL);
}
> CHOP是以下类似于函数的C宏,它也在Chops.c中定义.它将算术运算的结果限制在range(256)之内.请注意,在此代码中,“#”不会开始注释,而“ #define”是用于定义常量或创建宏的C指令.
#define CHOP(operation, mode)\
int x, y;\
Imaging imOut;\
imOut = create(imIn1, imIn2, mode);\
if (!imOut)\
return NULL;\
for (y = 0; y < imOut->ysize; y++) {\
UINT8* out = (UINT8*) imOut->image[y];\
UINT8* in1 = (UINT8*) imIn1->image[y];\
UINT8* in2 = (UINT8*) imIn2->image[y];\
for (x = 0; x < imOut->linesize; x++) {\
int temp = operation;\
if (temp <= 0)\
out[x] = 0;\
else if (temp >= 255)\
out[x] = 255;\
else\
out[x] = temp;\
}\
}\
return imOut;
>使用GNU C预处理器cpp对(4)和(3)中的代码进行预处理之后,结果是可编译的ImagingChopDifference函数,如下所示:
static Imaging
ImagingChopDifference(Imaging imIn1, Imaging imIn2)
{
int x, y;
Imaging imOut;
imOut = create(imIn1, imIn2, NULL);
if (!imOut)
return NULL;
for (y = 0; y < imOut->ysize; y++) {
UINT8* out = (UINT8*) imOut->image[y];
UINT8* in1 = (UINT8*) imIn1->image[y];
UINT8* in2 = (UINT8*) imIn2->image[y];
for (x = 0; x < imOut->linesize; x++) {
int temp = abs((int) in1[x] - (int) in2[x]);
if (temp <= 0)
out[x] = 0;
else if (temp >= 255)
out[x] = 255;
else out[x] = temp;
}
}
return imOut;
}
GNU C预处理程序cpp广泛用于Linux和Unix系统.它的CentOS 7.0手册页为http://www.unix.com/man-page/centos/1/cpp/.其完整手册为https://gcc.gnu.org/onlinedocs/cpp/index.html,其中包括有关宏的章节.
CHOP是CHannel OPeration的首字母缩写,其中channel表示数字图像通道(请参见https://en.wikipedia.org/wiki/Channel_%28digital_image),并且剪切通道操作的结果记录在http://effbot.org/imagingbook/imagechops.htm中).
内容总结
以上是互联网集市为您收集整理的python-ImageChops.difference的定义全部内容,希望文章能够帮你解决python-ImageChops.difference的定义所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。