Imread 灰度与 imread + 不同转换为灰度

发布于 2025-01-12 23:55:15 字数 757 浏览 5 评论 0原文

在使用 OpenCV 时,我发现了一种奇怪的行为,而我的知识无法解释它。 或许有人会有答案。

image= cv.imread("image.jpg", 0)
__, thre = cv.threshold(image, 1, 255, cv.THRESH_BINARY)
plt.imshow(thre)

结果

image = cv.imread("image.jpg")
image=  cv.cvtColor(output, cv.COLOR_BGR2GRAY)
__, thre = cv.threshold(image, 1, 255, cv.THRESH_BINARY) 
plt.imshow(thre)

result 2

正如您所看到的,这两个图像有点不同。 有人可以解释一下为什么使用带标志 0(灰度)的 imread 会产生与使用不带标志的 imread 并在之后将其转换为灰度的结果不同的结果。

是因为 cvtColor 标志吗?

While working with OpenCV, I found a curious behavior and my knoledge couldn't explain it.
Maybe someone will have the answer.

image= cv.imread("image.jpg", 0)
__, thre = cv.threshold(image, 1, 255, cv.THRESH_BINARY)
plt.imshow(thre)

result

image = cv.imread("image.jpg")
image=  cv.cvtColor(output, cv.COLOR_BGR2GRAY)
__, thre = cv.threshold(image, 1, 255, cv.THRESH_BINARY) 
plt.imshow(thre)

result 2

As you can see, the two images are a bit different.
Could someone explain me why using imread with flag 0 (grayscale) make a different result than using imread without a flag and convert it to grayscale after.

Is it because of the cvtColor flag ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒与心事 2025-01-19 23:55:15

我不确定这是否是您观察到的效果的原因,但我想指出,存在两种不同的方式 RGB 到灰度转换:即使用均值和加权平均值,在后面的情况下可能会使用不同的权重,据说 cvtColor() 使用以下公式,

Y = 0.299*R + 0.587*G + 0.114*B

链接文章中还提供了另一个公式,其权重根据人类感知进行调整,如下所示

Z = 0.2126*R + 0.7152*G + 0.0722*B

请注意,由于上述转换是幂等的,您可能会不小心将已经灰度图像输入其中。

I am not sure if this is reason for effect you have observed, but I want to note that there exist two distinct ways of RGB to Grayscale Conversion: that is using mean and using weighted average, in later case different weights might be used, cvtColor() is said to use following formula

Y = 0.299*R + 0.587*G + 0.114*B

there is also another formula provided in linked article with weights adjusted for human perception as follows

Z = 0.2126*R + 0.7152*G + 0.0722*B

Note that as above transformations are idempotent you might feed already grayscale image into them without care.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文