| “?,python"/>

首页 > 解决方案 > python中这种运算符组合有什么用,"\| “?

问题描述

好吧,我跑了一段代码,但我无法弄清楚它的功能。它是这样的:

thresholds = (image[:,:,0] < rgbThreshold[0]) \
            | (image[:,:,1] < rgbThreshold[1]) \
            | (image[:,:,2] < rgbThreshold[2])

就在那边,

\ <"返回"> | (图像[:,:,1] < ....

我不太清楚它的作用。

如果有人想知道这段代码是做什么的,这里有一组 RGB 阈值(redThreshold、green....)和一个图像“图像”。

我只是选择低于指定阈值的所有像素。然后我通过将它们colorSelect[threshold] = [0,0,0]变黑来访问它们(colorSelect 是一个 numpy 数组,它通过其 RBG 像素值表示图像。)

标签: python

解决方案


|表示or并且\仅用于使下一行的内容符合 PEP-8 指南,并且用户能够以这种方式更好地阅读代码。

这里:

thresholds = (image[:,:,0] < rgbThreshold[0]) \
            | (image[:,:,1] < rgbThreshold[1]) \
            | (image[:,:,2] < rgbThreshold[2])

等同于:

thresholds = (image[:,:,0] < rgbThreshold[0]) | (image[:,:,1] < rgbThreshold[1]) | (image[:,:,2] < rgbThreshold[2])

推荐阅读