opencv(14)---图像阈值化

来源:互联网 发布:python开发web应用 编辑:程序博客网 时间:2024/06/08 08:37

基本概念

  • 图像阈值化是图像处理的重要基础部分, 应用很广泛, 可以根据灰度差异来分割图像不同部分
  • 阈值化处理的图像一般为单通道图像(灰度图)
  • 阈值化参数的设置可以使用滑动条来debug
  • 阈值化处理易光照影响, 处理时应注意
  • 本节主要介绍的图像阈值化函数方法:
    固定阈值:threshold()
    自适应阈值:adaptiveThreshold()

固定阈值—threshold()

函数原型

CV_EXPORTS_W double threshold( InputArray src, OutputArray dst,                               double thresh, double maxval, int type );
  • src: 单通道图像(灰度图或二值图)
  • dst: 输出图像要求和src一样的尺寸和类型
  • maxValue
    使用 CV_THRESH_BINARY 和 CV_THRESH_BINARY_INV 的 最大值

  • adaptiveMethod: 指定自适应阈值算法, 可取值为CV_ADAPTIVE_THRESH_MEAN_C 或CV_ADAPTIVE_THRESH_GAUSSIAN_C

  • thresholdType: 取阈值类型取值必须为CV_THRESH_BINARY、CV_THRESH_BINARY_INV二者之一
  • blockSize: 用来计算阈值的邻域大小3, 5, 7,…
    C: 减去平均或加权平均后的常数值

阈值类型

这里写图片描述

代码

#include "mainwindow.h"#include<opencv2/opencv.hpp>#include<opencv2/imgproc/imgproc.hpp>using namespace cv;using namespace std;MainWindow::MainWindow(QWidget *parent)    : QMainWindow(parent){    Mat srcImg=imread("D:\\1\\1.png",0);    Mat dstImg1;    Mat dstImg2;    Mat dstImg3;    Mat dstImg4;    Mat dstImg5;    threshold(srcImg,dstImg1,100,255,CV_THRESH_BINARY);    threshold(srcImg,dstImg2,100,255,CV_THRESH_BINARY_INV);    threshold(srcImg,dstImg3,100,255,CV_THRESH_TRUNC);    threshold(srcImg,dstImg4,100,255,CV_THRESH_TOZERO);    threshold(srcImg,dstImg5,100,255,CV_THRESH_TOZERO_INV);    imshow("srcImg",srcImg);    imshow("dstImg1",dstImg1);    imshow("dstImg2",dstImg2);    imshow("dstImg3",dstImg3);    imshow("dstImg4",dstImg4);    imshow("dstImg5",dstImg5);}MainWindow::~MainWindow(){}

运行结果

CV_THRESH_BINARY

CV_THRESH_BINARY_INV
这里写图片描述

CV_THRESH_TRUNC
这里写图片描述

CV_THRESH_TOZERO
这里写图片描述

CV_THRESH_TOZERO_INV
这里写图片描述

srcImg
这里写图片描述

知识点讲解

如何将灰度图转为二值图
方式一

//1.读入方式Mat srcImg=imread("D:\\1\\1.png",0);

方式二

Mat srcImg=imread("D:\\1\\1.png");//2.cvtColorcvtColor(srcImg,srcImg,CV_BGR2GRAY);

自适应阈值—adaptiveThreshold()

概念

1.对矩阵采用自适应阈值操作, 自适应阈值是根据像素的邻域块的像素值分布来确定该像素位置上的二值化阈值
2.函数 adaptiveThreshold 将灰度图像变换到二值图像
采用的公式
这里写图片描述

其中T(x, y)为分别计算每个单独像素的阈值, 取值如下:

  • 对方法 CV_ADAPTIVE_THRESH_MEAN_C, 先求出块中的均值,再减掉C
  • 对方法 CV_ADAPTIVE_THRESH_GAUSSIAN_C, 先求出块中的加权和(gaussian), 再减掉C

函数原型

CV_EXPORTS_W void adaptiveThreshold( InputArray src, OutputArray dst,                                     double maxValue, int adaptiveMethod,                                     int thresholdType, int blockSize, double C );
  • src: 单通道图像(灰度图或二值图)
  • dst: 输出图像要求和src一样的尺寸和类型
  • maxValue:使用 CV_THRESH_BINARY 和 CV_THRESH_BINARY_INV 的最大值
  • adaptiveMethod: 指定自适应阈值算法, 可取值为CV_ADAPTIVE_THRESH_MEAN_C 或
    CV_ADAPTIVE_THRESH_GAUSSIAN_C
  • thresholdType: 取阈值类型取值必须为CV_THRESH_BINARY、
    CV_THRESH_BINARY_INV二者之一

  • blockSize: 用来计算阈值的邻域大小3, 5, 7,…

  • C: 减去平均或加权平均后的常数值

代码

MEAN_C 均值滤波

adaptiveThreshold(srcImg,dstImg,255,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY,11,5);

GUUSSIAN 高斯滤波,处理边缘效果比较好

adaptiveThreshold(srcImg,dstImg2,255,CV_ADAPTIVE_THRESH_GAUSSIAN_C,CV_THRESH_BINARY,11,5);

图像阈值化与滑动条

#include "mainwindow.h"#include<opencv2/opencv.hpp>#include<opencv2/imgproc/imgproc.hpp>#define thresholdWindow "GuDingWindow"#define thresholdAdaptWindow "AdaptWindow"using namespace cv;using namespace std;Mat srcImg;Mat dstImg,dstImg2;int thres_min=20;int block_size=3;int C0=3;void OnThreshold(int,void*){    threshold(srcImg,dstImg,thres_min,255,CV_THRESH_BINARY);    imshow(thresholdWindow,dstImg);}void OnAdaptThreshold(int,void*){    if(block_size%2==0){    block_size++;    }    adaptiveThreshold(srcImg,dstImg2,255,CV_ADAPTIVE_THRESH_GAUSSIAN_C,CV_THRESH_BINARY,block_size,C0);    imshow(thresholdAdaptWindow,dstImg2);}MainWindow::MainWindow(QWidget *parent)    : QMainWindow(parent){    namedWindow(thresholdWindow,CV_WINDOW_AUTOSIZE);    namedWindow(thresholdAdaptWindow,CV_WINDOW_AUTOSIZE);    srcImg=imread("D:\\1\\1.png",0);    createTrackbar("Threshold",thresholdWindow,&thres_min,255,OnThreshold,0);    createTrackbar("blockSize",thresholdAdaptWindow,&block_size,255,OnAdaptThreshold,0);    createTrackbar("C",thresholdAdaptWindow,&C0,9,OnAdaptThreshold,0);    OnThreshold(thres_min,0);    OnAdaptThreshold(block_size,0);    OnAdaptThreshold(C0,0);    imshow("img",srcImg);    waitKey(0);}MainWindow::~MainWindow(){}
0 0
原创粉丝点击