matlab边缘检测后如何提取ROI,边缘检测后从四面八方裁剪图像

正如您已经找到clean canny edge,然后要裁剪矩形区域,您应该计算rectangle boundary。

步骤:

a94c3023462302b05df0e705397beb6e.png

结果是:

0f0c2a1b50c0a7291629a54bf04db548.png

要计算canny区域边界,只需找不到零点,然后得到min-max coords。使用NumPy很容易实现。然后使用slice-op进行裁剪#!/usr/bin/python3

# 2018.01.20 15:18:36 CST

#!/usr/bin/python3

# 2018.01.20 15:18:36 CST

import cv2

import numpy as np

#img = cv2.imread("test.png")

img = cv2.imread("img02.png")

blurred = cv2.blur(img, (3,3))

canny = cv2.Canny(blurred, 50, 200)

## find the non-zero min-max coords of canny

pts = np.argwhere(canny>0)

y1,x1 = pts.min(axis=0)

y2,x2 = pts.max(axis=0)

## crop the region

cropped = img[y1:y2, x1:x2]

cv2.imwrite("cropped.png", cropped)

tagged = cv2.rectangle(img.copy(), (x1,y1), (x2,y2), (0,255,0), 3, cv2.LINE_AA)

cv2.imshow("tagged", tagged)

cv2.waitKey()

当然,在点上做boundingRect也行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值