双阈值法二值化操作

////3.1.4双阈值法二值化操作
////利用OpenCV的threshold函数实现双阈值法二值化操作的源码!
////SourceCode:https://blog.youkuaiyun.com/wenhao_ir/article/details/51566817
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
int main()
{
    // 图像读取及判断
    cv::Mat srcImage = cv::imread("D:\\0604.png");
    if (!srcImage.data)
        return 1;
    // 灰度转换
    cv::Mat srcGray;
    cv::cvtColor(srcImage, srcGray, CV_RGB2GRAY);
    cv::imshow("srcGray", srcGray);
    // 初始化阈值参数
    const int maxVal = 255;
    int low_threshold = 150;
    int high_threshold = 210;
    cv::Mat dstTempImage1, dstTempImage2, dstImage;
    // 小阈值对源灰度图像进行阈值化操作
    cv::threshold(srcGray, dstTempImage1,
        low_threshold, maxVal, cv::THRESH_BINARY);
    // 大阈值对源灰度图像进行阈值化操作
    cv::threshold(srcGray, dstTempImage2,
        high_threshold, maxVal, cv::THRESH_BINARY_INV);//要特别注意这里的最后一个参数是INV哦
                                                       // 矩阵与运算得到二值化结果
    cv::bitwise_and(dstTempImage1,
        dstTempImage2, dstImage);
    cv::imshow("dstImage", dstImage);
    cv::waitKey(0);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值