Computer Vision: Assignment #02

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Computer Vision

Assignment #02
Task: Optical Mark Recognition from Camera Based Images

CODE:
clear all;
close all;
%Loading Image
InputImage = im2bw( imread('C:\Users\Sara Latif
Khan\OneDrive\Desktop\CV2\img.jpg'), 0.3);
figure, imshow(InputImage);
% Binarizing Image
binImage = ~InputImage;
binImage = 1-InputImage;
binImage = (InputImage == 0);
figure, imshow(binImage), title('Inverted Binary Image');

% Removing Noise Pixels


Image = medfilt2(binImage);

%Reducing Size
Image= imresize(Image,[800 800]);
% 'imtool' Function to get coordinates information from image
imtool(Image);
% Strcuturing element
se = strel('rectangle', [1, 85]);
%Dilation
img_dilated = imdilate(Image, se);
figure, imshow(img_dilated), title('Dilated Image')
% Labelling image to extract all connected lines
[Level,number] = bwlabeln(img_dilated,4);
disp(number);
% Initializing Arrays to store Answer, x and y coordinates respectively
Ans = blanks(number-2);
Array1 = [];
Array2 = [];
% Loop for analysis each line
for l=1: number
% 'regionprops' method to extract area and centeriod information
components = regionprops(Level==l, 'area','centroid');
% A condition to discard first two line with labels of Options
if(components(1).Area>5000)
% Intersecting each dilated indiviusal line from complement of image to get
indivisual labeled line
img = Level==l & Image;

% Erodong Image to extract only filled circle


SE = strel('rectangle', [4 4]);
Level1 = imerode(img,SE);
%figure, imshow(Level1);
% Dilating to fill holes in circle
SE = strel('rectangle', [10 10]);
Level1 = imdilate(Level1,SE);
9/19/2020

2
%figure, imshow(Level1);
% Finding possition of filled circle
[column,row] = imfindcircles(Level1,[10,20]);
% A condition to check if question is unattempted
if (size(column)==0)
Array1(l) = round(components(1).Centroid(:,1));
Array2(l) =round( components(1).Centroid(:,2));
Ans(l)='0';
else
% Else If Question is attempted then finding possition of circle
x= round(column(:,1));
y= round(column(:,2));

% Conditions to identify answer on the base of coordinate of filled circle


if( x(1)>100 && x(1)<130)||( x(1)>500 && x(1)<570)
Array1(l) = x;
Array2(l) = y;
Ans(l)='A';
elseif(x(1)>=140 && x(1)<=195)||( x(1)>575 && x(1)<635)
Array1(l) = x;
Array2(l) = y;
Ans(l)='B';
elseif( x(1)>=200 && x(1)<=240)||( x(1)>640 && x(1)<675)
Array1(l) = x;
Array2(l) = y;
Ans(l)='C';
elseif( x(1)>=245 && x(1)<=295)||( x(1)>680 && x(1)<720)
Array1(l) = x;
Array2(l) = y;
Ans(l)='D';
elseif( x(1)>=300 && x(1)<=335)||( x(1)>725 && x(1)<780)
Array1(l) = x;
Array2(l) = y;
Ans(l)='E';
end
end
end
end

% Separating Left and Right Column


LeftRow = [];
RightRow = [];
ArrayX=blanks(15);
LeftColumn = [];
RightColumn = [];
ArrayY=blanks(15);
for i=1:31
if(Array1(i) <500)
LeftRow(i)= Array1(i);
LeftColumn(i)= Array2(i);
ArrayX(i)= Ans(i);
else
RightRow(i) = Array1(i);
RightColumn(i)= Array2(i);
ArrayY(i)= Ans(i);
9/19/2020

3
end
end
% Zero ‘0’ Represents unattempt Question
disp('Left Column Answer');
disp(ArrayX);
disp('Right Column Answer');
disp(ArrayY);

Output:
Original Binary Image:

Inverted Binary Image:


9/19/2020

4
9/19/2020

5
Resized Image with Coordinates Info:

9/19/2020

6
Dilated Binary Image, with rectangular Structuring Element of Size [1,85]:

9/19/2020

7
Detected Answers :

9/19/2020

You might also like