Code covered by the BSD License

### Highlights from Freeman Chain Code

4.33333
4.3 | 9 ratings Rate this file 85 Downloads (last 30 days) File Size: 2.35 KB File ID: #29518 Version: 1.0

# Freeman Chain Code

### Alessandro Mannini (view profile)

Gives Freeman chain code 8-connected representation of a boundary

File Information
Description

usage:
--------------------------------------------------------
[cc] = chaincode(b,u)

INPUT:
--------------------------------------------------------
b - boundary as np-by-2 array
unwrap - (optional, default=false) unwrap code

OUTPUT:
--------------------------------------------------------
cc is a structure with the following fields:
cc.code - 8-connected Freeman chain code 1-by-np
cc.x0,cc.y0 - coordinates of start point
cc.ucode - unwrapped 8-connected Freeman chain code

MATLAB release MATLAB 7.8 (R2009a)

Respected sir,
when i run this code:
C(C<225)=0;
s=strel('disk',4,0);%Structuring element
D=~im2bw(C);%binary Image
F=imerode(D,s);%Erode the image by structuring element
figure,imshow(C);title('Original Image');
figure,imshow(D);title('Binary Image');
AH=D-F;

figure,imshow(AH);
title('Boundary extracted Image');
h=fspecial('average',9);
g=imfilter(D,h,'replicate');
gb=im2bw(g,0.5);
B=bwboundaries(gb,'noholes');
d=cellfun('length',B);
[maxd,k]=max(d);
b=B{1};
[M,N]=size(g);
g=bound2im(b,M,N,min(b(:,1)),min(b(:,2)));
[s,su]=bsubsamp(b,50);
cc=chaincode(su);

Respected sir,
this chain code i run and getting same code for different characters also...please suggest me...

Comment only

Dear sir
i am using chain code for different characters A-Z but i want every characters chain code would be same size.
i am not getting the chain code of same size please suggest me.

Comment only
15 Apr 2015 Christin Panjaitan

### Christin Panjaitan (view profile)

Dear Alessandro,

I have question about the equation below :
idx=3*(dy+1)+(dx+1)=3dy+dx+4 (adding 1 to have idx starting % from 1)

You made the equation for 8-connected.
Why did you multiply dy with 3 ?
How do I form the equation ?

Comment only

Respected sir,
i want to apply chain code on Character image R but using this function how to apply on image.....suggest me...

Comment only
11 Mar 2015 Munika Patel

### Munika Patel (view profile)

work awsomee...

how to get value of b passed as parameter? Thanking in advanced

Comment only
09 Feb 2015 Xia Lin

### Xia Lin (view profile)

11 Dec 2014 farinaz

### farinaz (view profile)

25 May 2014 cspit

### cspit (view profile)

Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your available stack space can

Error in ==> imformats>find_in_registry

what should i do now??

Comment only
28 Apr 2014 Monalisa Das

### Monalisa Das (view profile)

Hi, I keep getting the error "the xpression to the left of the equals sign is not a valid target" when I try to use the [cc]= chaincode(b,u)...what am I doing wrong?

Comment only
23 Apr 2014 Alessandro Mannini

### Alessandro Mannini (view profile)

Hi Monalisa, see the description and previous comments. Main steps are:
1) get the contour of the image
2) put coords of contour points in b
3) call chaincode(b,true/false)

Note: the contour must be 8-connected tha is, given a point on the boundary it must have only two neighbors, the previous and the next. There can be NO forks.

Comment only
23 Apr 2014 sujith

### sujith (view profile)

if u don't want to unwrap,just change function name to [cc] = chaincode(b)
and remove the unwrapping section from program.trace the boundary of your image using 'bwtraceboundary' function and input it to chain code function.

Comment only
21 Apr 2014 Monalisa Das

### Monalisa Das (view profile)

Hi, could you tell me how to use this function to get chaincode for an image? Thanks!

Comment only
21 Feb 2014 Max

### Max (view profile)

Since a few people have asked how to get a chain code from an image I have written a simple script to do this.

