Horizontal Projection of the Histogram

i am using matlab for the ligature recognition and i want to get the horizontal Histogram projection of the Image so that i can segment the Lines can any one guide me regarding this
here is my code
close all;
clear all
clc;
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
I = imread('C:\Users\ENZEE\Desktop\MUS\shah.jpg');
Im=im2double(I);%use to double the precision of the Image
Ir=imresize(Im,[100 150]);
%figure,imshow(Ir), title('Extracted Image');
text(size(I,2),size(I,1)+15, ...
'Image courtesy', ...
'FontSize',7,'HorizontalAlignment','right');
text(size(Ir,2),size(Ir,1)+25, ....
'Trying.........', ...
'FontSize',7,'HorizontalAlignment','right');
I = imnoise(Ir,'salt & pepper',0.02);
K = medfilt2(Ir);
%imshow(Ir), title('Noise Image'), figure, imshow(K), title('Filtered Image');
%otsu's method
[level EM]= graythresh(K);
%display(level)
BW = im2bw(K,level);
%figure, imshow(BW),title('Converted Image');
[imx,imy]=size(BW);
%imhist(K), title('Histogram')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%Segmentation%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
numofpixels=size(I,1)*size(I,2);% size(image array,dimensions)
L=bwlabel(BW);%L = bwlabel(BW, n) returns a matrix L, of the same size as BW,
%containing labels for the connected objects in BW.
mn=min(L);
%disp('~~~~~~~~~~~~~~~~~~~~~~~~~~MIN~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
%disp(mn)
%disp('~~~~~~~~~~~~~~~~~~~~~~~~~FINDED~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
fd=find(mn<1);
%disp(fd)
%disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
calc=length(fd)/numofpixels;
disp(length(fd))
disp(numofpixels)
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%[r c]=size(BW);
%contour = bwtraceboundary(BW, [r,c], 'W', 8, 50,...
% 'counterclockwise');
%STATS = regionprops(BW,'FilledArea');
% seD = strel('arbitrary',numofpixels);
% BWfinal = imerode(BW,seD);
% BWfinal = imerode(BW,seD);
% %figure, imshow(BWfinal), title('segmented image');
% BWoutline = bwperim(BWfinal);
% Segout = Ir;
% Segout(BWoutline) = 255;
%figure, imshow(Segout), title('outlined original image');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%NEW TRY%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
subplot(2,2,1)
hist(BW)
subplot(2,2,2)
barh(~BW,2)
% [pixelCount grayLevels] = imhist(BW);
%
%
%
% bar(pixelCount); title('Histogram of original image');
% xlim([0 grayLevels(end)]); % Scale x axis manually.
%x1=size(I,1);
%y1=size(I,2)
%disp(STATS)
%crp= imcontour(BW,150);
%figure,imshow(crp),title('Manually Image')
%crp=imcrop(BW);
%imcontour(crp,2);
%figure,imshow(crp),title('Manually Croped Image')
%a=bwperim(BW);
%imshow(a)
%bwdist()
%disp(calc)
%a=imcontour(I,2);
%imshow(a)
%CR=imcrop(BW,'rect');
%figure,imshow(CR)
%t=wpdec2(BW,2,'db1');
%plot(t)
% figure;
% hi=hist(BW);
% hi1=hi(1:2:256);
% horz=1:2:256;
% bar(horz,hi);
% axis([0 255 0 1400]);
% set(gca, 'xtick', 0:50:255);
% set(gca, 'ytick', 0:2000:15000);
% xlabel('Gray level' );
% ylabel('No of pixels' );
% title('Histogram before opening the image');
}

5 Commenti

http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Sir i have corrected the format would you please guide me further
thanks for answering
Your answer isn't quite clear so if you have clean suggestion for lines,words and character segmentation, give me my account (chitsanlwin.maths.mm@gmailcom). I wish you good luck and health for your whole life
sayar chit, who were you addressing that to? Whose answer is not quite clear? Which answer? What are some of the parts you are finding confusing?

Accedi per commentare.

Risposte (2)

hist(sum(double(BW),2))

18 Commenti

well it does not shows me the correct output i want the horizontal Projection of the Histogram of my Image so that i can segment the lines
Do you want a histogram of the projection, or a projection of the histogram? Histograms are usually 1D graphics, so a horizontal projection of a histogram would normally be a single box. The code I gave was for the histogram of the projection.
I don't know what this means either. The "horizontal Projection of the Histogram" would be just a single bar. Or, if you summed it horizontally across bins, you'd get a 1D array the meaning of which I'm not quite sure how to interpret and not sure why you'd want it. You need to explain further, if you can't figure it out yourself.
i want to segment the ligature words from the sentences and want to eliminate the white background from the Image.
the words be croped on the basis of Histogram ranges
I'm still not sure what you want but it sounds like maybe you need to research "automatic threshold methods."
http://www.flickr.com/photos/80572286@N07/?uploaded=1&magic_cookie=6999a1d23a8df99aa67efa8fc18cea97
here is the image i am working on and in the above code my Hist command shows me the histogram of the image and gives me the range , on the basis of range i want to crop the the Line and words
I suggest you look at papers published by those who have done it successfully already: http://iris.usc.edu/Vision-Notes/bibliography/contentschar.html#OCR,%20Document%20Analysis%20and%20Character%20Recognition%20Systems
Well i have watched the above link but i am feeling difficulty how to extract those character who are black
--> Second if i am going to get the histogram of each line i can easily Extract the line and then those words
-->the second technique that i am using is the regionprops
disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
mp=regionprops(BW,'All')
disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
for a=1:pixelCount;
if grayLevels>=0.3500;
%disp('Assad')
end
Box = mp. Extent; % Get list of pixels in current blob.
subImage = imcrop(BW,Box);
subplot(2,2,3)
imshow(subImage);
end
I don't know who told you that Extent is the list of pixels in the blob. It is not. PixelValues is. Plus, it's not the bounding box either. BoundingBox is. I'm sorry you feel that the published methods are too difficult. Robust algorithms tend to be complicated if they are to handle lots of different situations.
Too many errors in that code to bother to explain.
mp = regionprops(BW, 'All');
for K = 1 : length(mp)
Box = mp(K).BoundingBox;
subImage = imcrop(BW,Box);
subplot(2,2,3)
imshow(subImage);
drawnow();
end
Going back to your "histogram" question:
H = sum(double(BW),2);
H *is* now the histogram. Divide the rows up at places where H is 0 or very low in order to get an estimate of where the lines of text are.
can you recommend the code... how to do it Walter with Histogram?
linesWithText = H < 50; % Or whatever value works.
linesWithText will be true (1) for image rows where there are lots of white pixels in a line, and false (0) where there are not.
I think that should be H > 50 (or whatever value works).
close all;
clear all
clc;
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
I = imread('C:\Users\ENZEE\Desktop\MUS_WAS\shah.jpg');
Im=im2double(I);%use to double the precision of the Image
Ir=imresize(Im,[100 150]);
%figure,imshow(Ir), title('Extracted Image');
text(size(I,2),size(I,1)+15, ...
'Image courtesy', ...
'FontSize',7,'HorizontalAlignment','right');
text(size(Ir,2),size(Ir,1)+25, ....
'University', ...
'FontSize',7,'HorizontalAlignment','right');
I = imnoise(Ir,'salt & pepper',0.02);
K = medfilt2(Ir);
%imshow(Ir), title('Noise Image'), figure, imshow(K), title('Filtered Image');
%otsu's method
[level EM]= graythresh(K);
%display(level)
BW = im2bw(K,level);
%figure, imshow(BW),title('Converted Image');
BW = bwareaopen(BW,20);
[imx,imy]=size(BW);
%imhist(K), title('Histogram')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%Segmentation%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
numofpixels=size(I,1)*size(I,2);% size(image array,dimensions)
L=bwlabel(BW);%L = bwlabel(BW, n) returns a matrix L, of the same size as BW,
%containing labels for the connected objects in BW.
%imshow(L)
mn=min(BW);
%disp('~~~~~~~~~~~~~~~~~~~~~~~~~~MIN~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
%disp(mn)
%disp('~~~~~~~~~~~~~~~~~~~~~~~~~FINDED~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
fd=find(mn<1);
%disp(fd)
%disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
calc=length(fd)/numofpixels;
disp(length(fd))
disp(numofpixels)
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mp=regionprops(BW,'All')
disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%NEW TRY%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure
subplot(2,2,1)
%H = sum(double(BW),2);
%hist(H)
%hist(BW)
%[h,bins]=hist(BW);
subplot(2,2,2)
imhist(I)
[pixelCount,grayLevels] = hist(BW)
disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
disp(pixelCount)
disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
disp(grayLevels)
disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
mp=regionprops(BW,'All')
disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
for a=1:numofpixels;
if grayLevels>=0.3500;
%disp('Assad')
end
Box = mp.BoundingBox; % Get list of pixels in current blob.
subImage = imcrop(BW,Box);
subplot(2,2,3)
imshow(subImage);
end
------------------------------------------------------------------------------------------------------------------------------------------------------------
where should i use this code ...?
The text was dark blobs on a light background so that's why I used H < 50. I think graythresh() sets bright pixels to 1 and dark pixels to 0. So the text would have low values and the background high values when you do a sum horizontally of the binary image. I still think that looking at successful methods others have developed is the way to go.
Hmmm, I would assume that the foreground and background would have been reversed somewhere along the way, or else the regionprops() and labeling are going to produce pretty useless information. I guess that wasn't a good assumption.
The assignment to H and the thresholding against 50 (or whatever works) done on that, could go anywhere after the bwopenarea() and before the "for a".
I notice that the "for a" code has not been repaired even though I gave specific replacement code :(
what is BWopenarea() ?
are you talking about bwareaopen() i was thinking if i can segment the lines on the be half of thresolds\histograms whats the techniques of doing that

Accedi per commentare.

Image Analyst
Image Analyst il 25 Giu 2012

0 voti

I don't know if you have to use that algorithm, but there are plenty to choose from. How about this one:

2 Commenti

can you suggest me how to crop on the basis of Histogram values such as crop all those who are less than in Histogram
If H is your row histogram,
first = find(H >= 10, 1, 'first');
last = find(H >= 10, 1, 'last');
cropped_image = YourImage(first:last, :);

Accedi per commentare.

Categorie

Scopri di più su Display Image in Centro assistenza e File Exchange

Richiesto:

il 13 Giu 2012

Commentato:

il 19 Ott 2017

Community Treasure Hunt

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

Start Hunting!

Translated by