# -*- coding: utf-8 -*-
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
#读取图像
img = cv.imread('d:/paojie.png')
img1 = cv.cvtColor(img, cv.COLOR_BGR2RGB)
#转换为灰度图像
grayImage = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
#高斯滤波
gaussianBlur = cv.GaussianBlur(grayImage, (3,3), 0)
#自适应阈值处理
ret, binary = cv.threshold(gaussianBlur, 0, 255, cv.THRESH_BINARY+cv.THRESH_OTSU)
#Scharr算子
x = cv.Scharr(grayImage, cv.CV_32F, 1, 0) #X方向
y = cv.Scharr(grayImage, cv.CV_32F, 0, 1) #Y方向
absX = cv.convertScaleAbs(x)
absY = cv.convertScaleAbs(y)
Scharr = cv.addWeighted(absX, 0.5, absY, 0.5, 0)
#Canny算子
gaussian = cv.GaussianBlur(grayImage, (3,3), 0) #高斯滤波降噪
Canny = cv.Canny(gaussian, 50, 150)
#LOG算子
gaussian = cv.GaussianBlur(grayImage, (3,3), 0) #先通过高斯滤波降噪
dst = cv.Laplacian(gaussian, cv.CV_16S, ksize = 3) #再通过拉普拉斯算子做边缘检测
LOG = cv.convertScaleAbs(dst)
#效果图
titles = ['Source Image', 'Gray Image', 'Binary Image',
'Scharr Image','Canny Image', 'LOG Image']
images = [img1, grayImage, binary, Scharr, Canny, LOG]
for i in np.arange(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()