The script is really simple and makes some assumptions about a single contour in the image but should be easy to modify if needed.

Usage:

>> chain = mk_chain('path/to/image.png');
>> [cc] = chaincode(chain);

07 Oct 2013 missC

### missC (view profile)

can anyone give me an example on how to call or use this function. I am totally new on matlab and need to see how this chaincode works? many thanks and really appreciate it

Comment only
12 Jun 2013 Anton

### Anton (view profile)

Worked fine for me, thank you very much for the comments, they were VERY helpful!

30 Aug 2012 Daurat Sinaga

### Daurat Sinaga (view profile)

i was running this function and i get output like this :
CC =
x0:0
y0:0
code:[209x1 double], would you explain it for me ?

Comment only
18 May 2012 rana

### rana (view profile)

would you tell me please the exact steps to run this code ??
than you :)

Comment only
09 May 2012 Alessandro Mannini

### Alessandro Mannini (view profile)

Hi Ann, look to my previous comments (ex. 19/3/2011) the b parameter is an array containing coords of boundary points that should be 8-connected. 8-connected means that if Bn is the n-th element of b and Bn+1 the next their x y coords should satisfy this relations: | Xn - Xn+1 |<=1 AND | Yn - Yn+1 |<=1. Start checking that with a simple example. Regards Alessandro

Comment only
08 May 2012 Ann

### Ann (view profile)

i am trying to use this code on the image but i am getting the error "Curve isn't 8-connected in elements: "

what to do?

Comment only
17 Apr 2012 Alessandro Mannini

### Alessandro Mannini (view profile)

Hi Frb, please note than b contains coords of boundary points order so that they are 8-connected. The find function you use return coords of boundary points but in a different order. From Matlab Help seems it scans matrix column by column. Best regards Alessandro

Comment only
08 Apr 2012 Frb

### Frb (view profile)

hello,
I checked this code, seems very useful but I have problem, I give this piece of code to make it 8-connected:
BW1=im2bw(I);
BW1=~BW1;
BW2 = bwperim(BW1,8);
image=BW2;
[x y]=find(image==1);
b=[x y];
and then I put the result in b,
when I run the code error applies and says Curve isn''t 8-connected in elements. I appreciate any help.

Kind Regards,
Fariba

Comment only
23 Mar 2012 Anush

### Anush (view profile)

if i have an image what's the procedure to find the chain code for that particular image !

Comment only
14 Mar 2012 hamza

### hamza (view profile)

Thanks for your great work, and I have a question :-
I have image called XYZ.png and I passed that images to this function but I found an error.

i had a skeleton with me which i want to give as a input to this chaincode..and when i tried to do that...i am getting that subscript indices are must be either positive or real...please somebody help me in resolving this...

30 May 2011 Amr

### Amr (view profile)

i have a silhouette for human image(http://i.imgur.com/fTwkZ.png) and I put the image boundary in an array and then i passed it to the Freeman Chain Code function. this image shows the body boundary is connected to each other but in real it gives me this error
"Curve isn't 8-connected in elements:
idx -> 350
idx -> 700
idx -> 1050
idx -> 1400
....
"

so can any one help me please? :)

Comment only
19 Mar 2011 Alessandro Mannini

### Alessandro Mannini (view profile)

Look at description inside source code:

% b - boundary as np-by-2 array;
% np is the number of pixels and each element is a pair (y,x) of
% pixel coordinates

For example, if you have 4 points p=(y,x) like this p1=(5,1);p2=(5,2);p3=(4,2);p4=(3,1) b is:

b = [ 5 1;5 2;4 2;3 1]

note that points must be 8-connected.

Comment only
19 Mar 2011 Manhal

### Manhal (view profile)

hi,
what should be the input for "b"?
is it an image or what ??
need help as soon as possible pls ..
thanx

Comment only
19 Mar 2011 Santhosh R

### Santhosh R (view profile)

A very nice self explanatory code!!
Was very useful for my project.
Great job!!!!!!!!!