Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function [circles,cImg] = findCircles(img)
- edges = edge(img, 'Canny');
- [rows, cols] = size(edges);
- Rm = round(min(rows, cols) / 2);
- Count = zeros(rows, cols, Rm);
- for colIndex = 1 : cols
- for rowIndex = 1 : rows
- if(edges(rowIndex, colIndex) == 1)
- for Cx = 1 : cols
- for Cy = 1 : rows
- radius = round(sqrt((colIndex - Cx)^2 + (rowIndex - Cy)^2));
- if(radius > 0 && radius < Rm)
- Count(Cx, Cy, radius) = Count(Cx, Cy, radius) + 1;
- end
- end
- end
- end
- end
- end
- threshold = 5;
- result = [];
- for Cx = 1 : cols
- for Cy = 1 : rows
- for radius = 1 : min(rows, cols)
- threshold = 80;%0.7*pi * radius;
- if(radius > 0 && radius < Rm && Count(Cx, Cy, radius) > threshold)
- if(isMaximal(Count, Cx, Cy, radius))
- result = [result ; Cx, Cy, radius];
- end
- end
- end
- end
- end
- circles = filterCircles(result);
- cImg = addCircles(img, circles);
- end
- % [rows, cols] = size(img);
- %
- % %Find edges using edge detection algorithm.
- % edges = edge(img, 'Canny');%TODO return this: edges = edgeDetect(img);
- %
- % Rm = round(min(rows, cols) / 2);%round(sqrt(rows^2 + cols^2)); round(sqrt(rows^2 + cols^2));
- % Count = zeros(cols, rows, Rm);
- %
- % for y = 1: rows
- % for x = 1 : cols
- % if edges(y, x) == 1
- % for Cy = 1 : rows
- % for Cx = 1 : cols
- % r = round(sqrt((x - Cx)^2 + (y - Cy)^2));
- % if(r > 0 && r < Rm)
- % Count(Cx, Cy, r) = Count(Cx, Cy, r) + 1;
- % end;
- % end
- % end
- % end
- % end
- % end
- %
- % % for x = 1 : rows
- % % for y = 1 : cols
- % % if (edges(y, x) == 1)
- % % for cX = 1 : cols
- % % for cY = 1 : rows
- % % r = round(sqrt((x - cX)^2 + (y - cY)^2));
- % % %X = sprintf('r = %d x = %d y = %d cX = %d cY = %d', r, x, y, cX, cY);
- % % %disp(X);
- % % if(r > 0 && r < Rm)
- % % Count(cX, cY, r) = Count(cX, cY, r) + 1;
- % % end
- % % end
- % % end
- % % end
- % % end
- % % end
- %
- % T = 5; % TODO give a correct value
- %
- % [c_YMax, c_XMax, r_max] = size(Count);
- % counter = 1;
- %
- % %Stores the circles in the format [x, y, r]
- % circlesList = zeros(c_YMax, 3);
- %
- % for c_x = 1 : c_XMax
- % for c_y = 1 : c_YMax
- % for r = 1 : r_max
- % %Check if found a circle
- % if(Count(c_x, c_y, r) > T)
- % circlesList(counter, 1) = c_x;
- % circlesList(counter, 2) = c_y;
- % circlesList(counter, 3) = r;
- % counter = counter + 1;
- % end
- % end
- % end
- % end
- %
- % [rows, cols] = size(circlesList);
- %
- % for r = 1 : rows
- % for c = 1 : cols
- % CircleNumber = r;
- % OriginX = circlesList(r, 1);
- % OriginY = circlesList(r, 2);
- % Radius = circlesList(r, 3);
- % fprintf('Circle %d: %d, %d, %d\n',CircleNumber,OriginX,OriginY,Radius);
- % end
- % end
- %
- % circles = circlesList;
- % cImg = 0;
- %
- function filteredList = filterCircles(circles)
- [rows, cols] = size(circles);
- filteredList = [];
- counter = 1;
- for r1 = 1 : rows
- for r2 = 1 : rows
- if(r1 ~= r2)
- x1 = circles(r1, 1);
- y1 = circles(r1, 2);
- x2 = circles(r2, 1);
- y2 = circles(r2, 2);
- distance = sqrt((x1 - x2)^2 + (y1 - y2)^2);
- if(distance < 5)
- %filteredList = [filteredList ; x1, y1, circles(r1, 3)];
- %counter = counter + 1;
- circles(r2, 1) = 0;
- circles(r2, 2) = 0;
- circles(r2, 3) = 0;
- end
- end
- end
- end
- for r = 1 : size(circles, 1)
- if(circles(r, 3) ~= 0)
- filteredList = [filteredList ; circles(r, 1), circles(r, 2), circles(r, 3)];
- end
- end
- end
- function answer = isMaximal(Count, Cx, Cy, radius)
- currentValue = Count(Cx, Cy, radius);
- [rows, cols, depth] = size(Count);
- if(Cx > 1 &&Count(Cx - 1, Cy, radius) > currentValue)
- answer = 0;
- elseif (Cx < cols && Count(Cx + 1, Cy, radius) > currentValue)
- answer = 0;
- elseif(Cy > 1 && Count(Cx, Cy - 1, radius) > currentValue)
- answer = 0;
- elseif(Cy < rows && Count(Cx, Cy + 1, radius) > currentValue)
- answer = 0;
- elseif(radius > 1 && Count(Cx, Cy, radius - 1) > currentValue)
- answer = 0;
- elseif(radius < depth && Count(Cx, Cy, radius + 1) > currentValue)
- answer = 0;
- end
- answer = 1;
- end
- function imageWithCircles = addCircles(img, circles)
- rows = (size(circles, 1));
- if(rows > 0)
- circleParameters = circles(1, :);
- imageWithCircles = insertShape(img,'circle',circleParameters,'LineWidth',2);
- for r = 2 : rows
- circleParameters = circles(r, :);
- imageWithCircles = insertShape(imageWithCircles,'circle',circleParameters,'LineWidth',2);
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement