我需要计算机视觉相关作业的帮助,我必须使用指定的指令在 MATLAB 中构建 OMR,但我无法这样做。
这是作业:
You are provided with images of sample answer sheets. Using morphological image processing operators, you are required to develop a system in Matlab which extracts the answers provided by the student for each question.
Hints:
- Binarize the image – The initial Binarization may be a crude one.
- Apply dilation with a horizontal structuring element to merge components in lines.
- Extract each line using CC labeling.
- If required, again binarize each line separately.
- For each line, use CC labeling to find different components. Use the area information to distinguish filled and non-filled bubbles. _ For each question, find the correct answer of the student. Input: Camera-based image of answer sheet
Output: A vector of correct answers provided by the students.
Assumptions: There are no lines with multiple answers and answers for all questions are provided.
Useful MATLAB functions
bwlabel
,regionprops
,imdilate
,imerode
,strel
这是我编写的代码,但它没有给我带来结果,我需要更多代码或者最好是一些模块来帮助我从填写的答题卡中提取正确答案,附件是代码和输入表
%reading the images
[fn, pn]=uigetfile('.');
InputImage = im2bw(imread([pn fn]));
figure, imshow(InputImage ), title('Original Binary Image');
% Removing Noise Pixels
% Rnp = bwareaopen(InputImage,45);
%figure,imshow(Rnp),title('Removed Noise Pixels');
% Strcuturing element
% se = strel('rectangle',[1,1]);
se = strel('rectangle', [15,1]);
% Erosion
%img_eroded = imerode(InputImage,se);
% figure,imshow(img_eroded), title('Eroded Image');
%Dilation
img_dilated = imdilate(InputImage, se);
figure, imshow(img_dilated), title('Dilated Image')
% figure,imshow(InputImage);
% Calculate the connected components
CC = bwconncomp(img_dilated);
% Create a label matrix
L = labelmatrix(CC);
% Find the maximum value of the label matrix, this value indicates the number of detected objects
numObjects = max(L(:))
% Display the label matrix
figure, imshow(L,[]);
%subplot (1,3,1), imshow(InputImage ), title('Orignal Binary Image');
%subplot (1,3,2), imshow(img_eroded), title('Eroded Image');
%subplot (1,3,3), imshow(img_dilated), title('Dilated Image');
% To make it easier to differentiate the different connected components, display the label matrix as an RGB Image
figure, imshow(label2rgb(L,'jet', 'k', 'shuffle'));
最佳答案
以下是您的代码的问题:
- 您需要为
im2bw
指定一个级别
,因为纸张不是完全白色的。因此,当以0.5
作为level
(默认值)调用im2bw
时,黑白图像中背景的某些部分会变成黑色。 - 请注意,
bwconncomp
会查找黑色背景中的白色像素体。所以在继续之前需要先反转二值化图像。 - 最好通过调用
medfilt2
来消除椒盐噪声。 - 正如作业中所预期的,您需要水平扩展图像以连接线条的各个部分,因此在调用
strel
构造结构元素时需要一个水平矩形。 当您修复这些问题时,您将获得正确的连接组件:
以下是从获得正确的标记图像后需要执行的步骤:
- 通过调用
regionprops
查找每个组件的面积、质心和边界框。 - 通过指定组件面积的阈值来分离与答案对应的组件。
- 此外,通过为组件质心的 x 设置另一个阈值来分离左列和右列的组件。
- 对于每一列:
- 按照质心 y 的顺序排列每列的行。
- 对于该列的每一行:
- 根据行索引和列索引计算问题编号。
- 从二值化图像(不是包含标签的图像或膨胀图像)中提取组件边界框内的内容,并将其保存在单独的图像中。
- 对分割后的图像调用
regionprops
并找到其中各组件的面积。 - 查找面积最大的组件的索引,由于
regionprops
从左到右对组件进行排序,因此返回该索引作为所选答案。
answers =
1 1 2 3 3 1 4 5 5 2 6 2 7 4 8 1 9 3 10 1 11 4 12 3 13 2 14 4 15 2 16 3 17 2 18 5 19 1 20 3 21 2 22 4 23 2 24 3 25 2 26 4 27 3 28 5 29 2 30 5
PS:虽然作业中明确指出已提供所有问题的答案,但您发布的图片中并未指定问题 10 的答案。上述算法对此类问题的结果将是随机的。但是您可以使用组件面积的偏差来检测它们是否全部为空白,正如 @Adriaan 在评论中建议的那样。
关于matlab - 使用 MATLAB 对基于相机的图像进行 OMR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63956183/