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

  • Home
  • SEARCH
  • 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 7175107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:12:44+00:00 2026-05-28T16:12:44+00:00

I am trying to implement my own Thinning Algorithm in Matlab to understand the

  • 0

I am trying to implement my own Thinning Algorithm in Matlab to understand the thinning algorithm. I am following http://fourier.eng.hmc.edu/e161/lectures/morphology/node2.html and implementing my own code, but the result is incorrect.

Here is my code:

%for the sake of simplicity, the outermost pixels are ignored.
for x = 2:1:511
    for y = 2:1:511

        % if this pixel is not black, then, proceed in.
        if (frame2(y,x) > 0)                

            % the pos(1 to 8) here are for the surrounding pixels.
            pos(1) = frame2(y-1,x-1);
            pos(2) = frame2(y, x-1);
            pos(3) = frame2(y+1, x+1);
            pos(4) = frame2(y+1, x);
            pos(5) = frame2(y+1, x-1);
            pos(6) = frame2(y, x-1);
            pos(7) = frame2(y-1, x-1);
            pos(8) = frame2(y-1, x);

            nonZeroNeighbor = 0;
            transitSequence = 0;
            change = 0;

            for n = 1:1:8
                % for N(P1)
                if (pos(n) >= 1)
                    nonZeroNeighbor = nonZeroNeighbor + 1;
                end

                % for S(P1)
                if (n > 1)
                    if (pos(n) ~= change)
                        change = pos(n);
                        transitSequence = transitSequence + 1;
                    end
                else
                    change = pos(n);
                end

            end

            % also for S(P1)
            if ((nonZeroNeighbor > 1 && nonZeroNeighbor < 7) || transitSequence >= 2)
                markMatrix(y,x) = 1;
                fprintf(1, '(%d,%d) nonzero: %d transit: %d\n', y,x, nonZeroNeighbor, transitSequence);
            else %this else here is for the reverse.

            end

        end
    end
end


    for x = 2:1:511
        for y = 2:1:511
            if (markMatrix(y,x) > 0)
                frame2(y,x) = 0;
            end
        end
    end

    savePath = [path header number2 '.bmp']; 

    imwrite(frame2, savePath, 'bmp'); %output image here, replacing the original

From the site above, it states the function S(P1) as:

“S(P1): number of 0 to 1 (or 1 to 0) transitions in the sequence (P2, P3, …, P9)”

For this part, my codes are below “% for S(P1)” and “% also for S(P1)” comments. Am I implementing this function correctly? The output image I got is simply blank. Nothing at all.

For the correct output, I am aware that there is a logical problem. Regarding the site, it states:

When part of the shape is only 2-pixel wide, all pixels are boundary points and will be marked and then deleted.

This problem is to be ignored for now.

  • 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-05-28T16:12:45+00:00Added an answer on May 28, 2026 at 4:12 pm

    I’ve had a go at the problem and think I managed to get the algorithm to work. I’ve made several small edits along the way (please see the code below for details), but also found two fundamental problems with your initial implementation.

    Firstly, you assumed all would be done in the first pass of step 1 and 2, but really you need to let the algorithm work away at the image for some time. This is typical for iterative morphological steps ‘eating’ away at the image. This is the reason for the added while loop.

    Secondly, your way of calculating S() was wrong; it counted both steps from 0 to 1 and 1 to 0, counting twice when it shouldn’t and it didn’t take care of the symmetry around P(2) and P(9).

    My code:

    %Preliminary setups
    close all; clear all;
    set(0,'DefaultFigureWindowStyle','Docked')
    
    %Read image
    frame2 = imread('q1.jpg');
    
    %Code for spesific images
    %frame2(:,200:end) = [];
    %frame2 = rgb2gray(frame2);
    
    %Make binary
    frame2(frame2 < 128) = 1;
    frame2(frame2 >= 128) = 0;
    
    %Get sizes and set up mark
    [Yn Xn] = size(frame2);
    markMatrix = zeros(Yn,Xn);
    
    %First visualization
    figure();imagesc(frame2);colormap(gray)
    %%
    
    %While loop control
    cc = 0; 
    changed = 1;
    while changed && cc < 50;
    
        changed = 0;
        cc = cc + 1;
        markMatrix = zeros(Yn,Xn);
    
        for x = 2:1:Xn-1
            for y = 2:1:Yn-1
    
                % if this pixel is not black, then, proceed in.
                if (frame2(y,x) > 0)                
    
                    % the pos(2 to 9) here are for the surrounding pixels.
                    pos(1) = frame2(y,   x);
                    pos(2) = frame2(y-1, x);
                    pos(3) = frame2(y-1, x+1);
                    pos(4) = frame2(y,   x+1);
                    pos(5) = frame2(y+1, x+1);
                    pos(6) = frame2(y+1, x);
                    pos(7) = frame2(y+1, x-1);
                    pos(8) = frame2(y,   x-1);
                    pos(9) = frame2(y-1, x-1);
    
                    nonZeroNeighbor = 0;
                    transitSequence = 0;
                    change = pos(9);
    
                    for n = 2:1:9
    
                        %N()
                        nonZeroNeighbor = sum(pos(2:end));
    
                        %S()
                        if (double(pos(n)) - double(change)) < 0
                            transitSequence = transitSequence + 1;
                        end
                        change = pos(n);
    
                    end
    
                    %Test if pixel is to be removed
                    if ~( nonZeroNeighbor == 0 || nonZeroNeighbor == 1 ...
                        ||nonZeroNeighbor == 7 || nonZeroNeighbor == 8 ...
                        ||transitSequence >= 2)
    
                            markMatrix(y,x) = 1;
                            fprintf(1, '(%d,%d) nonzero: %d transit: %d\n', ...
                                y,x, nonZeroNeighbor, transitSequence);
                    end
    
                end
            end
        end
    
        %Mask out all pixels found to be deleted
        frame2(markMatrix > 0) = 0;
    
        %Check if anything has changed
        if sum(markMatrix(:)) > 0;changed = 1;end
    
    end
    
    %Final visualization
    figure();imagesc(frame2);colormap(gray)
    

    Lines before algorithm is run

    Lines after algorithm is run

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

Sidebar

Related Questions

I'm trying to implement my own GenericIdentity implementation but keep receiving the following error
Im trying to implement my own Huffman Coding algorithm and the priority queue for
Im trying to implement my own glOtho function from the opengles docs http://www.khronos.org/opengles/documentation/opengles1_0/html/glOrtho.html to
I'm trying to implement my own List system in Java. the List class file
I am trying to implement my own authentication method for AuthKit and am trying
I'm trying to implement my own version of the 'cd' command that presents the
For all those ligthning fast shop users. I'm trying to implement my own first
I am trying to implement my own memory allocation code which is simple yet
I am trying to implement my own is_member_function_pointer and I'm having trouble with it.
I'm trying to implement my own IRC client as a personal proejct and I

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.