• 阈值分割


    阈值分割原理:

    一副图像包括目标、背景和噪声,设定某一阈值T将图像分成两部分:大于T的像素群和小于T的像素群。

    在实际处理时候,为了显示需要一般用255表示背景,用0表示对象物。

    由于实际得到的图像目标和背景之间不一定单纯地分布在两个灰度范围内,此时就需要两个或以上的阈值来提取目标。

    图像阈值化分割是一种传统的最常用的图像分割方法,因其实现简单、计算量小、性能较稳定而成为图像分割中最基本和应用最广泛的分割技术。它特别适用于目标和背景占据不同灰度级范围的图像。难点在于如何选择一个合适的阈值实现较好的分割。

    应用实例:

     1 #include <opencv2/opencv.hpp>
     2 #include <opencv2/imgproc/imgproc.hpp>
     3 #include <opencv2/highgui/highgui.hpp>
     4 #include <iostream>
     5 #define WINDOW_NAME "程序窗口"
     6 using namespace cv;
     7 using namespace std;
     8 int g_nThresholdValue=100;
     9 int g_nThresholdType=3;
    10 Mat g_srcImage,g_dstImage,g_grayImage;
    11 static void ShowHelpText();
    12 void on_Threshold(int,void*)
    13     {
    14         threshold(g_grayImage,g_dstImage,g_nThresholdValue,255,g_nThresholdType);
    15         imshow(WINDOW_NAME,g_dstImage);
    16     }
    17         
    18 int main()
    19 {
    20     printf("用 0 进行 二进制阈值\n");
    21     printf("用 1 进行 反二进制阈值\n");
    22     printf("用 2 进行 截断阈值\n");
    23     printf("用 3 进行 反阈值化为0\n");
    24     printf("用 4 进行 阈值化为0");
    25 
    26     g_srcImage=imread("E:\\lena.jpg");//读取原图
    27     if(!g_srcImage.data)
    28     {
    29         printf("error!\n");
    30         return false;
    31     }
    32     cvtColor(g_srcImage,g_grayImage,COLOR_RGB2GRAY);
    33     namedWindow(WINDOW_NAME,WINDOW_AUTOSIZE);
    34     imshow(WINDOW_NAME,g_srcImage);//显示原图
    35     createTrackbar("模式",WINDOW_NAME,&g_nThresholdType,4,on_Threshold);
    36     createTrackbar("参数值",WINDOW_NAME,&g_nThresholdValue,255,on_Threshold);
    37     //on_Threshold=( 0, 0);
    38     while(1)
    39     {
    40     
    41         int key=waitKey(20);
    42         if((char)key==27)
    43         {
    44             break;
    45         }        
    46 
    47     }
    48       waitKey(0);
    49        return 0;
    50 }
    51         

    运行结果:

    萍水相逢逢萍水,浮萍之水水浮萍!
  • 相关阅读:
    racktable安装过程
    racktables
    EM上的按钮是方框的问题
    install oracle
    记一次ORACLE无法启动登陆事故
    安装rlwrap-0.37.tar.gz
    centos7 安装oracle 11g数据库
    centos 7 安装mariadb
    centos7 lamp
    Linux安全之SSH 密钥创建及密钥登录
  • 原文地址:https://www.cnblogs.com/AIBigTruth/p/9620015.html
Copyright © 2020-2023  润新知