while implementing kernel based grapg cut method, I am getting error at line: area(1+1)=999999999;
suggestion for correcting the error is line 147: the variable 'area' appears to change size on every loop iteration(within a script).
Considering preallocation for speed, please suggest me how to preallocate the variable area,in the matlab code?
Code:
clear all; close all;
%%%%%%%%%%%%%%%%%%%%%%%Main inputs and parameters%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%Note: The RBF-kernel parameters are given in function kernel RBF.m%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%Example with a color image%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%path = 'Images\Color_image.jpg';
%im = im2double(imread(path));
%alpha=1; %The weight of the smoothness constraint
%k =8; %The number of regions
%%%%%%%Example with a SAR image corrupted with a multiplicative noise%%%%%%
%%%%%%%%%%%%%%%%Uncomment the following to run the example)%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
path = 'Images\Sar_image.tif';
im = im2double(imread(path));
alpha=0.6;
k =4;
%%%%%%%%%%%%%%%%%%%%%%%%%%Example with a brain image%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%Uncomment the following to run the example)%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%path = 'Images\Brain_image.tif';
%im = im2double(imread(path));
%alpha=0.1;
%k =4;
sz = size(im);
Hc=ones(sz(1:2));
Vc=Hc;
i_ground = 0; % rank of the bakground for plotting, 0: the darkest;
%k-1 the brightest; 99: nowhere
diff=10000;
an_energy=999999999;
iter=0;
iter_v=0;
energy_global_min=99999999;
distance = 'sqEuclidean'; % Feature space distance
% Initialization: cluster the data into k regions
tic,
disp('Start kmeans');
data = ToVector(im);
[idx c] = kmeans(data, k, 'distance', distance,'EmptyAction','drop','maxiter',100);
c=c(isfinite(c(:,1)),:);
k=size(c,1);
k_max=k;
kmean_time=toc;
Dc = zeros([sz(1:2) k],'single');
c;
tic
while iter < 5
iter=iter+1;
clear Dc
clear K
c;
for ci=1:k
K=kernel_RBF(im,c(ci,:));
Dc(:,:,ci)=1-K;
end
clear Sc
clear K
%% The smoothness term
Sc = alpha*(ones(k) - eye(k));
gch = GraphCut('open', Dc, Sc, Vc, Hc);
[gch L] = GraphCut('swap',gch);
[gch se de] = GraphCut('energy', gch);
nv_energy=se+de;
gch = GraphCut('close', gch);
if (nv_energy<=energy_global_min)
diff=abs(energy_global_min-nv_energy)/energy_global_min;
energy_global_min=nv_energy;
L_global_min=L;
k_max=k;
nv_energy;
iter_v=0;
% Calculate region Pl of label l
if size(im, 3)==3 % Color image
for l=0:k-1
Pl=find(L==l);
card=length(Pl);
K1=kernel_RBF(im(Pl),c(l+1,1));K2=kernel_RBF(im(Pl),c(l+1,2));K3=kernel_RBF(im(Pl),c(l+1,3));
smKI(1)=sum(im(Pl).*K1); smKI(2)=sum(im(Pl+prod(sz(1:2))).*K2); smKI(3)=sum(im(Pl+2*prod(sz(1:2))).*K3);
smK1=sum(K1);smK2=sum(K2);smK3=sum(K3);
if (card~=0)
c(l+1,1)=smKI(1)/smK1;c(l+1,2)=smKI(2)/smK2;c(l+1,3)=smKI(3)/smK3;
else
c(l+1,1)=999999999;c(l+1,2)=999999999;c(l+1,3)=999999999;
end
end
end
if size(im, 1)==1 % Gray-level image
for l=0:k-1
Pl=find(L==l);
card=length(Pl);
K=kernel_RBF(im(Pl),c(l+1,1));
smKI=sum(im(Pl).*K);
smK=sum(K);
if (card~=0)
c(l+1,1)=smKI/smK;
else
c(l+1,1)=999999999;
end
end
end
c=c(c(:,1)~=999999999,:);
c_global_min=c;
k_global=length(c(:,1));
k=k_global;
else
iter_v=iter_v+1;
%---------------------------------
% Begin updating labels
%---------------------------------
% Calculate region Pl of label l
if size(im, 3)==3 % Color image
for l=0:k-1
Pl=find(L==l);
card=length(Pl);
K1=kernel_RBF(im(Pl),c(l+1,1));K2=kernel_RBF(im(Pl),c(l+1,2));K3=kernel_RBF(im(Pl),c(l+1,3));
smKI(1)=sum(im(Pl).*K1); smKI(2)=sum(im(Pl+prod(sz(1:2))).*K2); smKI(3)=sum(im(Pl+2*prod(sz(1:2))).*K3);
smK1=sum(K1);smK2=sum(K2);smK3=sum(K3);
% Calculate contour Cl of region Pl
if (card~=0)
c(l+1,1)=smKI(1)/smK1;c(l+1,2)=smKI(2)/smK2;c(l+1,3)=smKI(3)/smK3;
else
c(l+1,1)=999999999;c(l+1,2)=999999999;c(l+1,3)=999999999;
area(l+1)=999999999;
end
end
end
if size(im, 3)== 1 % Gray-level image
for l=0:k-1
Pl=find(L==l);
card=length(Pl);
K=kernel_RBF(im(Pl),c(l+1,1));
smKI=sum(im(Pl).*K);
smK=sum(K);
% Calculate contour Cl of region Pl
if (card~=0)
c(l+1,1)=smKI/smK;
else
c(l+1,1)=999999999;
area(l+1)=999999999;
end
end
end
c=c(c(:,1)~=999999999,:);
k=length(c(:,1));
end
end
L=L_global_min;
energy_global_min;
c=c_global_min;
size(c,1)
iter;
%Show the results
if size(im, 3)==3 % Color image
img=zeros(sz(1),sz(2),3);
j=1;
imagesc(im); axis off; hold on;
for i=0:k_max-1
LL=(L_global_min==i);
is_zero=sum(sum(LL));
if is_zero
img(:,:,1)=img(:,:,1)+LL*c(j,1);
img(:,:,2)=img(:,:,2)+LL*c(j,2);
img(:,:,3)=img(:,:,3)+LL*c(j,3);
j=j+1;
end
if i~=i_ground
color=[rand rand rand];
contour(LL,[1 1],'LineWidth',2.5,'Color',color); hold on;
end
end
figure(2);
imagesc(img); axis off;
end
if size(im, 3)==1 % Gray-level image
img=zeros(sz(1),sz(2));
j=1;
imagesc(im); axis off; hold on; colormap gray;
for i=0:k_max-1
LL=(L_global_min==i);
is_zero=sum(sum(LL));
if is_zero
img(:,:,1)=img(:,:,1)+LL*c(j,1);
j=j+1;
end
if i~=i_ground
color=[rand rand rand];
contour(LL,[1 1],'LineWidth',2.5,'Color',color); hold on;
end
end
figure(2);
imagesc(img); axis off;
end
A possible solution is to set the variable initially to zeros(k_max, 1 ).
Related
I am extracting and segmenting the characters from number plate.I am facing problems with character recognition. I wish to extract the number and symbol and do not consider about alphabets.I am getting one character as output out of eight. After running this code, i got answer is'1'. I am appending matlab code for location and segmentation of the plate. I have given the input image for the reference.`]5 []6[]7[]8Can anyone help me to rectify this error?
function [ImgPlate] = LocationPlate(I)
%% Cutting and resizing the original image %%
% I=imread('2222.jpg');
% figure,imshow(I);
[rows columns]=size(I);
columns=columns/3;
xmin=round(0.20*rows);
ymin=round(0.20*columns);
width=round((0.85*columns)-(0.10*columns));
height=round((0.85*rows)-(0.15*rows));
Io=imcrop(I,[xmin ymin width height]);
Io=imresize(Io,[480 640]);
Io=rgb2gray(Io);
Io=imadjust(Io);
%% Image processing to focus the area of number plate %%
%% Smooth edges and contours to delete characters.
se=strel('rectangle',[6 30]);
Ic=imclose(Io,se);
Ic=imadjust(Ic);
tophat=Ic-Io;
Ibw=(tophat>85);
%% Remove the related elements with fewer than 70 pixels %%
%% Remove objects that are not plate %%
plate= bwlabel(Ibw,4);
obj= max(max(plate));
dim1 = regionprops(plate, 'area')';
dim=[dim1.Area];
dim(find(dim<70))=0;
for i=1:obj
index=find(plate==i);
if dim(i)==0
plate(index)=0;
else
plate(index)=1;
end
end
CC=bwconncomp(plate);
P=regionprops(CC,'all');
[rp cp]=size(plate);
for i=1:CC.NumObjects
if P(i).MajorAxisLength>(2*cp/3)
plate(P(i).PixelIdxList(:,1))=0;
end
end
%% Remove objects that are not candidates for plate %%
se3=strel('rectangle',[30 70]);
r2=imclose(plate,se3);
se2=strel('rectangle',[5 30]);
r=imdilate(r2,se2);
CC=bwconncomp(r);
P=regionprops(CC,'all');
for i=1:CC.NumObjects
if P(i).MajorAxisLength>(2*cp/3)
r(P(i).PixelIdxList(:,1))=0;
end
end
%% select the largest connected component after preprocessing, the
%%plate
plate1= bwlabel(r,4);
dim2= regionprops(plate1, 'area')';
dim1=[dim2.Area];
f=max(dim1);
indMax=find(dim1==f);
plate1(find(plate1~=indMax))=0;
%% cutting of original image %%
[cuty, cutx] = find( plate1 > 0);
up = min(cuty);
down = max(cuty);
left = min(cutx);
right = max(cutx);
img_cut_v = Io(up:down,:,:);
img_cut_h = img_cut_v(:,left:right,:);
ImgPlate = img_cut_h;
%% different mask for location plate %%
[r c]=size(ImgPlate);
if r<25 || r>65
[rows columns]=size(I);
columns=columns/3;
xmin=round(0.20*rows);
ymin=round(0.20*columns);
width=round((0.85*columns)-(0.10*columns));
height=round((0.85*rows)-(0.15*rows));
Io=imcrop(I,[xmin ymin width height]);
Io=imresize(Io,[480 640]);
Io=rgb2gray(Io);
Io=imadjust(Io);
se=strel('rectangle',[6 30]);
Ic=imclose(Io,se);
Ic=imadjust(Ic);
tophat=Ic-Io;
Ibw1=(tophat>85);
mask=zeros(480,640);
for i=40:370
for j=40:575
mask(i,j)=1;
end
end
Ibw=Ibw1 & im2bw(mask);
plate= bwlabel(Ibw,4);
obj= max(max(plate));
dim1 = regionprops(plate, 'area')';
dim=[dim1.Area];
dim(find(dim<70))=0;
for i=1:obj
index=find(plate==i);
if dim(i)==0
plate(index)=0;
else
plate(index)=1;
end
end
CC=bwconncomp(plate);
P=regionprops(CC,'all');
[rp cp]=size(plate);
for i=1:CC.NumObjects
if P(i).MajorAxisLength>(cp/3)
plate(P(i).PixelIdxList(:,1))=0;
end
end
se3=strel('rectangle',[30 70])
r2=imclose(plate,se3);
se2=strel('rectangle',[5 30]);
r=imdilate(r2,se2);
plate1= bwlabel(r,4);
dim2= regionprops(plate1, 'area')';
dim1=[dim2.Area];
f=max(dim1);
indMax=find(dim1==f);
plate1(find(plate1~=indMax))=0;
[cuty, cutx] = find( plate1 > 0);
up = min(cuty);
down = max(cuty);
left = min(cutx);
right = max(cutx);
img_cut_v = Io(up:down,:,:);
img_cut_h = img_cut_v(:,left:right,:);
ImgPlate = img_cut_h;
end
%% Representation %%
% figure(1);
% imshow(I);
% subplot(2,2,1);imshow(I);
% subplot(2,2,2);imshow(Ic);% subplot(2,2,3);imshow(plate);
% subplot(2,2,4);imshow(plate1);
figure(2); imshow(img_cut_h);title('output location plate');
end
function [Objects,ImgChar]=Segmentation(ImgPlate)
%% Binarize the image %%
level = graythresh(ImgPlate);
Ibw =(im2bw(ImgPlate,level));
%% Select the orientation of the largest object in the image.
%% Turn this angle at the picture.
%% Plate cutting to segment the characters that compose %%
Fl=bwlabel(Ibw);
Statsbf=regionprops(Fl,'all');
Flmax=find([Statsbf.Area]==max([Statsbf.Area]));
angle=Statsbf(Flmax).Orientation;
F2=imrotate(Fl,-angle);
L=bwlabel(F2);
Statsbf=regionprops(L,'all');
maxi=find([Statsbf.Area]==max([Statsbf.Area]));
BB=Statsbf(maxi).BoundingBox;
F2=imcrop(F2,[BB(1,1) BB(1,2) BB(1,3) BB(1,4)]);
% figure,imshow(F2);
%% First three and last three rows to zero.
%% First two and last two columns to zero.
%% So remove connectivity between characters and background %%
%% Remove small impurities %%
L4=not(F2);
[r c]=size(L4);
L4(1,:)=0;
L4(2,:)=0;
L4(3,:)=0;
L4(r,:)=0;
L4(r-1,:)=0;
L4(r-2,:)=0;
L4(:,1)=0;
L4(:,2)=0;
L4(:,c)=0;
L4(:,c-1)=0;
L4b=bwlabel(L4);
Stats3=regionprops(L4b,'all');
sarea3=[Stats3.Area];
G=find(sarea3<70);
for cv=1:length(G)
G1=find(L4b==G(cv));
L4(G1)=0;
end
[r c]=size(L4);
CC=bwconncomp(L4);
L=bwlabel(L4);
ind2=max(L(:,c-2));
P=regionprops(CC,'all');
%% Remove objects smaller and larger than a character %%
i=1;
if (max(P(i,1).PixelList(:,1))-min(P(i,1).PixelList(:,1)))<(c/13)
L4(CC.PixelIdxList{1,i})=0;
end
for i=1:CC.NumObjects
if (max(P(i,1).PixelList(:,1))-min(P(i,1).PixelList(:,1)))>(2*c/3)
L4(CC.PixelIdxList{1,i})=0;
end
if (max(P(i,1).PixelList(:,2))-min(P(i,1).PixelList(:,2)))<(r/3)
L4(CC.PixelIdxList{1,i})=0;
end
if (max(P(i,1).PixelList(:,1))-min(P(i,1).PixelList(:,1)))<(c/8)
L4(find(L==ind2))=0;
end
end
L4=imclose(L4,strel('disk',1));
L4=imopen(L4,strel('disk',1));
% figure(4);
imshow(L4);
L4b=bwlabel(L4);
Stats3b=regionprops(L4b,'all');
N=length(Stats3b);
while N>8
L4=imdilate(L4,strel('disk',1));
L4b=bwlabel(L4);
Stats3b=regionprops(L4b,'all');
N=length(Stats3b);
end
L4b=bwlabel(L4);
Stats3b=regionprops(L4b,'all');
ImgChar=zeros(100,100,N);
%% Dividing characters which are connected %%
%% Remove objects that have been listed as characters but are not%
%% Show every character in the correct position %%
cont=0;
cont1=0;
for i=1:N
[r1 c1]=size(Stats3b(i,1).Image);
if c1>round(c/6)
cont1=cont;
Stats3b(i,1).Image(:,round(c1/2))=0;
L5=Stats3b(i,1).Image;
CC=bwconncomp(L5);
CC1=regionprops(CC,'all');
for j=1:CC.NumObjects
[r2 c2]=size(CC1(j,1).Image);
if c2>round(c/7)
CC1(j,1).Image(:,round(c2/2))=0;
L6=CC1(j,1).Image;
LL=bwconncomp(L6);
CC2=regionprops(LL,'all');
for k=1:LL.NumObjects
CC2(k).Image=imresize(CC2(k).Image, [100 100]);
figure;imshow((CC2(k).Image))
ImgChar(:,:,i+cont1)=not(CC2(k).Image);
cont1=cont1+1;
end
cont=cont+1;
else
CC1(j).Image=imresize(CC1(j).Image, [100 100]);
figure;imshow((CC1(j).Image))
ImgChar(:,:,i+cont1)=not(CC1(j).Image);
cont1=cont1+1;
end
end
cont=cont+1;
else
Stats3b(i).Image=imresize(Stats3b(i).Image, [100 100]);
figure;imshow((Stats3b(i).Image));
if cont~=0
ImgChar(:,:,i+cont)=not(Stats3b(i).Image);
else
ImgChar(:,:,i)=not(Stats3b(i).Image);
end
end
end
%% Remove spurious %%
[x, y, Objects]=size(ImgChar);
end
function [strPlate] = Recognition(I)
I=imread('2222.jpg');
[ImgPlate] = LocationPlate(I);
[Objects,ImgChar]=Segmentation(ImgPlate);
N=struct('Image',{});
numbers={'0','1','2','3','4','5','sign'};
N(1).Image=imresize(im2bw(uint8(imread('untitled0.bmp'))),[100 100]);
N(2).Image=imresize(im2bw(uint8(imread('untitled1.bmp'))),[100 100]);
N(3).Image=imresize(im2bw(uint8(imread('untitled2.bmp'))),[100 100]);
N(4).Image=imresize(im2bw(uint8(imread('untitled3.bmp'))),[100 100]);
N(5).Image=imresize(im2bw(uint8(imread('untitled4.bmp'))),[100 100]);
N(6).Image=imresize(im2bw(uint8(imread('untitled5.bmp'))),[100 100]);
N(7).Image=imresize(im2bw(uint8(imread('sign.jpg'))),[100 100]);
if Objects==8
strPlate=[];
for i=1:Objects
char=ImgChar(:,:,i);
if (i==2)||(i==3)||(i==4) || (i==5) || (i==6)||(i==7) ||(i==8)
list_corr=[];
for j=1:7
corr=corr2(N(j).Image,char);
list_corr=[list_corr corr];
end
f=max(list_corr);
maxcorr=find(list_corr==f);
strPlate=[strPlate numbers(maxcorr)];
end
end
end
end
I find you compare char image with the default value of ImgChar (zeros). I discovered this by add imshow(char) as you can see in the below code.
for i=1:Objects
char=ImgChar(:,:,i);
if (i==2)||(i==3)||(i==4) || (i==5) || (i==6)||(i==7) ||(i==8)
list_corr=[];
for j=1:6
corr=corr2(N(j).Image,char);
list_corr=[list_corr corr];
end
close all
figure;imshow(char)
f=max(list_corr);
maxcorr=find(list_corr==f);
strPlate=[strPlate numbers(maxcorr)];
end
end
To correct the above error, I modified the below code to store images in ImgChar. this line maybe not correct but I believe my answer may help you.
Stats3b(i).Image=imresize(Stats3b(i).Image, [100 100]);
figure;
imshow((Stats3b(i).Image));
ImgChar(:,:,i) = Stats3b(i).Image;% added
if cont~=0
else
ImgChar(:,:,i)=not(Stats3b(i).Image);
end
i am doing my project on graph extraction in hand written word in this i divided a binary image into equal segments using
C = (width of image image / 10 ) and R = (height of image image / 10 ) word image divided into 'C x R' segments of equal size, For each
segment sij (i = 1, . . . , C; j = 1, . . . , R) a node is inserted into the resulting graph and labelled by the (x, y)-coordinates of the center of mass (xm, ym) Formally, we compute the number of foreground pixel in segment sij, while xw and yw denote the x- and y-coordinates of the foreground pixels in sij. If a segment does not contain any foreground pixel, no centre of mass can be determined and thus no node is created for this segment.
in my code i compute the center of mass in each segments but i want to connect the each node on the image please help to solve this problem
clc;
clear all;
close all;
X=imread('math.jpg');
imfinfo('math.jpg')
figure,imshow(X)
b = imresize(X,[100,100]);
si = size(b,1);
sj = size(b,2);
figure;imshow(b);
% Binarization
th = graythresh(b);
I = im2bw(b,th);
w = 5;
h = 5;
c=si/w;
r=sj/h;
% Skeletonised
kl=bwmorph(~I,'thin',inf);
figure,imshow(kl)
R(:,:)=kl(:,:);
I=1;
U1=w;
J=1;
U2=h;
E=1;
for i=1:r
for j=1:c
B(I:U1,J:U2)=R(I:U1,J:U2);
[x,y]=find(B==1);
XX=mean(x);
YY=mean(y);
XXX(E)=CX;
YYY(E)=CY;
T(I:U1,J:U2)=B(I:U1,J:U2);
J=J+w;
U2=U2+h;
E=E+1;
clear B x y
end
I=I+w;
U1=U1+h;
J=1;
U2=h;
end
imshow(R)
hold on
plot(XX,YY, 'g*');
hold off
in my code i am getting center of mass of foreground pixels in each segment
this is my output
output of my code
help me add the plotted point on image as a node to resulting graph
Edited,
input image
clc;
clear all;
close all;
X=imread('math.jpg');
imfinfo('math.jpg')
figure,imshow(X)
b = imresize(X,[100,100]);
si = size(b,1);
sj = size(b,2);
%figure;imshow(b);
% Binarization
th = graythresh(b);
I = im2bw(b,th);
%Skeletonised
kl=bwmorph(~I,'thin',inf);
figure,imshow(kl)
R(:,:)=kl(:,:);
%grid size
t1=10;
D=100;
I=1;
U1=t1;
J=1;
U2=t1;
E=1;
t2=D/t1;
%Z=1;
for i=1:t2
for j=1:t2
B(I:U1,J:U2)=R(I:U1,J:U2);
[x,y]=find(B==1);
CX=mean(x);
CY=mean(y);
CXXX(E)=CX;
CYYY(E)=CY;
CXX(i,j)=CX;
CYY(i,j)=CY;
T(I:U1,J:U2)=B(I:U1,J:U2);
J=J+t1;
U2=U2+t1;
E=E+1;
clear B x y
end
I=I+t1;
U1=U1+t1;
J=1;
U2=t1;
end
%plot and grid
figure,imshow(R)
hold on
M = size(R,1);
N = size(R,2);
a=t1;
b=t1;
for k = 1:a:M
x = [1 N];
y = [k k];
plot(x,y,'Color','white');
set(findobj('Tag','MyGrid'),'Visible','on')
end
for k = 1:b:N
x = [k k];
y = [1 M];
plot(x,y,'Color','white');
set(findobj('Tag','MyGrid'),'Visible','on')
end
plot(CXX,CYY, 'g*');
hold off
Sir please try this edited code.
If I understand your question correctly, you want to draw lines between the green stars. This can been done as follows:
plot(CYY(isfinite(CYY)),CXX(isfinite(CYY)), 'g*-');
I removed the invalid values using logical indexing, otherwise not all the connections are drawn.
I want to segment an Arabic word into single characters. Based on the histogram/profile, I assume that I can do the segmentation process by cut/segment the characters based on it's baseline (it have similar pixel values).
But, unfortunately, I still stuck to build the appropriate code, to make it works.
% Original Code by Soumyadeep Sinha
% Saving each single segmented character as one file
function [segm] = trysegment (a)
myFolder = 'D:\1. Thesis FINISH!!!\Data set\trial';
level = graythresh (a);
bw = im2bw (a, level);
b = imcomplement (bw);
i= padarray(b,[0 10]);
verticalProjection = sum(i, 1);
set(gcf, 'Name', 'Trying Segmentation for Cursive', 'NumberTitle', 'Off')
subplot(2, 2, 1);imshow(i);
subplot(2,2,3);
plot(verticalProjection, 'b-'); %histogram show by this code
% hist(reshape(input,[],3),1:max(input(:)));
grid on;
% % t = verticalProjection;
% % t(t==0) = inf;
% % mayukh = min(t)
% 0 where there is background, 1 where there are letters
letterLocations = verticalProjection > 0;
% Find Rising and falling edges
d = diff(letterLocations);
startingColumns = find(d>0);
endingColumns = find(d<0);
% Extract each region
y=1;
for k = 1 : length(startingColumns)
% Get sub image of just one character...
subImage = i(:, startingColumns(k):endingColumns(k));
% se = strel('rectangle',[2 4]);
% dil = imdilate(subImage, se);
th = bwmorph(subImage,'thin',Inf);
n = imresize (th, [64 NaN], 'bilinear');
figure, imshow (n);
[L,num] = bwlabeln(n);
for z= 1 : num
bw= ismember(L, z);
% Construct filename for this particular image.
baseFileName = sprintf('char %d.png', y);
y=y+1;
% Prepend the folder to make the full file name.
fullFileName = fullfile(myFolder, baseFileName);
% Do the write to disk.
imwrite(bw, fullFileName);
% subplot(2,2,4);
% pause(2);
% imshow(bw);
end
% y=y+1;
end;
segm = (n);
Word image is as follow:
Why the code isn't work?
do you have any recommendation of another codes?
or suggested algorithm to make it works, to do a good segmentation on cursive character?
Thanks before.
Replace this code part from the posted code
% 0 where there is background, 1 where there are letters
letterLocations = verticalProjection > 0;
% Find Rising and falling edges
d = diff(letterLocations);
startingColumns = find(d>0);
endingColumns = find(d<0);
with the new code part
threshold=max(verticalProjection)/3;
thresholdedProjection=verticalProjection > threshold;
count=0;
startingColumnsIndex=0;
for i=1:length(thresholdedProjection)
if thresholdedProjection(i)
if(count>0)
startingColumnsIndex=startingColumnsIndex+1;
startingColumns(startingColumnsIndex)= i-floor(count/2);
count=0;
end
else
count=count+1;
end
end
endingColumns=[startingColumns(2:end)-1 i-floor(count/2)];
No changes needed for the rest of the code.
I want to plot the graph of the numerical solution of bisection method and show how it get close to the real solution .
my code:
% f=input('please enter the function:');
% xL=input('please enter the limits .from:');
% XR=input('to:');
% eps=input('please enter epsilon:');
f=#(x)x.^2-1;
XR=2;
xL=-2;
XL=xL ;
eps=0.001;
subplot(2,1,1);
title('graph 1');
ezplot(f);
hold on ;
% line([0 0],40);
% line(-40,[0 0]);
plot(XR,f(XR),'r*');
plot(xL,f(xL),'r*');
disp( 'the answers is : ');
for df=xL:0.15:XR
if f(xL)*f(df)<= 0
xR=df;
Roots = BisectionM(f,xR,xL,eps);
plot(Roots,f(Roots),'gs');
disp(Roots);
xL=df;
xR=XR;
end
end
subplot(2,1,2);
title('graph 2');
x0=fzero(f,xL);
sol = BisectionM(f,xR,xL,eps);
plot(1:1:100,ones(1,100)*x0);
hold on ;
plot(1:1:100,sol);
the function :
function[sol,Roots] = BisectionM(f,xR,xL,eps)
while abs(xR - xL) > eps
xM = (xR + xL) / 2;
if (f(xL))*(f(xM)) > 0
xL = xM;
sol=xM;
plot(xL,f(xL),'.');
else
xR = xM;
plot(xR,f(xR),'.');
sol=xM;
end
Roots = xM;
end
end
I don't know how to plot this so it will get closer to the solution (the blue line at the end). anyone?
I do not understand many things in your code, e.g. why BisectionM has two identical outputs under different variable names (sol, Roots), moreover only one output is used throughout the main function. Besides here is my guess what you might want:
The figure shows how the numerical solution converges with respect to iteration number. For this you have to store the iteration results in a vector (sol), please see below the modified code:
main.m
f=#(x)x.^2-1;
XR=2;
xL=-2;
XL=xL ;
eps=0.001;
subplot(2,1,1);
title('graph 1');
ezplot(f);
hold on ;
% line([0 0],40);
% line(-40,[0 0]);
plot(XR,f(XR),'r*');
plot(xL,f(xL),'r*');
disp( 'the answers is : ');
for df=xL:0.15:XR
if f(xL)*f(df)<= 0
xR=df;
Roots = BisectionM(f,xR,xL,eps);
plot(Roots(end),f(Roots(end)),'gs');
disp(Roots(end));
xL=df;
xR=XR;
end
end
subplot(2,1,2);
title('graph 2');
% give the wide interval again because it was changed previously
XR=2;
xL=-2;
x0=fzero(f,xL);
Roots = BisectionM(f,xR,xL,eps);
plot(1:length(Roots),ones(length(Roots),1)*x0, 'g');
hold on ;
plot(1:length(Roots),Roots,'Marker', '.');
xlabel('iteration number')
BisectionM.m
function Roots = BisectionM(f,xR,xL,eps)
% no preallocation because of while
ii = 1;
while abs(xR - xL) > eps
xM = (xR + xL) / 2;
if (f(xL))*(f(xM)) > 0
xL = xM;
sol=xM;
plot(xL,f(xL),'.');
else
xR = xM;
plot(xR,f(xR),'.');
sol=xM;
end
Roots(ii) = xM;
ii = ii + 1;
end
end
I would like to plot an expotential curve between an interval based on a different multipler for each interval.
I have tried this:
%Plotting of h curve
function PlotQ(Time,h)
for i=2:size(Time,1)
for t=Time(i-1):0.1:Time(i)
plot([Time(i-1), Time(i)],exp(-h(i)*t))
hold on;
end
end
ymax = max();
xlim([1 max(Time)]);
ylim([-0.5 ymax+0.5]);
xlabel('Time')
ylabel('Rate')
end
The curve comes out like this:
Not sure what I am doing wrong.. Need some guidance..
New Edit:
T =[0;0.569444444444444;1.06666666666667;2.08611111111111;3.09722222222222;4.11111111111111;5.12500000000000;7.16111111111111;10.2000000000000;20.3444444444444;30.4944444444444];
%Plotting of h and Q
h = [0;0.0187;0.0194;0.0198;0.0215;0.0225;0.0241;0.0316;0.0379;0.0437;0.0452];
PlotQ(Time,h)
If I understood correctly, You are looking for something like this.
%Plotting of h curve
function PlotQ(Time,h)
for i=2:size(Time,1)
tVector=Time(i-1):0.1:Time(i);
sizetVector=length(tVector);
for t=2:sizetVector
plot([tVector(t-1), tVector(t)],[exp(-h(i)*tVector(t-1)),exp(-h(i)*tVector(t))]);
hold on
end
end
ymax = max();
xlim([1 max(Time)]);
ylim([-0.5 ymax+0.5]);
xlabel('Time')
ylabel('Rate')
end
I think, this is what you want:
function PlotQ(Time,h)
% Parameters
res = 0.1;
n = numel(h);
% Pre-allocate
y = cell(1,n);
t = cell(1,n);
% Calculate
for ii=2:n
t{ii} = Time(ii-1):res:Time(ii);
y{ii} = exp(-h(ii)*t{ii});
end
% Plot
t_ = [t{:}];
y_ = [y{:}];
figure;
plot(t_,y_);
axis([1 max(t_) -0.5 max(y_)+0.5]);
xlabel('Time');
ylabel('Rate');
end
It gives the following plot: