Hi @MechenG ,
You mentioned, I am trying to find the centre (x,y) and diamter of the circles in the given image. I have played around a bit with imfindcircles. Unfortunately I didn't succed with the detection. I have attached my sample image here. Note that, in the sample image, some circles are not fully visible, this can be ignored.. Could someone help with this?
Please see my response to your comments below.
To effectively detect circles in your image and going through documentation provided at the link below,
https://www.mathworks.com/help/images/ref/imfindcircles.html
you can employ several preprocessing techniques before applying imfindcircles. The following code includes Gaussian filtering for noise reduction and morphological operations to enhance circle detection. It will also save the detected centers and diameters to a text file in the specified directory. Here is a complete MATLAB code snippet tailored to your requirements:
% Read the image A = imread('/MATLAB Drive/Sample image.jpg'); % the image provided by you imshow(A); title('Original Image');
% Convert to grayscale if necessary if size(A, 3) == 3 A = rgb2gray(A); end
% Apply Gaussian filtering to reduce noise filteredImage = imgaussfilt(A, 2); % Adjust the sigma value as needed figure; imshow(filteredImage); title('Filtered Image');
% Use edge detection to enhance circle boundaries edges = edge(filteredImage, 'Canny'); figure; imshow(edges); title('Edge Detection');
% Apply morphological operations (dilation followed by erosion) se = strel('disk', 2); % Create a structural element morphedImage = imdilate(edges, se); morphedImage = imerode(morphedImage, se); figure; imshow(morphedImage); title('Morphological Operations');
% Define radius range for circle detection radiusRange = [10 50]; % Adjust according to expected circle sizes
% Detect circles using imfindcircles [centers, radii, metric] = imfindcircles(morphedImage, radiusRange, ... 'ObjectPolarity', 'bright', ... 'Sensitivity', 0.9, ... 'EdgeThreshold', 0.1);
% Display detected circles on the original image viscircles(centers, radii,'EdgeColor','b'); title('Detected Circles');
% Save results in text format resultsFilePath = '/MATLAB Drive/circle_detection_results.txt'; fileID = fopen(resultsFilePath, 'w'); fprintf(fileID, 'Center (x,y), Diameter\n'); for i = 1:length(radii) fprintf(fileID, '(%.2f, %.2f), %.2f\n', centers(i,1), centers(i,2), 2*radii(i)); end fclose(fileID);
disp(['Results saved to ', resultsFilePath]);
Please see attached.
As you can see in the code, the image is read and displayed. Since the image was in color (RGB), it is converted to grayscale since imfindcircles works on single-channel images. Afterwards, applied Gaussian Filtering because this helps reduce noise using a Gaussian filter with a specified standard deviation (sigma). You can adjust this value based on your specific image characteristics. A canny edge detection highlights the edges of potential circles. Then, utilized Morphological Operations since dilation followed by erosion helps to close gaps and enhance the shapes of detected edges. The imfindcircles function is called with a specified radius range and sensitivity settings to find circles. Circles detected are drawn on the original image for visual confirmation. The coordinates of circle centers and their diameters are saved in a specified text file format for later analysis.
Depending on your specific image conditions (e.g., lighting, contrast), you may need to adjust parameters such as the radius range or sensitivity.
After running the code, evaluate the detected circles against your expectations and refine preprocessing steps as needed for improved accuracy.
This comprehensive approach should help you successfully detect circles within your provided image while accommodating for noise and partial visibility issues.
If you have any further questions, please let me know.