crop an image per words

1 view (last 30 days)
Dwi Putra Alexander
Dwi Putra Alexander on 19 Mar 2013
Moved: DGM on 12 Feb 2023
i wanna crop an image per words,,,example : in image i have a sentence : "everybody going crazy"..and i wanna crop it per words so the result has 3 parts : parts 1 is an image with "everybody' words,parts 2 is an image with "going' words,and part3 is an image with "crazy' words,..what should i do to separated that words ?
  11 Comments
Matt Kindig
Matt Kindig on 19 Mar 2013
Also, I think part of your problem is that you are working with the RGB (color) images directly, whereas performing the word separation on the black and white images directly is probably easier. As a reminder, the bwmorph() operations work on the white pixels, so you might want to invert the black and white matrix so that the text is white and the background is black, rather than the way you have shown it here.
Image Analyst
Image Analyst on 20 Mar 2013
Dwi, see my code in my Answer below. Basically I did it for you. At least it works for that one image you uploaded.

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 19 Mar 2013
Edited: Image Analyst on 20 Mar 2013
Whole companies of people have been working on this for decades, so as you can guess it's not trivial. Go here : http://iris.usc.edu/Vision-Notes/bibliography/contentschar.html#OCR,%20Document%20Analysis%20and%20Character%20Recognition%20Systems and pick an algorithm, then code it up in MATLAB. We can't help you until you get to that point. There is no OCR toolbox for MATLAB that I'm aware of.
Here's what I would do
  1. threshold
  2. call imdilate
  3. call regionprops
  4. crop the bounding boxes.
EDIT:
Dwi, I haven't heard from you so I assume you are having trouble. Run my code to see how it's done.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
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;
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\DWI\Documents\Temporary';
baseFileName = '9jmwll.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.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, '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.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst for Dwi Putra Alexander','numbertitle','off')
% Convert to grayscale
if numberOfColorBands > 1
grayImage = grayImage(:,:,2); % Take green channel
end
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 3, 2);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Threshold the image.
binaryImage = grayImage < 175;
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Dilate to connect all the letters
binaryImage = imdilate(binaryImage, true(7));
% Get rid of blobs less than 200 pixels (the dot of the i).
binaryImage = bwareaopen(binaryImage, 200);
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Find the areas and bounding boxes.
measurements = regionprops(binaryImage, 'Area', 'BoundingBox');
allAreas = [measurements.Area]
% Crop out each word
for blob = 1 : length(measurements)
% Get the bounding box.
thisBoundingBox = measurements(blob).BoundingBox;
% Crop it out of the original gray scale image.
thisWord = imcrop(grayImage, thisBoundingBox);
% Display the cropped image
subplot(2,3, 3+blob); % Switch to proper axes.
imshow(thisWord); % Display it.
% Put a caption above it.
caption = sprintf('Word #%d', blob);
title(caption, 'FontSize', fontSize);
end
  23 Comments
Image Analyst
Image Analyst on 14 Aug 2018
Moved: DGM on 12 Feb 2023
No algorithm works on all possible images.
Juzer
Juzer on 14 Aug 2018
Moved: DGM on 12 Feb 2023
Doesn't work correctly on IAM handwriting dataset. Please find the attached result. Can you recommend what changes can I do for the improvement?

Sign in to comment.


sayar chit
sayar chit on 16 Oct 2017
Edited: Image Analyst on 16 Oct 2017
Hi Sir. I used your code with my images. But both of your codes are not working. So, help me please. Figure 1 is my image, figure 2 is what I got when your first code used and figure 3 is got when your second code used. I am waiting for your reply. Thanks you sir!
My input image is below:
  10 Comments
sayar chit
sayar chit on 25 Oct 2017
Moved: DGM on 12 Feb 2023
Sir! I don't get my result. So Sir help me please, if Sir has free. Thanks in advanced.
sayar chit
sayar chit on 31 Oct 2017
Moved: DGM on 12 Feb 2023
Sir! I got line and word segmentation for your codes and your suggestion. Thanks Sir. But I have a little problem. If my input paragraph images is incline (skew, my code do not work as well. So how do I need to overcome it problem? Help me please Sir. Thanks for all REALLY! my inclination input image is below as a sample.

Sign in to comment.


SS Jabeen
SS Jabeen on 8 Feb 2018
How do I use the above given code to segment the words in my image, can someone please explain me why is the code not working for my image?
  6 Comments
SS Jabeen
SS Jabeen on 20 Mar 2018
Thanks for the help, and impoint() would be helpful in verifying but while finding for multiple images manually moving the cursor would be a tad bit difficult. Is there any another alternative?
Image Analyst
Image Analyst on 20 Mar 2018
Another manual way is to use ginput(). If it's automatic, you can use find() to get the leftmost, topmost, rightmost, and bottom most black pixels in the image. If you want it by letter, then you'll have to get the locations of pixels in each letter, like you can get from calling regionprops() and asking for "PixelList'.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!