首页 > 编程知识 正文

OpenCV4 cv threshold 阈值函数

时间:2023-05-06 08:25:25 阅读:271368 作者:1482

 

官方文档:参考链接

1.函数原型

2.解释 2.1 头文件 #include <opencv2/imgproc.hpp> 2.2  功能

Applies a fixed-level threshold to each array element.

The function applies fixed-level thresholding to a multiple-channel array. The function is typically used to get a bi-level (binary) image out of a grayscale image ( compare could be also used for this purpose) or for removing a noise, that is, filtering out pixels with too small or too large values. There are several types of thresholding supported by the function. They are determined by type parameter.

Also, the special values THRESH_OTSU or THRESH_TRIANGLE may be combined with one of the above values. In these cases, the function determines the optimal threshold value using the Otsu's or Triangle algorithm and uses it instead of the specified thresh.

该函数用于定阈值的二值化(还有其他的自适应二值化adaptive threshold method,阈值在kernel中动态调整)

对一个多通道数组(图片),该方法(threshold)常被用来从一个灰度图中得到一个二值图或者用来去除噪声(用来过滤掉过小和过大的值),通过调整传入的参数,可以决定不同的二值化的方法。

大津阈值(THRESH_OTSU)和矩形阈值(THRESH_TRIANGLE)可以和上面的这些值结合起来使用。在这些情况下,theshold方法就会根据大津算法(OTsu's algorithm)和矩形算法(Triangle algorithm)来计算最优的阈值。

2.3 参数说明

Parameters

srcinput array (multiple-channel, 8-bit or 32-bit floating point).dstoutput array of the same size and type and the same number of channels as src.threshthreshold value.maxvalmaximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.typethresholding type (see ThresholdTypes).

src: 多通道的输入图像,8bits或者32bits float 

dst: 与src的同尺寸、类型、通道数的输出图像

thresh: 阈值(介于最大值和最小值之间的一个值)

maxval: 与两种阈值类型(THRESH_BINARY & THRESH_BINARY_INV)结合使用,用于指定最大的值

type: 阈值的类型(见下表)

2.4 阈值类型

2.4.1 THRESH_BINARY 

最常见的作法,设置一个阈值,大于的时候取最大值(maxval是threshold的参数),否则取0。

2.4.2 THRESH_BINARY_INV 

与THRESH_BINARY相反,大于阈值取0,否则取最大值,maxval对于THRESH_BINARY和THRESH_BINARY_INV有意义。

2.4.3 THRESH_TRUNC 

截断型,大于阈值的统一将为阈值,其余不变。

2.4.4 THRESH_TOZERO 

过滤型,大于阈值的保持不变,其余设置为0。

2.4.5 THRESH_TOZERO_INV 

大于阈值0,其他不变。

2.4.6 THRESH_MASK 

这个不知道是什么,也没有解释

2.4.7 THRESH_OTSU 

大津阈值

2.4.8 THRESH_TRIANGLE 

矩形阈值

3.Demo

下面这段代码读入图片后,首先使用二值化,之后对图片进行开闭操作等形态学作用。

// DigitalGraphicExp6.cpp #include "DigitalGraphicExp6.h"Mat src, dst, erosion_dst, dilation_dst;int morph_elem = 0;int morph_size = 0;int morph_operator = 0;int erosion_elem = 0;int erosion_size = 0;int dilation_elem = 0;int dilation_size = 0;int const max_operator = 4;int const max_elem = 2;int const max_kernel_size = 21;void Erosion(int, void*);void Dilation(int, void*);void Morphology_Operations(int, void*);const char* window_name = "Morphology Transformations Demo";int main(int argc, char** argv){String img_dir = "../media/cat.jpg";src = imread(img_dir, IMREAD_GRAYSCALE);threshold(src, src, 50, 255, THRESH_BINARY);if (src.empty()){cout << "Could not open or find the image!n" << endl;cout << "Usage: " << argv[0] << " <Input image>" << endl;return -1;}namedWindow("Erosion Demo", WINDOW_AUTOSIZE);namedWindow("Dilation Demo", WINDOW_AUTOSIZE);namedWindow(window_name, WINDOW_AUTOSIZE);moveWindow("Dilation Demo", src.cols, 0);createTrackbar("Element:n 0: Rect n 1: Cross n 2: Ellipse", "Erosion Demo",&erosion_elem, max_elem,Erosion);createTrackbar("Kernel size:n 2n +1", "Erosion Demo",&erosion_size, max_kernel_size,Erosion);createTrackbar("Element:n 0: Rect n 1: Cross n 2: Ellipse", "Dilation Demo",&dilation_elem, max_elem,Dilation);createTrackbar("Kernel size:n 2n +1", "Dilation Demo",&dilation_size, max_kernel_size,Dilation);createTrackbar("Operator:n 0: Opening - 1: Closing n 2: Gradient - 3: Top Hat n 4: Black Hat",window_name, &morph_operator, max_operator, Morphology_Operations);createTrackbar("Element:n 0: Rect - 1: Cross - 2: Ellipse", window_name,&morph_elem, max_elem,Morphology_Operations);createTrackbar("Kernel size:n 2n +1", window_name,&morph_size, max_kernel_size,Morphology_Operations);Erosion(0, 0);Dilation(0, 0);Morphology_Operations(0, 0);waitKey(0);return 0;}// Callback functionvoid Erosion(int, void*){int erosion_type = 0;if (erosion_elem == 0) { erosion_type = MORPH_RECT; }else if (erosion_elem == 1) { erosion_type = MORPH_CROSS; }else if (erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; }Mat element = getStructuringElement(erosion_type,Size(2 * erosion_size + 1, 2 * erosion_size + 1),Point(erosion_size, erosion_size));erode(src, erosion_dst, element);imshow("Erosion Demo", erosion_dst);}void Dilation(int, void*){int dilation_type = 0;if (dilation_elem == 0) { dilation_type = MORPH_RECT; }else if (dilation_elem == 1) { dilation_type = MORPH_CROSS; }else if (dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; }Mat element = getStructuringElement(dilation_type,Size(2 * dilation_size + 1, 2 * dilation_size + 1),Point(dilation_size, dilation_size));dilate(src, dilation_dst, element);imshow("Dilation Demo", dilation_dst);}void Morphology_Operations(int, void*){// Since MORPH_X : 2,3,4,5 and 6int operation = morph_operator + 2;Mat element = getStructuringElement(morph_elem, Size(2 * morph_size + 1, 2 * morph_size + 1), Point(morph_size, morph_size));morphologyEx(src, dst, operation, element);imshow(window_name, dst);} // DigitalGraphicExp6.h#pragma once#include <opencv2/opencv.hpp>#include <iostream>using namespace std;using namespace cv;

 

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。