正如您已经找到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也行。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