threshold 中文
时间: 2024-02-22 18:53:20 浏览: 31
"Threshold"在中文中可以翻译为“阈值”。它是一个在信号处理、图像处理等领域中常用的术语,指的是一个可以被设定的界限或标准,用来判断某个信号或图像是否满足特定的条件或要求。例如,在图像处理中,我们可能会设置一个阈值来决定一个像素值是否需要被改变(例如,将亮度低于某个阈值的像素变为黑色,高于某个阈值的像素变为白色)。
相关问题
中文 opencv threshold
阈值化 (thresholding) 是一种在图像处理中常用的操作,它通过将像素值与一个阈值进行比较,将像素分为两类:大于阈值的像素和小于阈值的像素。在 OpenCV 中,阈值化可以使用函数 cv2.threshold() 实现。
函数原型如下:
```python
retval, dst = cv2.threshold(src, thresh, maxval, type)
```
参数说明:
- src:输入图像,必须是灰度图像。
- thresh:阈值,用于将像素分为两类。
- maxval:当像素值大于阈值时,赋予的新值。
- type:阈值化类型,包括以下几种:
- cv2.THRESH_BINARY:二值化阈值化,大于阈值的像素赋值为 maxval,小于阈值的像素赋值为 0。
- cv2.THRESH_BINARY_INV:反二值化阈值化,大于阈值的像素赋值为 0,小于阈值的像素赋值为 maxval。
- cv2.THRESH_TRUNC:截断阈值化,大于阈值的像素赋值为 thresh,小于阈值的像素保持不变。
- cv2.THRESH_TOZERO:被零阈值化,大于阈值的像素保持不变,小于阈值的像素赋值为 0。
- cv2.THRESH_TOZERO_INV:反被零阈值化,大于阈值的像素赋值为 0,小于阈值的像素保持不变。
函数返回值:
- retval:计算出的阈值。
- dst:输出图像,与输入图像大小、类型相同。
示例代码:
```python
import cv2
img = cv2.imread('lena.jpg', cv2.IMREAD_GRAYSCALE) # 读入灰度图像
retval, dst = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) # 二值化阈值化
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
运行结果:
![thresholding](https://img-blog.csdn.net/20180522105851821?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW5nd2VuZ2pp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/85/blog_pic_1.jpg)
代码解释:public static String charsSegment(Mat inMat, PlateColor color, Boolean debug, String tempPath) { int charCount = 7; // 车牌字符个数 if (color.equals(PlateColor.GREEN)) { charCount = 8; } // 切换到灰度图 Mat gray = new Mat(); Imgproc.cvtColor(inMat, gray, Imgproc.COLOR_BGR2GRAY); ImageUtil.gaussianBlur(gray, gray, debug, tempPath); Imgcodecs.imwrite(tempPath + Constant.TEMP_CHAR_PLATE_PREDICT, gray); // 图像进行二值化 Mat threshold = new Mat(); switch (color) { case BLUE: Imgproc.threshold(gray, threshold, 10, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY); break; default: // GREEN YELLOW Imgproc.threshold(gray, threshold, 10, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY_INV); break; } ImageUtil.debugImg(debug, tempPath, "plateThreshold", threshold); // 输出二值图 Imgcodecs.imwrite(tempPath + Constant.TEMP_CHAR_PLATE_PREDICT, threshold); // 边缘腐蚀 threshold = ImageUtil.erode(threshold, debug, tempPath, 2, 2); Imgcodecs.imwrite(tempPath + Constant.TEMP_CHAR_ERODE, threshold); // 垂直方向投影,错切校正 // 理论上,还可以用于分割字符 Integer px = getShearPx(threshold); ImageUtil.shearCorrection(threshold, threshold, px, debug, tempPath); // 前面已经结果错切校正了,可以按照垂直、水平方向投影进行精确定位 // 垂直投影 + 垂直分割线,分割字符 // 水平投影,去掉上下边框、铆钉干扰 threshold = sepAndClear(threshold, px, charCount, debug, tempPath); Imgcodecs.imwrite(tempPath + Constant.TEMP_CHAR_SEP_AND_CLEAR, threshold); // 边缘膨胀 // 还原腐蚀操作产生的影响 // 会影响中文字符的精确度 threshold = ImageUtil.dilate(threshold, debug, tempPath, 2, 2, true); Imgcodecs.imwrite(tempPath + Constant.TEMP_CHAR_DILATE, threshold); // 提取外部轮廓 List<MatOfPoint> contours = Lists.newArrayList(); Imgproc.findContours(threshold, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); Vector<Rect> charRect = new Vector<Rect>(); // 字符轮廓集合 Mat dst; dst = inMat.clone(); Imgproc.cvtColor(threshold, dst, Imgproc.COLOR_GRAY2BGR); if (debug) {
这段代码是一个字符分割的函数,输入参数包括原始图像、车牌颜色、是否开启调试模式和临时文件保存路径。主要功能是对车牌图像进行预处理,包括切换到灰度图、二值化、边缘腐蚀、错切校正、垂直方向投影、字符分割、边缘膨胀和提取外部轮廓等步骤,最终输出字符轮廓集合。
在函数中,首先根据车牌颜色确定字符个数,然后将原始图像转换为灰度图并进行高斯模糊。接着根据不同车牌颜色使用不同的二值化方法,输出二值图并进行边缘腐蚀。然后进行错切校正,根据垂直方向投影进行精确定位,并根据垂直投影和垂直分割线进行字符分割,去掉上下边框和铆钉干扰。最后进行边缘膨胀,还原腐蚀操作产生的影响,并提取外部轮廓,输出字符轮廓集合。如果开启了调试模式,还会输出中间过程的图像以便调试。
相关推荐
![hdvp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)