Thanks in advance for any help
If you bwareaopen and subtract the result of that from the original
image, the result should be just the small objects.
--
"And that's the way it is." -- Walter Cronkite
Daphne
"Robert Maidhof" <rtm...@columbia.edu> wrote in message
<fub16i$jaf$1...@fred.mathworks.com>...
Hey thank you all for your Help. I finally managed to read the Braille with my algorithm. My Next Task is to detect from a video wether there is a Medicament Box in the picture or not then taking a shot of the Box and analyze it like I did with the single Picture. I just don't know how to tell matlab to analyze a live video an then take a frame out of it automatically. I would be very pleased by any help and suggestions.
------------------------------------------------------------------
Use bwareaopen() once for each size to cull the blobs of certain
sizes. Then use regular logical operations on the results. Write
back if you can't figure it out.
here i upload the image how it should look, if didn't understand wat i mean...tq...
-------------------------------------------------------------------------------------------------------------------
You don't call those "histograms." Just because they're bar charts,
like histograms often use, does not mean that they are histograms. In
fact, they are totally different. To get the vertical and horizontal
intensity profiles:
verticalProfile = sum(imageArray, 2); % Sum along columns
horizontalProfile = sum(imageArray, 1); % Sum along rows
the result like this....
horizontal profile
0 - 262 pixels = 68595 (white)
263 - 395 pixels = 42300 (image wanted)
395- 480 pixels = 20000 (not black pixels)
481 - 639 pixels = 68595 (white)
sir, i want cut the image pixel which contain intensity > 424000 and intensity < 20000,
so i just want image pixels which the intensity between (20000 to 42400) like tat...
sir can help to tell, wat command i have to use to do it...tq...
-----------------------------------------------
No. You basically need to process your profile to get it to a point
where you can then threshold it. You probably have to use some
algorithm to decide on the threshold rather than using a fixed one.
there also i use other method to remove black edges..example code as below..but i get error...what is the problem...
maskedImage2 = bwareaopen(maskedImage,50);
maskedImage2 = maskedImage - maskedImage2;
maskedImage = maskedImage2;
first .m file
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
% Find books
clc;
clear;
close all;
workspace;
fontSize = 20;
a=1;
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Read in books image.
folder = 'E:\design\image\';
baseFileName = 'IMG_0047.JPG';
fullFileName = fullfile(folder, baseFileName);
rgbImage = imread(fullFileName);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full screen.
set(gcf,'name','Library Robot','numbertitle','off')
grayImage = rgb2gray(rgbImage);
% Do a Canny edge detection on the image.
BW = edge(grayImage,'canny');
subplot(2, 2, 2);
imshow (~BW, []);
title('Canny Edge Image', 'FontSize', fontSize);
%Do hough transformation on image
[H,T,R] = hough(BW);
subplot(2, 2, 3);
imshow(H,[],'XData',T,'YData',R,...
'InitialMagnification','fit');
title('Hough Peaks', 'FontSize', fontSize);
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
P = houghpeaks(H,20,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2));
y = R(P(:,1));
plot(x,y,'s','color','red');
% Find lines
lines = houghlines(BW,T,R,P,'FillGap',25,'MinLength',100);
% find lines point
max_len = 0;
verticalLineIndexes = [];
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
% Determine it's angle
deltaX = xy(1, 1) - xy(2,1);
deltaY = xy(1, 2) - xy(2,2);
angle = atand(deltaY / deltaX)
if abs(angle) > 45
verticalLineIndexes = [verticalLineIndexes, k];
end
end
% Extract just the vertical lines:
lines = lines(verticalLineIndexes);
% Get the size of the image
[rows columns] = size(grayImage);
numberOfLines = length(lines);
for k = 1 : numberOfLines
% Plot the line
xy = [lines(k).point1; lines(k).point2];
% Turn the line and get it's equation.
% Get the formula for the line.
turnedX = xy(:, 2); % The old Y values are the new x values.
turnedY = xy(:, 1); % The old X values are the new y values.
[coeffs S mu] = polyfit(turnedX, turnedY, 1);
% Extend lines to bottom of screen
% Get the x values for y = 1.
fullTurnedY(1) = polyval(coeffs, turnedX(1),[],mu);
fullTurnedY(2) = polyval(coeffs, turnedX(2),[],mu);
fullLengthLines(k, 1, 1) = fullTurnedY(1); % x for point 1
fullLengthLines(k, 2, 1) = fullTurnedY(2); % x for point 2
fullLengthLines(k, 1, 2) = 1; % y for point 1
fullLengthLines(k, 2, 2) = rows; % y for point 2
end
subplot(2, 2, 4);
imshow(rgbImage);
title('Original Color Image with Boundary', 'FontSize', fontSize);
hold on;
format long;
for k = 1 : numberOfLines
% Plot the line
xCoords = fullLengthLines(k, :, 1);
yCoords = fullLengthLines(k, :, 2);
fprintf(1, '%d: point1 = (%.1f, %.1f), point2 = (%.1f, %.1f,)\n', ...
k, xCoords(1), yCoords(1), xCoords(2), yCoords(2));
plot(xCoords, yCoords,'LineWidth',2,'Color','green');
end
% Need to sort them based on their x value.
lines2D = zeros(numberOfLines, 4);
for k = 1 : numberOfLines
lines2D(k, :) = [fullLengthLines(k, 1,1),... % x1
fullLengthLines(k, 1,2), ... % y1
fullLengthLines(k, 2,1), ... % x2
fullLengthLines(k, 2,2)]; % y2
end
lines2D = sortrows(lines2D, 1);
% Crop out each book.
for k = 2:length(lines)
% Plot the line
xCoordsTop1 = lines2D(k-1, 1);
yCoordsTop1 = lines2D(k-1, 2);
xCoordsBottom1 = lines2D(k-1, 3);
yCoordsBottom1 = lines2D(k-1, 4);
xCoordsTop2 = lines2D(k, 1);
yCoordsTop2 = lines2D(k, 2);
xCoordsBottom2 = lines2D(k, 3);
yCoordsBottom2 = lines2D(k, 4);
verticesX = [xCoordsTop1 xCoordsTop2 xCoordsBottom2 xCoordsBottom1 xCoordsTop1];
verticesY = [yCoordsTop1 yCoordsTop2 yCoordsBottom2 yCoordsBottom1 yCoordsTop2];
mask = poly2mask(verticesX, verticesY, rows, columns);
mask = cast(mask, class(grayImage));
maskedImage = grayImage .* mask;
imagen= maskedImage;
figure;
set(gcf,'position', get(0,'Screensize'))
% Convert to gray scale
if size(imagen,3)==3 %RGB image
imagen=rgb2gray(imagen);
end
% Convert to BW
binaryImage = (imagen == 0);
imagen(binaryImage) = 255;
horizontalProfile = sum(imagen, 1); % Sum along rows
columnsToKeep= horizontalProfile > 26000 & horizontalProfile < 35000 ;
imagen =imagen(:, columnsToKeep);
thresholdValue= 110;
imagen=imagen < thresholdValue;
% Remove all object containing fewer than 20 pixels
imagen = bwareaopen(imagen,20);
imshow(imagen);
%Storage matrix word from image
word=[ ];
imshow(word);
re=imagen;
%Opens text.txt as file for write
fid = fopen('text.txt', 'wt');
% Load templates
load templates
global templates
% Compute the number of letters in template file
num_letras=size(templates,2);
while 1
%Fcn 'lines' separate lines in text
[fl re]=line2(re);
imgn=fl;
%Uncomment line below to see lines one by one
imshow(fl);pause(1.0)
%-----------------------------------------------------------------
% Label and count connected components
[L Ne] = bwlabel(imgn);
for n=1:Ne
[r,c] = find(L==n);
% Extract letter
n1=imgn(min(r):max(r),min(c):max(c));
% Resize letter (same size of template)
img_r=imresize(n1,[42 24]);
imwrite(img_r,['E:\design\image\book\',num2str(a),'.jpg']);
%Uncomment line below to see letters one by one
imshow(img_r);pause(0.5)
%-------------------------------------------------------------------
% Call fcn to convert image to text
letter=read_letter(img_r,num_letras);
% Letter concatenation
word=[word letter];
a = a + 1;
end
fprintf(fid,'%s\n',lower(word));%Write 'word' in text file (lower)
fid = fopen('text.txt','w');
fprintf(fid,'%s\n',word);%Write 'word' in text file (upper)
% Clear 'word' variable
%word=[ ];
%*When the sentences finish, breaks the loop
if isempty(re) %See variable 're' in Fcn 'lines'
break
end
end
word1='PRSBIF5B2005';
z=strcmp(word,word1);
if (z==1)
%error('no match file');
fprintf('correct book\n');
else
fprintf('not correct book\n');
end
fclose(fid);
%Open 'text.txt' file
winopen('text.txt')
end
------------------------------------------------------------------------------------------------
second.m file
function [fl re]=line2(im_texto)
% Divide text in lines
% im_texto->input image; fl->first line; re->remain line
% Example:
% im_texto=imread('TEST_3.jpg');
% [fl re]=lines(im_texto);
% subplot(3,1,1);imshow(im_texto);title('INPUT IMAGE')
% subplot(3,1,2);imshow(fl);title('FIRST LINE')
% subplot(3,1,3);imshow(re);title('REMAIN LINES')
im_texto=clip(im_texto);
num_filas=size(im_texto,1);
for s=1:num_filas
if sum(im_texto(s,:))==0
nm=im_texto(1:s-1, :); % First line matrix
rm=im_texto(s:end, :);% Remain line matrix
fl = clip(nm);
re=clip(rm);
%*-*-*Uncomment lines below to see the result*-*-*-*-
% subplot(2,1,1);imshow(fl);
% subplot(2,1,2);imshow(re);
break
else
fl=im_texto;%Only one line.
re=[ ];
end
end
function img_out=clip(img_in)
[f c]=find(img_in);
img_out=img_in(min(f):max(f),min(c):max(c));%Crops image
-----------------------------------------------------------------------------------------------------
third.m file
%CREATE TEMPLATES
%Letter
A=imread('letters_numbers\A.bmp');B=imread('letters_numbers\B.bmp');
C=imread('letters_numbers\C.bmp');D=imread('letters_numbers\D.bmp');
E=imread('letters_numbers\E.bmp');F=imread('letters_numbers\F.bmp');
G=imread('letters_numbers\G.bmp');H=imread('letters_numbers\H.bmp');
I=imread('letters_numbers\I.bmp');J=imread('letters_numbers\J.bmp');
K=imread('letters_numbers\K.bmp');L=imread('letters_numbers\L.bmp');
M=imread('letters_numbers\M.bmp');N=imread('letters_numbers\N.bmp');
O=imread('letters_numbers\O.bmp');P=imread('letters_numbers\P.bmp');
Q=imread('letters_numbers\Q.bmp');R=imread('letters_numbers\R.bmp');
S=imread('letters_numbers\S.bmp');T=imread('letters_numbers\T.bmp');
U=imread('letters_numbers\U.bmp');V=imread('letters_numbers\V.bmp');
W=imread('letters_numbers\W.bmp');X=imread('letters_numbers\X.bmp');
Y=imread('letters_numbers\Y.bmp');Z=imread('letters_numbers\Z.bmp');
%Number
one=imread('letters_numbers\1.bmp'); two=imread('letters_numbers\2.bmp');
three=imread('letters_numbers\3.bmp');four=imread('letters_numbers\4.bmp');
five=imread('letters_numbers\5.bmp'); six=imread('letters_numbers\6.bmp');
seven=imread('letters_numbers\7.bmp');eight=imread('letters_numbers\8.bmp');
nine=imread('letters_numbers\9.bmp'); zero=imread('letters_numbers\0.bmp');
%*-*-*-*-*-*-*-*-*-*-*-
letter=[A B C D E F G H I J K L M...
N O P Q R S T U V W X Y Z];
number=[one two three four five...
six seven eight nine zero];
character=[letter number];
templates=mat2cell(character,42,[24 24 24 24 24 24 24 ...
24 24 24 24 24 24 24 ...
24 24 24 24 24 24 24 ...
24 24 24 24 24 24 24 ...
24 24 24 24 24 24 24 24]);
save ('templates','templates')
clear all
------------------------------------------------------------------------------------------------------
forth.m file
function letter=read_letter(imagn,num_letras)
% Computes the correlation between template and input image
% and its output is a string containing the letter.
% Size of 'imagn' must be 42 x 24 pixels
% Example:
% imagn=imread('D.bmp');
% letter=read_letter(imagn)
global templates
comp=[ ];
for n=1:num_letras
sem=corr2(templates{1,n},imagn);
comp=[comp sem];
end
vd=find(comp==max(comp));
%*-*-*-*-*-*-*-*-*-*-*-*-*-
if vd==1
letter='A';
elseif vd==2
letter='B';
elseif vd==3
letter='C';
elseif vd==4
letter='D';
elseif vd==5
letter='E';
elseif vd==6
letter='F';
elseif vd==7
letter='G';
elseif vd==8
letter='H';
elseif vd==9
letter='I';
elseif vd==10
letter='J';
elseif vd==11
letter='K';
elseif vd==12
letter='L';
elseif vd==13
letter='M';
elseif vd==14
letter='N';
elseif vd==15
letter='O';
elseif vd==16
letter='P';
elseif vd==17
letter='Q';
elseif vd==18
letter='R';
elseif vd==19
letter='S';
elseif vd==20
letter='T';
elseif vd==21
letter='U';
elseif vd==22
letter='V';
elseif vd==23
letter='W';
elseif vd==24
letter='X';
elseif vd==25
letter='Y';
elseif vd==26
letter='Z';
%*-*-*-*-*
elseif vd==27
letter='1';
elseif vd==28
letter='2';
elseif vd==29
letter='3';
elseif vd==30
letter='4';
elseif vd==31
letter='5';
elseif vd==32
letter='6';
elseif vd==33
letter='7';
elseif vd==34
letter='8';
elseif vd==35
letter='9';
else
letter='0';
end
-------------------------------------------------------------------------------------------
here the input image for first.m file....
http://www.uploadhouse.com/viewfile.php?id=9428651
------------------------------------------------------------------------------------------
here is image template for OCR (character compare)
http://www.uploadhouse.com/viewfile.php?id=9428717
http://www.uploadhouse.com/viewfile.php?id=9428720
http://www.uploadhouse.com/viewfile.php?id=9428723
http://www.uploadhouse.com/viewfile.php?id=9428726
http://www.uploadhouse.com/viewfile.php?id=9428729
http://www.uploadhouse.com/viewfile.php?id=9428732
http://www.uploadhouse.com/viewfile.php?id=9428735
http://www.uploadhouse.com/viewfile.php?id=9428738
http://www.uploadhouse.com/viewfile.php?id=9428741
http://www.uploadhouse.com/viewfile.php?id=9428744
http://www.uploadhouse.com/viewfile.php?id=9428747
http://www.uploadhouse.com/viewfile.php?id=9428750
http://www.uploadhouse.com/viewfile.php?id=9428759
http://www.uploadhouse.com/viewfile.php?id=9428756
http://www.uploadhouse.com/viewfile.php?id=9428753
http://www.uploadhouse.com/viewfile.php?id=9428762
http://www.uploadhouse.com/viewfile.php?id=9428765
http://www.uploadhouse.com/viewfile.php?id=9428768
http://www.uploadhouse.com/viewfile.php?id=9428771
http://www.uploadhouse.com/viewfile.php?id=9428774
http://www.uploadhouse.com/viewfile.php?id=9428867
http://www.uploadhouse.com/viewfile.php?id=9428864
http://www.uploadhouse.com/viewfile.php?id=9428861
http://www.uploadhouse.com/viewfile.php?id=9428870
http://www.uploadhouse.com/viewfile.php?id=9428855
http://www.uploadhouse.com/viewfile.php?id=9428858
http://www.uploadhouse.com/viewfile.php?id=9428846
http://www.uploadhouse.com/viewfile.php?id=9428849
http://www.uploadhouse.com/viewfile.php?id=9428852
http://www.uploadhouse.com/viewfile.php?id=9428843
http://www.uploadhouse.com/viewfile.php?id=9428840
http://www.uploadhouse.com/viewfile.php?id=9428834
http://www.uploadhouse.com/viewfile.php?id=9428837
http://www.uploadhouse.com/viewfile.php?id=9428831
http://www.uploadhouse.com/viewfile.php?id=9428828
http://www.uploadhouse.com/viewfile.php?id=9428825
http://www.uploadhouse.com/viewfile.php?id=9428768
http://www.uploadhouse.com/viewfile.php?id=9428771
http://www.uploadhouse.com/viewfile.php?id=9428774
http://www.uploadhouse.com/viewfile.php?id=9428765
---------------------------------------------------------------------------------------------
tq sir....