Vous êtes sur la page 1sur 8

TEMPLATE:

CODE:

clc; % Clear the command window.


close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;

% Get the name of the image the user wants to use.


baseFileName = 'dsppp.jpg';
% Get the full filename, with path prepended.
folder = pwd
fullFileName = fullfile(folder, baseFileName);

%pangkuha ng answers key na naka numerical matrix format


s=input('Enter the correct answers in this format (Ex: A = 1, B = 2 and so
on) [ 1 2 3 2 ]: ')

% Check if file exists.


if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping
off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path
folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%============================================================================
===
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[imageRows, imageColumns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');

% Set up figure properties:


% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over
the image.

% Extract the individual red, green, and blue color channels.


redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);

% Get the means so we can tell if the template is red or blue


redMean = mean2(redChannel);
greenMean = mean2(greenChannel);
blueMean = mean2(blueChannel);

if redMean > greenMean && redMean > blueMean


% Template is red. Extract the red channel for the highest contrast
marks
% scribbled in by the test taker.
grayImage = rgbImage(:, :, 1);
colorChannelUsed = 'Red';
elseif greenMean > redMean && greenMean > blueMean
% Template is green. Extract the green channel for the highest contrast
marks
% scribbled in by the test taker.
grayImage = rgbImage(:, :, 2);
colorChannelUsed = 'Green';
else
% Template is blue. Extract the bluechannel for the highest contrast
marks
% scribbled in by the test taker.
grayImage = rgbImage(:, :, 3);
colorChannelUsed = 'Blue';
end

% Display the image.


subplot(2, 2, 2);
imshow(grayImage, []);
axis on;
caption = sprintf('Gray Scale Image of the %s Color Channel',
colorChannelUsed);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;

%==================== IMPORTANT PART: SPECIFY THE CIRCLE CENTER LOCATIONS


=============================================
% The location of the circles/bubbles may vary depending on the form.
% You must determine where the circles are for each form.
% If it varies from test sheet to test sheet, then you must align/register
all your images
% such that all circles appear at the same pixel in every image.
% The method I use below will apply to THIS PARTICULAR FORM, NOT to all
forms.
% Determine grid, knowing there are 15 question rows between line 52 and 400;
rows = linspace(77, 256, 10);%ung 10 dapat 15 %iaadjust yung 50 & 274 depende
sa layout
% Plot rows.
hold on;
for k = 1 : length(rows)
line([1, imageColumns], [rows(k), rows(k)], 'Color', 'r');
end

% Determine grid, knowing there are 7 question columns between column 45 and
947;
cols = linspace(310, 54, 1); %ung 1 dapat 7 %iaadjust 155 & 45 depende sa
layout din bes
% Plot rows.
hold on;
for k = 1 : length(cols)
line([cols(k), cols(k)], [1, imageRows], 'Color', 'r');
end

% Get the increments from one bubble to another within the same question
bubbleSpacing = linspace(0, 258, 5);
circleRadius = 11; % pixels.

% Get a list of all (x,y) locations for the start of the questions and plot +
symbols on them
[x, y] = meshgrid(cols, rows);

% Display the image.


