设为首页收藏本站 |天气与日历| 2025-04-20 星期日 07:44:00 乙巳(蛇)年 三月廿三 辰时 谷雨
     
切换到窄版

私人站点

 找回密码
 立即注册
搜索
查看: 230|回复: 0

基本阈值操作

[复制链接]

954

主题

954

帖子

3875

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3875
发表于 2022-2-28 08:36:23 | 显示全部楼层 |阅读模式
图像阈值

阈值 是什么?简单点说是把图像分割的标尺,这个标尺是根据什么产生的,阈值产生算法?阈值类型。(Binary segmentation)

阈值类型




阈值类型一阈值二值化(threshold binary)
1.png 2.png 3.png


阈值类型一阈值反二值化(threshold binary Inverted)


a1.png a2.png a3.png

阈值类型一截断 (truncate)
c1.png c2.png c3.png

阈值类型一阈值取零 (threshold to zero)

d1.png d2.png d3.png



阈值类型一阈值反取零 (threshold to zero inverted)

e1.png e2.png e3.png



dddddddd.jpg



[C++] 纯文本查看 复制代码
/*
注意:
        THRESH_BINARY     = 0, //!< \f[\texttt{dst} (x,y) =  \fork{\texttt{maxval}}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{0}{otherwise}\f]
        THRESH_BINARY_INV = 1, //!< \f[\texttt{dst} (x,y) =  \fork{0}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{\texttt{maxval}}{otherwise}\f]
        THRESH_TRUNC      = 2, //!< \f[\texttt{dst} (x,y) =  \fork{\texttt{threshold}}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{\texttt{src}(x,y)}{otherwise}\f]
        THRESH_TOZERO     = 3, //!< \f[\texttt{dst} (x,y) =  \fork{\texttt{src}(x,y)}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{0}{otherwise}\f]
        THRESH_TOZERO_INV = 4, //!< \f[\texttt{dst} (x,y) =  \fork{0}{if \(\texttt{src}(x,y) > \texttt{thresh}\)}{\texttt{src}(x,y)}{otherwise}\f]
        THRESH_MASK       = 7,
        THRESH_OTSU       = 8, //!< flag, use Otsu algorithm to choose the optimal threshold value
        THRESH_TRIANGLE   = 16 //!< flag, use Triangle algorithm to choose the optimal threshold value
*/
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;

Mat src, gray_src, dst;
//阈值二值化的模式0-4 参考注意点,默认设为0
int threshold_mod=0;
//阈值二值化最大值,一共5种模式0-4
int threshold_mod_max=4;
//--------------------------------------

//设定阈值
int threshold_value = 127;
//设定最大阈值
int threshold_max = 255;
// 定义输出窗口标题
char output_title[] = "输出窗口";

// -----------------------------
//声明函数
void Thershold_Demo(int, void*);

int main(int argc, char** argv) {
        src = imread("D:/IDE/opencv-3.1.0/demo.jpg");
        if (!src.data) {
                printf("加载图片异常\n");
                return -1;
        }
        namedWindow("input", CV_WINDOW_AUTOSIZE);
        namedWindow(output_title, CV_WINDOW_AUTOSIZE);
        imshow("input", src);


        createTrackbar("拖动条:",output_title, &threshold_value, threshold_max, Thershold_Demo);
        //创建效果拖动条
        createTrackbar("模式:", output_title, &threshold_mod, threshold_mod_max, Thershold_Demo);
        //阈值二值化的模式的变化
        Thershold_Demo(threshold_value, 0);
        waitKey(0);
        return 0;
}
void Thershold_Demo(int val, void*  ) {

        
                switch (threshold_mod)
                {
                case 0: cout << "THRESH_BINARY模式(阈值二值化)" << endl; break;
                case 1: cout << "THRESH_BINARY_INV模式(阈值反二值化)" << endl; break;
                case 2: cout << "THRESH_TRUNC模式(阈值截断)" << endl; break;
                case 3: cout << "THRESH_TOZERO模式(阈值取零)" << endl; break;
                case 4: cout << "THRESH_TOZERO_INV模式(阈值反取零)" << endl; break;
                default:
                        break;
                }
        



        printf("调整的数值为:%d\n", val);
        //转灰度图
        cvtColor(src, gray_src, CV_BGR2GRAY);
        //阈值的处理
        threshold(gray_src, dst, threshold_value, threshold_max, threshold_mod);
        imshow(output_title, dst);

}


0.jpg 2.jpg




自动寻找阈值:
THRESH_OTSU
THRESH_TRIANGLE



[C++] 纯文本查看 复制代码
/*
注意:
THRESH_OTSU THRESH_TRIANGLE处理的图像必须是8位单通道(灰度图)

自动寻找阈值
THRESH_OTSU
THRESH_TRIANGLE
*/
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
//声明函数
void demo(int, void*);
//定义滑块条最小值
int val = 0;

Mat src, gray_src,dst;

int main(int argc, char** argv) {
	
	src = imread("D:/IDE/opencv-3.1.0/demo.jpg");
	if (!src.data) {
		printf("加载图片异常\n");
		return -1;
	}
	namedWindow("input", CV_WINDOW_AUTOSIZE);

	namedWindow("CV_demo", CV_WINDOW_AUTOSIZE);
	//创建滑块条
	createTrackbar("模式调整:", "CV_demo",&val,4, demo);
	demo(0, 0);
	imshow("input", src);
	waitKey(0);
	return 0;
}
void demo(int, void*){
	//转灰度图
	cvtColor(src, gray_src, CV_BGR2GRAY);
	//通过THRESH_OTSU 自动获取阈值,处理效果
	//threshold(gray_src, dst, 0, 255, THRESH_OTSU|val);

	//通过THRESH_TRIANGLE 自动获取阈值,处理效果
	threshold(gray_src, dst, 0, 255, THRESH_TRIANGLE | val);
	imshow("CV_demo", dst);
}

888.png

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|编程站点 ( 冀ICP备2023028127号-2 )|友链申请|

GMT+8, 2025-4-20 07:44 , Processed in 0.102909 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表