首页 > 解决方案 > 裁剪带有轮廓的图像

问题描述

我有一张捕获的图像,该图像由一张桌子组成。我想从该图像中裁剪表格。 这是一个示例图像。 有人可以建议可以做什么吗?我必须在android中使用它。

标签: javaandroidandroid-image

解决方案


将图像转换为灰度。

对图像设置阈值以降低噪点。

找到非空白像素的最小面积矩形。

在 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)  

对我来说,这会产生以下图像。 在此处输入图像描述

如果您的图像没有红色方块,它将更紧密。


推荐阅读