python 矩形大小图_检测矩形并将图像裁剪到其大小

如果你是一个轮廓查找方法,OpenCV是目前最好的去处。如果你能用其他方法识别物体,略读可以很容易地识别坐标和裁剪图像。例如:import numpy as np

import skimage

from skimage import draw, measure

import matplotlib.pyplot as plt

# - Generate test data -

img = np.zeros((500, 500, 3), dtype=np.double)

poly = np.array([[25, 25],

[25, 75],

[75, 75],

[75, 25]])

rr, cc = draw.polygon(poly[:, 0], poly[:, 1], img.shape)

img[rr, cc, 1] = 1

# - End generate test data -

# Generate a mask with all objects close to green

green = (0, 1, 0)

mask = np.sum((img - green)**2, axis=2) < 0.1

# Label the different objects in the image

label_img = measure.label(mask)

# Find all objects

regions = measure.regionprops(label_img)

# Find the first object (since we only have one, that's the rectangle!)

r = regions[0]

# We'll use the `bbox` property to select the bounding box

# # For a full list of properties, take a look at the docstring of

# measure.regionprops

print('The bounding box is {}'.format(r.bbox))

r0, c0, r1, c1 = r.bbox

img_crop = img[r0:r1, c0:c1]

f, (ax0, ax1) = plt.subplots(1, 2)

ax0.imshow(img)

ax1.imshow(img_crop)

plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值