python通过robert、sobel、Laplace算子实现图像边缘提取详解
2019-08-21 17:09
4951 查看
实现思路:
1,将传进来的图片矩阵用算子进行卷积求和(卷积和取绝对值)
2,用新的矩阵(与原图一样大小)去接收每次的卷积和的值
3,卷积图片所有的像素点后,把新的矩阵数据类型转化为uint8
注意:
必须对求得的卷积和的值求绝对值;矩阵数据类型进行转化。
完整代码:
import cv2 import numpy as np # robert 算子[[-1,-1],[1,1]] def robert_suanzi(img): r, c = img.shape r_sunnzi = [[-1,-1],[1,1]] for x in range(r): for y in range(c): if (y + 2 <= c) and (x + 2 <= r): imgChild = img[x:x+2, y:y+2] list_robert = r_sunnzi*imgChild img[x, y] = abs(list_robert.sum()) # 求和加绝对值 return img # # sobel算子的实现 def sobel_suanzi(img): r, c = img.shape new_image = np.zeros((r, c)) new_imageX = np.zeros(img.shape) new_imageY = np.zeros(img.shape) s_suanziX = np.array([[-1,0,1],[-2,0,2],[-1,0,1]]) # X方向 s_suanziY = np.array([[-1,-2,-1],[0,0,0],[1,2,1]]) for i in range(r-2): for j in range(c-2): new_imageX[i+1, j+1] = abs(np.sum(img[i:i+3, j:j+3] * s_suanziX)) new_imageY[i+1, j+1] = abs(np.sum(img[i:i+3, j:j+3] * s_suanziY)) new_image[i+1, j+1] = (new_imageX[i+1, j+1]*new_imageX[i+1,j+1] + new_imageY[i+1, j+1]*new_imageY[i+1,j+1])**0.5 # return np.uint8(new_imageX) # return np.uint8(new_imageY) return np.uint8(new_image) # 无方向算子处理的图像 # Laplace算子 # 常用的Laplace算子模板 [[0,1,0],[1,-4,1],[0,1,0]] [[1,1,1],[1,-8,1],[1,1,1]] def Laplace_suanzi(img): r, c = img.shape new_image = np.zeros((r, c)) L_sunnzi = np.array([[0,-1,0],[-1,4,-1],[0,-1,0]]) # L_sunnzi = np.array([[1,1,1],[1,-8,1],[1,1,1]]) for i in range(r-2): for j in range(c-2): new_image[i+1, j+1] = abs(np.sum(img[i:i+3, j:j+3] * L_sunnzi)) return np.uint8(new_image) img = cv2.imread('1.jpg', cv2.IMREAD_GRAYSCALE) cv2.imshow('image', img) # # robers算子 out_robert = robert_suanzi(img) cv2.imshow('out_robert_image', out_robert) # sobel 算子 out_sobel = sobel_suanzi(img) cv2.imshow('out_sobel_image', out_sobel) # Laplace算子 out_laplace = Laplace_suanzi(img) cv2.imshow('out_laplace_image', out_laplace) cv2.waitKey(0) cv2.destroyAllWindows()
结果:
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- OpenCV之imgproc 模块. 图像处理(2)实现自己的线性滤波器 给图像添加边界 Sobel 导数 Laplace 算子 Canny 边缘检测
- OpenCV之imgproc 模块. 图像处理(2)实现自己的线性滤波器 给图像添加边界 Sobel 导数 Laplace 算子 Canny 边缘检测
- Roberts、Sobel、Prewitt、Kirsch、Canny、Laplacian图像边缘检测原理及其代码实现(Python3)
- 图像边缘检测——二阶微分算子(上)Laplace算子、LOG算子、DOG算子(Matlab实现)
- 13. 用Roberts、Sobel、Prewitt和Laplace算子对一幅灰度图像进行边缘检测。观察异同。
- 图像边缘检测——几种图像边缘检测算子的学习及python 实现
- 通过PYTHON来实现图像分割详解
- 图像特征提取:Sobel边缘检测
- 图像处理中各种边缘检测的微分算子简单比较(Sobel,Robert, Prewitt,Laplacian,Canny)
- python opencv实现图像边缘检测
- 通过形态学操作实现图像边缘
- OpenCV使用Sobel滤波器实现图像边缘检测
- [Python图像处理] 十七.图像锐化与边缘检测之Roberts算子、Prewitt算子、Sobel算子和Laplacian算子
- 图像处理中各种边缘检测的微分算子简单比较(Sobel,Robert, Prewitt,Laplacian,Canny)
- optimize_aop.hdev对sobel边缘检测算子 AOP的对不同大小图像并行加速效果 相关例程学习
- python实现LBP方法提取图像纹理特征实现分类
- 图像分割之(五)边缘检测Laplace详解
- [Python图像处理] 十八.图像锐化与边缘检测之Scharr算子、Canny算子和LOG算子
- opencv学习笔记 python实现 图像梯度与图像边缘
- 【数字图像处理】Canny边缘检测详解及编程实现