OpenCV学习笔记(二)之图像阈值化

一. 图像阈值(Threshold)

(一). 阈值类型

1. 阈值二值化(Threshold Binary)

       首先指定像素的灰度值的阈值,遍历图像中像素值,如果像素的灰度值大于这个阈值,则将这个像素设置为最大像素值(8位灰度值最大为255);若像素的灰度值小于阈值,则将该像素点像素值赋值为0。公式以及示意图如下:


蓝色水平线代表阈值

2. 阈值反二值化(Threshold Binary Inverted)

       首先也要指定一个阈值,不同的是在对图像进行阈值化操作时与阈值二值化相反,当像素的灰度值超过这个阈值的时候为该像素点赋值为0;当该像素的灰度值低于该阈值时赋值为最大值。​公式及示意图如下:


在这里插入图片描述

3. 截断(Truncate)

       给定像素值阈值,在图像中像素的灰度值大于该阈值的像素点被设置为该阈值,而小于该阈值的像素值保持不变。公式以及示意图如下:


在这里插入图片描述

4. 阈值取零(Threshold To Zero)

       与截断阈值化相反,像素点的灰度值如果大于该阈值则像素值不变,如果像素点的灰度值小于该阈值,则该像素值设置为0.公式以及示意图如下:


在这里插入图片描述

5. 阈值反取零(Threshold To Zero Inverted)

       像素值大于阈值的像素赋值为0,而小于该阈值的像素值则保持不变,公式以及示意图如下:


在这里插入图片描述

二. API介绍

       阈值化API: cv::threshold​

double cv::threshold  ( 
    InputArray  src,  
    OutputArray  dst,  
    double  thresh,  
    double  maxval,  
    int  type  
 )

       src:输入图像,须为单通道灰度图。
       dst:输出的边缘图像,为单通道黑白图。
       threshold:表示阈值
       max_value:表示最大值。
       threshold_type:表示二值化类型(即对图像取阈值的方法)

       下面是代码展示:

/*****图像阈值化*****/

#include<iostream>
#include<opencv2/opencv.hpp>
#include<string>
using namespace std;

cv::Mat src, dst;
string input_title = "input image";
string output_title = "binary image";
//阈值设置和最大值
int threshold_value = 127;
int threshold_max = 255;
void Threshold_Demo(int, void*);
//阈值类型设置
int type_value = 2;
int type_max = 4;

int main() {
    
    src = cv::imread("1.jpg");
    cv::namedWindow(input_title, cv::WINDOW_NORMAL);
    cv::namedWindow(output_title, cv::WINDOW_NORMAL);
    cv::imshow(input_title, src);

    //阈值调节
    cv::createTrackbar("Threshold Value:", output_title, &threshold_value,
        threshold_max, Threshold_Demo);
    //阈值类型调节
    cv::createTrackbar("Type Value:", output_title, &type_value,
        type_max, Threshold_Demo);
    Threshold_Demo(0, 0);

    cv::waitKey(0);
    return 0;
}

void Threshold_Demo(int, void*) {
    cv::cvtColor(src, dst, cv::COLOR_BGR2GRAY);
    cv::threshold(dst, dst, threshold_value, threshold_max,
        type_value);
    cv::imshow(output_title, dst);

}


       关注公号【开发小鸽】,获取海量计算机视觉与深度学习资源,实战项目源码,最新论文下载,大厂面试经验!!!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。