c++ OpenCV 二值化 threshold THRESH_BINARY THRESH_BINARY_INV

c++ OpenCV 二值化 threshold THRESH_BINARY THRESH_BINARY_INV

#include <iostream>
#include <filesystem>
#include <string>
#include <windows.h>
#include <opencv2/opencv.hpp>

namespace fs = std::filesystem;
using namespace cv;
using namespace std;

// 二进制阈值化/反阈值化
void test_threshold()
{
    Mat src = imread("F:/opencv/console/x64/Debug/jzh.png");
    Mat gray;
    Mat bin;
    Mat bin_inv;

    // 灰度图转化
    cvtColor(src, gray, COLOR_BGR2GRAY);

    // 二进制阈值化
    threshold(gray, bin, 100, 255, THRESH_BINARY);

    // 二进制反阈值化
    threshold(gray, bin_inv, 100, 255, THRESH_BINARY_INV);

    imshow("bin", bin);
    imshow("src", src);
    imshow("bin_inv", bin_inv);
}

int main()
{
    // 二进制阈值化/反阈值化
    test_threshold();

    waitKey(0);
    destroyAllWindows();
    return 0;
}

 

发表回复

您的电子邮箱地址不会被公开。