java - 裁剪带有轮廓的图像
问题描述
我有一张捕获的图像,该图像由一张桌子组成。我想从该图像中裁剪表格。 这是一个示例图像。 有人可以建议可以做什么吗?我必须在android中使用它。
解决方案
将图像转换为灰度。
对图像设置阈值以降低噪点。
找到非空白像素的最小面积矩形。
在 python 中,代码如下所示:
import cv2
import numpy as np
img = cv2.imread('table.jpg')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 222, 255, cv2.THRESH_BINARY )
# write out the thresholded image to debug the 222 value
cv2.imwrite("thresh.png", thresh)
indices = np.where(thresh != 255)
coords = np.array([(b,a) for a, b in zip(*(indices[0], indices[1]))])
# coords = cv2.convexHull(coords)
rect = cv2.minAreaRect(coords)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img, [box], 0, (0, 0, 255), 2)
cv2.imwrite("box.png", img)
如果您的图像没有红色方块,它将更紧密。
推荐阅读
- ruby-on-rails - 使用 before_filter 访问两个模型认证
- python - 无法从 Firebird blob sub_type 0 字段中找到文件扩展名
- c# - 将字符串解析为日期时间:字符串未被识别为有效的日期时间
- javascript - 更新多个圆环图 d3.js 的大小和值
- python - 检查是否可以在不运行脚本的情况下进行所有导入
- ios - 在 ios 中的整个应用程序中限制底部的标签栏
- javascript - 如何合并复选框和日期选择器的结果
- swift - 苹果电视上的 .ism/manifest 格式
- django - 不同域和平台之间的单点登录
- android - Firebase setCurrentScreen() 作为第一个事件没有记录正确的值