OpenCV之图像二值化与去噪
Posted MachineLP
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCV之图像二值化与去噪相关的知识,希望对你有一定的参考价值。
python代码:
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("./test.png")
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",
以上是关于OpenCV之图像二值化与去噪的主要内容,如果未能解决你的问题,请参考以下文章