Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8689871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:45:57+00:00 2026-06-12T23:45:57+00:00

I’m implementing chain code and I stumbled across a little problem. First of all

  • 0

I’m implementing chain code and I stumbled across a little problem. First of all what I do:

I start at a certain pixel on my boundary and then I check which neighboring pixel is present acoriding to this:

3  2  1
 \ | /
4-- --0
 / | \
5  6  7

Though here i have a problem:

enter image description here

the little red dot is where the algorithm starts. then the first direction is 2 then 1 then 0 then 0 etc…

If you follow the yellow dots you’ll see it ends at two dots next to each other. This is where the algorithm went in direction 4 (since there was no pixel with value 1 at direction 0,1,2 or 3). Though at the next step it checks the direction 0 (which is always the first direction to start) … and of course it exists, since it is the previous spot and will go right. Here it is stuck in an infinite loop (going left to right and right to left).

My question is now, how can i fix this ?

The code that I use is:

% Implementation of tangent angle function (chain code)

clear 
clc

directions = [ 1, 0
               1,-1
               0,-1
              -1,-1
              -1, 0
              -1, 1
               0, 1
               1, 1]

I = imread('./IMAGES/1/M_201005_1_0001.pgm');
Ibw = im2bw(I,0.15); % This is also by setting all the pixels with intesity lower than 17 to 0;


tanAngFunc ={};

[row,col] = find(Ibw);

y = col(find(max(row)==row))
x = max(row)

imshow(I)
hold on;
plot(y,x,'r.','MarkerSize',1)
hold on

l=1;

  not_done = true;
  while not_done
    if l== 36
        'test'
    end



      % Right (0)
      if Ibw(x+directions(1,2),y+directions(1,1)) ~=0

            tanAngFunc{l} = 0;
           x=  x+directions(1,2);
           y= y+directions(1,1);

      % Above right(1)
      elseif Ibw(x+directions(2,2),y+directions(2,1)) ~=0
            tanAngFunc{l} = 2;
            x= x+directions(2,2);
            y= y+directions(2,1);

      % Above (2)
      elseif Ibw(x+directions(3,2),y+directions(3,1)) ~=0
            tanAngFunc{l} = 2;
            x= x+directions(3,2);
            y= y+directions(3,1); 

      % Above left (3)
      elseif Ibw(x+directions(4,2),y+directions(4,1)) ~=0
            tanAngFunc{l} = 2;
            x= x+directions(4,2);
            y= y+directions(4,1);

      % Left (4)
      elseif Ibw(x+directions(5,2),y+directions(5,1)) ~=0
            tanAngFunc{l} = 2;
            x= x+directions(5,2);
            y= y+directions(5,1);

      % Bottom left (5)
      elseif Ibw(x+directions(6,2),y+directions(6,1)) ~=0
            tanAngFunc{l} = 2;
            x= x+directions(6,2);
            y= y+directions(6,1);

      % Bottom (6)
      elseif Ibw(x+directions(7,2),y+directions(7,1)) ~=0
            tanAngFunc{l} = 2;
            x= x+directions(7,2);
            y= y+directions(7,1);

      % Bottom right (7)
      elseif Ibw(x+directions(8,2),y+directions(8,1)) ~=0
            tanAngFunc{l} = 3;
            x= x+directions(8,2);
            y= y+directions(8,1);
      end


      plot(y,x,'y.','MarkerSize',3)
      hold on

      pause(1)
      l = l + 1;





    not_done = (x ~= col(find(max(row)==row)) && y ~= max(row));
  end

On this image :
enter image description here

In case you want to try this yourself

Edit:

As suggested in the comments, In my 2nd version I made sure the algorithm didn’t use previously visited pixels. The result:
enter image description here

As you can see this isn’t a working answer.

Edit 2

Concerning the updated solution of @jonas. I’ve quickly put it in excel so it’s easier to understand. (pixel with 1 is not yet found, pixel x is already found).

enter image description here

So this doesn’t work 100%, It’s rather impossible to make sure the edge of every picture is 2 pixels wide when working with a huge dataset.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:45:58+00:00Added an answer on June 12, 2026 at 11:45 pm

    The problem with your algorithm is that it only checks whether the “candidate” pixel to be visited is part of the object — but performs no additional checks to make sure that the pixel is part of the boundary. This is why it will happily walk through the inside of the object.

    A popular algorithm for boundary tracing is the Moore-neighbourhood tracing algorithm. In addition to the Wikipedia page, take a look at this page, which explains the idea behind the algorithm in more detail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Specifically, suppose I start with the string string =hello \'i am \' me And
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.