ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

图像的分割之基于边缘的分割

2021-11-02 22:34:59  阅读:188  来源: 互联网

标签:分割 img 算子 基于 图像 np array filter2D cv


边缘检测

import cv2 as cv
import numpy as np

# 边缘检测
img = cv.imread("./image.jpg", cv.IMREAD_GRAYSCALE)
# 梯度算子
# 标准定义形式
# g(x, y) ≈ |∂f(x, y)/∂x|+|∂f(x, y)/∂y|,其中原函数为f(x, y),处理后的函数为g(x, y)
# ∂f(x, y)/∂x = f(x, y+1)-f(x, y) ∂f(x, y)/∂y = f(x, y)-f(x+1, y)
# 另一种表现形式
# G = |Gx*I| + |Gy*I|
# 即使用内核Gx和Gy分别对图像I进行卷积,再进行相加,得到图像G
# roberts算子
# 内核Gx=[[1, 0], [0, -1]] Gy=[[0, 1], [-1, 0]]
kernelx = np.array([[1, 0], [0, -1]])
kernely = np.array([[0, 1], [-1, 0]])
robertsx = cv.filter2D(img, -1, kernelx)
robertsx = cv.convertScaleAbs(robertsx)
robertsy = cv.filter2D(img, -1, kernely)
robertsy = cv.convertScaleAbs(robertsy)
roberts = cv.addWeighted(robertsx, 0.5, robertsy, 0.5, 0)
# prewitt算子
#
kernelx = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])
kernely = np.array([[-1, -1, -1], [0, 0, 0], [0, 0, 0]])
prewittx = cv.filter2D(img, -1, kernelx)
prewittx = cv.convertScaleAbs(prewittx)
prewitty = cv.filter2D(img, -1, kernely)
prewittx = cv.convertScaleAbs(prewittx)
prewitt = cv.addWeighted(robertsx, 0.5, robertsy, 0.5, 0)
# sobel算子
# 正确的用法,若原函数为f(x, y),处理后的函数为g(x, y),
# 则这里相当于g(x, y) = |∂f(x, y)/∂x|+|∂f(x, y)/∂y|
# 注意第二个参数ddepth表示存储图像每个像素的位数,
# 确定彩色图像的每个像素可能有的颜色数,或者确定灰度图像的每个像素可能有的灰度级数.
# ddepth=-1表示输出与原图像一样的图像深度
sobelx = cv.Sobel(img, -1, 1, 0)
sobelx = cv.convertScaleAbs(sobelx)
sobely = cv.Sobel(img, -1, 0, 1)
sobely = cv.convertScaleAbs(sobely)
sobel1 = cv.addWeighted(sobelx, 0.5, sobely, 0.5, 0)
# 错误的用法,若原函数为f(x, y),处理后的函数为g(x, y),
# 则这里相当于g(x, y) = ∂²f(x, y)/∂x∂y,显然不符合梯度算子的定义
sobel2 = cv.Sobel(img, -1, 1, 1)
# scharr算子
# 类似于sobel算子,只是使用的内核不同
# sobel算子使用的内核
# Gx = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]
# Gy = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]
# scharr算子使用的内核
# Gx = [[-3, 0, 3], [-10, 0, 10], [-3, 0, 3]]
# Gy = [[-3, -10, -3], [0, 0, 0], [3, 10, 3]]
scharrx = cv.Sobel(img, -1, 1, 0, ksize=cv.FILTER_SCHARR)
scharrx = cv.convertScaleAbs(scharrx)
scharry = cv.Sobel(img, -1, 1, 0, ksize=cv.FILTER_SCHARR)
scharry = cv.convertScaleAbs(scharry)
scharr = cv.addWeighted(scharrx, 0.5, scharry, 0.5, 0)
# laplacian算子
# 内核
# Gx = [[0, -1, 0], [-1, 4, -1], [0, -1, 0]]
# Gy = [[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]
img_laplacian = cv.Laplacian(img, -1)
# kirsch算子
# 定义的8个内核
kernel1 = np.array([[5, 5, 5], [-3, 0, -3], [-3, -3, -3]])
kernel2 = np.array([[-3, 5, 5], [-3, 0, 5], [-3, -3, -3]])
kernel3 = np.array([[-3, -3, 5], [-3, 0, 5], [-3, -3, 5]])
kernel4 = np.array([[-3, -3, -3], [-3, 0, 5], [-3, 5, 5]])
kernel5 = np.array([[-3, -3, -3], [-3, 0, -3], [5, 5, 5]])
kernel6 = np.array([[-3, -3, -3], [5, 0, -3], [5, 5, -3]])
kernel7 = np.array([[5, -3, -3], [5, 0, -3], [5, -3, -3]])
kernel8 = np.array([[5, 5, -3], [5, 0, -3], [-3, -3, -3]])
# 用8个内核分别与原图像进行卷积
kirsch1 = cv.filter2D(img, -1, kernel1)
kirsch2 = cv.filter2D(img, -1, kernel2)
kirsch3 = cv.filter2D(img, -1, kernel3)
kirsch4 = cv.filter2D(img, -1, kernel4)
kirsch5 = cv.filter2D(img, -1, kernel5)
kirsch6 = cv.filter2D(img, -1, kernel6)
kirsch7 = cv.filter2D(img, -1, kernel7)
kirsch8 = cv.filter2D(img, -1, kernel8)
rows, cols = img.shape
kirsch = np.zeros((rows, cols), np.uint8)
for i in range(rows):
    for j in range(cols):
        # 找出每幅图像的不同像素中最大的值
        kirsch[i, j] = max(kirsch1[i, j], kirsch2[i, j], kirsch3[i, j], kirsch4[i, j],
                           kirsch5[i, j], kirsch6[i, j], kirsch7[i, j], kirsch8[i, j])
# LOG算子
# 先用高斯滤波对图像进行平滑,再用laplacian算子对图像进行边缘检测
# canny算子
canny = cv.Canny(img, 100, 200)

标签:分割,img,算子,基于,图像,np,array,filter2D,cv
来源: https://blog.csdn.net/weixin_49346755/article/details/121110868

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有