以下是C++和Opencv实现通过滑块调节图像二值化阈值的代码:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int thresholdValue = 128; // 初始阈值

void onThresholdSlide(int, void*) {
    // 读取灰度图像
    Mat grayImage = imread("image.jpg", IMREAD_GRAYSCALE);

    // 应用阈值
    Mat binaryImage;
    threshold(grayImage, binaryImage, thresholdValue, 255, THRESH_BINARY);

    // 显示二值化图像
    imshow("Binary Image", binaryImage);
}

int main() {
    // 创建窗口和滑块
    namedWindow("Binary Image", WINDOW_NORMAL);
    createTrackbar("Threshold", "Binary Image", &thresholdValue, 255, onThresholdSlide);

    // 初始化
    onThresholdSlide(0, 0);

    // 等待按键
    waitKey(0);

    return 0;
}

在这个例子中,我们创建了一个名为“Binary Image”的窗口,并在其中创建了一个名为“Threshold”的滑块。滑块的初始值为128,当滑块值发生变化时,会调用onThresholdSlide函数。在该函数中,我们首先读取灰度图像,然后应用阈值,并将结果显示在窗口中。

main函数中,我们初始化窗口和滑块,并调用onThresholdSlide函数进行初始显示。最后,我们等待按键以保持窗口打开

C++和Opencv实现通过滑块调节图像二值化阈值代码

原文地址: https://www.cveoy.top/t/topic/cIu4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录