图像分割·阈值化分割

  • 图像阈值化分割

    对图像进行灰度阈值化是最简单的分割处理。图像阈值化算法简单高效,在很多场景中依然得到很多应用,实时性很好。图像阈值化的缺陷也是明显的,不能够很好的利用图像中的诸如色彩、纹理等语义信息,因此在复杂场景中无法得到目标结果。对一幅图像R进行完全分割,是使得区域 R1,...,Rs 的有限集合满足如下关系:


R=Si=1Ri,  RiRj=,  ij

    图像阈值化分为全局阈值和局部阈值。全局阈值是对整幅图像使用单个阈值,局部阈值是根据图像局部信息在局部执行阈值化。阈值化操作有许多改进算法,例如:局部阈值化、带阈值化、半阈值化、多阈值化等。阈值化的关键在于如何选择阈值。
    如果事先知道经过分割后的图像的某种性质,就可以简化阈值选择任务,因为阈值可以按照确保该性质得以满足的条件来选择。阈值检测方法主要有:p 率阈值化、直方图形状分析、最优阈值化等。
    * 在二模态直方图中,阈值可以确定为两个最大的局部极大值之间的极小值位置。
    * 最优阈值化确定阈值为离对应于两个或更多个正态分布最大值之间的最小概率处最近的灰度值,其结果即为最小错误分割。
    * 对于彩色图像和多谱段图像适合采用多光谱阈值化方法。
    

  • 图像阈值化分割·OpenCV实现

    OpenCV提供了几个阈值化函数,可以实现图像阈值化分割,常用的有如下两个函数: threshold, adaptiveThreshold

    1. cv::threshold()

    函数介绍:


void cv::adaptiveThreshold(InputArray   src,
                           OutputArray  dst,
                           double   maxValue,
                           int  adaptiveMethod,
                           int  thresholdType,
                           int  blockSize,
                           double   C 
                           )        

Parameters
     src:   input array (single-channel, 8-bit or 32-bit      floating point).
     dst:   output array of the same size and type as src.
     thresh:    threshold value.
     maxval:    maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.
     type:  thresholding type

    函数参数type可选值,也即阈值化操作类型:


这里写图片描述
图1:阈值化操作可选值

    
    阈值化操作类型图示:


这里写图片描述
图2:阈值化操作图示

    threshold函数应用示例:

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

//全局变量
#define WINDOW_NAME "Binaryzation"

int g_thresholdValue = 45;
int g_thresholdType = 1;
Mat g_grayImg;

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

int main()
{
    Mat srcImg = imread("D:/Developer/Qt/images/std/lena.bmp");
    if(!srcImg.data){
        cout << "Failed to read picture!" << endl;
        return false;
    }

    //转灰度图
    cvtColor(srcImg, g_grayImg, COLOR_RGB2GRAY);

    //创建显示窗口
    namedWindow(WINDOW_NAME, cv::WINDOW_KEEPRATIO);

    createTrackbar("ThresholdTypes", WINDOW_NAME, &g_thresholdType, 4, onThreshold);
    createTrackbar("ThresholdValue", WINDOW_NAME, &g_thresholdValue, 255, onThreshold);

    onThreshold(0, 0);

    waitKey();
    return 0;
}

//回调函数
void onThreshold(int, void*)
{
    Mat dstImg;
    threshold(g_grayImg, dstImg, g_thresholdValue, 255, g_thresholdType);
    imshow(WINDOW_NAME, dstImg);
}

    执行效果:


这里写图片描述
图3:原图
这里写图片描述
图4:运行效果图

    
    2. cv::adaptiveThreshold()
    函数介绍:


void cv::adaptiveThreshold(InputArray   src,
                           OutputArray  dst,
                           double   maxValue,
                           int  adaptiveMethod,
                           int  thresholdType,
                           int  blockSize,
                           double   C 
                           )    

Parameters
     src:   Source 8-bit single-channel image.
     dst:   Destination image of the same size and the same type as src.
     maxValue:  Non-zero value assigned to the pixels for which the condition is satisfied
     adaptiveMethod:    Adaptive thresholding algorithm to use, see cv::AdaptiveThresholdTypes
     thresholdType: Thresholding type that must be either THRESH_BINARY or THRESH_BINARY_INV, see cv::ThresholdTypes.
     blockSize: Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on.
     C: Constant subtracted from the mean or weighted mean (see the details below). Normally, it is positive but may be zero or negative as well.   


这里写图片描述
这里写图片描述

    adaptiveThreshold函数应用示例:

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

//全局变量
#define WINDOW_NAME "Binaryzation"

int main()
{
    Mat srcImg = imread("D:/Developer/Qt/images/std/lena.bmp");
    if(!srcImg.data){
        cout << "Failed to read picture!" << endl;
        return false;
    }

    //转灰度图
    Mat grayImg;
    cvtColor(srcImg, grayImg, COLOR_RGB2GRAY);

    //创建显示窗口
    namedWindow(WINDOW_NAME, cv::WINDOW_KEEPRATIO);

    Mat dstImg;
    adaptiveThreshold(grayImg, dstImg, 255, cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY, 5, 10);

    imshow(WINDOW_NAME, dstImg);

    waitKey();
    return 0;
}

    执行效果:


这里写图片描述
图5:运行效果图

猜你喜欢

转载自blog.csdn.net/u010189457/article/details/78358410