crop an image per words

12 views (last 30 days)
Dwi Putra Alexander
Dwi Putra Alexander on 19 Mar 2013
Commented: Juzer on 14 Aug 2018
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
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 (8)

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
  20 Comments
lotus whit
lotus whit on 11 Aug 2016
Edited: lotus whit on 11 Aug 2016
Thank you a lot for your comments

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:
  3 Comments
sayar chit
sayar chit on 24 Oct 2017
Sir My input image is 4.jpg. I want to get it as four pieces for words segmentation. I want to get images as part one like a.jpg figure, second part like b.jpg, third part is c.jpg and final part is d.jpg. This image is as follows.

Sign in to comment.


sayar chit
sayar chit on 24 Oct 2017
<<
<<
<<
>>
>>
>>

sayar chit
sayar chit on 24 Oct 2017
I got like as below figure using your code. Please help me Sir. I wish you good luck and health for your whole life. Thanks a lot again.

sayar chit
sayar chit on 24 Oct 2017
I want to get like this.
  3 Comments
sayar chit
sayar chit on 31 Oct 2017
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.


sayar chit
sayar chit on 25 Oct 2017
Sir! I don't get my result. So Sir help me please, if Sir has free. Thanks in advanced.

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
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.


Juzer
Juzer on 14 Aug 2018
Hello ImageAnalyst,
I would like to know that the code you provided. Is it a generic code and can be applied to all text images or I have to tweak the code for every image I want to crop for text?
Thank you. Looking forward to your reply.
  2 Comments
Juzer
Juzer on 14 Aug 2018
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.

Community Treasure Hunt

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

Start Hunting!