Optical character recognition for seven-segment display digit.[MATLAB]
5 views (last 30 days)
Show older comments
Hi. recently i am doing on a project of Optical character recognition for seven-segment display digit. I am asked to crop out [attach photo] the A,B,C,D,E,F,G accordingly using AUTO -CROPPING.
and i had a hard time figuring the codes & ways succesfully do it. I hope any of you can assist me with coding and explanations. thank you so much! :)
2 Comments
Manpreet Singh
on 27 Feb 2017
sir how to recognize seven segment characters in this image by ocr plz help
Accepted Answer
Image Analyst
on 8 Sep 2012
Edited: Image Analyst
on 8 Sep 2012
You don't need to crop, unless you want to, or unless your segments wander all over from image to image. You segments should be in the same place in each image unless for some reason your camera is not mounted properly, or your display is bouncing around.
To check whether segments are on or off is pretty easy. Just look at the color channels to extract the segments, and look in boxes located at certain locations (the middle of where each segment is expected to be) for whether the pixels there are white or black. See my demo to get you started:
% Demo to check a 7 segment display.
% By ImageAnalyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\Esther\Documents\Temporary';
baseFileName = '7segmentdisplay.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Display the original color image.
subplot(2, 2, 2);
imshow(redChannel, []);
title('Red Channel', 'FontSize', fontSize);
subplot(2, 2, 3);
imshow(greenChannel, []);
title('Green Channel', 'FontSize', fontSize);
subplot(2, 2, 4);
imshow(blueChannel, []);
title('Blue Channel', 'FontSize', fontSize);
% Extract just the segments and not the numbers.
figure;
binaryImage = ~(redChannel < 200) & (blueChannel < 200);
% subplot(2, 2, 1);
imshow(binaryImage, []);
axis on;
drawnow;
hold on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Now define locations to check.
row1 = 35;
row2 = 110;
row3 = 175;
row4 = 260;
row5 = 330;
col1 = 70;
col2 = 175;
col3 = 275;
% Plot boxes around there to check.
title('Checking Image Inside Red Boxes', 'FontSize', fontSize);
boxWidth = 30;
% First check top segment.
row = row1;
col = col2;
boxX = [col col+boxWidth col+boxWidth col col];
boxY = [row row row + boxWidth, row + boxWidth, row];
plot(boxX, boxY, 'ro-');
imageInsideBox = binaryImage(row:row + boxWidth, col:col+boxWidth)
% Determine if there are any pixels set inside that box.
segmentState(1) = any(imageInsideBox(:))
% Now check upper left segment.
row = row2;
col = col1;
boxX = [col col+boxWidth col+boxWidth col col];
boxY = [row row row + boxWidth, row + boxWidth, row];
plot(boxX, boxY, 'ro-');
imageInsideBox = binaryImage(row:row + boxWidth, col:col+boxWidth)
% Determine if there are any pixels set inside that box.
segmentState(2) = any(imageInsideBox(:))
% Now check lower right segment.
row = row4;
col = col3;
boxX = [col col+boxWidth col+boxWidth col col];
boxY = [row row row + boxWidth, row + boxWidth, row];
plot(boxX, boxY, 'ro-');
imageInsideBox = binaryImage(row:row + boxWidth, col:col+boxWidth)
% Determine if there are any pixels set inside that box.
segmentState(6) = any(imageInsideBox(:))
24 Comments
Image Analyst
on 25 Jan 2016
You don't need to do that. You can just have 7 skinny rectangles and get the mean gray level in each. If it's less than 128, there is a black stick in it. If it's more than that, it's all white and the segment is not turned on. Then you just have a look up table of 128 elements that gives you the number based on what elements are on. For example, if a "1" is elements "f" and "e" on, and all others off, and if the bits are abcdefg, then the pattern for a 1 would be 0000110. Well, that's a 6, so in your look up table, you'd have a 1 at element 6. The other 8 digits would be located at 8 other numbers because they have different patterns. As another example, 5 would have segments afgcd on, so the index would be 1011011, which is 91, so if you get 91, that means the pattern is a 5.
lookUpTable = zeros(128, 1);
lookUpTable(6) = 1;
lookUpTable(91) = 5;
% And so on for the other 8 digits.
So just read the intensities in the segments, convert to a binary number then a decimal number for the index and get the number.
theNumber = lookUpTable(index);
Brett Shoelson
on 3 Mar 2021
I would suggest that instead of trying to do this with image morphology or segment detection, you install a 7-segment font (freely available--Google), and train the font using the OCR Training App. Probably easier, almost certainly more accurate.
Cheers,
Brett
More Answers (1)
Rubina Easmin
on 20 Feb 2020
How do I crop every single character automatically in matlab by using segmentation?
please suggest me, how can I crop every single alphabet from this image automatically?
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!