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 9088451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:54:39+00:00 2026-06-16T21:54:39+00:00

I wrote a matlab code for detecting circles in gray scale image using Hough

  • 0

I wrote a matlab code for detecting circles in gray scale image using Hough Transform. I would like to minimize running time as much as possible.

The edge detection I use is custom implementation, but its running time is fast enough for what I need (about 0.06 seconds). However, the bottleneck is the rest of the code (total running time is about 6.35 seconds). BTW I used tic/toc to calculate running time.

Here is the code, I would really appreciate it if anyone could have a look:

function [ circles ] = findCircles(img)

 % set low and high bounds for radii values
    minR = 9; 
    [imgRows, imgCols] = size(img);
    maxR = ceil(min(imgRows, imgCols)/2);

    tic

    % run edge detection on image
    edgeImg = edgeDetect(img);    
    % get image size
    [rows, cols] = size(edgeImg); 
    % initialize accumulator
    houghAcc = zeros(rows, cols, maxR);
    % get all edge pixels from image
    edges = find(edgeImg);

    % find number of edge pixels
    edgeNum = size(edges);

    % scan each edge 
    for currEdge = 1 : edgeNum

        % get current edge x and y coordinations
        [edgeY edgeX] =  ind2sub([rows, cols], edges(currEdge));
        % scan each all possible radii
        for r = minR : maxR
            % go over all possible 2*pi*r circle centers
            for ang = 0 : 360
                t = (ang * pi) / 180;
                cX = round(edgeX - r*cos(t));
                cY = round(edgeY - r*sin(t));

                % check if center found is within image boundaries
                if ( cX < cols && cX > 0 && cY < rows && cY > 0 )
                    % found circle with (cX,cY) as center and r as radius
                    houghAcc(cY,cX,r)=houghAcc(cY,cX,r)+1; % increment matching counter

                end
            end
        end
    end

    % initialize circle list
    circles = []; 
    % intialize index for next found circle
    nextCircleIndx = 1;
    % get counter list dimensions
    [sizeX sizeY sizeR] = size(houghAcc);

    % get max counter value from hough counter matrix
    m = max(max(max(houghAcc))); 
    % calculate the minimal pixels that circle should have on perimeter
    t = m * 0.42; 

    % scan each found circle
    for cX = 1 : sizeX
        for cY = 1 : sizeY
            for r = 1 : sizeR

                % threshold values
                if houghAcc(cX, cY, r) > t
                    % circle is dominant enough, add it
                    circles(nextCircleIndx,:) = [cY , cX , r ,houghAcc(cX, cY, r)];
                    % increment index
                    nextCircleIndx = nextCircleIndx + 1;                    
                end

            end

        end
    end

     % sort counters in descending order (according to votes for each
    % circle)
    circles = flipud(sortrows(circles,4));

    % get circle list's size
    [rows cols] = size(circles);

    % scan circle list and check each pair of found circles 
    for i = 1 : rows-1
        % get first circle's details:
        % center
        cX1 = circles(i,1);
        cY1 = circles(i,2);
        % radius
        r1 = circles(i,3);
        %hough counter
        h1 = circles(i,4); 

        for j = i+1 : rows

            %get second circle's details:
            % center
            cX2 = circles(j,1);
            cY2 = circles(j,2);
            % radius
            r2 = circles(j,3);
            %hough counter
            h2 = circles(j,4); 


            % check if circle's actual difference is smaller than minimal
            % radius allowed
            if (cX1 - cX2)*(cX1 - cX2)+ (cY1 - cY2)*(cY1 - cY2) < (min(r1,r2))*(min(r1,r2))  && abs(r1 - r2) < minR
                % both circles are similar, sum their counters and merge
                % them to a circle with their avaraged values
                circles(i,:)=[(cX1+cX2)/2, (cY1+cY2)/2, (r1+r2)/2, h1+h2];
                % remove similar circle
                circles(j,:)=[0,0,0,0]; 
            end
        end

    end

    sortParam = 3; % 1: x-center, 2: y-center, 3: radius, 4: hough counter

    % sort the circles by the sort parameter, in descending order
    circles = flipud(sortrows(circles,sortParam));

    % get number of remained circles (= rows with non-zero values)
    len = length(find(circles~=0))/4;

    % remove duplicate similar circles from previus step
    circles(circles == 0) = [];

    % reshape circle list back to matrix form (previous step converted it
    % to a vector)
    circles = reshape(circles,len,4);

    % get max value according to sort parameter
    m = max(circles(:,sortParam));

    %get size of new circle list (with no duplicate circles)
    [newH newW] = size(circles);

    % thresholding: remove hough counters that are less than 30% from sort
    % parameter
    for  i= 1 : newH
        % check if current circle's sorting parameter's value is smaller
        % than threshold
        if  m - circles(i,sortParam) < m * 0.3 
    %         plot(circles(i,1),circles(i,2),'xr'); % DEBUG - show centers
        else
            % remove current circle
            circles(i,:)=[0,0,0,0];
        end
    end

    % find number of remaining circles after thresholding
    len = length(find(circles~=0))/4;
    % delete rows that match circles removed in thresholding
    circles(circles==0)=[];
    % reshape circle list back to matrix form
    circles=reshape(circles,len,4);

    % convert circle list's values to integers (hough counters are already
    % integers)
    circles = uint8(circles(:,1:3));
    toc
end

Where can this code be improved? thanks for any help!

  • 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-16T21:54:40+00:00Added an answer on June 16, 2026 at 9:54 pm

    For the first for block that populates houghAcc matrix, I propose the following replacement:

    r = minR : maxR;    
    t = ( 0 : 359 ) * pi / 180; % following HighPerformaceMark suggestion
    rsin = bsxfun( @times, r', sin(t) ); %'
    rcos = bsxfun( @times, r', cos(t) ); %'
    [edgeY edgeX] = find( edgeImg );
    cX = round( bsxfun( @minus, edgeX, permute( rcos, [3 1 2] ) ) );
    cY = round( bsxfun( @minus, edgeY, permute( rsin, [3 1 2] ) ) );
    R = permute( repmat( r', [ 1 size(cX,1) size(cX,3) ] ), [2 1 3] ); %' to index accHough
    % select valid indices
    sel = ( cX > 0 & cY > 0 & cY < rows & cX < cols );
    houghAcc = accumarray( {cY(sel(:)), cX(sel(:)), R(sel(:))}, 1, [rows, cols, maxR] );
    

    For the second for block that scans each found circle, I propose the following replacement:

    ind = find( houghAcc > t );
    % sort the scores
    sc = houghAcc(ind);
    [sc si] = sort(  sc , 'descend' );
    % convert linear indices to x,y,r
    [cX cY r] = ind2sub( size( houghAcc ), ind(si) );
    circles = [ cX(:) cY(:) r(:) sc(:) ];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a piece of code for computing Self Quotient Image (SQI) in MATLAB.
I wrote a code in matlab to compute an integral using Gauss-Chebyshev quadrature ,
Hi everyone I wrote this code using MatLab and I need to design a
I recently wrote some code using Matlab's OOP. In each class object I save
I wrote the following C/MEX code using the FFTW library to control the number
Earlier I wrote a code in Matlab for this sort of lottery function, just
LANGUAGE: I am writing an object-oriented code in MATLAB. I wrote almost all of
I just started to translate a Matlab code to numpy, how can I write
In MATLAB, how do you write a matrix into an image of EPS format?
I wrote some quick code for visualization of superposition of two waves with different

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.