python裁剪图片边缘_如何在Python中检测边缘并裁剪图像

本文介绍了如何使用Python的OpenCV库进行图像处理,通过将图像转换为灰度,设置阈值,然后检测和裁剪边缘,以获取图像中的特定区域。示例代码展示了从原始图像中提取签名的过程。
摘要由CSDN通过智能技术生成

你需要的是阈值。在OpenCV中,您可以使用^{}来实现这一点。

我试过了。我的方法如下:转换为灰度

对图像设置阈值以仅获取签名而不获取其他内容

查找阈值图像中显示的像素的位置

在原始灰度中围绕该区域裁剪

从对显示没有那么严格的裁剪创建新的阈值图像

这是我的尝试,我觉得效果很好。import cv2

import numpy as np

# load image

img = cv2.imread('image.jpg')

rsz_img = cv2.resize(img, None, fx=0.25, fy=0.25) # resize since image is huge

gray = cv2.cvtColor(rsz_img, cv2.COLOR_BGR2GRAY) # convert to grayscale

# threshold to get just the signature

retval, thresh_gray = cv2.threshold(gray, thresh=100, maxval=255, type=cv2.THRESH_BINARY)

# find where the signature is and make a cropped region

points = np.argwhere(thresh_gray==0) # find where the black pixels are

points = np.fliplr(points) # store them in x,y coordinates instead of row,col indices

x, y, w, h = cv2.bounding

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值