c++视觉处理----固定阈值操作:Threshold()函数,实时处理:二值化,反二值化,截断,设为零,反向设为零

固定阈值操作: Threshold()函数

cv::threshold() 函数是OpenCV中用于执行固定阈值二值化操作的函数。它可以用来将图像中的像素值根据用户定义的阈值转换为二进制值(0或255),以便进行图像分割、物体检测和特征提取等任务。

cv::threshold() 函数的基本语法如下:

double cv::threshold(
    cv::InputArray src,       // 输入图像
    cv::OutputArray dst,      // 输出图像
    double thresh,            // 阈值
    double maxval,            // 阈值以上像素的新值
    int type                  // 阈值类型
);

参数解释:

  • src:输入图像,应为单通道灰度图像。
  • dst:输出图像,函数将处理后的图像存储在这里。
  • thresh:阈值,用于将像素分为两类。像素值大于等于阈值将被赋予 maxval 值,小于阈值的将被赋予0。
  • maxval:阈值以上像素的新值,通常为255。
  • type:阈值类型,用于指定阈值化的方式,常见的类型包括
    • cv::THRESH_BINARY(二值化)、
    • cv::THRESH_BINARY_INV(反二值化)、
    • cv::THRESH_TRUNC(截断)、
    • cv::THRESH_TOZERO(设为零)、
    • cv::THRESH_TOZERO_INV(反向设为零)等。

以下是一个简单的示例代码,演示如何使用 cv::threshold() 函数对图像进行二值化:

#include <opencv2/opencv.hpp>

int main() {
    cv::Mat image = cv::imread("input_image.jpg", cv::IMREAD_GRAYSCALE);

    if (image.empty()) {
        std::cerr << "Failed to open the image!" << std::endl;
        return -1;
    }

    // 设置阈值和阈值类型
    double thresholdValue = 128;
    double maxVal = 255;
    int thresholdType = cv::THRESH_BINARY; // 二值化

    // 应用阈值操作
    cv::Mat thresholdedImage;
    cv::threshold(image, thresholdedImage, thresholdValue, maxVal, thresholdType);

    // 显示处理后的图像
    cv::imshow("Thresholded Image", thresholdedImage);
    cv::waitKey(0);

    return 0;
}

在这里插入图片描述

使用相机实时处理:二值化,反二值化,截断,设为零,反向设为零

#include <opencv2/opencv.hpp>

// 回调函数,用于处理滑动条变化
void onThresholdChange(int thresholdType, void* userdata) {
    cv::Mat* inputImage = static_cast<cv::Mat*>(userdata);

    // 初始化阈值和阈值类型
    int thresholdValue = 128;
    int maxVal = 255;

    // 应用不同类型的阈值操作
    switch (thresholdType) {
    case 0: // 二值化
        cv::threshold(*inputImage, *inputImage, thresholdValue, maxVal, cv::THRESH_BINARY);
        break;
    case 1: // 反二值化
        cv::threshold(*inputImage, *inputImage, thresholdValue, maxVal, cv::THRESH_BINARY_INV);
        break;
    case 2: // 截断
        cv::threshold(*inputImage, *inputImage, thresholdValue, maxVal, cv::THRESH_TRUNC);
        break;
    case 3: // 设为零
        cv::threshold(*inputImage, *inputImage, thresholdValue, maxVal, cv::THRESH_TOZERO);
        break;
    case 4: // 反向设为零
        cv::threshold(*inputImage, *inputImage, thresholdValue, maxVal, cv::THRESH_TOZERO_INV);
        break;
    default:
        break;
    }

    // 显示处理后的图像
    cv::imshow("Thresholded Image", *inputImage);
}

int main() {
    cv::VideoCapture cap(0); // 打开本地相机
    if (!cap.isOpened()) {
        std::cerr << "Failed to open the camera!" << std::endl;
        return -1;
    }

    cv::Mat frame;
    cap >> frame; // 读取一帧图像

    // 创建窗口
    cv::namedWindow("Thresholded Image");

    // 初始化阈值类型滑动条
    int initialThresholdType = 0;
    cv::createTrackbar("Threshold Type", "Thresholded Image", &initialThresholdType, 4, onThresholdChange, &frame);

    // 显示原始图像
    cv::imshow("Thresholded Image", frame);

    // 循环捕获并处理图像,直到按下ESC键退出
    while (true) {
        int key = cv::waitKey(10);
        if (key == 27) // 按下ESC键退出循环
            break;

        cap >> frame; // 读取一帧图像

        // 实时更新阈值类型滑动条的值,触发回调函数
        cv::setTrackbarPos("Threshold Type", "Thresholded Image", initialThresholdType);

        // 显示原始图像
       // cv::imshow("Thresholded Image", frame);
    }

    // 关闭相机和窗口
    cap.release();
    cv::destroyAllWindows();

    return 0;
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枭玉龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值