필터 지우기
필터 지우기

crop an image per words

조회 수: 4 (최근 30일)
Dwi Putra Alexander
Dwi Putra Alexander 2013년 3월 19일
이동: DGM 2023년 2월 12일
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
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
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
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
  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
Image Analyst
Image Analyst 2018년 8월 14일
이동: DGM 2023년 2월 12일
No algorithm works on all possible images.
Juzer
Juzer 2018년 8월 14일
이동: DGM 2023년 2월 12일
Doesn't work correctly on IAM handwriting dataset. Please find the attached result. Can you recommend what changes can I do for the improvement?

댓글을 달려면 로그인하십시오.


sayar chit
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!
My input image is below:
  댓글 수: 10
sayar chit
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
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.

댓글을 달려면 로그인하십시오.


SS Jabeen
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
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
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'.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by