Reputation: 21
I have used the above code for image segmentation and extraction but how can we use knn for classification? I need help with the code. I have searched knn classify in mathworks but I am unable to understand the syntax. Any help with code would be appreciated.
I have got the result below after the execution:
If I am correct, my aim is to predict the characters in matlab compiler or in notepad after classification using knn but I am unable to code the k nearest neighbor after the above code.
%% Image segmentation and extraction
%% Read Image
imagen=imread('C:\Documents and Settings\vijaykumar\Desktop\v.jpg');
%% Show image
figure(1)
imshow(imagen);
title('INPUT IMAGE WITH NOISE')
%% Convert to gray scale
if size(imagen,3)==3 % RGB image
imagen=rgb2gray(imagen);
end
%% Convert to binary image
threshold = graythresh(imagen);
imagen =~im2bw(imagen,threshold);
%% Remove all object containing fewer than 30 pixels
imagen = bwareaopen(imagen,30);
pause(1)
%% Show image binary image
figure(2)
imshow(~imagen);
title('INPUT IMAGE WITHOUT NOISE')
%% Label connected components
[L Ne]=bwlabel(imagen);
%% Measure properties of image regions
propied=regionprops(L,'BoundingBox');
hold on
%% Plot Bounding Box
for n=1:size(propied,1)
rectangle('Position',propied(n).BoundingBox,'EdgeColor','g','LineWidth',2)
end
hold off
pause (1)
%% Objects extraction
figure
for n=1:Ne
[r,c] = find(L==n);
n1=imagen(min(r):max(r),min(c):max(c));
imshow(~n1);
pause(0.5)
end
Upvotes: 2
Views: 3594
Reputation: 24127
So what you've done here is to take an image of a word, and successfully separated the image into some smaller images of individual characters.
That's an important first step, but there are quite a few more steps to go before you can think about applying KNN for character recognition.
You will need to get a much larger set of character images. I've never personally done character recognition before, but from my experience in other classification tasks, I would estimate that to get good results you are likely to need at least a few thousand images of characters.
You will need to manually determine the correct character for each image.
If you're doing this as an exercise in teaching yourself, then instead of doing 1 and 2 yourself, I'm sure there are datasets available on the internet for free, containing large sets of pre-labelled character images, for people to use in trying out new algorithms.
You will need to do some feature extraction on each image. This means that you need to come up with several features that you measure for each image - features that are likely to correlate highly with the thing you're trying to predict (in this case which character the image is of). I'm not an expert in character recognition at all, but I would suggest that useful features might be things like the number of holes in the character, the number of straight lines in the character, whether the lines are horizontal or vertical, and the length of the lines relative to the overall character.
You will need to evaluate those features on each of your character images. In order to automate that, you will need to write some more image processing code to detect and measure each of the features.
When you've done all that, you'll have a single dataset that looks roughly like this:
______feature1 feature2 feature3 feature4 ... class
image1 1 2 1 0 P
image2 3 4 0 0 R
image3 2 1 1 0 P
image4 5 3 0 3 A
image5 1 6 0 0 D
...
image5768 2 3 1 1 Z
image5769 1 6 0 0 D
This dataset is what you apply KNN to. You would typically split the dataset into two parts, one for training and one for testing. Pass the training and test datasets, along with the training labels, into the MATLAB command knnclassify
. The output is predicted labels for the test dataset. Compare these with the actual labels to see how accurate you were.
I hope this helps - good luck!
Upvotes: 2