我需要在文档中找到最大的空白区域,并使用python在其中放置QR码,以显示其坐标,中心点和区域。
我认为OpenCV和Numpy应该足以完成此任务。
灰色,带颜色的BW,以及如何正确找到轮廓?
附加了Google的首次扫描,您可以在其中看到代码
应该找到最大的空平方面积。
@Mark Setchell谢谢!这段代码适用于所有带有白色背景的文档,但是当我在背景中使用带有某种颜色的smth时,它会发现一个完全不同的区域。另外,为了在文档中保留细线,我在阈值化后使用了Erode。试图更改阈值和侵 eclipse 参数,但仍无法正常工作。
编辑帖子,添加彩色图片。
最佳答案
这是一种可能的方法:
#!/usr/bin/env python3
import cv2
import numpy as np
def largestSquare(im):
# Make image square of 100x100 to simplify and speed up
s = 100
work = cv2.resize(im, (s,s), interpolation=cv2.INTER_NEAREST)
# Make output accumulator - uint16 is ok because...
# ... max value is 100x100, i.e. 10,000 which is less than 65,535
# ... and you can make a PNG of it too
p = np.zeros((s,s), np.uint16)
# Find largest square
for i in range(1, s):
for j in range(1, s):
if (work[i][j] > 0 ):
p[i][j] = min(p[i][j-1], p[i-1][j], p[i-1][j-1]) + 1
else:
p[i][j] = 0
# Save result - just for illustration purposes
cv2.imwrite("result.png",p)
# Work out what the actual answer is
ind = np.unravel_index(np.argmax(p, axis=None), p.shape)
print(f'Location: {ind}')
print(f'Length of side: {p[ind]}')
# Load image and threshold
im = cv2.imread('page.png', cv2.IMREAD_GRAYSCALE)
_, thr = cv2.threshold(im,127,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
# Get largest white square
largestSquare(thr)
输出Location: (21, 77)
Length of side: 18
注意:
关键字:图像处理,图像,Python,OpenCV,最大的白色正方形,最大的空白空间。
关于python - 如何在文档中找到最大的空白(白色)正方形区域并返回其坐标和面积?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64369800/