python - 有什么方法可以在 OpenCV 的轮廓检测中只检测矩形,而忽略其他文本检测?
解决方案
使用以下代码作为起点:
img = cv2.imread('amazon.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# inverse thresholding
thresh = cv2.threshold(gray, 195, 255, cv2.THRESH_BINARY_INV)[1]
# find contours
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
mask = np.ones(img.shape[:2], dtype="uint8") * 255
for c in contours:
# get the bounding rect
x, y, w, h = cv2.boundingRect(c)
if w*h>1000:
cv2.rectangle(mask, (x, y), (x+w, y+h), (0, 0, 255), -1)
res_final = cv2.bitwise_and(img, img, mask=cv2.bitwise_not(mask))
cv2.imshow("boxes", mask)
cv2.imshow("final image", res_final)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
图 1:原始图像:
图 2:所需轮廓的掩码:
图 3:原始图像中检测到的轮廓(期望):
推荐阅读
- c++ - 带有invoke_result的重载命名非成员函数的返回类型
- .net - 在.net中使用语音识别时如何停止听写窗口?
- python - 如何在一行 html 前端中自动构建 3 个列卡
- android - 地址选择器 Android
- php - 邮递员请求:ImportError: cannot import name DEFAULT_CIPHERS
- python - 如何在 Python 中使用 Xades-EPES 标准进行签名?
- javascript - 错误指出 createAppConbtainer 未定义并且它不是函数
- batch-file - 如何使用 Effective("flyer", 100%) 搜索行;在文件中并获取分配给变量的数字?
- spring-boot - 如何将错误绑定到 thymeleaf 页面中的字段
- javascript - 如何使用 PDF.js 在 WebView (JavaFX) 面板中显示 PDF 文件?