【问题标题】:Thresholding image in OpenCV for a range of max and min values在 OpenCV 中为一系列最大值和最小值设置阈值图像
【发布时间】:2014-03-07 02:22:03
【问题描述】:

我喜欢在最大值和最小值范围内对图像进行阈值处理,并为超出该范围的像素值填充 255。我在 OpenCV 文档 here 中找不到这样的阈值。 谢谢

【问题讨论】:

    标签: opencv


    【解决方案1】:
    inRange(src, lowerBound, upperBound, dst);
    bitwise_not(src, src);
    

    【讨论】:

    • 仅供其他读者参考:docs.opencv.org/modules/core/doc/…
    • 如果您只是添加一些评论来解释每一行的作用,这个答案将对人们有更多帮助。
    • 这可能会迟到,但在这种情况下,bitwise_not 究竟完成了什么?我只尝试了 Python 中的第一行,它似乎不需要其他任何东西就可以完成这项工作。
    • @MLavrentyev inRange 将边界内的值设置为 255,否则设置为 0。这个问题似乎要求相反,因此 bitwise_not 操作。
    【解决方案2】:

    基本的:

    threshold(src, dst, threshold value, max value, threshold type);
    

    在哪里

    src_gray: Our input image
    dst: Destination (output) image
    threshold_value: The thresh value with respect to which the thresholding operation is made
    max_BINARY_value: The value used with the Binary thresholding operations (to set the chosen pixels)
    threshold_type: One of the 5 thresholding operations. 
    

    例如,

    threshold(image, fImage, 125, 255, cv::THRESH_BINARY);
    

    表示低于 125 的每个值都将设置为零,高于 125 的值将设置为 255。

    如果您要查找特定范围,例如 50 到 150,我建议您执行 for 循环,并自己检查和编辑像素。这很简单。看看我的这个 c++ 代码:

    for (int i=0; i< image.rows; i++)
        { 
            for (int j=0; j< image.cols; j++)
            {
                int editValue=image.at<uchar>(i,j);
    
                if((editValue>50)&&(editValue<150)) //check whether value is within range.
                {
                    image.at<uchar>(i,j)=255;
                }
                else
                {
                    image.at<uchar>(i,j)=0;
                }
            }
        }
    

    希望我解决了您的问题。干杯(:如果您需要更多帮助,请发表评论。

    【讨论】:

      猜你喜欢
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 2020-06-29
      • 2013-03-11
      • 1970-01-01
      • 2019-02-10
      相关资源
      最近更新 更多