Last active
February 8, 2025 16:16
-
-
Save mansoormemon/071934d1fb46b25a52bf797ac84e8441 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
path = 'coins.png'; | |
image = imread(path); | |
figure, imshow(image), title('Original'); | |
figure; | |
if size(image, 3) == 3 | |
gray = rgb2gray(image); | |
else | |
gray = image; | |
end | |
subplot(1, 3, 1), imshow(gray), title('Grayscale'); | |
adjusted = imadjust(gray); | |
subplot(1, 3, 2), imshow(adjusted), title('Adjusted'); | |
filtered = medfilt2(adjusted, [7 7]); | |
subplot(1, 3, 3), imshow(filtered), title('Filtered'); | |
canny = edge(filtered, 'canny'); | |
figure, imshow(canny), title('Edges'); | |
figure; | |
sc = strel('disk', 2); | |
closed = imclose(canny, sc); | |
subplot(1, 3, 1), imshow(closed), title('Closed Edges'); | |
sd = strel('disk', 1); | |
dilated = imdilate(closed, sd); | |
subplot(1, 3, 2), imshow(dilated), title('Dilated Edges'); | |
filled = imfill(dilated, 'holes'); | |
subplot(1, 3, 3), imshow(filled), title('Filled Holes'); | |
min_area = 512; | |
connected_comps = bwconncomp(filled); | |
stats = regionprops(filled, 'Area', 'BoundingBox'); | |
large_regions = find([stats.Area] >= min_area); | |
fprintf("Object count: %d", size(large_regions, 2)); | |
filtered_image = ismember(labelmatrix(connected_comps), large_regions); | |
figure, imshow(filtered_image), title('RegionProps'); | |
figure; | |
imshow(image); | |
hold on; | |
for i = 1:length(large_regions) | |
k = large_regions(i); | |
rectangle('Position', stats(k).BoundingBox, 'EdgeColor', 'g', 'LineWidth', 1); | |
end | |
hold off; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment