opencv-045-图像二值化与去噪

知识点

对于一张需要二值化的图像,我们有两种选择:
选择一
直接对输入图像转换为灰度图像,然后二值化
选择二
首先对输入图像进行降噪,去除噪声干扰,然后再二值化

在进行去噪声的时候,可以选择的有:

  • 均值模糊去噪声

  • 高斯模糊去噪声

  • 双边/均值迁移模糊去噪声

  • 非局部均值去噪声

下面以三种方式进行实验,

  • 第一张图是输入图像直接转换为二值图像

  • 第二张图是输入图像先高斯模糊去噪声,然后二值化图像

  • 第三张图是输入图像先均值迁移去噪声,然后二值化的图像

代码(c++,python)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

/*
* 图像二值化与去噪
*/
int main() {
Mat src = imread("../images/coins.jpg");
if (src.empty()) {
cout << "could not load image.." << endl;
}
imshow("input", src);

Mat gray, blurred, binary;

// 直接二值化
cvtColor(src, gray, COLOR_BGR2GRAY);
threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
imshow("binary_direct", binary);

// 先高斯模糊,再二值化
GaussianBlur(src, blurred, Size(3,3), 0, 0);
cvtColor(blurred, gray, COLOR_BGR2GRAY);
threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
imshow("binary_gaussian", binary);

// 先均值迁移模糊,再二值化
pyrMeanShiftFiltering(src, blurred, 10, 100);
cvtColor(blurred, gray, COLOR_BGR2GRAY);
threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
imshow("binary_pyrmean", binary);

waitKey(0);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import cv2 as cv
import numpy as np


def method_1(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
t, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
return binary


def method_2(image):
blurred = cv.GaussianBlur(image, (3, 3), 0)
gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
t, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
return binary


def method_3(image):
blurred = cv.pyrMeanShiftFiltering(image, 10, 100)
gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)
t, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
return binary


src = cv.imread("D:/images/coins.jpg")
h, w = src.shape[:2]
ret = method_3(src)

result = np.zeros([h, w*2, 3], dtype=src.dtype)
result[0:h,0:w,:] = src
result[0:h,w:2*w,:] = cv.cvtColor(ret, cv.COLOR_GRAY2BGR)
cv.putText(result, "input", (10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2)
cv.putText(result, "binary", (w+10, 30), cv.FONT_ITALIC, 1.0, (0, 0, 255), 2)
cv.imshow("result", result)
cv.imwrite("D:/binary_result.png", result)

cv.waitKey(0)
cv.destroyAllWindows()

结果

代码地址

github