计算机系统应用教程网站

网站首页 > 技术文章 正文

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

btikc 2024-09-30 13:13:13 技术文章 13 ℃ 0 评论

一. 图像阈值(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:表示阈值 maxvalue:表示最大值。 thresholdtype:表示二值化类型(即对图像取阈值的方法)

下面是代码展示:

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

#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);

}



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

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表