# -*- coding: utf-8 -*-
# gengzg@126.com
import sys
import cv2
import numpy as np
def preprocess(gray):
# # 直方图均衡化
# equ = cv2.equalizeHist(gray)
# 高斯平滑
gaussian = cv2.GaussianBlur(gray, (3, 3), 0, 0, cv2.BORDER_DEFAULT)
cv2.imwrite('c:/img/gaussian.jpg',gaussian)
# 中值滤波
median = cv2.medianBlur(gaussian, 5)
cv2.imwrite('c:/img/median.jpg', median)
# Sobel算子,X方向求梯度
sobel = cv2.Sobel(median, cv2.CV_8U, 1, 0, ksize=3)
cv2.imwrite('c:/img/sobel.jpg', sobel)
# 二值化
# ret, binary = cv2.threshold(sobel, 127, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)
ret, binary = cv2.threshold(sobel, 127, 255, cv2.THRESH_BINARY)
cv2.imwrite('c:/img/binary.jpg', binary)
ret, thresh2 = cv2.threshold(sobel, 127, 255, cv2.THRESH_BINARY_INV)
cv2.imwrite('c:/img/thresh2.jpg', thresh2)
ret, thresh3 = cv2.threshold(sobel, 127,
python3+opencv
最新推荐文章于 2024-05-16 21:48:15 发布