如何填充 numpy.array 使其成为 np.vsplit()?
How to pad an numpy.array so it can be np.vsplit()?
我正在尝试使用 Python 3.8 和 OpenCV 构建自己的 Bubble sheet OMR 引擎。
尽管进行了几天的调试,但我还是无法克服在单独裁剪气泡时出现的错误:
Traceback (most recent call last):
File "C:\Users\hsolatges\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\lib\shape_base.py", line 867, in split
len(indices_or_sections)
TypeError: object of type 'int' has no len()
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/autoQuiz/omr.py", line 80, in <module>
anwsers_array = tb.parse_anwsers(anwsers_field, MCQ.get('QUESTIONS'), MCQ.get('CHOICES'))
bubbles = get_bubbles(padded_img, questions, choices)
File "c:\autoQuiz\toolbox.py", line 81, in get_bubbles
rows = np.vsplit(img, questions)
File "<__array_function__ internals>", line 5, in vsplit
File "C:\Users\hsolatges\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\lib\shape_base.py", line 991, in vsplit
return split(ary, indices_or_sections, 0)
File "<__array_function__ internals>", line 5, in split
File "C:\Users\hsolatges\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\lib\shape_base.py", line 872, in split
raise ValueError(
ValueError: array split does not result in an equal division
由于气泡区域的大小是任意的,我尝试对其进行边缘填充,使其宽度和高度均为问题数/选项数(A B C D E)的倍数。不幸的是,它不能正常工作。除了tests/omr-1.jpg,其他都失败了。
代码摘录如下:
def to_next_multiple(n,b):
return int(ceil(n/b) * b)
def pad_image(img, questions, choices):
w, h = img.shape[:2]
w_final, h_final, = to_next_multiple(w, choices), to_next_multiple(h, questions)
w_padding, h_padding = max(0, w-w_final), max(0, h-h_final)
padded_img = np.pad(img, ((0, h_padding), (0, w_padding)), 'edge')
return padded_img
def get_bubbles(img, questions, choices):
bubbles = []
rows = np.vsplit(img, questions)
for row in rows:
cells = np.hsplit(row, choices)
bubbles.append(cells)
return bubbles
def parse_anwsers(img, questions, choices):
# Otsu's thresholding after Gaussian filtering
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
blurred = cv.GaussianBlur(gray, (5,5), 0)
retValue, thresh = cv.threshold(blurred, 0, 255, cv.THRESH_BINARY+cv.THRESH_OTSU)
padded_img = pad_image(thresh, questions, choices)
w, h = padded_img.shape[:2]
# Debugging
print(f'width: {w}, w_division: {w/choices}')
print(f'height: {h}, h_division: {h/questions}')
bubbles = get_bubbles(padded_img, questions, choices)
answers_array = bubbles
return answers_array
可在此处找到回购协议:https://github.com/hsolatges/autoQuiz
我怎样才能始终如一地准备好成为 np.vsplit/np.hsplit 图像?
所以问题来自以下几行:
w_padding, h_padding = max(0, w-w_final), max(0, h-h_final)
padded_img = np.pad(img, ((0, h_padding), (0, w_padding)), 'edge')
变为:
w_padding, h_padding = w_final-w, h_final-h
padded_img = np.pad(img, ((0, w_padding), (0, h_padding)), 'edge')
我在做蹩脚的数学并且错误地计算了 numpy 轴系统。我认为轴 #0 上的填充是填充更多行,而轴 #1 上的填充是填充更多列;虽然它是另一种方式。
我正在尝试使用 Python 3.8 和 OpenCV 构建自己的 Bubble sheet OMR 引擎。 尽管进行了几天的调试,但我还是无法克服在单独裁剪气泡时出现的错误:
Traceback (most recent call last):
File "C:\Users\hsolatges\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\lib\shape_base.py", line 867, in split
len(indices_or_sections)
TypeError: object of type 'int' has no len()
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/autoQuiz/omr.py", line 80, in <module>
anwsers_array = tb.parse_anwsers(anwsers_field, MCQ.get('QUESTIONS'), MCQ.get('CHOICES'))
bubbles = get_bubbles(padded_img, questions, choices)
File "c:\autoQuiz\toolbox.py", line 81, in get_bubbles
rows = np.vsplit(img, questions)
File "<__array_function__ internals>", line 5, in vsplit
File "C:\Users\hsolatges\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\lib\shape_base.py", line 991, in vsplit
return split(ary, indices_or_sections, 0)
File "<__array_function__ internals>", line 5, in split
File "C:\Users\hsolatges\AppData\Local\Programs\Python\Python38\lib\site-packages\numpy\lib\shape_base.py", line 872, in split
raise ValueError(
ValueError: array split does not result in an equal division
由于气泡区域的大小是任意的,我尝试对其进行边缘填充,使其宽度和高度均为问题数/选项数(A B C D E)的倍数。不幸的是,它不能正常工作。除了tests/omr-1.jpg,其他都失败了。
代码摘录如下:
def to_next_multiple(n,b):
return int(ceil(n/b) * b)
def pad_image(img, questions, choices):
w, h = img.shape[:2]
w_final, h_final, = to_next_multiple(w, choices), to_next_multiple(h, questions)
w_padding, h_padding = max(0, w-w_final), max(0, h-h_final)
padded_img = np.pad(img, ((0, h_padding), (0, w_padding)), 'edge')
return padded_img
def get_bubbles(img, questions, choices):
bubbles = []
rows = np.vsplit(img, questions)
for row in rows:
cells = np.hsplit(row, choices)
bubbles.append(cells)
return bubbles
def parse_anwsers(img, questions, choices):
# Otsu's thresholding after Gaussian filtering
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
blurred = cv.GaussianBlur(gray, (5,5), 0)
retValue, thresh = cv.threshold(blurred, 0, 255, cv.THRESH_BINARY+cv.THRESH_OTSU)
padded_img = pad_image(thresh, questions, choices)
w, h = padded_img.shape[:2]
# Debugging
print(f'width: {w}, w_division: {w/choices}')
print(f'height: {h}, h_division: {h/questions}')
bubbles = get_bubbles(padded_img, questions, choices)
answers_array = bubbles
return answers_array
可在此处找到回购协议:https://github.com/hsolatges/autoQuiz
我怎样才能始终如一地准备好成为 np.vsplit/np.hsplit 图像?
所以问题来自以下几行:
w_padding, h_padding = max(0, w-w_final), max(0, h-h_final)
padded_img = np.pad(img, ((0, h_padding), (0, w_padding)), 'edge')
变为:
w_padding, h_padding = w_final-w, h_final-h
padded_img = np.pad(img, ((0, w_padding), (0, h_padding)), 'edge')
我在做蹩脚的数学并且错误地计算了 numpy 轴系统。我认为轴 #0 上的填充是填充更多行,而轴 #1 上的填充是填充更多列;虽然它是另一种方式。