how to detect and extracts the subtitle from the given image ?

Answers (2)

Try ocr(). Here's a start, though it would be better if you had a good image to start with, not one highly subsampled both in space and intensity.
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 = 22;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'subtitles.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% 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
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
axis('on', 'image');
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
% Crop the subtitles
grayImage = grayImage(221:end, :);
subplot(2, 2, 2);
imshow(grayImage, []);
axis('on', 'image');
title('Cropped Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% Show the histogram
subplot(2, 2, 3);
imhist(grayImage);
grid on;
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Get a mask that is the entire image.
% mask = true(size(grayImage));
% Alternative : get a mask that is the thresholded part of the image.
threshold = 200;
mask = grayImage >= threshold;
% Get rid of small things
% mask = bwareaopen(mask, 50);
% xline(threshold, 'LineWidth', 2, 'Color', 'r');
subplot(2, 2, 4);
imshow(mask, []);
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% First try OCR on the binary image mask.
txt = ocr(mask)
% Then try it on the gray scale image.
txtg = ocr(grayImage)
alphaIndexes = isstrprop(txt.Text, 'alpha') | (txt.Text == ' ')
detectedText = txt.Text(alphaIndexes)
xlabel(detectedText);
message = sprintf('Done!\nDetected text:\n%s', detectedText)
msgbox(message);

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products

Release

R2017a

Asked:

on 19 Dec 2020

Answered:

on 19 Dec 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!