Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %318810637 Perov Danny
- %318455813 Eden Klainer
- %
- %The function finds circles in the given grayscale image using Hough transform.
- %It also prints a list of the circles in the image.
- %Input: grayscale image
- %Output: list of circles, image which contains the original image with the
- %marked circles.
- function [circles,cImg] = findCircles(img)
- %Detect the edges in the image.
- edges = edgeDetect(img);
- [rows, cols] = size(edges);
- %Create the count matrix.
- Rm = round(min(rows, cols) / 2);
- Count = zeros(cols, rows, Rm);
- %Go over all the pixels, and for the ones that are turned on, calculate the
- %radius, and increment the Count matrix.
- 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
- %TODO add explanation to the threshold
- threshold = max(max(max(Count))) * 0.85; % there is a good result when it's 60
- result = [];
- %Go over the Count matrix, detect the circles, and add them to the list.
- for Cx = 1 : cols
- for Cy = 1 : rows
- for radius = 1 : min(rows, cols)
- %threshold = 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);
- %Print the list into the screen.
- printList(circles);
- %Mark the circles in the original image.
- cImg = addCircles(img, circles);
- end
- %The function filters the circles list.
- %Input: circles list
- %Output: filtered list
- 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)
- %if(y1 < y2)
- %filteredList = [filteredList ; x1, y1, circles(r1, 3)];
- %counter = counter + 1;
- circles(r2, 1) = 0;
- circles(r2, 2) = 0;
- circles(r2, 3) = 0;
- % else
- % circles(r1, 1) = 0;
- % circles(r1, 2) = 0;
- % circles(r1, 3) = 0;
- % end
- 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
- %The function prints a given list according to the stated format.
- %Input: circles list.
- function [] = printList(circles)
- rows = size(circles, 1);
- for r = 1 : rows
- fprintf('Circle %d: %d, %d, %d\n',r ,circles(r, 1), circles(r, 2), circles(r, 3));
- end
- end
- %The function checks if the given value is maximal when compared to its
- %neighbours.
- %Input: Count matrix, Cx, Cy, radius.
- %Output: boolean
- 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;
- else
- answer = 1;
- end
- end
- %The function draws circles on the given image, using the given list.
- 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