##1.环境的搭建
- Python 3.6
- OpenCV
Open Source Computer Vision Library.OpenCV于1999年由Intel建立,如今由Willow Garage提供支持。OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows、MacOS操作系统上。它轻量级而且高效——由一系列 C 函数和少量C++类构成,同时提供了Python、Ruby、MATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。最新版本是3.1 ,2016年1月29日发布。
OpenCV 安装命令
pip install opencv-python
觉得有用就点颗星吧
######代码:https://github.com/luojie1024/Computer-vision-Classwork
##2.卷积的实现
卷积的概念自行百度吧
# 卷积
def imgConvolve(image, kernel):''':param image: 图片矩阵:param kernel: 滤波窗口:return:卷积后的矩阵'''img_h = int(image.shape[0])img_w = int(image.shape[1])kernel_h = int(kernel.shape[0])kernel_w = int(kernel.shape[1])# paddingpadding_h = int((kernel_h - 1) / 2)padding_w = int((kernel_w - 1) / 2)convolve_h = int(img_h + 2 * padding_h)convolve_W = int(img_w + 2 * padding_w)# 分配空间img_padding = np.zeros((convolve_h, convolve_W))# 中心填充图片img_padding[padding_h:padding_h + img_h, padding_w:padding_w + img_w] = image[:, :]# 卷积结果image_convolve = np.zeros(image.shape)# 卷积for i in range(padding_h, padding_h + img_h):for j in range(padding_w, padding_w + img_w):image_convolve[i - padding_h][j - padding_w] = int(np.sum(img_padding[i - padding_h:i + padding_h+1, j - padding_w:j + padding_w+1]*kernel))return image_convolve
##3.均值滤波的实现
均值滤波实际上也就是卷积和的平均,卷积和/滤波大小
原图
# 均值滤波
def imgAverageFilter(image, kernel):''':param image: 图片矩阵:param kernel: 滤波窗口:return:均值滤波后的矩阵'''return imgConvolve(image, kernel) * (1.0 / kernel.size)
3x3均值滤波效果图
5x5均值滤波效果图
参考:https://www.cnblogs.com/wangguchangqing/p/6399293.html
##4.高斯滤波的实现
- 1.根据σ获得高斯滤波器的模板
- 2.然后使用该模板对图像进行卷积
σ是标准差。要想得到一个高斯滤波器的模板,可以对高斯函数进行离散化,得到的高斯函数值作为模板的系数。
原图
# 高斯滤波
def imgGaussian(sigma):''':param sigma: σ标准差:return: 高斯滤波器的模板'''img_h = img_w = 2 * sigma + 1gaussian_mat = np.zeros((img_h, img_w))for x in range(-sigma, sigma + 1):for y in range(-sigma, sigma + 1):gaussian_mat[x + sigma][y + sigma] = np.exp(-0.5 * (x ** 2 + y ** 2) / (sigma ** 2))return gaussian_mat
sigma=1 效果图
sigma=2 效果图
sigma=3 效果图
参考:https://www.cnblogs.com/wangguchangqing/p/6407717.html
##5. Sobel算子的实现
原图
使用Sobel算子作为模板,用来对图像进行边缘检测,水平 竖直边缘
# sobel
sobel_1 = np.array([[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]])sobel_2 = np.array([[-1, -2, -1],[0, 0, 0],[1, 2, 1]])
其中sobel_1是对竖直方向的边缘检查,sobel_2是对水平方向的边缘检测
# Sobel Edge
def sobelEdge(image, sobel):''':param image: 图片矩阵:param sobel: 滤波窗口:return: Sobel处理后的矩阵'''return imgConvolve(image, sobel)
sobel_1 效果图
sobel_2 效果图
参考:https://www.cnblogs.com/freeblues/p/5738987.html
##6. Prewitt算子的实现
使用分别使用Prewitt1算子、Prewitt2算子作为模板,用来对图像进行边缘检测,水平+竖直边缘
# prewitt 算子
prewitt_1 = np.array([[-1, 0, 1],[-1, 0, 1],[-1, 0, 1]])prewitt_2 = np.array([[-1, -1, -1],[0, 0, 0],[1, 1, 1]])
# Prewitt Edge
def prewittEdge(image, prewitt_x, prewitt_y):''':param image: 图片矩阵:param prewitt_x: 竖直方向:param prewitt_y: 水平方向:return:处理后的矩阵'''img_X = imgConvolve(image, prewitt_x)img_Y = imgConvolve(image, prewitt_y)img_prediction = np.zeros(img_X.shape)for i in range(img_prediction.shape[0]):for j in range(img_prediction.shape[1]):img_prediction[i][j] = max(img_X[i][j], img_Y[i][j])return img_prediction
Prewitt效果图
参考:https://baike.baidu.com/item/Prewitt%E7%AE%97%E5%AD%90/8415245?fr=aladdin
7.完整代码(图片+代码请看github)
# -*- coding: utf-8 -*-
import cv2
import numpy as np
import math'''
# @Time : 18-3-31 下午2:16
# @Author : 罗杰
# @ID : F1710w0249
# @File : assignments.py
# @Desc : 计算机视觉作业01
'''# 卷积
def imgConvolve(image, kernel):''':param image: 图片矩阵:param kernel: 滤波窗口:return:卷积后的矩阵'''img_h = int(image.shape[0])img_w = int(image.shape[1])kernel_h = int(kernel.shape[0])kernel_w = int(kernel.shape[1])# paddingpadding_h = int((kernel_h - 1) / 2)padding_w = int((kernel_w - 1) / 2)convolve_h = int(img_h + 2 * padding_h)convolve_W = int(img_w + 2 * padding_w)# 分配空间img_padding = np.zeros((convolve_h, convolve_W))# 中心填充图片img_padding[padding_h:padding_h + img_h, padding_w:padding_w + img_w] = image[:, :]# 卷积结果image_convolve = np.zeros(image.shape)# 卷积for i in range(padding_h, padding_h + img_h):for j in range(padding_w, padding_w + img_w):image_convolve[i - padding_h][j - padding_w] = int(np.sum(img_padding[i - padding_h:i + padding_h + 1, j - padding_w:j + padding_w + 1] * kernel))return image_convolve# 均值滤波
def imgAverageFilter(image, kernel):''':param image: 图片矩阵:param kernel: 滤波窗口:return:均值滤波后的矩阵'''return imgConvolve(image, kernel) * (1.0 / kernel.size)# 高斯滤波
def imgGaussian(sigma):''':param sigma: σ标准差:return: 高斯滤波器的模板'''img_h = img_w = 2 * sigma + 1gaussian_mat = np.zeros((img_h, img_w))for x in range(-sigma, sigma + 1):for y in range(-sigma, sigma + 1):gaussian_mat[x + sigma][y + sigma] = np.exp(-0.5 * (x ** 2 + y ** 2) / (sigma ** 2))return gaussian_mat# Sobel Edge
def sobelEdge(image, sobel):''':param image: 图片矩阵:param sobel: 滤波窗口:return:Sobel处理后的矩阵'''return imgConvolve(image, sobel)# Prewitt Edge
def prewittEdge(image, prewitt_x, prewitt_y):''':param image: 图片矩阵:param prewitt_x: 竖直方向:param prewitt_y: 水平方向:return:处理后的矩阵'''img_X = imgConvolve(image, prewitt_x)img_Y = imgConvolve(image, prewitt_y)img_prediction = np.zeros(img_X.shape)for i in range(img_prediction.shape[0]):for j in range(img_prediction.shape[1]):img_prediction[i][j] = max(img_X[i][j], img_Y[i][j])return img_prediction######################常量################################
# 滤波3x3
kernel_3x3 = np.ones((3, 3))
# 滤波5x5
kernel_5x5 = np.ones((5, 5))# sobel 算子
sobel_1 = np.array([[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]])sobel_2 = np.array([[-1, -2, -1],[0, 0, 0],[1, 2, 1]])
# prewitt 算子
prewitt_1 = np.array([[-1, 0, 1],[-1, 0, 1],[-1, 0, 1]])prewitt_2 = np.array([[-1, -1, -1],[0, 0, 0],[1, 1, 1]])# ######################均值滤波################################
# 读图片
image = cv2.imread('balloonGrayNoisy.jpg', cv2.IMREAD_GRAYSCALE)
# 均值滤波
img_k3 = imgAverageFilter(image, kernel_3x3)# 写图片
cv2.imwrite('average_3x3.jpg', img_k3)
# 均值滤波
img_k5 = imgAverageFilter(image, kernel_5x5)
# 写图片
cv2.imwrite('average_5x5.jpg', img_k5)######################高斯滤波################################
image = cv2.imread('balloonGrayNoisy.jpg', cv2.IMREAD_GRAYSCALE)
img_gaus1 = imgAverageFilter(image, imgGaussian(1))
cv2.imwrite('gaussian1.jpg', img_gaus1)
img_gaus2 = imgAverageFilter(image, imgGaussian(2))
cv2.imwrite('gaussian2.jpg', img_gaus2)
img_gaus3 = imgAverageFilter(image, imgGaussian(3))
cv2.imwrite('gaussian3.jpg', img_gaus3)######################Sobel算子################################
image=cv2.imread('buildingGray.jpg',cv2.IMREAD_GRAYSCALE)
img_spbel1 = sobelEdge(image, sobel_1)
cv2.imwrite('sobel1.jpg',img_spbel1)
img_spbel2 = sobelEdge(image, sobel_2)
cv2.imwrite('sobel2.jpg',img_spbel2)######################prewitt算子################################
img_prewitt1 = prewittEdge(image, prewitt_1,prewitt_2)
cv2.imwrite('prewitt1.jpg',img_prewitt1)



![[Python从零到壹] 五十七.图像增强及运算篇之图像锐化Roberts、Prewitt算子实现边缘检测](https://img-blog.csdnimg.cn/1dea2c88b4ec42f3ae67b7e8ada41bfd.png#pic_center)













