【问题标题】:What does opencv threshold THRESH_BINARY do on colored images?opencv 阈值 THRESH_BINARY 对彩色图像有什么作用?
【发布时间】:2018-07-18 19:48:39
【问题描述】:

The documentation on THRESH_BINARY 说:

dst(x,y) = maxval if src(x,y) > thresh else 0

这对我来说并不意味着这不适用于彩色图像。即使应用于彩色图像,我也希望有两种颜色的输出,但输出是多色的。为什么?当像素x,y 被分配的可能值只有maxval0 时,这怎么可能?

例子:

from sys import argv
import cv2
import numpy as np

img = cv2.imread(argv[1])

ret, threshold = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)

cv2.imshow('threshold', threshold)
cv2.imshow('ori', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

【问题讨论】:

    标签: python opencv image-processing image-thresholding


    【解决方案1】:

    假设您有来自 3 通道 RGB 图像的像素,其值为 rgb(66, 134, 244)。现在假设你给thresh 赋值135。你认为会发生什么?

    r = 66
    g = 134
    b = 244
    
    if(r > thresh) r = 255 else r = 0; // we have r = 0
    if(g > thresh) g = 255 else g = 0; // we have g = 0
    if(b > thresh) b = 255 else b = 0; // we have b = 255
    

    新的像素值为rgb(0, 0, 255)。由于您的图像是 RGB 彩色图像,因此现在像素颜色为 BLUE 而不是 WHITE

    【讨论】:

      【解决方案2】:

      阈值分别应用于每个颜色通道。如果小于 tahn 阈值,则颜色通道设置为 0,如果不是,则设置为 maxval。通道独立处理,这就是为什么结果是具有多种颜色的彩色图像。可以得到的颜色有:(0,0,0), (255,0,0), (0,255,0), (255,255,0), (0,0,255),(255,0,255), (0,255,255)和 (255,255,255)。

      【讨论】:

      • 哦,我明白了。谢谢!
      猜你喜欢
      • 2016-11-15
      • 2022-01-19
      • 2021-04-01
      • 2023-02-26
      • 2015-11-07
      • 1970-01-01
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多