OpenCV使用阈值截断实现二值分割(黑白图)

一、概述

  二值化比较简单,具体步骤如下:

  1.将输入图像转为灰度图

  2.执行二值化(再这之前也可以去除噪声,视情况而定)

  3.执行输出

二、代码  

/**
 * 对图像进行二值分割
 * @param inputImagePath
 */
void showThresholdImage(char *inputImagePath) {
    Mat src = imread(inputImagePath);
    Mat gray;
    //灰度图
    cvtColor(src, gray, COLOR_BGR2GRAY);
    //对图像进行二值分割
    Mat result;
    threshold(gray, result, 140, 255, THRESH_BINARY);
    imshow("src", src);
    waitKey(0);
    imshow("threshold", result);
    waitKey(0);
}

三、对比图

原文地址:https://www.cnblogs.com/tony-yang-flutter/p/14845817.html