如何解决如何填充numpy.array使其可以为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>",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,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,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,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,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
padded_img = pad_image(thresh,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,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,w_padding),h_padding)),'edge')
我正在做糟糕的数学运算,并且弄不清numpy轴系统。我以为轴0上的填充要填充更多的行,轴1上的填充要填充更多的列;虽然是另一种方式。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。