Image segmentation applied to text
Afficher commentaires plus anciens
Suppose I have the following image:

I want to remove the surrounded white and black pixels and convert it to the following image (it is a kind of segmentation starting from the first white pixel related to the text (at the top and at the left) and ending at the white pixel at the left and the bottom.

Réponses (1)
Image Analyst
le 25 Oct 2015
first of all, don't use JPG images for image processing - it makes for blurry, inaccurate edges. Use PNG instead.
Try using bwconvhull() followed by regionprops() to get the bounding box, followed by indexing to crop out the box.
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 = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in a gray scale demo image.
folder = pwd;
baseFileName = '3.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
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 image.
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(3, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', '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')
%============================================
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(3, 2, 2);
bar(grayLevels, pixelCount); % Plot it as a bar chart.
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Binarize the image
binaryImage = grayImage > 25;
% Get rid of white touching the border
binaryImage = imclearborder(binaryImage);
% Display the image.
subplot(3, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Get the convex hull
binaryImage = bwconvhull(binaryImage, 'union');
% Display the image.
subplot(3, 2, 5);
imshow(binaryImage, []);
title('After convex hull', 'FontSize', fontSize, 'Interpreter', 'None');
% Use regionprops to get bounding box of it
measurements = regionprops(bwlabel(binaryImage), 'BoundingBox');
leftColumn = ceil(measurements.BoundingBox(1));
rightColumn = leftColumn + measurements.BoundingBox(3);
topRow = ceil(measurements.BoundingBox(2));
bottomRow = topRow + measurements.BoundingBox(4);
% Crop out the original image
croppedImage = grayImage(topRow:bottomRow, leftColumn:rightColumn);
% Display the image.
subplot(3, 2, 6);
imshow(croppedImage, []);
title('Gray Image After Cropping', 'FontSize', fontSize, 'Interpreter', 'None');

2 commentaires
Mohammad
le 26 Oct 2015
Image Analyst
le 26 Oct 2015
If it worked, can you click the link to "Accept this answer"? Thanks in advance.
Catégories
En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!