crop an image per words
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
Matt Kindig
2013-3-19
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
2013-3-20
Dwi, see my code in my Answer below. Basically I did it for you. At least it works for that one image you uploaded.
回答(3 个)
Image Analyst
2013-3-19
编辑:Image Analyst
2013-3-20
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
- threshold
- call imdilate
- call regionprops
- 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
sayar chit
2017-10-16
编辑:Image Analyst
2017-10-16
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!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178676/image.jpeg)
My input image is below:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178677/image.jpeg)
10 个评论
sayar chit
2017-10-25
移动:DGM
2023-2-12
Sir! I don't get my result. So Sir help me please, if Sir has free. Thanks in advanced.
sayar chit
2017-10-31
移动:DGM
2023-2-12
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/186662/image.jpeg)
SS Jabeen
2018-2-8
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 个评论
SS Jabeen
2018-3-20
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
2018-3-20
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'.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!