使用opencv提取灰度图的边界

import cv2
import numpy as np

def roberts_edge_extraction(image, border_width):
    # Convert the image to float32
    image = image.astype(np.float32)
    
    # Apply Roberts edge detection
    roberts_x = cv2.filter2D(image, -1, np.array([[1, 0], [0, -1]], dtype=np.float32))
    roberts_y = cv2.filter2D(image, -1, np.array([[0, 1], [-1, 0]], dtype=np.float32))
    
    # Calculate the gradient magnitude
    gradient_magnitude = np.sqrt(roberts_x**2 + roberts_y**2)
    
    # Threshold the gradient magnitude to obtain the edges
    threshold = np.max(gradient_magnitude) * 0.1  # Adjust the threshold value as needed
    edges = np.where(gradient_magnitude > threshold, 255, 0).astype(np.uint8)
    
    # Dilate the edges to increase the width
    kernel = np.ones((border_width, border_width), np.uint8)
    dilated_edges = cv2.dilate(edges, kernel, iterations=1)
    
    return dilated_edges

# Load a grayscale image
image = cv2.imread('your_image.jpg', cv2.IMREAD_GRAYSCALE)

# Apply Roberts edge extraction with a border width of 3 pixels
border_width = 3
edges = roberts_edge_extraction(image, border_width)

# Display the original image and the edges
cv2.imshow('Original Image', image)
cv2.imshow('Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值