opencv基本阀值操作-二值化

main.cpp

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

using namespace cv;
using namespace std;
Mat src, gray_src, dst;
int threshold_value = 127;
int threshold_max = 255;
int type_value = 2;
int type_max = 4;
const char *threshold_title = "threshold image";

void threshold_callback(int position, void *userdata);

int main(int argc, char **argv) {
    //    加载图片
    src = imread("../../picture/bg1.webp", 1);
    if (!src.data) {
        printf("No image data \n");
        return -1;
    }
    char src_title[] = "src";
    namedWindow(src_title, WINDOW_AUTOSIZE);
    imshow(src_title, src);

    namedWindow(threshold_title, WINDOW_AUTOSIZE);
//    createTrackbar("threshold Value:", threshold_title, &threshold_value, threshold_max, threshold_callback);
    createTrackbar("Type Value:", threshold_title, &type_value, type_max, threshold_callback);
    //    等待按键
    waitKey(0);
    return 0;
}

void threshold_callback(int position, void *userdata) {
    cvtColor(src, gray_src, COLOR_BGR2GRAY);
//    threshold(gray_src, dst, threshold_value, threshold_max, THRESH_BINARY);
    threshold(gray_src, dst, 0, 255, THRESH_TRIANGLE | type_value);
    imshow(threshold_title, dst);
}

  

 

posted @ 2022-06-04 13:00  NAVYSUMMER  阅读(58)  评论(0)    收藏  举报
交流群 编程书籍