subplot(2, 2, 3);
imshow(grayImage, []);
axis on;
caption = sprintf('Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hold on;

%==================== NOW FIND THE ANSWERS


=============================================
% Examine the mean gray level in a little box at each circle location.
% Create a counter for the question number and answers.
questionNumber = 0; % An index.
answers = zeros(1, numel(y)); % Initalize to all zeros for all questions.

comparing = s; %pagseset ng mga tamang sagooooot

answerMeans = zeros(1, numel(y)); % Gray level of the circle chosen as the


answer.
for qCol = 1 : size(y, 2)
for qRow = 1 : size(y, 1)
% For each question...
questionNumber = questionNumber + 1;
% Get the row and column of the first bubble in that question's row.
thisRow = y(qRow, qCol); % X location of the first bubble in the row
thisCol = x(qRow, qCol); % X location of the first bubble in the row
% Plot +'s along each bubble in that question's row.
for c = 1 : 5 % For 5 bubbles
bubbleColumn = thisCol + bubbleSpacing(c);
% Get the rows and columns for this one bubble.
r1 = thisRow - circleRadius;
r2 = thisRow + circleRadius;
c1 = bubbleColumn - circleRadius;
c2 = bubbleColumn + circleRadius;
thisBubble = imcrop(grayImage, [c1, r1, 2*circleRadius,
2*circleRadius]);
bubbleMeans(qRow, qCol, c) = mean2(thisBubble);

% Show out progress on the image by plotting a + over where we


are.
subplot(2, 2, 3);
caption = sprintf('Our Progress: Circle #%d of question #%d', c,
questionNumber);
title(caption, 'FontSize', fontSize);
plot(bubbleColumn, thisRow, 'r+', 'LineWidth', 1, 'MarkerSize',
8);

% Show this particular circle.


subplot(2, 2, 4);
imshow(thisBubble, []);
axis on;
caption = sprintf('Circle #%d of question #%d', c,
questionNumber);
title(caption, 'FontSize', fontSize);
drawnow;
end
% Find the darkest bubble in this row.
[minGrayLevel, answers(questionNumber)] = min(bubbleMeans(qRow, qCol,
:));
answerMeans(questionNumber) = minGrayLevel;
% Plot a red circle around the darkest circle.
subplot(2, 2, 3);
bubbleColumn = thisCol + bubbleSpacing(answers(questionNumber));
plot(bubbleColumn, thisRow, 'mo', 'LineWidth', 2, 'MarkerSize', 12);
% Print the answers to the command window. Both the number of the
circle and the letter corresponding to it.
fprintf('For question #%3d the selected answer was circle #%d =
%c\n',...
questionNumber, answers(questionNumber),
char(answers(questionNumber) + 64));
end

%try here hahaha dito ilagay yung condition for compairing para safe sa
%mga codes HAHAHAHA
answers
comparing
Result=0
for i = 1:10
if answers(i) == comparing(i)
Result = Result + 1;
end
end

end
Result
% Get the answers in a different form, a character array:

% Basically we're done now. Let's just do a little further analysis of the
results, just for fun.....

% Permute so that we'll keep all circles for a particular question adjacent
to each other
% after we turn it into a column vector with (*).
bMeans = permute(bubbleMeans, [3,1,2]);

% Get the mean gray level of those circles that were, and were not, chosen as
answers
% using kmeans(). Requires the Statistics and Machine Learning Toolbox.
[theClass, clusterCenters] = kmeans(bMeans(:), 2);
meanAnswerGL = min(clusterCenters)
meanNonAnswerGL = max(clusterCenters)

% Plot the circle means for all questions


figure;
darkGreen = [0, 0.5, 0];
subplot(2, 1, 1);
class1Indexes = find(theClass == 1);
plot(class1Indexes, bMeans(class1Indexes), 'b*');
hold on;
class2Indexes = find(theClass == 2);
plot(class2Indexes, bMeans(class2Indexes), '*', 'Color', darkGreen);
grid on;
xlim([1, numel(bMeans)]);
title('Mean Gray Levels of the 5 circles for all 10 questions', 'FontSize',
fontSize);
xlabel('Circle Number', 'FontSize', fontSize);
ylabel('Mean Gray Level', 'FontSize', fontSize);

% Plot lines along the means of answers and non-answers.


line(xlim, [meanAnswerGL, meanAnswerGL], 'Color', 'r', 'LineWidth', 2);
line(xlim, [meanNonAnswerGL, meanNonAnswerGL], 'Color', 'r', 'LineWidth', 2);

% Now plot the gray levels of just the answers,


% so we can see if any of them should really be non-answers.
subplot(2, 1, 2);
plot(answerMeans(:), 'b*');
grid on;
xlim([1, numel(answerMeans)]);
title('Mean Gray Levels of the "Answer" circle for all 10 questions.
Questionable ones circled.', 'FontSize', fontSize);
xlabel('Question Number', 'FontSize', fontSize);
ylabel('Mean Gray Level', 'FontSize', fontSize);

% Plot lines along the means of answers and non-answers.


line(xlim, [meanAnswerGL, meanAnswerGL], 'Color', 'r', 'LineWidth', 2);
line(xlim, [meanNonAnswerGL, meanNonAnswerGL], 'Color', 'r', 'LineWidth', 2);

% Determine which answers are questionable or unanswered. They will be a


gray level that is a
% certain percentage of the way from the good answers to the non-answers gray
levels.
% The decision as to whether an answer is questionable does not necessarily
correspond to
% whether kmeans() decided it was in the non-answer class. For example, some
x's and checkmarks
% may be in the non-answer class according to kmeans(), but you may well
decide
% that they were responses that SHOULD be accepted as intended answers.
unansweredPercentage = 0.9;
questionablePercentage = 0.75;
unanswered = answerMeans > meanAnswerGL + unansweredPercentage *
abs(meanNonAnswerGL - meanAnswerGL);
% Questionable should not include unanswered, to AND with !unanswered.
questionable = ~unanswered & (answerMeans > meanAnswerGL +
questionablePercentage * abs(meanNonAnswerGL - meanAnswerGL));
% convert from logical indexes to actual indexes.
unanswered = find(unanswered);
questionable = find(questionable);
% Print unasnwered answers to the command window.
fprintf('Unanswered questions:\n');
fprintf('%d ', unanswered);
fprintf('\n');
% Print questionable answers to the command window.
fprintf('Questionable answers:\n');
fprintf('%d ', questionable);
fprintf('\n');
% Plot circles around those questionable ones.
hold on;
plot(questionable, answerMeans(questionable), 'mo', 'LineWidth', 2,
'MarkerSize', 12);
% Plot black o's over the unanswered ones.
hold on;
plot(unanswered, answerMeans(unanswered), 'ko', 'LineWidth', 2, 'MarkerSize',
12);

%para makita ng maayos yung result na naka in order hahaha


answersLetters = char(answers + 64);
aaa = s;
letters = 'ABCDE'; %eto yung pang-convert from numbers to letters
KeyToCorrection = letters(aaa);
StudentAnswers = answersLetters;

StudentAnswers
KeyToCorrection
Result

if ~isempty(questionable)
message = sprintf('There were some questionable answers.\nSee the command
window.\nYou need to decide what to do about those.');
uiwait(warndlg(message));
end
OPTICAL MARK RECOGNITION

Submitted By:
Acuzar, Jenny
Marasigan, Ryan Reeve B.
Miranda, John Derick V.

Submitted To:
Engr. Oliver Medina

October 13, 2017

Vous aimerez peut-être aussi