美文网首页计算机视觉OpenCvOpenCV
实现opencv阈值操作函数threshold

实现opencv阈值操作函数threshold

作者: 芒果浩明 | 来源:发表于2018-09-26 10:05 被阅读4次

理论部分已经在这篇opencv自带例子学习-灰度图像的阈值操作解释过,这里就不再赘述了。

下面直接贴代码

//灰度的阈值变换,实现opencv的threshold函数
#include<opencv2/opencv.hpp>
#include<iostream>
#include<cassert>

namespace mycv {
    enum ThresholdType
    {
        //详情参考https://docs.opencv.org/3.4.3/d7/d1b/group__imgproc__misc.html#gaa9e58d2860d4afa658ef70a9b1115576
        THRESH_BINARY = 0,
        THRESH_BINARY_INV = 1,
        THRESH_TRUNC = 2,
        THRESH_TOZERO = 3,
        THRESH_TOZERO_INV = 4
    };
    void threshold(cv::Mat& input, cv::Mat& output, const uchar thresh = 128, const uchar max_val = 255, int type = 0);

}//mycv

int main()
{
    cv::Mat img = cv::imread("lena.jpg", 0);
    if (img.empty()) return -1;
    cv::Mat dst;
    
    mycv::threshold(img, dst);
    cv::imwrite("dst.bmp", dst);
    cv::imshow("img", img);
    cv::imshow("dst", dst);
    cv::waitKey(0);

    return 0;
}

void mycv::threshold(cv::Mat& input, cv::Mat& output, const uchar thresh, const uchar max_val, int type)
{
    assert(1 == input.channels());
    output = input.clone();
    const int rows = input.rows;//行
    const int cols = input.cols;//列

    switch (type)//五种操作方式
    {
    case 0:
        for(int i = 0; i < rows; ++i)
            for (int j = 0; j < cols; ++j)
            {
                //THRESH_BINARY
                if (input.at<uchar>(i, j) > thresh)
                    output.at<uchar>(i, j) = max_val;
                else
                    output.at<uchar>(i, j) = 0;
            }
        break;
    case 1:
        for (int i = 0; i < rows; ++i)
            for (int j = 0; j < cols; ++j)
            {
                //THRESH_BINARY_INV
                if (input.at<uchar>(i, j) < thresh)
                    output.at<uchar>(i, j) = max_val;
                else
                    output.at<uchar>(i, j) = 0;
            }
        break;
    case 2:
        for (int i = 0; i < rows; ++i)
            for (int j = 0; j < cols; ++j)
            {
                //THRESH_TRUNC
                if (input.at<uchar>(i, j) > thresh)
                    output.at<uchar>(i, j) = thresh;
                else
                    output.at<uchar>(i, j) = input.at<uchar>(i, j);
            }
        break;
    case 3:
        for (int i = 0; i < rows; ++i)
            for (int j = 0; j < cols; ++j)
            {
                //THRESH_TOZERO
                if (input.at<uchar>(i, j) < thresh)
                    output.at<uchar>(i, j) = input.at<uchar>(i, j);
                else
                    output.at<uchar>(i, j) = 0;
            }
        break;
    case 4:
        for (int i = 0; i < rows; ++i)
            for (int j = 0; j < cols; ++j)
            {
                //THRESH_TOZERO_INV
                if (input.at<uchar>(i, j) > thresh)
                    output.at<uchar>(i, j) = input.at<uchar>(i, j);
                else
                    output.at<uchar>(i, j) = 0;
            }
        break;
    default:
        assert(false);
    }
}//threshold


相关文章

  • OpenCV 之ios 基本的阈值操作

    OpenCV 之ios 基本的阈值操作 目标: 本节简介: OpenCV中的阈值(threshold)函数: th...

  • 实现opencv阈值操作函数threshold

    理论部分已经在这篇opencv自带例子学习-灰度图像的阈值操作解释过,这里就不再赘述了。 下面直接贴代码

  • OpenCV 教程 10 : 阈值化

    固定阈值 OpenCV 中提供了阈值化函数 threshold,该函数有 5 种阈值化类型参数: THRESH_B...

  • 014-Opencv笔记-基本阈值操作

    阈值类型一阈值二值化(threshold binary) 阈值类型一阈值反二值化(threshold binary...

  • 如何科学的训练乳酸阈值跑?

    田径中,乳酸阈值(lactic threshold)也叫无氧阈值(anaerobic threshold),简称L...

  • OpenCV阈值操作

    原理: 阈值操作的对象是灰度图像,而阈值是一个灰度值,作用相当于一个分界线,当一个像素值大于阈值时,这个像素值会指...

  • 阈值处理

    OpenCV提供了两个函数cv2.threshold() 和 cv2.adaptiveThreshold(),用于...

  • 图像去噪

    图像去噪可以分为固定阈值去噪和自适应阈值去噪 固定阈值去噪 opencv函数(python):cv2.thresh...

  • 生理🌝

    stimulation 刺激 threshold value simple 阈值 excitability兴奋性 ...

  • LeetCode #1283 Find the Smallest

    1283 Find the Smallest Divisor Given a Threshold 使结果不超过阈值...

网友评论

    本文标题:实现opencv阈值操作函数threshold

    本文链接:https://www.haomeiwen.com/subject/tacdoftx.html